Skip to content

Instantly share code, notes, and snippets.

@crswll
Created January 23, 2022 22:26
Show Gist options
  • Save crswll/d4e2721fdcf7a6f72627d2111bd51360 to your computer and use it in GitHub Desktop.
Save crswll/d4e2721fdcf7a6f72627d2111bd51360 to your computer and use it in GitHub Desktop.
diff --git a/src/util/parseBoxShadowValue.js b/src/util/parseBoxShadowValue.js
index eb4872c7..5ed74a9e 100644
--- a/src/util/parseBoxShadowValue.js
+++ b/src/util/parseBoxShadowValue.js
@@ -1,58 +1,27 @@
-let KEYWORDS = new Set(['inset', 'inherit', 'initial', 'revert', 'unset'])
+let KEYWORDS = ['inset', 'inherit', 'initial', 'revert', 'unset']
let COMMA = /\,(?![^(]*\))/g // Comma separator that is not located between brackets. E.g.: `cubiz-bezier(a, b, c)` these don't count.
let SPACE = /\ +(?![^(]*\))/g // Similar to the one above, but with spaces instead.
-let LENGTH = /^-?(\d+)(.*?)$/g
-export function parseBoxShadowValue(input) {
+export function parseBoxShadowValue (input) {
let shadows = input.split(COMMA)
+
return shadows.map((shadow) => {
let value = shadow.trim()
- let result = { raw: value }
let parts = value.split(SPACE)
- let seen = new Set()
-
- for (let part of parts) {
- // Reset index, since the regex is stateful.
- LENGTH.lastIndex = 0
-
- // Keyword
- if (!seen.has('KEYWORD') && KEYWORDS.has(part)) {
- result.keyword = part
- seen.add('KEYWORD')
- }
+ let output = { raw: value }
- // Length value
- else if (LENGTH.test(part)) {
- if (!seen.has('X')) {
- result.x = part
- seen.add('X')
- } else if (!seen.has('Y')) {
- result.y = part
- seen.add('Y')
- } else if (!seen.has('BLUR')) {
- result.blur = part
- seen.add('BLUR')
- } else if (!seen.has('SPREAD')) {
- result.spread = part
- seen.add('SPREAD')
- }
- }
-
- // Color or unknown
- else {
- if (!result.color) {
- result.color = part
- } else {
- if (!result.unknown) result.unknown = []
- result.unknown.push(part)
- }
- }
+ if (KEYWORDS.includes(parts[0])) {
+ output.keyword = parts.shift()
}
- // Check if valid
- result.valid = result.x !== undefined && result.y !== undefined
+ output.color = parts.pop()
+ output.x = parts.shift()
+ output.y = parts.shift()
+ output.blur = parts.shift()
+ output.spread = parts.shift()
+ output.valid = output.x !== undefined && output.y !== undefined
- return result
+ return output
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment