@yahoo/uds-create-utils 1.1.1

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.
Files changed (60) hide show
  1. package/dist/array-utils/closestItem.d.ts +4 -0
  2. package/dist/array-utils/closestItem.js +9 -0
  3. package/dist/array-utils/removeItem.d.ts +4 -0
  4. package/dist/array-utils/removeItem.js +8 -0
  5. package/dist/arrayUtils.d.ts +4 -0
  6. package/dist/arrayUtils.js +9 -0
  7. package/dist/component-style-defaults.d.ts +5 -0
  8. package/dist/component-style-defaults.js +68 -0
  9. package/dist/config-snippet.d.ts +15 -0
  10. package/dist/config-snippet.js +261 -0
  11. package/dist/index.d.ts +25 -0
  12. package/dist/index.js +25 -0
  13. package/dist/logger.d.ts +20 -0
  14. package/dist/logger.js +79 -0
  15. package/dist/math-utils/clamp.d.ts +12 -0
  16. package/dist/math-utils/clamp.js +14 -0
  17. package/dist/motion-utils/interpolate.d.ts +10 -0
  18. package/dist/motion-utils/interpolate.js +25 -0
  19. package/dist/motion-utils/resolveMotionState.d.ts +34 -0
  20. package/dist/motion-utils/resolveMotionState.js +33 -0
  21. package/dist/object-utils/entries.d.ts +4 -0
  22. package/dist/object-utils/entries.js +6 -0
  23. package/dist/object-utils/flattenObj.d.ts +12 -0
  24. package/dist/object-utils/flattenObj.js +21 -0
  25. package/dist/object-utils/fromEntries.d.ts +4 -0
  26. package/dist/object-utils/fromEntries.js +6 -0
  27. package/dist/object-utils/keys.d.ts +4 -0
  28. package/dist/object-utils/keys.js +6 -0
  29. package/dist/object-utils/mapKeys.d.ts +5 -0
  30. package/dist/object-utils/mapKeys.js +10 -0
  31. package/dist/object-utils/mapValues.d.ts +5 -0
  32. package/dist/object-utils/mapValues.js +9 -0
  33. package/dist/string-utils/arrayToUnion.d.ts +4 -0
  34. package/dist/string-utils/arrayToUnion.js +6 -0
  35. package/dist/string-utils/capitalize.d.ts +4 -0
  36. package/dist/string-utils/capitalize.js +6 -0
  37. package/dist/string-utils/componentClassName.d.ts +58 -0
  38. package/dist/string-utils/componentClassName.js +70 -0
  39. package/dist/string-utils/createTemplate.d.ts +16 -0
  40. package/dist/string-utils/createTemplate.js +142 -0
  41. package/dist/string-utils/cssVar.d.ts +55 -0
  42. package/dist/string-utils/cssVar.js +63 -0
  43. package/dist/string-utils/cssVars.d.ts +81 -0
  44. package/dist/string-utils/cssVars.js +108 -0
  45. package/dist/string-utils/dedent.d.ts +47 -0
  46. package/dist/string-utils/dedent.js +68 -0
  47. package/dist/string-utils/indent.d.ts +4 -0
  48. package/dist/string-utils/indent.js +6 -0
  49. package/dist/string-utils/join.d.ts +5 -0
  50. package/dist/string-utils/join.js +6 -0
  51. package/dist/string-utils/kebabCase.d.ts +13 -0
  52. package/dist/string-utils/kebabCase.js +14 -0
  53. package/dist/string-utils/objectToInterface.d.ts +1 -0
  54. package/dist/string-utils/objectToInterface.js +6 -0
  55. package/dist/string-utils/split.d.ts +10 -0
  56. package/dist/string-utils/split.js +12 -0
  57. package/dist/string-utils/tsProperties.d.ts +29 -0
  58. package/dist/string-utils/tsProperties.js +60 -0
  59. package/dist/tsconfig.tsbuildinfo +1 -0
  60. package/package.json +40 -0
@@ -0,0 +1,108 @@
1
+ import { cssVarDeclarations } from "./cssVar.js";
2
+ //#region src/string-utils/cssVars.ts
3
+ /**
4
+ * Tagged template function for CSS variable generation.
5
+ * Automatically converts objects to CSS variable declarations.
6
+ *
7
+ * Handles three types of inputs:
8
+ * 1. Flat objects → CSS variables
9
+ * 2. Nested objects → CSS blocks with selectors
10
+ * 3. Arrays → joined with double newlines
11
+ *
12
+ * @example Flat object (CSS variables)
13
+ * ```tsx
14
+ * const vars = { color: 'red', fontSize: '16px' };
15
+ *
16
+ * const output = cssVars`
17
+ * :root {
18
+ * ${vars}
19
+ * }
20
+ * `;
21
+ *
22
+ * // Output:
23
+ * // :root {
24
+ * // --color: red;
25
+ * // --font-size: 16px;
26
+ * // }
27
+ * ```
28
+ *
29
+ * @example Nested object (CSS blocks)
30
+ * ```tsx
31
+ * const modeBlocks = {
32
+ * dark: { colorPrimary: '#000', colorSecondary: '#333' },
33
+ * light: { colorPrimary: '#fff', colorSecondary: '#eee' }
34
+ * };
35
+ *
36
+ * const output = cssVars`${modeBlocks}`;
37
+ *
38
+ * // Output:
39
+ * // .dark {
40
+ * // --color-primary: #000;
41
+ * // --color-secondary: #333;
42
+ * // }
43
+ * //
44
+ * // .light {
45
+ * // --color-primary: #fff;
46
+ * // --color-secondary: #eee;
47
+ * // }
48
+ * ```
49
+ *
50
+ * @example Media queries (special @media handling)
51
+ * ```tsx
52
+ * const mediaQueries = {
53
+ * '@media (min-width: 768px)': { spacing: '2rem' },
54
+ * '@media (min-width: 1024px)': { spacing: '3rem' }
55
+ * };
56
+ *
57
+ * const output = cssVars`${mediaQueries}`;
58
+ *
59
+ * // Output:
60
+ * // @media (min-width: 768px) {
61
+ * // :root {
62
+ * // --spacing: 2rem;
63
+ * // }
64
+ * // }
65
+ * //
66
+ * // @media (min-width: 1024px) {
67
+ * // :root {
68
+ * // --spacing: 3rem;
69
+ * // }
70
+ * // }
71
+ * ```
72
+ *
73
+ * @example Array (joined blocks)
74
+ * ```tsx
75
+ * const blocks = ['.dark { --color: black; }', '.light { --color: white; }'];
76
+ * const output = cssVars`${blocks}`;
77
+ * // Output: joined blocks with double newlines
78
+ * ```
79
+ */
80
+ function cssVars(strings, ...expr) {
81
+ const isNestedObject = (obj) => {
82
+ return Object.values(obj).some((val) => val && typeof val === "object" && !Array.isArray(val));
83
+ };
84
+ expr.forEach((item) => {
85
+ if (item && typeof item === "object" && !Array.isArray(item)) {
86
+ let stringResult;
87
+ const itemObj = item;
88
+ if (isNestedObject(itemObj)) stringResult = Object.entries(itemObj).map(([selector, vars]) => {
89
+ const varDeclarations = cssVarDeclarations(vars).replace(/\n/g, "\n ");
90
+ if (selector.startsWith("@media ")) return `${selector} {\n :root {\n ${varDeclarations}\n }\n}`;
91
+ return `.${selector} {\n ${varDeclarations}\n}`;
92
+ }).join("\n\n");
93
+ else stringResult = cssVarDeclarations(itemObj);
94
+ Object.defineProperty(itemObj, "toString", { value() {
95
+ return stringResult;
96
+ } });
97
+ } else if (Array.isArray(item)) Object.defineProperty(item, "toString", { value() {
98
+ return item.filter(Boolean).join("\n\n");
99
+ } });
100
+ });
101
+ let str = "";
102
+ strings.forEach((string, i) => {
103
+ str += string + (expr[i] || "");
104
+ });
105
+ return str;
106
+ }
107
+ //#endregion
108
+ export { cssVars };
@@ -0,0 +1,47 @@
1
+ //#region src/string-utils/dedent.d.ts
2
+ /**
3
+ * Dedent tagged template literal function.
4
+ * Removes common leading indentation from all lines in a template literal,
5
+ * based on how the code appears in the source.
6
+ *
7
+ * Inspired by @qnighy/dedent
8
+ * @see https://github.com/qnighy/dedent-js
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const text = dedent`
13
+ * Hello World
14
+ * This line is indented
15
+ * Back to normal
16
+ * `;
17
+ * // Result:
18
+ * // "Hello World\n This line is indented\nBack to normal\n"
19
+ * ```
20
+ *
21
+ * @example With leading backslash to avoid first line issues
22
+ * ```typescript
23
+ * const text = dedent`\
24
+ * First line
25
+ * Second line
26
+ * `;
27
+ * ```
28
+ */
29
+ declare function dedent(strings: TemplateStringsArray, ...values: unknown[]): string;
30
+ /**
31
+ * Dedent utility specifically for TypeScript declarations.
32
+ * Same as dedent but ensures a trailing newline for proper file formatting.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const types = dedentTS`\
37
+ * declare module 'uds/types' {
38
+ * export interface Foo {
39
+ * bar: string;
40
+ * }
41
+ * }
42
+ * `;
43
+ * ```
44
+ */
45
+ declare function dedentTS(strings: TemplateStringsArray, ...values: unknown[]): string;
46
+ //#endregion
47
+ export { dedent, dedentTS };
@@ -0,0 +1,68 @@
1
+ //#region src/string-utils/dedent.ts
2
+ /**
3
+ * Dedent tagged template literal function.
4
+ * Removes common leading indentation from all lines in a template literal,
5
+ * based on how the code appears in the source.
6
+ *
7
+ * Inspired by @qnighy/dedent
8
+ * @see https://github.com/qnighy/dedent-js
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const text = dedent`
13
+ * Hello World
14
+ * This line is indented
15
+ * Back to normal
16
+ * `;
17
+ * // Result:
18
+ * // "Hello World\n This line is indented\nBack to normal\n"
19
+ * ```
20
+ *
21
+ * @example With leading backslash to avoid first line issues
22
+ * ```typescript
23
+ * const text = dedent`\
24
+ * First line
25
+ * Second line
26
+ * `;
27
+ * ```
28
+ */
29
+ function dedent(strings, ...values) {
30
+ let result = "";
31
+ for (let i = 0; i < strings.length; i++) {
32
+ result += strings[i];
33
+ if (i < values.length) result += String(values[i] ?? "");
34
+ }
35
+ const lines = result.split("\n");
36
+ let minIndent = Number.POSITIVE_INFINITY;
37
+ for (const line of lines) {
38
+ if (line.trim().length === 0) continue;
39
+ const match = line.match(/^[ \t]*/);
40
+ const indent = match ? match[0].length : 0;
41
+ minIndent = Math.min(minIndent, indent);
42
+ }
43
+ if (minIndent === Number.POSITIVE_INFINITY) minIndent = 0;
44
+ return lines.map((line) => {
45
+ if (line.trim().length === 0) return line.length >= minIndent ? line.slice(minIndent) : "";
46
+ return line.slice(minIndent);
47
+ }).join("\n");
48
+ }
49
+ /**
50
+ * Dedent utility specifically for TypeScript declarations.
51
+ * Same as dedent but ensures a trailing newline for proper file formatting.
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * const types = dedentTS`\
56
+ * declare module 'uds/types' {
57
+ * export interface Foo {
58
+ * bar: string;
59
+ * }
60
+ * }
61
+ * `;
62
+ * ```
63
+ */
64
+ function dedentTS(strings, ...values) {
65
+ return `${dedent(strings, ...values).trimEnd()}\n`;
66
+ }
67
+ //#endregion
68
+ export { dedent, dedentTS };
@@ -0,0 +1,4 @@
1
+ //#region src/string-utils/indent.d.ts
2
+ declare function indent(str: string, n: number): string;
3
+ //#endregion
4
+ export { indent };
@@ -0,0 +1,6 @@
1
+ //#region src/string-utils/indent.ts
2
+ function indent(str, n) {
3
+ return str.split("\n").map((l) => " ".repeat(n) + l).join("\n");
4
+ }
5
+ //#endregion
6
+ export { indent };
@@ -0,0 +1,5 @@
1
+ //#region src/string-utils/join.d.ts
2
+ type Join<T extends readonly string[], D extends string> = T extends [] ? '' : T extends [infer F extends string] ? F : T extends [infer F extends string, ...infer R extends readonly string[]] ? `${F}${D}${Join<R, D>}` : string;
3
+ declare function join<T extends readonly string[]>(...parts: T): <D extends string>(delimiter: D) => Join<T, D>;
4
+ //#endregion
5
+ export { join };
@@ -0,0 +1,6 @@
1
+ //#region src/string-utils/join.ts
2
+ function join(...parts) {
3
+ return (delimiter) => parts.join(delimiter);
4
+ }
5
+ //#endregion
6
+ export { join };
@@ -0,0 +1,13 @@
1
+ //#region src/string-utils/kebabCase.d.ts
2
+ type KebabCase<S extends string> = S extends `${infer First}${infer Rest}` ? First extends Uppercase<First> ? Rest extends `${infer RestFirst}${infer _RestRest}` ? RestFirst extends Uppercase<RestFirst> ? `${Lowercase<First>}${KebabCase<Rest>}` : `-${Lowercase<First>}${KebabCase<Rest>}` : `-${Lowercase<First>}${KebabCase<Rest>}` : `${First}${KebabCase<Rest>}` : Lowercase<S>;
3
+ /**
4
+ * Convert a string to kebab-case.
5
+ *
6
+ * - camelCase & PascalCase → hyphens
7
+ * - letter→digit boundaries → hyphens
8
+ * - collapses non-alphanumerics into single hyphens
9
+ * - trims edge hyphens, lowercases
10
+ */
11
+ declare function kebabCase<Input extends string>(input: Input): KebabCase<Input>;
12
+ //#endregion
13
+ export { KebabCase, kebabCase };
@@ -0,0 +1,14 @@
1
+ //#region src/string-utils/kebabCase.ts
2
+ /**
3
+ * Convert a string to kebab-case.
4
+ *
5
+ * - camelCase & PascalCase → hyphens
6
+ * - letter→digit boundaries → hyphens
7
+ * - collapses non-alphanumerics into single hyphens
8
+ * - trims edge hyphens, lowercases
9
+ */
10
+ function kebabCase(input) {
11
+ return input.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/([a-z])([0-9]+)/gi, "$1-$2").replace(/[^A-Za-z0-9]+/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "").toLowerCase();
12
+ }
13
+ //#endregion
14
+ export { kebabCase };
@@ -0,0 +1 @@
1
+ export declare function objectToInterface<T extends Record<string, string>>(object: T): `${Extract<keyof T, string>}: ${T[Extract<keyof T, string>]};`;
@@ -0,0 +1,6 @@
1
+ //#region src/string-utils/objectToInterface.ts
2
+ function objectToInterface(object) {
3
+ return Object.entries(object).map(([key, value]) => `${key}: ${value};`).join(" ");
4
+ }
5
+ //#endregion
6
+ export { objectToInterface };
@@ -0,0 +1,10 @@
1
+ //#region src/string-utils/split.d.ts
2
+ type Split<S extends string, D extends string> = string extends S ? string[] : S extends '' ? [] : S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S];
3
+ type BuildArray<L extends number, A extends unknown[] = []> = A['length'] extends L ? A : BuildArray<L, [...A, unknown]>;
4
+ type Decrement<N extends number> = BuildArray<N> extends [unknown, ...infer Rest] ? Rest['length'] : never;
5
+ type SplitDepth<S extends string, D extends string, N extends number> = N extends 0 ? [S] : S extends `${infer Head}${D}${infer Tail}` ? N extends 1 ? [Head, Tail] : [Head, ...SplitDepth<Tail, D, Decrement<N>>] : [S];
6
+ declare function split<T extends string, Delimiter extends string, Depth extends number = number>(str: T, delimiter: Delimiter, options?: {
7
+ depth?: Depth;
8
+ }): Split<T, Delimiter> | SplitDepth<T, Delimiter, Depth>;
9
+ //#endregion
10
+ export { split };
@@ -0,0 +1,12 @@
1
+ //#region src/string-utils/split.ts
2
+ function split(str, delimiter, options) {
3
+ if (!options || options.depth == null || options.depth <= 0) return str.split(delimiter);
4
+ const { depth } = options;
5
+ const parts = str.split(delimiter);
6
+ if (parts.length <= depth) return parts;
7
+ const head = parts.slice(0, depth);
8
+ const tail = parts.slice(depth).join(delimiter);
9
+ return [...head, tail];
10
+ }
11
+ //#endregion
12
+ export { split };
@@ -0,0 +1,29 @@
1
+ //#region src/string-utils/tsProperties.d.ts
2
+ /**
3
+ * Template literal utility for formatting TypeScript interface properties
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * const props = tsProperties`${{
8
+ * brand: void,
9
+ * primary: void
10
+ * }}`;
11
+ * // Returns: " 'brand': void;\n 'primary': void;"
12
+ * ```
13
+ */
14
+ declare function tsProperties<T extends Record<string, unknown>>(strings: TemplateStringsArray, ...expr: [T]): string;
15
+ /**
16
+ * Template literal utility for formatting TypeScript namespace type exports
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const types = tsTypes`${{
21
+ * ColorMode: "'light' | 'dark'",
22
+ * Responsive: "'sm' | 'md'"
23
+ * }}`;
24
+ * // Returns: " export type ColorMode = 'light' | 'dark';\n export type Responsive = 'sm' | 'md';"
25
+ * ```
26
+ */
27
+ declare function tsTypes<T extends Record<string, string>>(strings: TemplateStringsArray, ...expr: [T]): string;
28
+ //#endregion
29
+ export { tsProperties, tsTypes };
@@ -0,0 +1,60 @@
1
+ import { entries } from "../object-utils/entries.js";
2
+ //#region src/string-utils/tsProperties.ts
3
+ /**
4
+ * Template literal utility for formatting TypeScript interface properties
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const props = tsProperties`${{
9
+ * brand: void,
10
+ * primary: void
11
+ * }}`;
12
+ * // Returns: " 'brand': void;\n 'primary': void;"
13
+ * ```
14
+ */
15
+ function tsProperties(strings, ...expr) {
16
+ let result = "";
17
+ expr.forEach((item) => {
18
+ if (item && typeof item === "object") {
19
+ const propertyLines = entries(item).map(([key, value]) => {
20
+ return ` '${key}': ${value === void 0 || value === null ? "void" : JSON.stringify(value)};`;
21
+ }).join("\n");
22
+ Object.defineProperty(item, "toString", { value() {
23
+ return propertyLines;
24
+ } });
25
+ }
26
+ });
27
+ strings.forEach((string, i) => {
28
+ result += string + (expr[i] || "");
29
+ });
30
+ return result;
31
+ }
32
+ /**
33
+ * Template literal utility for formatting TypeScript namespace type exports
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * const types = tsTypes`${{
38
+ * ColorMode: "'light' | 'dark'",
39
+ * Responsive: "'sm' | 'md'"
40
+ * }}`;
41
+ * // Returns: " export type ColorMode = 'light' | 'dark';\n export type Responsive = 'sm' | 'md';"
42
+ * ```
43
+ */
44
+ function tsTypes(strings, ...expr) {
45
+ let result = "";
46
+ expr.forEach((item) => {
47
+ if (item && typeof item === "object") {
48
+ const typeLines = entries(item).map(([typeName, typeValue]) => ` export type ${typeName} = ${typeValue};`).join("\n");
49
+ Object.defineProperty(item, "toString", { value() {
50
+ return typeLines;
51
+ } });
52
+ }
53
+ });
54
+ strings.forEach((string, i) => {
55
+ result += string + (expr[i] || "");
56
+ });
57
+ return result;
58
+ }
59
+ //#endregion
60
+ export { tsProperties, tsTypes };