Skip to content

Commit d004749

Browse files
authored
Merge pull request #7 from dev-five-git/multi
Support multi
2 parents dcc7ee2 + b142de7 commit d004749

File tree

2 files changed

+68
-22
lines changed

2 files changed

+68
-22
lines changed

src/__tests__/utils.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ describe('organizeProps', () => {
125125
).toEqual({
126126
bg: 'linear-gradient(202deg, $primary 3.96%, #6D7EDC 85.94%)',
127127
})
128+
129+
expect(
130+
organizeProps({
131+
bg: '0px 0px 15px 0px var(--clientShadow, rgba(0, 0, 0, 0.07))',
132+
}),
133+
).toEqual({
134+
bg: '0 0 15px 0 $clientShadow',
135+
})
136+
137+
expect(
138+
organizeProps({
139+
bg: '0px 0px 15px 0px var(--clientShadow, var(--primary, rgba(0, 0, 0, 0.07)))',
140+
}),
141+
).toEqual({
142+
bg: '0 0 15px 0 $clientShadow',
143+
})
128144
})
129145
})
130146

src/utils.ts

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -163,78 +163,78 @@ const COLOR_PROPS = ['color', 'bg', 'borderColor']
163163
const SPACE_PROPS = ['m', 'p']
164164
const DEFAULT_PROPS_MAP = {
165165
flex: {
166-
default: '1 0 0',
166+
default: /1 0 0/,
167167
value: '1',
168168
},
169169
p: {
170-
default: '0px',
170+
default: /\b0(px)?\b/,
171171
value: null,
172172
},
173173
pr: {
174-
default: '0px',
174+
default: /\b0(px)?\b/,
175175
value: null,
176176
},
177177
pt: {
178-
default: '0px',
178+
default: /\b0(px)?\b/,
179179
value: null,
180180
},
181181
pb: {
182-
default: '0px',
182+
default: /\b0(px)?\b/,
183183
value: null,
184184
},
185185
px: {
186-
default: '0px',
186+
default: /\b0(px)?\b/,
187187
value: null,
188188
},
189189
py: {
190-
default: '0px',
190+
default: /\b0(px)?\b/,
191191
value: null,
192192
},
193193
pl: {
194-
default: '0px',
194+
default: /\b0(px)?\b/,
195195
value: null,
196196
},
197197
m: {
198-
default: '0px',
198+
default: /\b0(px)?\b/,
199199
value: null,
200200
},
201201
mt: {
202-
default: '0px',
202+
default: /\b0(px)?\b/,
203203
value: null,
204204
},
205205
mb: {
206-
default: '0px',
206+
default: /\b0(px)?\b/,
207207
value: null,
208208
},
209209
mr: {
210-
default: '0px',
210+
default: /\b0(px)?\b/,
211211
value: null,
212212
},
213213
ml: {
214-
default: '0px',
214+
default: /\b0(px)?\b/,
215215
value: null,
216216
},
217217
mx: {
218-
default: '0px',
218+
default: /\b0(px)?\b/,
219219
value: null,
220220
},
221221
my: {
222-
default: '0px',
222+
default: /\b0(px)?\b/,
223223
value: null,
224224
},
225225
} as const
226226

227227
const CONVERT_PROPS_MAP = {
228228
p: [
229229
{
230-
test: /^0px (\d*[1-9]|\d{2,})px$/,
230+
test: /^0(px)? (\d*[1-9]|\d{2,})px$/,
231231
value: {
232232
prop: 'px',
233233
value: (value: string) => value.split(' ')[1],
234234
},
235235
},
236236
{
237-
test: /^(\d*[1-9]|\d{2,})px 0px$/,
237+
test: /^(\d*[1-9]|\d{2,})px 0(px)?$/,
238238
value: {
239239
prop: 'py',
240240
value: (value: string) => value.split(' ')[0],
@@ -307,14 +307,14 @@ const CONVERT_PROPS_MAP = {
307307
],
308308
m: [
309309
{
310-
test: /^0px (\d*[1-9]|\d{2,})px$/,
310+
test: /^0(px)? (\d*[1-9]|\d{2,})px$/,
311311
value: {
312312
prop: 'mx',
313313
value: (value: string) => value.split(' ')[1],
314314
},
315315
},
316316
{
317-
test: /^(\d*[1-9]|\d{2,})px 0px$/,
317+
test: /^(\d*[1-9]|\d{2,})px 0(px)?$/,
318318
value: {
319319
prop: 'my',
320320
value: (value: string) => value.split(' ')[0],
@@ -402,6 +402,34 @@ const CONVERT_PROPS_VALUE_MAP = {
402402
},
403403
],
404404
}
405+
406+
function replaceAllVarFunctions(
407+
str: string,
408+
replacer: (v: string) => string,
409+
): string {
410+
let result = ''
411+
let i = 0
412+
while (i < str.length) {
413+
const varStart = str.indexOf('var(', i)
414+
if (varStart === -1) {
415+
result += str.slice(i)
416+
break
417+
}
418+
result += str.slice(i, varStart)
419+
let open = 1
420+
let end = varStart + 4
421+
for (; end < str.length; end++) {
422+
if (str[end] === '(') open++
423+
if (str[end] === ')') open--
424+
if (open === 0) break
425+
}
426+
const varContent = str.slice(varStart, end + 1)
427+
result += replacer(varContent)
428+
i = end + 1
429+
}
430+
return result
431+
}
432+
405433
export function organizeProps(props: Record<string, string>) {
406434
const ret = { ...props }
407435
for (const key of COLOR_PROPS)
@@ -435,11 +463,12 @@ export function organizeProps(props: Record<string, string>) {
435463
delete ret[key]
436464
continue
437465
}
466+
if (ret[key].includes('0px')) ret[key] = ret[key].replace(/\b0px\b/g, '0')
438467
if (ret[key].startsWith('"') && ret[key].endsWith('"'))
439468
ret[key] = ret[key].slice(1, -1)
440469
if (ret[key].includes('/*')) ret[key] = ret[key].split('/*')[0].trim()
441470
if (ret[key].includes('var(--'))
442-
ret[key] = ret[key].replace(/var\(--[^)]*\)/g, extractVariableName)
471+
ret[key] = replaceAllVarFunctions(ret[key], extractVariableName)
443472
}
444473
for (const key in CONVERT_PROPS_VALUE_MAP) {
445474
if (!ret[key]) continue
@@ -454,8 +483,9 @@ export function organizeProps(props: Record<string, string>) {
454483
}
455484
for (const key in DEFAULT_PROPS_MAP)
456485
if (
457-
ret[key] ===
458-
DEFAULT_PROPS_MAP[key as keyof typeof DEFAULT_PROPS_MAP].default
486+
DEFAULT_PROPS_MAP[key as keyof typeof DEFAULT_PROPS_MAP].default.test(
487+
ret[key],
488+
)
459489
) {
460490
const defaultValue =
461491
DEFAULT_PROPS_MAP[key as keyof typeof DEFAULT_PROPS_MAP].value

0 commit comments

Comments
 (0)