Repository navigation
Expand file tree
/
Copy pathhelpers.mjs
More file actions
316 lines (293 loc) · 9.51 KB
/
Copy pathhelpers.mjs
File metadata and controls
316 lines (293 loc) · 9.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// src/assert.ts
function isObject(value) {
return typeof value === "object" && value != null && !Array.isArray(value);
}
var isObjectOrArray = (obj) => typeof obj === "object" && obj !== null;
// src/compact.ts
function compact(value) {
return Object.fromEntries(Object.entries(value ?? {}).filter(([_, value2]) => value2 !== void 0));
}
// src/condition.ts
var isBaseCondition = (v) => v === "base";
function filterBaseConditions(c) {
return c.slice().filter((v) => !isBaseCondition(v));
}
// src/hash.ts
function toChar(code) {
return String.fromCharCode(code + (code > 25 ? 39 : 97));
}
function toName(code) {
let name = "";
let x;
for (x = Math.abs(code); x > 52; x = x / 52 | 0) name = toChar(x % 52) + name;
return toChar(x % 52) + name;
}
function toPhash(h, x) {
let i = x.length;
while (i) h = h * 33 ^ x.charCodeAt(--i);
return h;
}
function toHash(value) {
return toName(toPhash(5381, value) >>> 0);
}
// src/important.ts
var importantRegex = /\s*!(important)?/i;
function isImportant(value) {
return typeof value === "string" ? importantRegex.test(value) : false;
}
function withoutImportant(value) {
return typeof value === "string" ? value.replace(importantRegex, "").trim() : value;
}
function withoutSpace(str) {
return typeof str === "string" ? str.replaceAll(" ", "_") : str;
}
// src/memo.ts
var memo = (fn) => {
const cache = /* @__PURE__ */ new Map();
const get = (...args) => {
const key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
const result = fn(...args);
cache.set(key, result);
return result;
};
return get;
};
// src/merge-props.ts
var MERGE_OMIT = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
function mergeProps(...sources) {
return sources.reduce((prev, obj) => {
if (!obj) return prev;
Object.keys(obj).forEach((key) => {
if (MERGE_OMIT.has(key)) return;
const prevValue = prev[key];
const value = obj[key];
if (isObject(prevValue) && isObject(value)) {
prev[key] = mergeProps(prevValue, value);
} else {
prev[key] = value;
}
});
return prev;
}, {});
}
// src/walk-object.ts
var isNotNullish = (element) => element != null;
function walkObject(target, predicate, options = {}) {
const { stop, getKey } = options;
function inner(value, path = []) {
if (isObjectOrArray(value)) {
const result = {};
for (const [prop, child] of Object.entries(value)) {
const key = getKey?.(prop, child) ?? prop;
const childPath = [...path, key];
if (stop?.(value, childPath)) {
return predicate(value, path);
}
const next = inner(child, childPath);
if (isNotNullish(next)) {
result[key] = next;
}
}
return result;
}
return predicate(value, path);
}
return inner(target);
}
function mapObject(obj, fn) {
if (Array.isArray(obj)) return obj.map((value) => fn(value));
if (!isObject(obj)) return fn(obj);
return walkObject(obj, (value) => fn(value));
}
// src/normalize-style-object.ts
function toResponsiveObject(values, breakpoints) {
return values.reduce(
(acc, current, index) => {
const key = breakpoints[index];
if (current != null) {
acc[key] = current;
}
return acc;
},
{}
);
}
function normalizeStyleObject(styles, context, shorthand = true) {
const { utility, conditions } = context;
const { hasShorthand, resolveShorthand } = utility;
return walkObject(
styles,
(value) => {
return Array.isArray(value) ? toResponsiveObject(value, conditions.breakpoints.keys) : value;
},
{
stop: (value) => Array.isArray(value),
getKey: shorthand ? (prop) => hasShorthand ? resolveShorthand(prop) : prop : void 0
}
);
}
// src/classname.ts
var fallbackCondition = {
shift: (v) => v,
finalize: (v) => v,
breakpoints: { keys: [] }
};
var sanitize = (value) => typeof value === "string" ? value.replaceAll(/[\n\s]+/g, " ") : value;
function createCss(context) {
const { utility, hash, conditions: conds = fallbackCondition } = context;
const formatClassName = (str) => [utility.prefix, str].filter(Boolean).join("-");
const hashFn = (conditions, className) => {
let result;
if (hash) {
const baseArray = [...conds.finalize(conditions), className];
result = formatClassName(utility.toHash(baseArray, toHash));
} else {
const baseArray = [...conds.finalize(conditions), formatClassName(className)];
result = baseArray.join(":");
}
return result;
};
return memo(({ base, ...styles } = {}) => {
const styleObject = Object.assign(styles, base);
const normalizedObject = normalizeStyleObject(styleObject, context);
const classNames = /* @__PURE__ */ new Set();
walkObject(normalizedObject, (value, paths) => {
if (value == null) return;
const important = isImportant(value);
const [prop, ...allConditions] = conds.shift(paths);
const conditions = filterBaseConditions(allConditions);
const transformed = utility.transform(prop, withoutImportant(sanitize(value)));
let className = hashFn(conditions, transformed.className);
if (important) className = `${className}!`;
classNames.add(className);
});
return Array.from(classNames).join(" ");
});
}
function compactStyles(...styles) {
return styles.flat().filter((style) => isObject(style) && Object.keys(compact(style)).length > 0);
}
function createMergeCss(context) {
function resolve(styles) {
const allStyles = compactStyles(...styles);
if (allStyles.length === 1) return allStyles;
return allStyles.map((style) => normalizeStyleObject(style, context));
}
function mergeCss(...styles) {
return mergeProps(...resolve(styles));
}
function assignCss(...styles) {
return Object.assign({}, ...resolve(styles));
}
return { mergeCss: memo(mergeCss), assignCss };
}
// src/hypenate-property.ts
var wordRegex = /([A-Z])/g;
var msRegex = /^ms-/;
var hypenateProperty = memo((property) => {
if (property.startsWith("--")) return property;
return property.replace(wordRegex, "-$1").replace(msRegex, "-ms-").toLowerCase();
});
// src/is-css-function.ts
var fns = ["min", "max", "clamp", "calc"];
var fnRegExp = new RegExp(`^(${fns.join("|")})\\(.*\\)`);
var isCssFunction = (v) => typeof v === "string" && fnRegExp.test(v);
// src/is-css-unit.ts
var lengthUnits = "cm,mm,Q,in,pc,pt,px,em,ex,ch,rem,lh,rlh,vw,vh,vmin,vmax,vb,vi,svw,svh,lvw,lvh,dvw,dvh,cqw,cqh,cqi,cqb,cqmin,cqmax,%";
var lengthUnitsPattern = `(?:${lengthUnits.split(",").join("|")})`;
var lengthRegExp = new RegExp(`^[+-]?[0-9]*.?[0-9]+(?:[eE][+-]?[0-9]+)?${lengthUnitsPattern}$`);
var isCssUnit = (v) => typeof v === "string" && lengthRegExp.test(v);
// src/is-css-var.ts
var isCssVar = (v) => typeof v === "string" && /^var\(--.+\)$/.test(v);
// src/pattern-fns.ts
var patternFns = {
map: mapObject,
isCssFunction,
isCssVar,
isCssUnit
};
var getPatternStyles = (pattern, styles) => {
if (!pattern?.defaultValues) return styles;
const defaults = typeof pattern.defaultValues === "function" ? pattern.defaultValues(styles) : pattern.defaultValues;
return Object.assign({}, defaults, compact(styles));
};
// src/slot.ts
var getSlotRecipes = (recipe = {}) => {
const init = (slot) => ({
className: [recipe.className, slot].filter(Boolean).join("__"),
base: recipe.base?.[slot] ?? {},
variants: {},
defaultVariants: recipe.defaultVariants ?? {},
compoundVariants: recipe.compoundVariants ? getSlotCompoundVariant(recipe.compoundVariants, slot) : []
});
const slots = recipe.slots ?? [];
const recipeParts = slots.map((slot) => [slot, init(slot)]);
for (const [variantsKey, variantsSpec] of Object.entries(recipe.variants ?? {})) {
for (const [variantKey, variantSpec] of Object.entries(variantsSpec)) {
recipeParts.forEach(([slot, slotRecipe]) => {
slotRecipe.variants[variantsKey] ??= {};
slotRecipe.variants[variantsKey][variantKey] = variantSpec[slot] ?? {};
});
}
}
return Object.fromEntries(recipeParts);
};
var getSlotCompoundVariant = (compoundVariants, slotName) => compoundVariants.filter((compoundVariant) => compoundVariant.css[slotName]).map((compoundVariant) => ({ ...compoundVariant, css: compoundVariant.css[slotName] }));
// src/split-props.ts
function splitProps(props, ...keys) {
const descriptors = Object.getOwnPropertyDescriptors(props);
const dKeys = Object.keys(descriptors);
const split = (k) => {
const clone = {};
for (let i = 0; i < k.length; i++) {
const key = k[i];
if (descriptors[key]) {
Object.defineProperty(clone, key, descriptors[key]);
delete descriptors[key];
}
}
return clone;
};
const fn = (key) => split(Array.isArray(key) ? key : dKeys.filter(key));
return keys.map(fn).concat(split(dKeys));
}
// src/uniq.ts
var uniq = (...items) => {
const set = items.reduce((acc, currItems) => {
if (currItems) {
currItems.forEach((item) => acc.add(item));
}
return acc;
}, /* @__PURE__ */ new Set([]));
return Array.from(set);
};
export {
compact,
createCss,
createMergeCss,
filterBaseConditions,
getPatternStyles,
getSlotCompoundVariant,
getSlotRecipes,
hypenateProperty,
isBaseCondition,
isObject,
mapObject,
memo,
mergeProps,
patternFns,
splitProps,
toHash,
uniq,
walkObject,
withoutSpace
};
export function __spreadValues(a, b) {
return { ...a, ...b }
}
export function __objRest(source, exclude) {
return Object.fromEntries(Object.entries(source).filter(([key]) => !exclude.includes(key)))
}