@cyberalien/svg-utils 0.0.7 → 0.0.8

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.
@@ -1,5 +1,16 @@
1
+ interface Options {
2
+ prefix?: string;
3
+ length: number;
4
+ lengths?: Record<string, number>;
5
+ }
1
6
  /**
2
7
  * Hash an object, make sure hash is unique
8
+ *
9
+ * Number of unique hashes per length, with prefix:
10
+ * 6 chars = 2b unique hashes
11
+ * 7 chars = 78b unique hashes <-- got collision here
12
+ * 8 chars = 2.9t unique hashes
13
+ * 9 chars = 113t unique hashes
3
14
  */
4
- declare function getUniqueHash(data: unknown, hasPrefix?: boolean, size?: number): string;
15
+ declare function getUniqueHash(data: unknown, options: Options): string;
5
16
  export { getUniqueHash };
@@ -6,10 +6,20 @@ const uniqueHashes = Object.create(null);
6
6
  const uniqueWithPrefixHashes = Object.create(null);
7
7
  /**
8
8
  * Hash an object, make sure hash is unique
9
+ *
10
+ * Number of unique hashes per length, with prefix:
11
+ * 6 chars = 2b unique hashes
12
+ * 7 chars = 78b unique hashes <-- got collision here
13
+ * 8 chars = 2.9t unique hashes
14
+ * 9 chars = 113t unique hashes
9
15
  */
10
- function getUniqueHash(data, hasPrefix = true, size = 8) {
16
+ function getUniqueHash(data, options) {
17
+ const { prefix, length, lengths } = options;
11
18
  const str = typeof data === "string" ? data : JSON.stringify(sortObject(data));
12
- const hash = hashToString(hashString(str), hasPrefix, size);
19
+ const hasPrefix = !!prefix;
20
+ const values = hashString(str);
21
+ let hash = hashToString(values, hasPrefix, length);
22
+ if (lengths?.[hash]) hash = hashToString(values, hasPrefix, lengths[hash]);
13
23
  const cache = hasPrefix ? uniqueWithPrefixHashes : uniqueHashes;
14
24
  if (!cache[hash]) cache[hash] = str;
15
25
  else if (cache[hash] !== str) {
@@ -17,7 +27,7 @@ function getUniqueHash(data, hasPrefix = true, size = 8) {
17
27
  console.warn("Data 2:", str);
18
28
  throw new Error(`Hash collision detected: ${hash}`);
19
29
  }
20
- return hash;
30
+ return `${prefix}${hash}`;
21
31
  }
22
32
 
23
33
  export { getUniqueHash };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "type": "module",
4
4
  "description": "Common functions for working with SVG used by various packages.",
5
5
  "author": "Vjacheslav Trushkin",
6
- "version": "0.0.7",
6
+ "version": "0.0.8",
7
7
  "license": "MIT",
8
8
  "bugs": "https://github.com/cyberalien/svg-utils/issues",
9
9
  "homepage": "https://cyberalien.dev/",