zss-engine 0.2.51 → 0.2.52

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,34 +1,65 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.genBase36Hash = genBase36Hash;
4
- const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
5
- function simpleHash(str) {
6
- let hash = 0;
7
- for (let i = 0; i < str.length; i++) {
8
- const char = str.charCodeAt(i);
9
- hash = (hash << 5) - hash + char;
4
+ function murmurhash3_32(str, seed = 0) {
5
+ let h = seed;
6
+ const len = str.length;
7
+ const c1 = 0xcc9e2d51;
8
+ const c2 = 0x1b873593;
9
+ const r1 = 15;
10
+ const r2 = 13;
11
+ const m = 5;
12
+ const n = 0xe6546b64;
13
+ let i = 0;
14
+ while (i < len - 3) {
15
+ let k = (str.charCodeAt(i) & 0xff) | ((str.charCodeAt(i + 1) & 0xff) << 8) | ((str.charCodeAt(i + 2) & 0xff) << 16) | ((str.charCodeAt(i + 3) & 0xff) << 24);
16
+ k = Math.imul(k, c1);
17
+ k = (k << r1) | (k >>> (32 - r1));
18
+ k = Math.imul(k, c2);
19
+ h ^= k;
20
+ h = (h << r2) | (h >>> (32 - r2));
21
+ h = Math.imul(h, m) + n;
22
+ i += 4;
10
23
  }
11
- return Math.abs(hash);
12
- }
13
- function encodeBase36(num) {
14
- let result = '';
15
- do {
16
- result = chars[num % 36] + result;
17
- num = Math.floor(num / 36);
18
- } while (num > 0);
19
- return result;
20
- }
21
- function getStartingChar(hash) {
22
- const chars = 'abcdefghijklmnopqrstuvwxyz';
23
- return chars[hash % chars.length];
24
+ let k = 0;
25
+ switch (len % 4) {
26
+ case 3:
27
+ k ^= (str.charCodeAt(i + 2) & 0xff) << 16;
28
+ case 2:
29
+ k ^= (str.charCodeAt(i + 1) & 0xff) << 8;
30
+ case 1:
31
+ k ^= str.charCodeAt(i) & 0xff;
32
+ k = Math.imul(k, c1);
33
+ k = (k << r1) | (k >>> (32 - r1));
34
+ k = Math.imul(k, c2);
35
+ h ^= k;
36
+ }
37
+ h ^= len;
38
+ h ^= h >>> 16;
39
+ h = Math.imul(h, 0x85ebca6b);
40
+ h ^= h >>> 13;
41
+ h = Math.imul(h, 0xc2b2ae35);
42
+ h ^= h >>> 16;
43
+ return h >>> 0;
24
44
  }
25
- function genBase36Hash(object, n) {
26
- const serialized = JSON.stringify(object);
27
- const hash = simpleHash(serialized);
28
- let base36Hash = encodeBase36(hash);
29
- const startingChar = getStartingChar(hash);
30
- while (base36Hash.length < n - 1) {
31
- base36Hash = chars[hash % chars.length] + base36Hash;
45
+ function genBase36Hash(obj, seed, length) {
46
+ const normalized = JSON.stringify(obj, Object.keys(obj || {}).sort());
47
+ const hashValue = murmurhash3_32(normalized, seed);
48
+ const hashStr = hashValue.toString(36);
49
+ const firstChar = 'abcdefghijklmnopqrstuvwxyz'[hashValue % 26];
50
+ let result = firstChar + hashStr;
51
+ if (result.length > length) {
52
+ result = result.slice(0, length);
53
+ }
54
+ else if (result.length < length) {
55
+ const paddingNeeded = length - result.length;
56
+ const paddingChars = '0123456789abcdefghijklmnopqrstuvwxyz';
57
+ let paddingHash = hashValue;
58
+ for (let i = 0; i < paddingNeeded; i++) {
59
+ paddingHash = paddingHash * 1103515245 + 12345;
60
+ const paddingChar = paddingChars[Math.abs(paddingHash) % 36];
61
+ result += paddingChar;
62
+ }
32
63
  }
33
- return startingChar + base36Hash.slice(0, n - 1);
64
+ return result;
34
65
  }
@@ -34,7 +34,7 @@ function processAtomicProps(flatProps, parentAtRule, atomicHashes, allStyleSheet
34
34
  const CSSProp = (0, index_js_1.camelToKebabCase)(property);
35
35
  const normalizedValue = (0, index_js_1.applyCssValue)(value, CSSProp);
36
36
  const singlePropObj = { [property]: normalizedValue };
37
- const atomicHash = (0, index_js_1.genBase36Hash)(singlePropObj, 6);
37
+ const atomicHash = (0, index_js_1.genBase36Hash)(singlePropObj, 1, 7);
38
38
  atomicHashes?.push(atomicHash);
39
39
  let styleSheet = (0, index_js_1.transpileAtomic)(property, value, atomicHash);
40
40
  if (parentAtRule) {
@@ -1,31 +1,62 @@
1
- const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
2
- function simpleHash(str) {
3
- let hash = 0;
4
- for (let i = 0; i < str.length; i++) {
5
- const char = str.charCodeAt(i);
6
- hash = (hash << 5) - hash + char;
1
+ function murmurhash3_32(str, seed = 0) {
2
+ let h = seed;
3
+ const len = str.length;
4
+ const c1 = 0xcc9e2d51;
5
+ const c2 = 0x1b873593;
6
+ const r1 = 15;
7
+ const r2 = 13;
8
+ const m = 5;
9
+ const n = 0xe6546b64;
10
+ let i = 0;
11
+ while (i < len - 3) {
12
+ let k = (str.charCodeAt(i) & 0xff) | ((str.charCodeAt(i + 1) & 0xff) << 8) | ((str.charCodeAt(i + 2) & 0xff) << 16) | ((str.charCodeAt(i + 3) & 0xff) << 24);
13
+ k = Math.imul(k, c1);
14
+ k = (k << r1) | (k >>> (32 - r1));
15
+ k = Math.imul(k, c2);
16
+ h ^= k;
17
+ h = (h << r2) | (h >>> (32 - r2));
18
+ h = Math.imul(h, m) + n;
19
+ i += 4;
7
20
  }
8
- return Math.abs(hash);
9
- }
10
- function encodeBase36(num) {
11
- let result = '';
12
- do {
13
- result = chars[num % 36] + result;
14
- num = Math.floor(num / 36);
15
- } while (num > 0);
16
- return result;
17
- }
18
- function getStartingChar(hash) {
19
- const chars = 'abcdefghijklmnopqrstuvwxyz';
20
- return chars[hash % chars.length];
21
+ let k = 0;
22
+ switch (len % 4) {
23
+ case 3:
24
+ k ^= (str.charCodeAt(i + 2) & 0xff) << 16;
25
+ case 2:
26
+ k ^= (str.charCodeAt(i + 1) & 0xff) << 8;
27
+ case 1:
28
+ k ^= str.charCodeAt(i) & 0xff;
29
+ k = Math.imul(k, c1);
30
+ k = (k << r1) | (k >>> (32 - r1));
31
+ k = Math.imul(k, c2);
32
+ h ^= k;
33
+ }
34
+ h ^= len;
35
+ h ^= h >>> 16;
36
+ h = Math.imul(h, 0x85ebca6b);
37
+ h ^= h >>> 13;
38
+ h = Math.imul(h, 0xc2b2ae35);
39
+ h ^= h >>> 16;
40
+ return h >>> 0;
21
41
  }
22
- export function genBase36Hash(object, n) {
23
- const serialized = JSON.stringify(object);
24
- const hash = simpleHash(serialized);
25
- let base36Hash = encodeBase36(hash);
26
- const startingChar = getStartingChar(hash);
27
- while (base36Hash.length < n - 1) {
28
- base36Hash = chars[hash % chars.length] + base36Hash;
42
+ export function genBase36Hash(obj, seed, length) {
43
+ const normalized = JSON.stringify(obj, Object.keys(obj || {}).sort());
44
+ const hashValue = murmurhash3_32(normalized, seed);
45
+ const hashStr = hashValue.toString(36);
46
+ const firstChar = 'abcdefghijklmnopqrstuvwxyz'[hashValue % 26];
47
+ let result = firstChar + hashStr;
48
+ if (result.length > length) {
49
+ result = result.slice(0, length);
50
+ }
51
+ else if (result.length < length) {
52
+ const paddingNeeded = length - result.length;
53
+ const paddingChars = '0123456789abcdefghijklmnopqrstuvwxyz';
54
+ let paddingHash = hashValue;
55
+ for (let i = 0; i < paddingNeeded; i++) {
56
+ paddingHash = paddingHash * 1103515245 + 12345;
57
+ const paddingChar = paddingChars[Math.abs(paddingHash) % 36];
58
+ result += paddingChar;
59
+ }
29
60
  }
30
- return startingChar + base36Hash.slice(0, n - 1);
61
+ return result;
31
62
  }
@@ -30,7 +30,7 @@ function processAtomicProps(flatProps, parentAtRule, atomicHashes, allStyleSheet
30
30
  const CSSProp = camelToKebabCase(property);
31
31
  const normalizedValue = applyCssValue(value, CSSProp);
32
32
  const singlePropObj = { [property]: normalizedValue };
33
- const atomicHash = genBase36Hash(singlePropObj, 6);
33
+ const atomicHash = genBase36Hash(singlePropObj, 1, 7);
34
34
  atomicHashes?.push(atomicHash);
35
35
  let styleSheet = transpileAtomic(property, value, atomicHash);
36
36
  if (parentAtRule) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zss-engine",
3
- "version": "0.2.51",
3
+ "version": "0.2.52",
4
4
  "description": "Zero-runtime StyleSheet Engine",
5
5
  "keywords": [
6
6
  "zero-runtime",
@@ -1,2 +1 @@
1
- import { CreateStyle, CreateKeyframes, CSSProperties } from '../index.js';
2
- export declare function genBase36Hash(object: CreateStyle | CSSProperties | CreateKeyframes, n: number): string;
1
+ export declare function genBase36Hash(obj: {}, seed: number, length: number): string;