diff options
author | Florian Dold <florian@dold.me> | 2021-04-07 16:12:40 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-04-07 16:13:16 +0200 |
commit | 29d710c392c2b28e8c8c2a177c8de40061a58e77 (patch) | |
tree | d259fe5571de9c3a10eaa9548423c4387fd9ec2e /packages/taler-util/src/helpers.ts | |
parent | 46056c416b51b783d1b9c88385aba0d293021524 (diff) | |
download | wallet-core-29d710c392c2b28e8c8c2a177c8de40061a58e77.tar.xz |
fix issue in JSON canonicalization (and move stuff to taler-util)
Diffstat (limited to 'packages/taler-util/src/helpers.ts')
-rw-r--r-- | packages/taler-util/src/helpers.ts | 54 |
1 files changed, 3 insertions, 51 deletions
diff --git a/packages/taler-util/src/helpers.ts b/packages/taler-util/src/helpers.ts index d3093d1b1..282724464 100644 --- a/packages/taler-util/src/helpers.ts +++ b/packages/taler-util/src/helpers.ts @@ -84,57 +84,6 @@ export function canonicalJson(obj: any): string { } /** - * Check for deep equality of two objects. - * Only arrays, objects and primitives are supported. - */ -export function deepEquals(x: any, y: any): boolean { - if (x === y) { - return true; - } - - if (Array.isArray(x) && x.length !== y.length) { - return false; - } - - const p = Object.keys(x); - return ( - Object.keys(y).every((i) => p.indexOf(i) !== -1) && - p.every((i) => deepEquals(x[i], y[i])) - ); -} - -export function deepCopy(x: any): any { - // FIXME: this has many issues ... - return JSON.parse(JSON.stringify(x)); -} - -/** - * Map from a collection to a list or results and then - * concatenate the results. - */ -export function flatMap<T, U>(xs: T[], f: (x: T) => U[]): U[] { - return xs.reduce((acc: U[], next: T) => [...f(next), ...acc], []); -} - -/** - * Compute the hash function of a JSON object. - */ -export function hash(val: any): number { - const str = canonicalJson(val); - // https://github.com/darkskyapp/string-hash - let h = 5381; - let i = str.length; - while (i) { - h = (h * 33) ^ str.charCodeAt(--i); - } - - /* JavaScript does bitwise operations (like XOR, above) on 32-bit signed - * integers. Since we want the results to be always positive, convert the - * signed int to an unsigned by doing an unsigned bitshift. */ - return h >>> 0; -} - -/** * Lexically compare two strings. */ export function strcmp(s1: string, s2: string): number { @@ -147,6 +96,9 @@ export function strcmp(s1: string, s2: string): number { return 0; } +/** + * Shorthand function for formatted JSON stringification. + */ export function j2s(x: any): string { return JSON.stringify(x, undefined, 2); } |