clava 0.0.1 → 0.0.2
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/CHANGELOG.md +7 -0
- package/dist/index.d.ts +124 -0
- package/dist/index.js +522 -0
- package/dist/index.js.map +1 -0
- package/license +21 -0
- package/package.json +31 -14
- package/rolldown.config.ts +12 -0
- package/src/index.ts +698 -0
- package/src/test.ts +1996 -0
- package/src/types.ts +567 -0
- package/src/utils.ts +164 -0
- package/tsconfig.json +7 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type * as CSS from "csstype";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
StyleValue,
|
|
5
|
+
JSXCSSProperties,
|
|
6
|
+
HTMLCSSProperties,
|
|
7
|
+
} from "./types.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Converts a hyphenated CSS property name to camelCase.
|
|
11
|
+
* @example
|
|
12
|
+
* hyphenToCamel("background-color") // "backgroundColor"
|
|
13
|
+
* hyphenToCamel("--custom-var") // "--custom-var" (CSS variables are preserved)
|
|
14
|
+
*/
|
|
15
|
+
export function hyphenToCamel(str: string) {
|
|
16
|
+
// CSS custom properties (variables) should not be converted
|
|
17
|
+
if (str.startsWith("--")) {
|
|
18
|
+
return str;
|
|
19
|
+
}
|
|
20
|
+
return str.replace(/-([a-z])/gi, (_, letter) => letter.toUpperCase());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Converts a camelCase CSS property name to hyphenated form.
|
|
25
|
+
* @example
|
|
26
|
+
* camelToHyphen("backgroundColor") // "background-color"
|
|
27
|
+
* camelToHyphen("--customVar") // "--customVar" (CSS variables are preserved)
|
|
28
|
+
*/
|
|
29
|
+
export function camelToHyphen(str: string) {
|
|
30
|
+
// CSS custom properties (variables) should not be converted
|
|
31
|
+
if (str.startsWith("--")) {
|
|
32
|
+
return str;
|
|
33
|
+
}
|
|
34
|
+
return str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Parses a length value, adding "px" if it's a number.
|
|
39
|
+
* @example
|
|
40
|
+
* parseLengthValue(16); // "16px"
|
|
41
|
+
* parseLengthValue("2em"); // "2em"
|
|
42
|
+
*/
|
|
43
|
+
export function parseLengthValue(value: string | number) {
|
|
44
|
+
if (typeof value === "string") return value;
|
|
45
|
+
return `${value}px`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Parses a CSS style string into a StyleValue object.
|
|
50
|
+
* @example
|
|
51
|
+
* htmlStyleToStyleValue("background-color: red; font-size: 16px;");
|
|
52
|
+
* // { backgroundColor: "red", fontSize: "16px" }
|
|
53
|
+
*/
|
|
54
|
+
export function htmlStyleToStyleValue(styleString: string) {
|
|
55
|
+
if (!styleString) return {};
|
|
56
|
+
|
|
57
|
+
const result: StyleValue = {};
|
|
58
|
+
const declarations = styleString.split(";");
|
|
59
|
+
|
|
60
|
+
for (const declaration of declarations) {
|
|
61
|
+
const trimmed = declaration.trim();
|
|
62
|
+
if (!trimmed) continue;
|
|
63
|
+
|
|
64
|
+
const colonIndex = trimmed.indexOf(":");
|
|
65
|
+
if (colonIndex === -1) continue;
|
|
66
|
+
|
|
67
|
+
const property = trimmed.slice(0, colonIndex).trim();
|
|
68
|
+
const value = trimmed.slice(colonIndex + 1).trim();
|
|
69
|
+
if (!property) continue;
|
|
70
|
+
if (!value) continue;
|
|
71
|
+
|
|
72
|
+
const camelProperty = hyphenToCamel(property) as any;
|
|
73
|
+
result[camelProperty] = value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Converts a hyphenated style object to a camelCase StyleValue object.
|
|
81
|
+
* @example
|
|
82
|
+
* htmlObjStyleToStyleValue({ "background-color": "red", "font-size": "16px" });
|
|
83
|
+
* // { backgroundColor: "red", fontSize: "16px" }
|
|
84
|
+
*/
|
|
85
|
+
export function htmlObjStyleToStyleValue(style: HTMLCSSProperties) {
|
|
86
|
+
const result: StyleValue = {};
|
|
87
|
+
for (const [key, value] of Object.entries(style)) {
|
|
88
|
+
if (value == null) continue;
|
|
89
|
+
const property = hyphenToCamel(key) as any;
|
|
90
|
+
result[property] = parseLengthValue(value);
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Converts a camelCase style object to a StyleValue object.
|
|
97
|
+
* @example
|
|
98
|
+
* jsxStyleToStyleValue({ backgroundColor: "red", fontSize: 16 });
|
|
99
|
+
* // { backgroundColor: "red", fontSize: "16px" }
|
|
100
|
+
*/
|
|
101
|
+
export function jsxStyleToStyleValue(style: JSXCSSProperties) {
|
|
102
|
+
const result: StyleValue = {};
|
|
103
|
+
for (const [key, value] of Object.entries(style)) {
|
|
104
|
+
if (value == null) continue;
|
|
105
|
+
result[key as any] = parseLengthValue(value);
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Converts a StyleValue object to a CSS style string.
|
|
112
|
+
* @example
|
|
113
|
+
* styleValueToHTMLStyle({ backgroundColor: "red", fontSize: "16px" });
|
|
114
|
+
* // "background-color: red; font-size: 16px;"
|
|
115
|
+
*/
|
|
116
|
+
export function styleValueToHTMLStyle(style: StyleValue): string {
|
|
117
|
+
const parts: string[] = [];
|
|
118
|
+
for (const [key, value] of Object.entries(style)) {
|
|
119
|
+
if (value == null) continue;
|
|
120
|
+
parts.push(`${camelToHyphen(key)}: ${value}`);
|
|
121
|
+
}
|
|
122
|
+
if (!parts.length) return "";
|
|
123
|
+
return `${parts.join("; ")};`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Converts a StyleValue object to a hyphenated style object.
|
|
128
|
+
* @example
|
|
129
|
+
* styleValueToHTMLObjStyle({ backgroundColor: "red", fontSize: "16px" });
|
|
130
|
+
* // { "background-color": "red", "font-size": "16px" }
|
|
131
|
+
*/
|
|
132
|
+
export function styleValueToHTMLObjStyle(style: StyleValue) {
|
|
133
|
+
const result: CSS.PropertiesHyphen = {};
|
|
134
|
+
for (const [key, value] of Object.entries(style)) {
|
|
135
|
+
if (value == null) continue;
|
|
136
|
+
const property = camelToHyphen(key) as keyof HTMLCSSProperties;
|
|
137
|
+
result[property] = value;
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Converts a StyleValue object to a camelCase style object.
|
|
144
|
+
* @example
|
|
145
|
+
* styleValueToJSXStyle({ backgroundColor: "red", fontSize: "16px" });
|
|
146
|
+
* // { backgroundColor: "red", fontSize: "16px" }
|
|
147
|
+
*/
|
|
148
|
+
export function styleValueToJSXStyle(style: StyleValue) {
|
|
149
|
+
return style as JSXCSSProperties;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Type guard to check if a style object has hyphenated keys.
|
|
154
|
+
* @example
|
|
155
|
+
* isHTMLObjStyle({ "background-color": "red" }); // true
|
|
156
|
+
* isHTMLObjStyle({ backgroundColor: "red" }); // false
|
|
157
|
+
*/
|
|
158
|
+
export function isHTMLObjStyle(
|
|
159
|
+
style: CSS.Properties<any> | CSS.PropertiesHyphen<any>,
|
|
160
|
+
): style is CSS.PropertiesHyphen {
|
|
161
|
+
return Object.keys(style).some(
|
|
162
|
+
(key) => key.includes("-") && !key.startsWith("--"),
|
|
163
|
+
);
|
|
164
|
+
}
|