-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_internal.ts
More file actions
105 lines (101 loc) · 2.66 KB
/
_internal.ts
File metadata and controls
105 lines (101 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Internal utilities for the CSS library.
*
* This module provides low-level hashing and string transformation functions
* used by other modules in the library. These are implementation details and
* should not be relied upon for external use.
*
* @module
* @since 0.0.4
*/
/**
* A CSS value that can be either a string or number.
*
* @example
* ```ts
* import type { CssValue } from "./_internal.ts";
*
* const color: CssValue = "red";
* const fontSize: CssValue = 16;
* const padding: CssValue = "8px";
* ```
*
* @since 0.0.4
*/
export type CssValue = string | number;
/**
* DJB2 hash - fast, simple, good distribution.
*
* A non-cryptographic hash function that produces a 32-bit unsigned integer.
* Used internally for generating unique class names from style objects.
*
* @param str - The string to hash
* @returns A 32-bit unsigned integer hash value
*
* @example
* ```ts
* import { djb2 } from "./_internal.ts";
*
* djb2("hello"); // 261238937
* djb2("world"); // 279393645
* ```
*
* @internal
* @since 0.0.4
*/
export function djb2(str: string): number {
let hash = 5381;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) + hash) ^ str.charCodeAt(i);
}
return hash >>> 0;
}
/**
* Hash any value to a 7-character base36 string.
*
* Serializes the input to JSON and computes a DJB2 hash, then converts
* to a zero-padded base36 string. Used to generate deterministic,
* collision-resistant class names from style objects.
*
* @param input - Any JSON-serializable value to hash
* @returns A 7-character base36 hash string
*
* @example
* ```ts
* import { hashObject } from "./_internal.ts";
*
* hashObject({ color: "red" }); // "0a1b2c3"
* hashObject([1, 2, 3]); // "x7y8z9a"
* hashObject("hello"); // "1kf3a9r"
* ```
*
* @since 0.0.4
*/
export function hashObject(input: unknown): string {
const content = JSON.stringify(input);
const hash = djb2(content);
return hash.toString(36).padStart(7, "0"); // 7 chars, zero-padded
}
/**
* Converts a camelCase string to kebab-case.
*
* Used to transform JavaScript-style CSS property names (e.g., `backgroundColor`)
* into their CSS equivalents (e.g., `background-color`).
*
* @param str - The camelCase string to convert
* @returns The kebab-case equivalent
*
* @example
* ```ts
* import { camelToKebab } from "./_internal.ts";
*
* camelToKebab("backgroundColor"); // "background-color"
* camelToKebab("fontSize"); // "font-size"
* camelToKebab("borderTopWidth"); // "border-top-width"
* ```
*
* @since 0.0.4
*/
export function camelToKebab(str: string): string {
return str.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
}