@esmalley/ts-utils 1.0.0
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.
- package/LICENSE +674 -0
- package/README.md +2 -0
- package/dist/Arithmetic.d.ts +4 -0
- package/dist/Arithmetic.d.ts.map +1 -0
- package/dist/Arithmetic.js +5 -0
- package/dist/Arrayifier.d.ts +29 -0
- package/dist/Arrayifier.d.ts.map +1 -0
- package/dist/Arrayifier.js +65 -0
- package/dist/CSV.d.ts +10 -0
- package/dist/CSV.d.ts.map +1 -0
- package/dist/CSV.js +35 -0
- package/dist/Color.d.ts +93 -0
- package/dist/Color.d.ts.map +1 -0
- package/dist/Color.js +344 -0
- package/dist/Dates.d.ts +51 -0
- package/dist/Dates.d.ts.map +1 -0
- package/dist/Dates.js +354 -0
- package/dist/Kontororu/Socket.d.ts +44 -0
- package/dist/Kontororu/Socket.d.ts.map +1 -0
- package/dist/Kontororu/Socket.js +218 -0
- package/dist/Kontororu.d.ts +8 -0
- package/dist/Kontororu.d.ts.map +1 -0
- package/dist/Kontororu.js +23 -0
- package/dist/Objector.d.ts +32 -0
- package/dist/Objector.d.ts.map +1 -0
- package/dist/Objector.js +120 -0
- package/dist/Sorter.d.ts +8 -0
- package/dist/Sorter.d.ts.map +1 -0
- package/dist/Sorter.js +28 -0
- package/dist/Style.d.ts +63 -0
- package/dist/Style.d.ts.map +1 -0
- package/dist/Style.js +471 -0
- package/dist/Text.d.ts +5 -0
- package/dist/Text.d.ts.map +1 -0
- package/dist/Text.js +35 -0
- package/dist/Theme.d.ts +1413 -0
- package/dist/Theme.d.ts.map +1 -0
- package/dist/Theme.js +503 -0
- package/dist/Toaster.d.ts +24 -0
- package/dist/Toaster.d.ts.map +1 -0
- package/dist/Toaster.js +46 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/package.json +58 -0
package/dist/Style.js
ADDED
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-syntax */
|
|
2
|
+
/* eslint-disable no-bitwise */
|
|
3
|
+
/**
|
|
4
|
+
* Class to get the style
|
|
5
|
+
* Has base CSS functions
|
|
6
|
+
*/
|
|
7
|
+
export class Style {
|
|
8
|
+
static getStyle() {
|
|
9
|
+
return {
|
|
10
|
+
zIndex: Style.getZIndex(),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
static getZIndex() {
|
|
14
|
+
return {
|
|
15
|
+
appBar: 1100,
|
|
16
|
+
drawer: 1200,
|
|
17
|
+
fab: 1050,
|
|
18
|
+
calendar: 1000,
|
|
19
|
+
mobileStepper: 1000,
|
|
20
|
+
modal: 1300,
|
|
21
|
+
toast: 1400,
|
|
22
|
+
speedDial: 1050,
|
|
23
|
+
tooltip: 1500,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
static getNavBar() {
|
|
27
|
+
return {
|
|
28
|
+
width: '100%',
|
|
29
|
+
display: 'flex',
|
|
30
|
+
justifyContent: 'center',
|
|
31
|
+
zIndex: Style.getZIndex().drawer,
|
|
32
|
+
position: 'fixed',
|
|
33
|
+
overflowX: 'scroll',
|
|
34
|
+
overflowY: 'hidden',
|
|
35
|
+
scrollbarWidth: 'none',
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
static getShadow(depth) {
|
|
39
|
+
const shadows = [
|
|
40
|
+
'none',
|
|
41
|
+
'0px 2px 1px -1px rgba(0,0,0,0.2),0px 1px 1px 0px rgba(0,0,0,0.14),0px 1px 3px 0px rgba(0,0,0,0.12)',
|
|
42
|
+
'0px 3px 1px -2px rgba(0,0,0,0.2),0px 2px 2px 0px rgba(0,0,0,0.14),0px 1px 5px 0px rgba(0,0,0,0.12)',
|
|
43
|
+
'0px 3px 3px -2px rgba(0,0,0,0.2),0px 3px 4px 0px rgba(0,0,0,0.14),0px 1px 8px 0px rgba(0,0,0,0.12)',
|
|
44
|
+
'0px 2px 4px -1px rgba(0,0,0,0.2),0px 4px 5px 0px rgba(0,0,0,0.14),0px 1px 10px 0px rgba(0,0,0,0.12)',
|
|
45
|
+
'0px 3px 5px -1px rgba(0,0,0,0.2),0px 5px 8px 0px rgba(0,0,0,0.14),0px 1px 14px 0px rgba(0,0,0,0.12)',
|
|
46
|
+
'0px 3px 5px -1px rgba(0,0,0,0.2),0px 6px 10px 0px rgba(0,0,0,0.14),0px 1px 18px 0px rgba(0,0,0,0.12)',
|
|
47
|
+
'0px 4px 5px -2px rgba(0,0,0,0.2),0px 7px 10px 1px rgba(0,0,0,0.14),0px 2px 16px 1px rgba(0,0,0,0.12)',
|
|
48
|
+
'0px 5px 5px -3px rgba(0,0,0,0.2),0px 8px 10px 1px rgba(0,0,0,0.14),0px 3px 14px 2px rgba(0,0,0,0.12)',
|
|
49
|
+
'0px 5px 6px -3px rgba(0,0,0,0.2),0px 9px 12px 1px rgba(0,0,0,0.14),0px 3px 16px 2px rgba(0,0,0,0.12)',
|
|
50
|
+
'0px 6px 6px -3px rgba(0,0,0,0.2),0px 10px 14px 1px rgba(0,0,0,0.14),0px 4px 18px 3px rgba(0,0,0,0.12)',
|
|
51
|
+
'0px 6px 7px -4px rgba(0,0,0,0.2),0px 11px 15px 1px rgba(0,0,0,0.14),0px 4px 20px 3px rgba(0,0,0,0.12)',
|
|
52
|
+
'0px 7px 8px -4px rgba(0,0,0,0.2),0px 12px 17px 2px rgba(0,0,0,0.14),0px 5px 22px 4px rgba(0,0,0,0.12)',
|
|
53
|
+
'0px 7px 8px -4px rgba(0,0,0,0.2),0px 13px 19px 2px rgba(0,0,0,0.14),0px 5px 24px 4px rgba(0,0,0,0.12)',
|
|
54
|
+
'0px 7px 9px -4px rgba(0,0,0,0.2),0px 14px 21px 2px rgba(0,0,0,0.14),0px 5px 26px 4px rgba(0,0,0,0.12)',
|
|
55
|
+
'0px 8px 9px -5px rgba(0,0,0,0.2),0px 15px 22px 2px rgba(0,0,0,0.14),0px 6px 28px 5px rgba(0,0,0,0.12)',
|
|
56
|
+
'0px 8px 10px -5px rgba(0,0,0,0.2),0px 16px 24px 2px rgba(0,0,0,0.14),0px 6px 30px 5px rgba(0,0,0,0.12)',
|
|
57
|
+
'0px 8px 11px -5px rgba(0,0,0,0.2),0px 17px 26px 2px rgba(0,0,0,0.14),0px 6px 32px 5px rgba(0,0,0,0.12)',
|
|
58
|
+
'0px 9px 11px -5px rgba(0,0,0,0.2),0px 18px 28px 2px rgba(0,0,0,0.14),0px 7px 34px 6px rgba(0,0,0,0.12)',
|
|
59
|
+
'0px 9px 12px -6px rgba(0,0,0,0.2),0px 19px 29px 2px rgba(0,0,0,0.14),0px 7px 36px 6px rgba(0,0,0,0.12)',
|
|
60
|
+
'0px 10px 13px -6px rgba(0,0,0,0.2),0px 20px 31px 3px rgba(0,0,0,0.14),0px 8px 38px 7px rgba(0,0,0,0.12)',
|
|
61
|
+
'0px 10px 13px -6px rgba(0,0,0,0.2),0px 21px 33px 3px rgba(0,0,0,0.14),0px 8px 40px 7px rgba(0,0,0,0.12)',
|
|
62
|
+
'0px 10px 14px -6px rgba(0,0,0,0.2),0px 22px 35px 3px rgba(0,0,0,0.14),0px 8px 42px 7px rgba(0,0,0,0.12)',
|
|
63
|
+
'0px 11px 14px -7px rgba(0,0,0,0.2),0px 23px 36px 3px rgba(0,0,0,0.14),0px 9px 44px 8px rgba(0,0,0,0.12)',
|
|
64
|
+
'0px 11px 15px -7px rgba(0,0,0,0.2),0px 24px 38px 3px rgba(0,0,0,0.14),0px 9px 46px 8px rgba(0,0,0,0.12)',
|
|
65
|
+
];
|
|
66
|
+
if (depth > shadows.length || depth < 0) {
|
|
67
|
+
throw new Error(`min depth is 0, max depth is ${shadows.length}. Sent ${depth}`);
|
|
68
|
+
}
|
|
69
|
+
return shadows[depth];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
*
|
|
73
|
+
*
|
|
74
|
+
*
|
|
75
|
+
*
|
|
76
|
+
* Below is all the CSS injection from js
|
|
77
|
+
*
|
|
78
|
+
*
|
|
79
|
+
*
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* Call this to add css as a style-sheet, so you can do css selectors like hover td {} etc
|
|
83
|
+
*/
|
|
84
|
+
static getStyleClassName(cssString, debug = false) {
|
|
85
|
+
if (debug) {
|
|
86
|
+
console.log('getStyleClassName', cssString);
|
|
87
|
+
}
|
|
88
|
+
// console.time('getStyleClassName')
|
|
89
|
+
const className = `css-${this.hashCSS(cssString, debug)}`;
|
|
90
|
+
this.injectStyle(className, cssString, debug);
|
|
91
|
+
// console.timeEnd('getStyleClassName')
|
|
92
|
+
return className;
|
|
93
|
+
}
|
|
94
|
+
// SSR helper to get all collected styles
|
|
95
|
+
static getCSS() {
|
|
96
|
+
console.warn('this does not work yet, in root layout need to add a context thing so it attaches css to style when streamed from server');
|
|
97
|
+
return Array.from(this.cssMap.values()).join('\n');
|
|
98
|
+
}
|
|
99
|
+
// Call after SSR render to clean up for next render
|
|
100
|
+
static flush() {
|
|
101
|
+
this.styleCache.clear();
|
|
102
|
+
this.cssMap.clear();
|
|
103
|
+
}
|
|
104
|
+
static hashCSS(css, debug = false) {
|
|
105
|
+
const canonicalize = (obj) => {
|
|
106
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
107
|
+
// Return primitives as is
|
|
108
|
+
return obj;
|
|
109
|
+
}
|
|
110
|
+
if (Array.isArray(obj)) {
|
|
111
|
+
// Recursively canonicalize array elements
|
|
112
|
+
return obj.map(canonicalize);
|
|
113
|
+
}
|
|
114
|
+
// 1. Get and sort the keys of the current object level
|
|
115
|
+
const sortedKeys = Object.keys(obj).sort();
|
|
116
|
+
const canonical = {};
|
|
117
|
+
// cast object to record
|
|
118
|
+
const sourceObj = obj;
|
|
119
|
+
// 2. Build a new object using the sorted keys, and recursively process values
|
|
120
|
+
for (const key of sortedKeys) {
|
|
121
|
+
canonical[key] = canonicalize(sourceObj[key]);
|
|
122
|
+
}
|
|
123
|
+
return canonical;
|
|
124
|
+
};
|
|
125
|
+
const normalize = (val) => {
|
|
126
|
+
if (typeof val === 'string') {
|
|
127
|
+
return val;
|
|
128
|
+
}
|
|
129
|
+
if (typeof val === 'object' && val !== null) {
|
|
130
|
+
const canonicalObject = canonicalize(val);
|
|
131
|
+
// Step B: Stringify the canonical object without a replacer
|
|
132
|
+
return JSON.stringify(canonicalObject);
|
|
133
|
+
}
|
|
134
|
+
return String(val);
|
|
135
|
+
};
|
|
136
|
+
const normalizedInput = normalize(css);
|
|
137
|
+
if (debug) {
|
|
138
|
+
console.log('normalizedInput', normalizedInput);
|
|
139
|
+
}
|
|
140
|
+
// Example hash using a simple DJB2 hash (or use a stronger hash like SHA-1/MD5 if needed)
|
|
141
|
+
let hash = 5381;
|
|
142
|
+
for (let i = 0; i < normalizedInput.length; i++) {
|
|
143
|
+
hash = (hash * 33) ^ normalizedInput.charCodeAt(i);
|
|
144
|
+
}
|
|
145
|
+
return (hash >>> 0).toString(36); // base36 for short string
|
|
146
|
+
}
|
|
147
|
+
static injectStyle(className, css, debug = false) {
|
|
148
|
+
if (this.styleCache.has(className)) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const finalCSS = this.processCSS(className, css, false, debug);
|
|
152
|
+
// If on server, store it only
|
|
153
|
+
if (typeof window === 'undefined') {
|
|
154
|
+
this.cssMap.set(className, finalCSS);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
const styleEl = document.createElement('style');
|
|
158
|
+
styleEl.textContent = finalCSS;
|
|
159
|
+
document.head.appendChild(styleEl);
|
|
160
|
+
}
|
|
161
|
+
this.styleCache.add(className);
|
|
162
|
+
this.cssMap.set(className, finalCSS);
|
|
163
|
+
}
|
|
164
|
+
static processCSS(className, css, isRecursive = false, debug = false) {
|
|
165
|
+
// Helper: remove trailing colon from selectors (e.g. 'td:' -> 'td')
|
|
166
|
+
const cleanSelector = (sel) => {
|
|
167
|
+
return sel.replace(/:$/g, '');
|
|
168
|
+
};
|
|
169
|
+
// Convert camelCase to kebab-case for property names
|
|
170
|
+
const toKebabCase = (str) => {
|
|
171
|
+
return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
|
172
|
+
};
|
|
173
|
+
// Helper: Remove the specific '}' that closed the block from the accumulated lines
|
|
174
|
+
// so we don't duplicate it when wrapping the result in new braces.
|
|
175
|
+
const removeLastBrace = (lines) => {
|
|
176
|
+
if (lines.length === 0)
|
|
177
|
+
return;
|
|
178
|
+
const lastIdx = lines.length - 1;
|
|
179
|
+
const lastLine = lines[lastIdx];
|
|
180
|
+
const braceIdx = lastLine.lastIndexOf('}');
|
|
181
|
+
if (braceIdx !== -1) {
|
|
182
|
+
// Remove the brace
|
|
183
|
+
const newLine = lastLine.substring(0, braceIdx) + lastLine.substring(braceIdx + 1);
|
|
184
|
+
// If the line is now empty (or just whitespace), remove it entirely
|
|
185
|
+
if (!newLine.trim()) {
|
|
186
|
+
lines.pop();
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
// eslint-disable-next-line no-param-reassign
|
|
190
|
+
lines[lastIdx] = newLine;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
const requiresQuotes = new Set([
|
|
195
|
+
'content',
|
|
196
|
+
'quotes',
|
|
197
|
+
'cue',
|
|
198
|
+
'cue-before',
|
|
199
|
+
'cue-after',
|
|
200
|
+
'src',
|
|
201
|
+
]);
|
|
202
|
+
// List of properties that should have units if the value is a number
|
|
203
|
+
const lengthProps = new Set([
|
|
204
|
+
'width', 'height', 'top', 'left', 'right', 'bottom',
|
|
205
|
+
'margin', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left',
|
|
206
|
+
'padding', 'padding-top', 'padding-right', 'padding-bottom', 'padding-left',
|
|
207
|
+
'font-size', 'border-width', 'border-radius', 'gap', 'column-gap', 'row-gap',
|
|
208
|
+
'min-width', 'min-height', 'max-width', 'max-height',
|
|
209
|
+
]);
|
|
210
|
+
const non_recursive_at_rules = [
|
|
211
|
+
'@keyframes',
|
|
212
|
+
'@-webkit-keyframes',
|
|
213
|
+
'@font-face',
|
|
214
|
+
'@counter-style',
|
|
215
|
+
];
|
|
216
|
+
const isNonRecursiveRule = (ruleName) => {
|
|
217
|
+
return non_recursive_at_rules.some((prefix) => ruleName.startsWith(prefix));
|
|
218
|
+
};
|
|
219
|
+
const normalizeValue = (property, value) => {
|
|
220
|
+
if (!lengthProps.has(property)) {
|
|
221
|
+
return value;
|
|
222
|
+
}
|
|
223
|
+
// If it's a number, add px
|
|
224
|
+
if (typeof value === 'number') {
|
|
225
|
+
return `${value}px`;
|
|
226
|
+
}
|
|
227
|
+
// If it's a numeric string with no unit, add px
|
|
228
|
+
if (typeof value === 'string') {
|
|
229
|
+
const trimmed = value.trim();
|
|
230
|
+
// Match numeric string (integer or decimal), no unit
|
|
231
|
+
if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
|
|
232
|
+
return `${trimmed}px`;
|
|
233
|
+
}
|
|
234
|
+
// If it ends in known units or keywords, leave it alone
|
|
235
|
+
return trimmed;
|
|
236
|
+
}
|
|
237
|
+
return value;
|
|
238
|
+
};
|
|
239
|
+
// Convert a line like "backgroundColor: red;" to "background-color: red;"
|
|
240
|
+
const normalizeCSSLine = (line) => {
|
|
241
|
+
const colonIndex = line.indexOf(':');
|
|
242
|
+
if (colonIndex === -1) {
|
|
243
|
+
return line; // Not a CSS property line
|
|
244
|
+
}
|
|
245
|
+
const rawProperty = line.slice(0, colonIndex).trim();
|
|
246
|
+
let value = line.slice(colonIndex + 1).trim();
|
|
247
|
+
const property = toKebabCase(rawProperty);
|
|
248
|
+
// Remove trailing comma
|
|
249
|
+
if (value.endsWith(',')) {
|
|
250
|
+
value = value.slice(0, -1).trim();
|
|
251
|
+
}
|
|
252
|
+
// Only strip quotes if it's NOT a property like 'content'
|
|
253
|
+
if (!requiresQuotes.has(property)) {
|
|
254
|
+
// Strip leading/trailing double or single quotes
|
|
255
|
+
// This handles: "Arial" -> Arial or 'Arial' -> Arial
|
|
256
|
+
if ((value.startsWith('"') && value.endsWith('"')) ||
|
|
257
|
+
(value.startsWith("'") && value.endsWith("'"))) {
|
|
258
|
+
value = value.slice(1, -1);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
value = normalizeValue(property, value);
|
|
262
|
+
let cssLine = `${property}: ${value}`;
|
|
263
|
+
if (!cssLine.endsWith(';')) {
|
|
264
|
+
cssLine += ';';
|
|
265
|
+
}
|
|
266
|
+
return cssLine;
|
|
267
|
+
};
|
|
268
|
+
// Convert CSS object into flat lines
|
|
269
|
+
const objectToLines = (obj) => {
|
|
270
|
+
const lines = [];
|
|
271
|
+
const sourceObj = obj;
|
|
272
|
+
for (const key in sourceObj) {
|
|
273
|
+
const value = sourceObj[key];
|
|
274
|
+
if (typeof value === 'object' && value !== null) {
|
|
275
|
+
lines.push(`${key} {`);
|
|
276
|
+
const nested = objectToLines(value);
|
|
277
|
+
lines.push(...nested);
|
|
278
|
+
lines.push('}');
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
// Note:
|
|
282
|
+
// - We add a comma here to support object syntax, normalizeCSSLine will handle removing it and adding semicolons later.
|
|
283
|
+
// - Don't stringify strings, to avoid double-escaping quotes (e.g. content: '"*"')
|
|
284
|
+
const finalValue = typeof value === 'string' ? value : String(value);
|
|
285
|
+
lines.push(`${key}: ${finalValue},`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return lines;
|
|
289
|
+
};
|
|
290
|
+
// Prepare raw lines
|
|
291
|
+
const lines = typeof css === 'string'
|
|
292
|
+
? css.trim().split('\n').map((line) => line.trim()).filter(Boolean)
|
|
293
|
+
: objectToLines(css);
|
|
294
|
+
const topLevelRules = [];
|
|
295
|
+
const nestedRules = [];
|
|
296
|
+
const atRules = [];
|
|
297
|
+
let currentNestedSelector = null;
|
|
298
|
+
let insideNestedBlock = false;
|
|
299
|
+
let nestedBlockLines = [];
|
|
300
|
+
let currentAtRule = null;
|
|
301
|
+
let insideAtRule = false;
|
|
302
|
+
let atRuleLines = [];
|
|
303
|
+
let atRuleBraceCount = 0;
|
|
304
|
+
// if (debug) {
|
|
305
|
+
// console.log('lines', lines)
|
|
306
|
+
// }
|
|
307
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
308
|
+
for (const line of lines) {
|
|
309
|
+
// PRIORITY 1: If we are actively collecting an At-Rule (@media), we consume EVERYTHING
|
|
310
|
+
// until braces balance. We do NOT check for '&' or property types here, because
|
|
311
|
+
// they belong to the recursive scope.
|
|
312
|
+
if (insideAtRule) {
|
|
313
|
+
// Do not normalize here, accumulate raw lines
|
|
314
|
+
atRuleLines.push(line);
|
|
315
|
+
// Count braces to track nesting depth
|
|
316
|
+
atRuleBraceCount += (line.match(/{/g) || []).length;
|
|
317
|
+
atRuleBraceCount -= (line.match(/}/g) || []).length;
|
|
318
|
+
// When we've closed all braces, the at-rule is complete
|
|
319
|
+
if (atRuleBraceCount === 0) {
|
|
320
|
+
removeLastBrace(atRuleLines);
|
|
321
|
+
// Recursively process the content inside the media query
|
|
322
|
+
if (currentAtRule && isNonRecursiveRule(currentAtRule)) {
|
|
323
|
+
const normalizedBlock = atRuleLines.map((l) => normalizeCSSLine(l)).join('\n');
|
|
324
|
+
atRules.push(`${currentAtRule} {\n${normalizedBlock}\n}`);
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
const innerCSS = this.processCSS(className, atRuleLines.join('\n'), false, debug);
|
|
328
|
+
// if (debug) {
|
|
329
|
+
// console.log('innerCSS', innerCSS)
|
|
330
|
+
// }
|
|
331
|
+
atRules.push(`${currentAtRule} { ${innerCSS} }`);
|
|
332
|
+
}
|
|
333
|
+
currentAtRule = null;
|
|
334
|
+
insideAtRule = false;
|
|
335
|
+
atRuleLines = [];
|
|
336
|
+
}
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
// PRIORITY 2: If we are inside a generic nested block (like &:focus), consume lines
|
|
340
|
+
if (insideNestedBlock) {
|
|
341
|
+
if (line.includes('}')) {
|
|
342
|
+
// end nested block
|
|
343
|
+
insideNestedBlock = false;
|
|
344
|
+
const beforeBrace = line.split('}')[0].trim();
|
|
345
|
+
// if (debug) {
|
|
346
|
+
// console.log('beforeBrace', beforeBrace)
|
|
347
|
+
// }
|
|
348
|
+
if (beforeBrace) {
|
|
349
|
+
nestedBlockLines.push(normalizeCSSLine(beforeBrace));
|
|
350
|
+
}
|
|
351
|
+
if (currentNestedSelector) {
|
|
352
|
+
// if (debug) {
|
|
353
|
+
// console.log('nestedBlockLines', nestedBlockLines)
|
|
354
|
+
// }
|
|
355
|
+
nestedRules.push(`${cleanSelector(currentNestedSelector)} { ${nestedBlockLines.join(' ')} }`);
|
|
356
|
+
}
|
|
357
|
+
currentNestedSelector = null;
|
|
358
|
+
nestedBlockLines = [];
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
nestedBlockLines.push(normalizeCSSLine(line));
|
|
362
|
+
}
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
// PRIORITY 3: Start of a new At-Rule
|
|
366
|
+
if (line.startsWith('@')) {
|
|
367
|
+
// Start of at-rule
|
|
368
|
+
const atRulePart = line.split('{')[0].trim();
|
|
369
|
+
currentAtRule = atRulePart;
|
|
370
|
+
atRuleLines = [];
|
|
371
|
+
if (line.includes('{')) {
|
|
372
|
+
insideAtRule = true;
|
|
373
|
+
atRuleBraceCount = 1;
|
|
374
|
+
// Add rest of line after '{' if any
|
|
375
|
+
const afterBrace = line.split('{').slice(1).join('{').trim();
|
|
376
|
+
if (afterBrace) {
|
|
377
|
+
// Do not normalize here, keep raw line for recursive processing
|
|
378
|
+
atRuleLines.push(afterBrace);
|
|
379
|
+
// Check if there are closing braces on same line
|
|
380
|
+
atRuleBraceCount += (afterBrace.match(/{/g) || []).length;
|
|
381
|
+
atRuleBraceCount -= (afterBrace.match(/}/g) || []).length;
|
|
382
|
+
if (atRuleBraceCount === 0) {
|
|
383
|
+
removeLastBrace(atRuleLines);
|
|
384
|
+
// Recursively process the content inside the media query
|
|
385
|
+
if (isNonRecursiveRule(currentAtRule)) {
|
|
386
|
+
const normalizedBlock = atRuleLines.map((l) => normalizeCSSLine(l)).join('\n');
|
|
387
|
+
atRules.push(`${currentAtRule} {\n${normalizedBlock}\n}`);
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
// Pass isRecursive=false to wrap inner properties with the classname
|
|
391
|
+
const innerCSS = this.processCSS(className, atRuleLines.join('\n'), false, debug);
|
|
392
|
+
if (debug) {
|
|
393
|
+
console.log('innerCSS 2', innerCSS);
|
|
394
|
+
}
|
|
395
|
+
atRules.push(`${currentAtRule} { ${innerCSS} }`);
|
|
396
|
+
}
|
|
397
|
+
currentAtRule = null;
|
|
398
|
+
insideAtRule = false;
|
|
399
|
+
atRuleLines = [];
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
// PRIORITY 4: Start of a new Nested Selector
|
|
406
|
+
if (line.startsWith('&')) {
|
|
407
|
+
// Start of nested selector
|
|
408
|
+
if (currentNestedSelector && nestedBlockLines.length) {
|
|
409
|
+
nestedRules.push(`${cleanSelector(currentNestedSelector)} { ${nestedBlockLines.join(' ')} }`);
|
|
410
|
+
}
|
|
411
|
+
// Start new nested selector (grab until first '{' or whole line)
|
|
412
|
+
const selectorPart = line.split('{')[0].trim();
|
|
413
|
+
currentNestedSelector = selectorPart;
|
|
414
|
+
nestedBlockLines = [];
|
|
415
|
+
if (line.includes('{')) {
|
|
416
|
+
insideNestedBlock = true;
|
|
417
|
+
// Add rest of line after '{' if any
|
|
418
|
+
const afterBrace = line.split('{').slice(1).join('{').trim();
|
|
419
|
+
if (afterBrace) {
|
|
420
|
+
nestedBlockLines.push(normalizeCSSLine(afterBrace));
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
// PRIORITY 5: Standard property
|
|
426
|
+
topLevelRules.push(normalizeCSSLine(line));
|
|
427
|
+
}
|
|
428
|
+
// Close any remaining nested block
|
|
429
|
+
if (insideNestedBlock && currentNestedSelector && nestedBlockLines.length) {
|
|
430
|
+
// if (debug) {
|
|
431
|
+
// console.log('nestedBlockLines', nestedBlockLines)
|
|
432
|
+
// }
|
|
433
|
+
// if (debug) console.log('zzzzz2', `${cleanSelector(currentNestedSelector)} { ${nestedBlockLines.join(' ')} }`)
|
|
434
|
+
nestedRules.push(`${cleanSelector(currentNestedSelector)} { ${nestedBlockLines.join(' ')} }`);
|
|
435
|
+
}
|
|
436
|
+
// Handle edge case where atRule is unfinished (though valid CSS usually isn't)
|
|
437
|
+
if (currentAtRule && atRuleLines.length) {
|
|
438
|
+
if (isNonRecursiveRule(currentAtRule)) {
|
|
439
|
+
const normalizedBlock = atRuleLines.map((l) => normalizeCSSLine(l)).join('\n');
|
|
440
|
+
atRules.push(`${currentAtRule} {\n${normalizedBlock}\n}`);
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
const innerCSS = this.processCSS(className, atRuleLines.join('\n'), false, debug);
|
|
444
|
+
atRules.push(`${currentAtRule} { ${innerCSS} }`);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
// if (debug) {
|
|
448
|
+
// console.log('topLevelRules', topLevelRules)
|
|
449
|
+
// }
|
|
450
|
+
// Compose final CSS:
|
|
451
|
+
// Only generate top-level block if it's not recursive AND has actual rules
|
|
452
|
+
// (If isRecursive is true, we just return the unwrapped rules, expecting the caller to wrap them if needed.
|
|
453
|
+
// But our recursive calls for media queries now use isRecursive=false, so this logic mostly affects the very root call.)
|
|
454
|
+
const topLevelCSS = (!isRecursive && topLevelRules.length > 0)
|
|
455
|
+
? `.${className} { ${topLevelRules.join(' ')} }`
|
|
456
|
+
: topLevelRules.join(' '); // If recursive, return raw properties
|
|
457
|
+
// Replace & in nested rules with .classname
|
|
458
|
+
const nestedCSS = nestedRules.map((rule) => rule.replace(/&/g, `.${className}`)).join(' ');
|
|
459
|
+
// atRules already contain full CSS blocks thanks to recursion, but we need to ensure inner & replacement happened
|
|
460
|
+
// The recursion already handles replacement of & with .classname inside the recursive call!
|
|
461
|
+
// So we just join them.
|
|
462
|
+
const atCSS = atRules.join(' ');
|
|
463
|
+
// if (debug) {
|
|
464
|
+
// console.log('atRules', atRules);
|
|
465
|
+
// }
|
|
466
|
+
const finalCSS = `${topLevelCSS} ${nestedCSS} ${atCSS}`.trim();
|
|
467
|
+
return finalCSS;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
Style.styleCache = new Set();
|
|
471
|
+
Style.cssMap = new Map(); // key: className, value: finalCSS
|
package/dist/Text.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Text.d.ts","sourceRoot":"","sources":["../src/Text.ts"],"names":[],"mappings":"AACA,qBAAa,IAAI;WACD,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM;WAgCzC,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;CAWlD"}
|
package/dist/Text.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export class Text {
|
|
2
|
+
static levenshtein(a, b) {
|
|
3
|
+
const matrix = [];
|
|
4
|
+
if (!a || !b) {
|
|
5
|
+
return 0;
|
|
6
|
+
}
|
|
7
|
+
// Initialize the matrix with base case values
|
|
8
|
+
for (let i = 0; i <= a.length; i++) {
|
|
9
|
+
matrix[i] = [i];
|
|
10
|
+
}
|
|
11
|
+
for (let j = 0; j <= b.length; j++) {
|
|
12
|
+
matrix[0][j] = j;
|
|
13
|
+
}
|
|
14
|
+
// Populate the matrix with distances
|
|
15
|
+
for (let i = 1; i <= a.length; i++) {
|
|
16
|
+
for (let j = 1; j <= b.length; j++) {
|
|
17
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
18
|
+
matrix[i][j] = Math.min(matrix[i - 1][j] + 1, // Deletion
|
|
19
|
+
matrix[i][j - 1] + 1, // Insertion
|
|
20
|
+
matrix[i - 1][j - 1] + cost);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// The final value is the Levenshtein distance
|
|
24
|
+
return matrix[a.length][b.length];
|
|
25
|
+
}
|
|
26
|
+
static toSentenceCase(str) {
|
|
27
|
+
if (!str) {
|
|
28
|
+
return '';
|
|
29
|
+
}
|
|
30
|
+
// Trim the string to remove extra whitespace
|
|
31
|
+
const trimmed = str.trim();
|
|
32
|
+
// Convert the first letter to uppercase and the rest to lowercase
|
|
33
|
+
return trimmed.charAt(0).toUpperCase() + trimmed.slice(1).toLowerCase();
|
|
34
|
+
}
|
|
35
|
+
}
|