diff --git a/types/lexicographic-integer/index.d.ts b/types/lexicographic-integer/index.d.ts new file mode 100644 index 0000000000..b558a4c1d7 --- /dev/null +++ b/types/lexicographic-integer/index.d.ts @@ -0,0 +1,16 @@ +// Type definitions for lexicographic-integer 1.1 +// Project: https://github.com/substack/lexicographic-integer +// Definitions by: Carson Farmer +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * Return an array of byte values or a lexicographic hex string for an input integer. + * @param n The input integer as a javascript number. + * @param enc The encoding to use. Can be one of 'array' (default) or 'hex'. + */ +export function pack(n: number, enc?: T): T extends 'hex' ? string : T extends 'array' ? number[] : never; +/** + * Convert an array of bytes returned by .pack() back into the original javascript number. + * @param xs An array of bytes or a hex string. + */ +export function unpack(xs: number[] | string): number; diff --git a/types/lexicographic-integer/lexicographic-integer-tests.ts b/types/lexicographic-integer/lexicographic-integer-tests.ts new file mode 100644 index 0000000000..cf0962a837 --- /dev/null +++ b/types/lexicographic-integer/lexicographic-integer-tests.ts @@ -0,0 +1,26 @@ +import { pack, unpack } from 'lexicographic-integer'; + +const a = 1e55; +const b = 1.0000000000001e55; +const ha = pack(a, 'hex'); +const hb = pack(b, 'hex'); +console.log(ha, hb); + +const num = 555; +const encoded = pack(num, 'hex'); +const decoded1: number = unpack(encoded); +console.log(decoded1); + +let prev: string | number[] = pack(0); +let n: number; +let skip = 1; +for (n = 1; n < Number.MAX_VALUE; n += skip) { + const cur = pack(n, 'hex'); + if (cur <= prev.toString()) break; + prev = cur; + skip = 1 + Math.pow(245, Math.ceil(Math.log(n) / Math.log(256))); +} +console.log(n === Infinity); + +const decoded2: number = unpack(prev); +console.log(decoded2); diff --git a/types/lexicographic-integer/tsconfig.json b/types/lexicographic-integer/tsconfig.json new file mode 100644 index 0000000000..95a2dd78c0 --- /dev/null +++ b/types/lexicographic-integer/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "lexicographic-integer-tests.ts" + ] +} diff --git a/types/lexicographic-integer/tslint.json b/types/lexicographic-integer/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/lexicographic-integer/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }