aboutsummaryrefslogtreecommitdiff
path: root/src/helpers.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/helpers.ts')
-rw-r--r--src/helpers.ts14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/helpers.ts b/src/helpers.ts
index d85808acb..42863c65b 100644
--- a/src/helpers.ts
+++ b/src/helpers.ts
@@ -138,3 +138,17 @@ export function getTalerStampDate(stamp: string): Date | null {
return new Date(sec * 1000);
}
+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;
+}