diff --git a/packages/taler-util/src/helpers.test.d.ts b/packages/taler-util/src/helpers.test.d.ts new file mode 100644 index 000000000..cb0ff5c3b --- /dev/null +++ b/packages/taler-util/src/helpers.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/packages/taler-util/src/helpers.test.js b/packages/taler-util/src/helpers.test.js new file mode 100644 index 000000000..ed3198c85 --- /dev/null +++ b/packages/taler-util/src/helpers.test.js @@ -0,0 +1,29 @@ +/* + This file is part of TALER + (C) 2017 Inria and GNUnet e.V. + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ +import test from "ava"; +import * as helpers from "./helpers"; +test("URL canonicalization", (t) => { + // converts to relative, adds https + t.is("https://alice.example.com/exchange/", helpers.canonicalizeBaseUrl("alice.example.com/exchange")); + // keeps http, adds trailing slash + t.is("http://alice.example.com/exchange/", helpers.canonicalizeBaseUrl("http://alice.example.com/exchange")); + // keeps http, adds trailing slash + t.is("http://alice.example.com/exchange/", helpers.canonicalizeBaseUrl("http://alice.example.com/exchange#foobar")); + // Remove search component + t.is("http://alice.example.com/exchange/", helpers.canonicalizeBaseUrl("http://alice.example.com/exchange?foo=bar")); + t.pass(); +}); +//# sourceMappingURL=helpers.test.js.map \ No newline at end of file diff --git a/packages/taler-util/src/helpers.test.js.map b/packages/taler-util/src/helpers.test.js.map new file mode 100644 index 000000000..1289a5d9d --- /dev/null +++ b/packages/taler-util/src/helpers.test.js.map @@ -0,0 +1 @@ +{"version":3,"file":"helpers.test.js","sourceRoot":"","sources":["helpers.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,IAAI,MAAM,KAAK,CAAC;AACvB,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AAErC,IAAI,CAAC,sBAAsB,EAAE,CAAC,CAAC,EAAE,EAAE;IACjC,mCAAmC;IACnC,CAAC,CAAC,EAAE,CACF,qCAAqC,EACrC,OAAO,CAAC,mBAAmB,CAAC,4BAA4B,CAAC,CAC1D,CAAC;IAEF,kCAAkC;IAClC,CAAC,CAAC,EAAE,CACF,oCAAoC,EACpC,OAAO,CAAC,mBAAmB,CAAC,mCAAmC,CAAC,CACjE,CAAC;IAEF,kCAAkC;IAClC,CAAC,CAAC,EAAE,CACF,oCAAoC,EACpC,OAAO,CAAC,mBAAmB,CAAC,0CAA0C,CAAC,CACxE,CAAC;IAEF,0BAA0B;IAC1B,CAAC,CAAC,EAAE,CACF,oCAAoC,EACpC,OAAO,CAAC,mBAAmB,CAAC,2CAA2C,CAAC,CACzE,CAAC;IAEF,CAAC,CAAC,IAAI,EAAE,CAAC;AACX,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/packages/taler-util/src/helpers.test.ts b/packages/taler-util/src/helpers.test.ts new file mode 100644 index 000000000..dbecf14b8 --- /dev/null +++ b/packages/taler-util/src/helpers.test.ts @@ -0,0 +1,46 @@ +/* + This file is part of TALER + (C) 2017 Inria and GNUnet e.V. + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ + +import test from "ava"; +import * as helpers from "./helpers"; + +test("URL canonicalization", (t) => { + // converts to relative, adds https + t.is( + "https://alice.example.com/exchange/", + helpers.canonicalizeBaseUrl("alice.example.com/exchange"), + ); + + // keeps http, adds trailing slash + t.is( + "http://alice.example.com/exchange/", + helpers.canonicalizeBaseUrl("http://alice.example.com/exchange"), + ); + + // keeps http, adds trailing slash + t.is( + "http://alice.example.com/exchange/", + helpers.canonicalizeBaseUrl("http://alice.example.com/exchange#foobar"), + ); + + // Remove search component + t.is( + "http://alice.example.com/exchange/", + helpers.canonicalizeBaseUrl("http://alice.example.com/exchange?foo=bar"), + ); + + t.pass(); +}); 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 @@ -83,57 +83,6 @@ export function canonicalJson(obj: any): string { return s + "}"; } -/** - * 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(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. */ @@ -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); } diff --git a/packages/taler-util/src/index.ts b/packages/taler-util/src/index.ts index 255d6cbc7..3416c7d12 100644 --- a/packages/taler-util/src/index.ts +++ b/packages/taler-util/src/index.ts @@ -2,17 +2,18 @@ import { TalerErrorCode } from "./taler-error-codes.js"; export { TalerErrorCode }; -export * from "./codec.js"; export * from "./amounts.js"; -export * from "./talerconfig.js"; -export * from "./time.js"; -export * from "./walletTypes.js"; -export * from "./transactionsTypes.js"; +export * from "./backupTypes.js"; +export * from "./codec.js"; +export * from "./helpers.js"; +export * from "./libtool-version.js"; export * from "./notifications.js"; -export * from "./talerTypes.js"; -export * from "./taleruri.js"; +export * from "./payto.js"; export * from "./ReserveStatus.js"; export * from "./ReserveTransaction.js"; -export * from "./backupTypes.js"; -export * from "./payto.js"; -export * from "./libtool-version.js"; \ No newline at end of file +export * from "./talerconfig.js"; +export * from "./talerTypes.js"; +export * from "./taleruri.js"; +export * from "./time.js"; +export * from "./transactionsTypes.js"; +export * from "./walletTypes.js"; \ No newline at end of file