zss-engine 2.0.0 → 2.1.1

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,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.genBase36Hash = genBase36Hash;
4
+ const { asUintN } = BigInt;
4
5
  function deepNormalize(obj) {
5
6
  if (obj === null)
6
7
  return 'null';
@@ -16,65 +17,103 @@ function deepNormalize(obj) {
16
17
  const pairs = keys.map(key => `"${key}":${deepNormalize(recordObj[key])}`);
17
18
  return '{' + pairs.join(',') + '}';
18
19
  }
19
- function murmurhash3_32(str, seed) {
20
- let h = seed;
21
- const len = str.length;
22
- const c1 = 0xcc9e2d51;
23
- const c2 = 0x1b873593;
24
- const r1 = 15;
25
- const r2 = 13;
26
- const m = 5;
27
- const n = 0xe6546b64;
28
- let i = 0;
29
- while (i < len - 3) {
30
- let k = (str.charCodeAt(i) & 0xff) | ((str.charCodeAt(i + 1) & 0xff) << 8) | ((str.charCodeAt(i + 2) & 0xff) << 16) | ((str.charCodeAt(i + 3) & 0xff) << 24);
31
- k = Math.imul(k, c1);
32
- k = (k << r1) | (k >>> (32 - r1));
33
- k = Math.imul(k, c2);
34
- h ^= k;
35
- h = (h << r2) | (h >>> (32 - r2));
36
- h = Math.imul(h, m) + n;
37
- i += 4;
20
+ function rotl64(x, r) {
21
+ const shift = BigInt(r);
22
+ return asUintN(64, (x << shift) | (x >> (64n - shift)));
23
+ }
24
+ function fmix64(k) {
25
+ k ^= k >> 33n;
26
+ k = asUintN(64, k * 0xff51afd7ed558ccdn);
27
+ k ^= k >> 33n;
28
+ k = asUintN(64, k * 0xc4ceb9fe1a85ec53n);
29
+ k ^= k >> 33n;
30
+ return k;
31
+ }
32
+ function murmurhash3_x64_128(str, seed) {
33
+ let h1 = asUintN(64, BigInt(seed));
34
+ let h2 = asUintN(64, BigInt(seed));
35
+ const c1 = 0x87c37b91114253d5n;
36
+ const c2 = 0x4cf5ad432745937fn;
37
+ const encoder = new TextEncoder();
38
+ const key = encoder.encode(str);
39
+ const len = key.length;
40
+ const nblocks = Math.floor(len / 16);
41
+ const view = new DataView(key.buffer, key.byteOffset, key.byteLength);
42
+ for (let i = 0; i < nblocks; i++) {
43
+ const i16 = i * 16;
44
+ let k1 = view.getBigUint64(i16, true);
45
+ let k2 = view.getBigUint64(i16 + 8, true);
46
+ k1 = asUintN(64, k1 * c1);
47
+ k1 = rotl64(k1, 31);
48
+ k1 = asUintN(64, k1 * c2);
49
+ h1 ^= k1;
50
+ h1 = rotl64(h1, 27);
51
+ h1 = asUintN(64, h1 + h2);
52
+ h1 = asUintN(64, h1 * 5n + 0x52dce729n);
53
+ k2 = asUintN(64, k2 * c2);
54
+ k2 = rotl64(k2, 33);
55
+ k2 = asUintN(64, k2 * c1);
56
+ h2 ^= k2;
57
+ h2 = rotl64(h2, 31);
58
+ h2 = asUintN(64, h2 + h1);
59
+ h2 = asUintN(64, h2 * 5n + 0x38495ab5n);
38
60
  }
39
- let k = 0;
40
- switch (len % 4) {
61
+ const tailIndex = nblocks * 16;
62
+ let k1 = 0n;
63
+ let k2 = 0n;
64
+ switch (len & 15) {
65
+ case 15:
66
+ k2 ^= BigInt(key[tailIndex + 14]) << 48n;
67
+ case 14:
68
+ k2 ^= BigInt(key[tailIndex + 13]) << 40n;
69
+ case 13:
70
+ k2 ^= BigInt(key[tailIndex + 12]) << 32n;
71
+ case 12:
72
+ k2 ^= BigInt(key[tailIndex + 11]) << 24n;
73
+ case 11:
74
+ k2 ^= BigInt(key[tailIndex + 10]) << 16n;
75
+ case 10:
76
+ k2 ^= BigInt(key[tailIndex + 9]) << 8n;
77
+ case 9:
78
+ k2 ^= BigInt(key[tailIndex + 8]);
79
+ k2 = asUintN(64, k2 * c2);
80
+ k2 = rotl64(k2, 33);
81
+ k2 = asUintN(64, k2 * c1);
82
+ h2 ^= k2;
83
+ case 8:
84
+ k1 ^= BigInt(key[tailIndex + 7]) << 56n;
85
+ case 7:
86
+ k1 ^= BigInt(key[tailIndex + 6]) << 48n;
87
+ case 6:
88
+ k1 ^= BigInt(key[tailIndex + 5]) << 40n;
89
+ case 5:
90
+ k1 ^= BigInt(key[tailIndex + 4]) << 32n;
91
+ case 4:
92
+ k1 ^= BigInt(key[tailIndex + 3]) << 24n;
41
93
  case 3:
42
- k ^= (str.charCodeAt(i + 2) & 0xff) << 16;
94
+ k1 ^= BigInt(key[tailIndex + 2]) << 16n;
43
95
  case 2:
44
- k ^= (str.charCodeAt(i + 1) & 0xff) << 8;
96
+ k1 ^= BigInt(key[tailIndex + 1]) << 8n;
45
97
  case 1:
46
- k ^= str.charCodeAt(i) & 0xff;
47
- k = Math.imul(k, c1);
48
- k = (k << r1) | (k >>> (32 - r1));
49
- k = Math.imul(k, c2);
50
- h ^= k;
98
+ k1 ^= BigInt(key[tailIndex + 0]);
99
+ k1 = asUintN(64, k1 * c1);
100
+ k1 = rotl64(k1, 31);
101
+ k1 = asUintN(64, k1 * c2);
102
+ h1 ^= k1;
51
103
  }
52
- h ^= len;
53
- h ^= h >>> 16;
54
- h = Math.imul(h, 0x85ebca6b);
55
- h ^= h >>> 13;
56
- h = Math.imul(h, 0xc2b2ae35);
57
- h ^= h >>> 16;
58
- return h >>> 0;
104
+ h1 ^= BigInt(len);
105
+ h2 ^= BigInt(len);
106
+ h1 = asUintN(64, h1 + h2);
107
+ h2 = asUintN(64, h2 + h1);
108
+ h1 = fmix64(h1);
109
+ h2 = fmix64(h2);
110
+ h1 = asUintN(64, h1 + h2);
111
+ h2 = asUintN(64, h2 + h1);
112
+ return asUintN(64, h1 ^ h2);
59
113
  }
60
114
  function genBase36Hash(obj, seed, length) {
61
115
  const normalized = deepNormalize(obj);
62
- const hashValue = murmurhash3_32(normalized, seed);
63
- const hashStr = hashValue.toString(36);
64
- const firstChar = 'x';
65
- let result = firstChar + hashStr;
66
- if (result.length > length) {
67
- result = result.slice(0, length);
68
- }
69
- else if (result.length < length) {
70
- const paddingNeeded = length - result.length;
71
- const paddingChars = '0123456789abcdefghijklmnopqrstuvwxyz';
72
- let paddingHash = hashValue;
73
- for (let i = 0; i < paddingNeeded; i++) {
74
- paddingHash = paddingHash * 1103515245 + 12345;
75
- const paddingChar = paddingChars[Math.abs(paddingHash) % 36];
76
- result += paddingChar;
77
- }
78
- }
79
- return result;
116
+ const hashValue = murmurhash3_x64_128(normalized, seed);
117
+ const hash = hashValue.toString(36);
118
+ return 'x' + hash.slice(-(length - 1));
80
119
  }
@@ -1,3 +1,4 @@
1
+ const { asUintN } = BigInt;
1
2
  function deepNormalize(obj) {
2
3
  if (obj === null)
3
4
  return 'null';
@@ -13,65 +14,103 @@ function deepNormalize(obj) {
13
14
  const pairs = keys.map(key => `"${key}":${deepNormalize(recordObj[key])}`);
14
15
  return '{' + pairs.join(',') + '}';
15
16
  }
16
- function murmurhash3_32(str, seed) {
17
- let h = seed;
18
- const len = str.length;
19
- const c1 = 0xcc9e2d51;
20
- const c2 = 0x1b873593;
21
- const r1 = 15;
22
- const r2 = 13;
23
- const m = 5;
24
- const n = 0xe6546b64;
25
- let i = 0;
26
- while (i < len - 3) {
27
- let k = (str.charCodeAt(i) & 0xff) | ((str.charCodeAt(i + 1) & 0xff) << 8) | ((str.charCodeAt(i + 2) & 0xff) << 16) | ((str.charCodeAt(i + 3) & 0xff) << 24);
28
- k = Math.imul(k, c1);
29
- k = (k << r1) | (k >>> (32 - r1));
30
- k = Math.imul(k, c2);
31
- h ^= k;
32
- h = (h << r2) | (h >>> (32 - r2));
33
- h = Math.imul(h, m) + n;
34
- i += 4;
17
+ function rotl64(x, r) {
18
+ const shift = BigInt(r);
19
+ return asUintN(64, (x << shift) | (x >> (64n - shift)));
20
+ }
21
+ function fmix64(k) {
22
+ k ^= k >> 33n;
23
+ k = asUintN(64, k * 0xff51afd7ed558ccdn);
24
+ k ^= k >> 33n;
25
+ k = asUintN(64, k * 0xc4ceb9fe1a85ec53n);
26
+ k ^= k >> 33n;
27
+ return k;
28
+ }
29
+ function murmurhash3_x64_128(str, seed) {
30
+ let h1 = asUintN(64, BigInt(seed));
31
+ let h2 = asUintN(64, BigInt(seed));
32
+ const c1 = 0x87c37b91114253d5n;
33
+ const c2 = 0x4cf5ad432745937fn;
34
+ const encoder = new TextEncoder();
35
+ const key = encoder.encode(str);
36
+ const len = key.length;
37
+ const nblocks = Math.floor(len / 16);
38
+ const view = new DataView(key.buffer, key.byteOffset, key.byteLength);
39
+ for (let i = 0; i < nblocks; i++) {
40
+ const i16 = i * 16;
41
+ let k1 = view.getBigUint64(i16, true);
42
+ let k2 = view.getBigUint64(i16 + 8, true);
43
+ k1 = asUintN(64, k1 * c1);
44
+ k1 = rotl64(k1, 31);
45
+ k1 = asUintN(64, k1 * c2);
46
+ h1 ^= k1;
47
+ h1 = rotl64(h1, 27);
48
+ h1 = asUintN(64, h1 + h2);
49
+ h1 = asUintN(64, h1 * 5n + 0x52dce729n);
50
+ k2 = asUintN(64, k2 * c2);
51
+ k2 = rotl64(k2, 33);
52
+ k2 = asUintN(64, k2 * c1);
53
+ h2 ^= k2;
54
+ h2 = rotl64(h2, 31);
55
+ h2 = asUintN(64, h2 + h1);
56
+ h2 = asUintN(64, h2 * 5n + 0x38495ab5n);
35
57
  }
36
- let k = 0;
37
- switch (len % 4) {
58
+ const tailIndex = nblocks * 16;
59
+ let k1 = 0n;
60
+ let k2 = 0n;
61
+ switch (len & 15) {
62
+ case 15:
63
+ k2 ^= BigInt(key[tailIndex + 14]) << 48n;
64
+ case 14:
65
+ k2 ^= BigInt(key[tailIndex + 13]) << 40n;
66
+ case 13:
67
+ k2 ^= BigInt(key[tailIndex + 12]) << 32n;
68
+ case 12:
69
+ k2 ^= BigInt(key[tailIndex + 11]) << 24n;
70
+ case 11:
71
+ k2 ^= BigInt(key[tailIndex + 10]) << 16n;
72
+ case 10:
73
+ k2 ^= BigInt(key[tailIndex + 9]) << 8n;
74
+ case 9:
75
+ k2 ^= BigInt(key[tailIndex + 8]);
76
+ k2 = asUintN(64, k2 * c2);
77
+ k2 = rotl64(k2, 33);
78
+ k2 = asUintN(64, k2 * c1);
79
+ h2 ^= k2;
80
+ case 8:
81
+ k1 ^= BigInt(key[tailIndex + 7]) << 56n;
82
+ case 7:
83
+ k1 ^= BigInt(key[tailIndex + 6]) << 48n;
84
+ case 6:
85
+ k1 ^= BigInt(key[tailIndex + 5]) << 40n;
86
+ case 5:
87
+ k1 ^= BigInt(key[tailIndex + 4]) << 32n;
88
+ case 4:
89
+ k1 ^= BigInt(key[tailIndex + 3]) << 24n;
38
90
  case 3:
39
- k ^= (str.charCodeAt(i + 2) & 0xff) << 16;
91
+ k1 ^= BigInt(key[tailIndex + 2]) << 16n;
40
92
  case 2:
41
- k ^= (str.charCodeAt(i + 1) & 0xff) << 8;
93
+ k1 ^= BigInt(key[tailIndex + 1]) << 8n;
42
94
  case 1:
43
- k ^= str.charCodeAt(i) & 0xff;
44
- k = Math.imul(k, c1);
45
- k = (k << r1) | (k >>> (32 - r1));
46
- k = Math.imul(k, c2);
47
- h ^= k;
95
+ k1 ^= BigInt(key[tailIndex + 0]);
96
+ k1 = asUintN(64, k1 * c1);
97
+ k1 = rotl64(k1, 31);
98
+ k1 = asUintN(64, k1 * c2);
99
+ h1 ^= k1;
48
100
  }
49
- h ^= len;
50
- h ^= h >>> 16;
51
- h = Math.imul(h, 0x85ebca6b);
52
- h ^= h >>> 13;
53
- h = Math.imul(h, 0xc2b2ae35);
54
- h ^= h >>> 16;
55
- return h >>> 0;
101
+ h1 ^= BigInt(len);
102
+ h2 ^= BigInt(len);
103
+ h1 = asUintN(64, h1 + h2);
104
+ h2 = asUintN(64, h2 + h1);
105
+ h1 = fmix64(h1);
106
+ h2 = fmix64(h2);
107
+ h1 = asUintN(64, h1 + h2);
108
+ h2 = asUintN(64, h2 + h1);
109
+ return asUintN(64, h1 ^ h2);
56
110
  }
57
111
  export function genBase36Hash(obj, seed, length) {
58
112
  const normalized = deepNormalize(obj);
59
- const hashValue = murmurhash3_32(normalized, seed);
60
- const hashStr = hashValue.toString(36);
61
- const firstChar = 'x';
62
- let result = firstChar + hashStr;
63
- if (result.length > length) {
64
- result = result.slice(0, length);
65
- }
66
- else if (result.length < length) {
67
- const paddingNeeded = length - result.length;
68
- const paddingChars = '0123456789abcdefghijklmnopqrstuvwxyz';
69
- let paddingHash = hashValue;
70
- for (let i = 0; i < paddingNeeded; i++) {
71
- paddingHash = paddingHash * 1103515245 + 12345;
72
- const paddingChar = paddingChars[Math.abs(paddingHash) % 36];
73
- result += paddingChar;
74
- }
75
- }
76
- return result;
113
+ const hashValue = murmurhash3_x64_128(normalized, seed);
114
+ const hash = hashValue.toString(36);
115
+ return 'x' + hash.slice(-(length - 1));
77
116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zss-engine",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "Zero-runtime StyleSheet Engine",
5
5
  "funding": "https://github.com/sponsors/refirst11",
6
6
  "author": "Refirst 11",