Repository navigation
Expand file tree
/
Copy pathintegration.ts
More file actions
284 lines (259 loc) · 8.34 KB
/
Copy pathintegration.ts
File metadata and controls
284 lines (259 loc) · 8.34 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
/**
* Integration utilities for other libraries to use the alias and parsing system
*/
import { resolveProperty, getAllAliases, registerAlias, registerAliases } from './aliases.js';
import { parse as coreParse } from './index.js';
import type { ParsedStyles, ParsedStyle } from './types.js';
/**
* Integration helper for external libraries
* Provides utilities to integrate with the breakpoint parsing and alias system
*/
export class BreakpointIntegration {
/**
* Resolve a property alias to its full CSS property name
*
* @example
* const integration = new BreakpointIntegration();
* integration.resolveAlias('bg'); // Returns: 'background'
* integration.resolveAlias('unknown'); // Returns: 'unknown'
*/
resolveAlias(alias: string): string {
return resolveProperty(alias);
}
/**
* Get all registered aliases (built-in + custom)
*
* @example
* const integration = new BreakpointIntegration();
* const aliases = integration.getAliases();
* // { bg: 'background', text: 'color', ... }
*/
getAliases(): Record<string, string> {
return getAllAliases();
}
/**
* Register custom aliases for your library
*
* @example
* const integration = new BreakpointIntegration();
* integration.addAlias('bgc', 'background-color');
* integration.addAliases({
* 'txt': 'color',
* 'fw': 'font-weight'
* });
*/
addAlias(alias: string, property: string): this {
registerAlias(alias, property);
return this;
}
addAliases(aliases: Record<string, string>): this {
registerAliases(aliases);
return this;
}
/**
* Parse a style string and extract structured data
* Returns the parsed styles with resolved aliases
*
* @example
* const integration = new BreakpointIntegration();
* const parsed = integration.parseStyles('bg:blue; dark:text:white');
* // Returns: ParsedStyles with resolved properties
*/
parseStyles(input: string): ParsedStyles {
return coreParse(input);
}
/**
* Extract all properties from a style string (with aliases resolved)
* Useful for analysis, validation, or generating CSS
*
* @example
* const integration = new BreakpointIntegration();
* const properties = integration.extractProperties('bg:blue; text:white; dark:bg:black');
* // Returns: ['background', 'color']
*/
extractProperties(input: string): string[] {
const parsed = this.parseStyles(input);
const properties = new Set(parsed.styles.map(s => s.property));
return Array.from(properties).sort();
}
/**
* Extract all themes used in a style string
*
* @example
* const integration = new BreakpointIntegration();
* const themes = integration.extractThemes('bg:blue; dark:bg:black; light:bg:white');
* // Returns: ['dark', 'light']
*/
extractThemes(input: string): string[] {
const parsed = this.parseStyles(input);
const themes = new Set<string>();
parsed.styles.forEach(s => {
if (s.conditions.theme) themes.add(s.conditions.theme);
});
return Array.from(themes).sort();
}
/**
* Extract all breakpoints used in a style string
*
* @example
* const integration = new BreakpointIntegration();
* const breakpoints = integration.extractBreakpoints('p:10px; md:p:20px; lg:p:30px');
* // Returns: ['md', 'lg']
*/
extractBreakpoints(input: string): string[] {
const parsed = this.parseStyles(input);
const breakpoints = new Set<string>();
parsed.styles.forEach(s => {
if (s.conditions.breakpoint) breakpoints.add(s.conditions.breakpoint);
});
return Array.from(breakpoints).sort();
}
/**
* Extract all states used in a style string
*
* @example
* const integration = new BreakpointIntegration();
* const states = integration.extractStates('bg:blue; hover:bg:darkblue; active:bg:navy');
* // Returns: ['active', 'hover']
*/
extractStates(input: string): string[] {
const parsed = this.parseStyles(input);
const states = new Set<string>();
parsed.styles.forEach(s => {
if (s.conditions.states) {
s.conditions.states.forEach(state => states.add(state));
}
});
return Array.from(states).sort();
}
/**
* Filter styles by condition
* Useful for analyzing what styles apply under specific conditions
*
* @example
* const integration = new BreakpointIntegration();
* const darkStyles = integration.filterByCondition(
* 'bg:blue; text:white; dark:bg:black; dark:text:gray',
* { theme: 'dark' }
* );
* // Returns: ParsedStyles containing only dark theme styles
*/
filterByCondition(
input: string,
conditions: {
theme?: string;
breakpoint?: string;
states?: string[];
}
): ParsedStyle[] {
const parsed = this.parseStyles(input);
return parsed.styles.filter(style => {
if (conditions.theme && style.conditions.theme !== conditions.theme) return false;
if (conditions.breakpoint && style.conditions.breakpoint !== conditions.breakpoint) return false;
if (conditions.states && conditions.states.length > 0) {
// Check if all requested states are present in style's states
if (!style.conditions.states) return false;
if (!conditions.states.every(s => style.conditions.states!.includes(s))) return false;
}
return true;
});
}
/**
* Convert parsed styles to a plain object
* Useful for integrating with CSS-in-JS libraries
*
* @example
* const integration = new BreakpointIntegration();
* const cssObject = integration.toObject('bg:blue; text:white; p:10px');
* // Returns: { background: 'blue', color: 'white', padding: '10px' }
*/
toObject(input: string): Record<string, string> {
const parsed = this.parseStyles(input);
const obj: Record<string, string> = {};
// Only include base styles (no conditions)
parsed.styles.forEach(style => {
if (!style.conditions.theme && !style.conditions.breakpoint && (!style.conditions.states || style.conditions.states.length === 0)) {
obj[style.property] = style.value;
}
});
return obj;
}
/**
* Validate a style string
* Returns validation errors if any
*
* @example
* const integration = new BreakpointIntegration();
* const errors = integration.validate('bg:blue; invalid-syntax');
* // Returns: Array of validation error messages
*/
validate(input: string): string[] {
const errors: string[] = [];
try {
const parsed = this.parseStyles(input);
// Check for empty styles
if (parsed.styles.length === 0 && input.trim().length > 0) {
errors.push('No valid styles found');
}
// Check for styles without values
parsed.styles.forEach((style, index) => {
if (!style.value || style.value.trim() === '') {
errors.push(`Style at position ${index} has no value`);
}
if (!style.property || style.property.trim() === '') {
errors.push(`Style at position ${index} has no property`);
}
});
} catch (error) {
errors.push(`Parse error: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
return errors;
}
}
/**
* Create a new integration instance
*
* @example
* import { createIntegration } from '@componentor/breakpoint';
*
* const integration = createIntegration();
* integration.addAlias('primary', 'background-color');
* const properties = integration.extractProperties('primary:blue; dark:primary:black');
*/
export function createIntegration(): BreakpointIntegration {
return new BreakpointIntegration();
}
/**
* Quick utilities for common integration tasks
*/
export const integrationUtils = {
/**
* Quickly resolve an alias
*/
resolveAlias: (alias: string) => resolveProperty(alias),
/**
* Quickly get all aliases
*/
getAllAliases: () => getAllAliases(),
/**
* Quickly parse and extract properties
*/
extractProperties: (input: string): string[] => {
const integration = new BreakpointIntegration();
return integration.extractProperties(input);
},
/**
* Quickly convert to CSS object
*/
toObject: (input: string): Record<string, string> => {
const integration = new BreakpointIntegration();
return integration.toObject(input);
},
/**
* Quickly validate input
*/
validate: (input: string): string[] => {
const integration = new BreakpointIntegration();
return integration.validate(input);
}
};