-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_internal.ts
More file actions
141 lines (132 loc) · 3.51 KB
/
Copy path_internal.ts
File metadata and controls
141 lines (132 loc) · 3.51 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* 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
*/
/**
* Symbol key for storing the theme's hash identifier.
* @internal
* @since 0.0.8
*/
export const ThemeHash = Symbol("@baetheus/css/theme/hash");
/**
* Symbol key for storing the theme's original shape values.
* @internal
* @since 0.0.8
*/
export const ThemeValues = Symbol("@baetheus/css/theme/values");
/**
* Symbol key for storing the theme's flattened CSS variables.
* @internal
* @since 0.0.8
*/
export const ThemeVariables = Symbol("@baetheus/css/theme/variables");
/**
* Symbol key for storing the theme's var() reference mappings.
* @internal
* @since 0.0.8
*/
export const ThemeReferences = Symbol("@baetheus/css/theme/references");
/**
* Symbol key for accessing style block generators.
* Used by Style, Variant, and any type implementing HasStyles.
* @internal
* @since 0.0.9
*/
export const Styles = Symbol("@baetheus/css/style");
/**
* 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()}`);
}