@supernova-studio/client 0.47.39 → 0.47.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supernova-studio/client",
3
- "version": "0.47.39",
3
+ "version": "0.47.41",
4
4
  "description": "Supernova Data Models",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.js",
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./api";
2
+ export * from "./utils";
2
3
  export * from "./yjs";
@@ -0,0 +1,52 @@
1
+ function hash(input: string): string {
2
+ return farmhash(input).toString(16);
3
+ }
4
+
5
+ function farmhash(input: string): number {
6
+ const seed = 0x9e3779b9;
7
+ let hash = seed;
8
+
9
+ for (let i = 0; i < input.length; i++) {
10
+ const charCode = input.charCodeAt(i);
11
+ hash ^= charCode;
12
+ hash = Math.imul(hash, 0x5bd1e995);
13
+ hash ^= hash >>> 15;
14
+ }
15
+
16
+ hash = Math.imul(hash, 0x5bd1e995);
17
+ hash ^= hash >>> 13;
18
+ hash = Math.imul(hash, 0x5bd1e995);
19
+ hash ^= hash >>> 15;
20
+
21
+ return hash >>> 0;
22
+ }
23
+
24
+ function prepareObject(obj: any): any {
25
+ if (obj === null || typeof obj !== "object") {
26
+ return obj;
27
+ }
28
+ if (Array.isArray(obj)) {
29
+ return obj.map(prepareObject);
30
+ }
31
+ const sortedObj: any = {};
32
+ for (const key of Object.keys(obj).sort()) {
33
+ if (obj[key] === null || obj[key] === undefined) {
34
+ continue;
35
+ }
36
+ sortedObj[key] = prepareObject(obj[key]);
37
+ }
38
+ return sortedObj;
39
+ }
40
+
41
+ export function generateHash(input: object | string): string {
42
+ if (typeof input === "object") {
43
+ return hash(JSON.stringify(prepareObject(input)));
44
+ } else {
45
+ try {
46
+ const obj = JSON.parse(input);
47
+ return hash(JSON.stringify(prepareObject(obj)));
48
+ } catch {
49
+ return hash(input);
50
+ }
51
+ }
52
+ }
@@ -0,0 +1 @@
1
+ export * from "./hash";