Skip to content

Commit 70cb8b8

Browse files
committed
Clean up code
1 parent a939c79 commit 70cb8b8

File tree

2 files changed

+22
-53
lines changed

2 files changed

+22
-53
lines changed

index.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,61 +41,45 @@ export function renderShadows(type, inset, x, y, blur, color) {
4141
return shadows
4242
}
4343

44-
function isColorValue(value) {
44+
function isColor(value) {
4545
value = value.trim()
46-
4746
if (/^#([0-9a-fA-F]{3}){1,2}([0-9a-fA-F]{2})?$/.test(value)) {
4847
return true
4948
}
50-
5149
if (/^(rgb|rgba|hsl|hsla|oklch|oklab|lch|lab|color)\s*\(/i.test(value)) {
5250
return true
5351
}
54-
5552
if (/^[a-zA-Z]+$/.test(value)) {
5653
return true
5754
}
58-
5955
return false
6056
}
6157

62-
function checkParams(decl, funcName, params, startIndex) {
63-
if (params.trim() === '') {
64-
throw decl.error(
65-
`${funcName} requires exactly 4 parameters (x, y, blur, color), got 0`,
66-
{ index: startIndex }
67-
)
68-
}
69-
58+
function checkParams(decl, func, params, startIndex) {
7059
let parsedArgs = parseSpaceSeparatedParams(params.trim())
71-
let hasInset = false
72-
let processedArgs = []
73-
60+
let inset = false
61+
let args = []
7462
for (let arg of parsedArgs) {
7563
if (arg.toLowerCase() === 'inset') {
76-
hasInset = true
64+
inset = true
7765
} else {
78-
processedArgs.push(arg)
66+
args.push(arg)
7967
}
8068
}
81-
82-
let expectedParams = 4
83-
if (processedArgs.length !== expectedParams) {
69+
if (args.length !== 4) {
8470
throw decl.error(
85-
`${funcName} requires exactly ${expectedParams} parameters (x, y, blur, color), got ${processedArgs.length}`,
71+
`${func} requires 4 params (inset? x y blur color) got ${args.length}`,
8672
{ index: startIndex }
8773
)
8874
}
89-
90-
let firstParam = processedArgs[0].trim()
91-
if (isColorValue(firstParam)) {
75+
let first = args[0].trim()
76+
if (isColor(first)) {
9277
throw decl.error(
93-
`${funcName} first parameter must be a length value (x-offset), got: ${firstParam}`,
78+
`${func} first parameter must be a length not color ${first}`,
9479
{ index: startIndex }
9580
)
9681
}
97-
98-
return { args: processedArgs, inset: hasInset }
82+
return { args, inset }
9983
}
10084

10185
function parseSpaceSeparatedParams(params) {
@@ -152,7 +136,7 @@ function replaceFunctions(decl, func) {
152136
}
153137

154138
let { endIndex, params } = parseResult
155-
let paramResult = checkParams(decl, searchPattern, params, startIndex)
139+
let paramResult = checkParams(decl, searchPattern + ')', params, startIndex)
156140
let [x, y, blur, color] = paramResult.args
157141
let shadows = renderShadows(func, paramResult.inset, x, y, blur, color)
158142
let replacement = shadows.join(', ')

test/index.test.js

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,19 @@ test('ignores declarations without shadow functions', () => {
9090
test('throws error for wrong parameter count - too few', () => {
9191
throws(() => {
9292
run('a { box-shadow: --sharp-shadow(1px 2px 3px); }', '')
93-
}, /requires exactly 4 parameters/)
93+
}, /requires 4 params/)
9494
})
9595

9696
test('throws error for wrong parameter count - too many', () => {
9797
throws(() => {
9898
run('a { box-shadow: --soft-shadow(1px 2px 3px red extra); }', '')
99-
}, /requires exactly 4 parameters/)
99+
}, /requires 4 params/)
100100
})
101101

102102
test('throws error for wrong parameter count - no parameters', () => {
103103
throws(() => {
104104
run('a { box-shadow: --linear-shadow(); }', '')
105-
}, /requires exactly 4 parameters/)
105+
}, /requires 4 params/)
106106
})
107107

108108
test('handles different blur values for layer calculation', () => {
@@ -213,34 +213,19 @@ test('handles same shadow function multiple times in one declaration', () => {
213213
test('throws error for malformed parameters with extra spaces', () => {
214214
throws(() => {
215215
run('a { box-shadow: --sharp-shadow(1px 2px 3px red extra); }', '')
216-
}, /requires exactly 4 parameters.*got 5/)
216+
}, /requires 4 params/)
217217
})
218218

219219
test('throws error for empty parameters', () => {
220220
throws(() => {
221221
run('a { box-shadow: --linear-shadow( ); }', '')
222-
}, /requires exactly 4 parameters/)
222+
}, /requires 4 params/)
223223
})
224224

225225
test('throws error when hex color is used as first argument', () => {
226226
throws(() => {
227227
run('a { box-shadow: --sharp-shadow(#ff0000 2px 10px blue); }', '')
228-
}, /first parameter must be a length value.*got.*#ff0000/)
229-
})
230-
231-
test('throws error when oklch() color is used as first argument', () => {
232-
throws(() => {
233-
run('a { box-shadow: --soft-shadow(oklch(0.5 0.2 180) 2px 10px red); }', '')
234-
}, /first parameter must be a length value.*got.*oklch\(0\.5 0\.2 180\)/)
235-
})
236-
237-
test('throws error when rgba() color is used as first argument', () => {
238-
throws(() => {
239-
run(
240-
'a { box-shadow: --linear-shadow(rgba(255, 0, 0, 0.5) 2px 10px green); }',
241-
''
242-
)
243-
}, /first parameter must be a length value.*got.*rgba\(255, 0, 0, 0\.5\)/)
228+
}, /first parameter must be a length not color #ff0000/)
244229
})
245230

246231
test('throws error when hsl() color is used as first argument', () => {
@@ -249,19 +234,19 @@ test('throws error when hsl() color is used as first argument', () => {
249234
'a { box-shadow: --sharp-shadow(hsl(0, 100%, 50%) 2px 10px black); }',
250235
''
251236
)
252-
}, /first parameter must be a length value.*got.*hsl\(0, 100%, 50%\)/)
237+
}, /first parameter must be a length not color hsl/)
253238
})
254239

255240
test('throws error when named color is used as first argument', () => {
256241
throws(() => {
257242
run('a { box-shadow: --soft-shadow(red 2px 10px blue); }', '')
258-
}, /first parameter must be a length value.*got.*red/)
243+
}, /first parameter must be a length not color red/)
259244
})
260245

261246
test('throws error when currentColor is used as first argument', () => {
262247
throws(() => {
263248
run('a { box-shadow: --linear-shadow(currentColor 2px 10px red); }', '')
264-
}, /first parameter must be a length value.*got.*currentColor/)
249+
}, /first parameter must be a length not color currentColor/)
265250
})
266251

267252
test('handles multiple --soft-shadow in same declaration', () => {

0 commit comments

Comments
 (0)