biome format --write
This commit is contained in:
parent
8372348148
commit
9262e803ec
415 changed files with 54076 additions and 17419 deletions
|
|
@ -15,18 +15,18 @@ export const unroll = (item) => {
|
|||
// This gives you an array of arrays of all possible unique (i.e. order-insensitive) combinations
|
||||
// Can only accept primitives. Duplicates are not supported and can cause unexpected behavior
|
||||
export const getAllPossibleCombinations = (array) => {
|
||||
const combos = [array.map(x => [x])]
|
||||
const combos = [array.map((x) => [x])]
|
||||
for (let comboSize = 2; comboSize <= array.length; comboSize++) {
|
||||
const previous = combos[combos.length - 1]
|
||||
const newCombos = previous.map(self => {
|
||||
const newCombos = previous.map((self) => {
|
||||
const selfSet = new Set()
|
||||
self.forEach(x => selfSet.add(x))
|
||||
const nonSelf = array.filter(x => !selfSet.has(x))
|
||||
return nonSelf.map(x => [...self, x])
|
||||
self.forEach((x) => selfSet.add(x))
|
||||
const nonSelf = array.filter((x) => !selfSet.has(x))
|
||||
return nonSelf.map((x) => [...self, x])
|
||||
})
|
||||
const flatCombos = newCombos.reduce((acc, x) => [...acc, ...x], [])
|
||||
const uniqueComboStrings = new Set()
|
||||
const uniqueCombos = flatCombos.map(sortBy).filter(x => {
|
||||
const uniqueCombos = flatCombos.map(sortBy).filter((x) => {
|
||||
if (uniqueComboStrings.has(x.join())) {
|
||||
return false
|
||||
} else {
|
||||
|
|
@ -56,75 +56,98 @@ export const getAllPossibleCombinations = (array) => {
|
|||
*
|
||||
* @returns {String} CSS selector (or path)
|
||||
*/
|
||||
export const genericRuleToSelector = components => (rule, ignoreOutOfTreeSelector, liteMode, children) => {
|
||||
const isParent = !!children
|
||||
if (!rule && !isParent) return null
|
||||
const component = components[rule.component]
|
||||
const { states = {}, variants = {}, outOfTreeSelector } = component
|
||||
export const genericRuleToSelector =
|
||||
(components) => (rule, ignoreOutOfTreeSelector, liteMode, children) => {
|
||||
const isParent = !!children
|
||||
if (!rule && !isParent) return null
|
||||
const component = components[rule.component]
|
||||
const { states = {}, variants = {}, outOfTreeSelector } = component
|
||||
|
||||
const expand = (array = [], subArray = []) => {
|
||||
if (array.length === 0) return subArray.map(x => [x])
|
||||
if (subArray.length === 0) return array.map(x => [x])
|
||||
return array.map(a => {
|
||||
return subArray.map(b => [a, b])
|
||||
}).flat()
|
||||
}
|
||||
|
||||
let componentSelectors = Array.isArray(component.selector) ? component.selector : [component.selector]
|
||||
if (ignoreOutOfTreeSelector || liteMode) componentSelectors = [componentSelectors[0]]
|
||||
componentSelectors = componentSelectors.map(selector => {
|
||||
if (selector === ':root') {
|
||||
return ''
|
||||
} else if (isParent) {
|
||||
return selector
|
||||
} else {
|
||||
if (outOfTreeSelector && !ignoreOutOfTreeSelector) return outOfTreeSelector
|
||||
return selector
|
||||
const expand = (array = [], subArray = []) => {
|
||||
if (array.length === 0) return subArray.map((x) => [x])
|
||||
if (subArray.length === 0) return array.map((x) => [x])
|
||||
return array
|
||||
.map((a) => {
|
||||
return subArray.map((b) => [a, b])
|
||||
})
|
||||
.flat()
|
||||
}
|
||||
})
|
||||
|
||||
const applicableVariantName = (rule.variant || 'normal')
|
||||
let variantSelectors = null
|
||||
if (applicableVariantName !== 'normal') {
|
||||
variantSelectors = variants[applicableVariantName]
|
||||
} else {
|
||||
variantSelectors = variants?.normal ?? ''
|
||||
let componentSelectors = Array.isArray(component.selector)
|
||||
? component.selector
|
||||
: [component.selector]
|
||||
if (ignoreOutOfTreeSelector || liteMode)
|
||||
componentSelectors = [componentSelectors[0]]
|
||||
componentSelectors = componentSelectors.map((selector) => {
|
||||
if (selector === ':root') {
|
||||
return ''
|
||||
} else if (isParent) {
|
||||
return selector
|
||||
} else {
|
||||
if (outOfTreeSelector && !ignoreOutOfTreeSelector)
|
||||
return outOfTreeSelector
|
||||
return selector
|
||||
}
|
||||
})
|
||||
|
||||
const applicableVariantName = rule.variant || 'normal'
|
||||
let variantSelectors = null
|
||||
if (applicableVariantName !== 'normal') {
|
||||
variantSelectors = variants[applicableVariantName]
|
||||
} else {
|
||||
variantSelectors = variants?.normal ?? ''
|
||||
}
|
||||
variantSelectors = Array.isArray(variantSelectors)
|
||||
? variantSelectors
|
||||
: [variantSelectors]
|
||||
if (ignoreOutOfTreeSelector || liteMode)
|
||||
variantSelectors = [variantSelectors[0]]
|
||||
|
||||
const applicableStates = (rule.state || []).filter((x) => x !== 'normal')
|
||||
// const applicableStates = (rule.state || [])
|
||||
const statesSelectors = applicableStates.map((state) => {
|
||||
const selector = states[state] || ''
|
||||
let arraySelector = Array.isArray(selector) ? selector : [selector]
|
||||
if (ignoreOutOfTreeSelector || liteMode)
|
||||
arraySelector = [arraySelector[0]]
|
||||
arraySelector
|
||||
.sort((a) => {
|
||||
if (a.startsWith(':')) return 1
|
||||
if (/^[a-z]/.exec(a)) return -1
|
||||
else return 0
|
||||
})
|
||||
.join('')
|
||||
return arraySelector
|
||||
})
|
||||
|
||||
const statesSelectorsFlat = statesSelectors.reduce((acc, s) => {
|
||||
return expand(acc, s).map((st) => st.join(''))
|
||||
}, [])
|
||||
|
||||
const componentVariant = expand(componentSelectors, variantSelectors).map(
|
||||
(cv) => cv.join(''),
|
||||
)
|
||||
const componentVariantStates = expand(
|
||||
componentVariant,
|
||||
statesSelectorsFlat,
|
||||
).map((cvs) => cvs.join(''))
|
||||
const selectors = expand(componentVariantStates, children).map((cvsc) =>
|
||||
cvsc.join(' '),
|
||||
)
|
||||
/*
|
||||
*/
|
||||
|
||||
if (rule.parent) {
|
||||
return genericRuleToSelector(components)(
|
||||
rule.parent,
|
||||
ignoreOutOfTreeSelector,
|
||||
liteMode,
|
||||
selectors,
|
||||
)
|
||||
}
|
||||
|
||||
return selectors.join(', ').trim()
|
||||
}
|
||||
variantSelectors = Array.isArray(variantSelectors) ? variantSelectors : [variantSelectors]
|
||||
if (ignoreOutOfTreeSelector || liteMode) variantSelectors = [variantSelectors[0]]
|
||||
|
||||
const applicableStates = (rule.state || []).filter(x => x !== 'normal')
|
||||
// const applicableStates = (rule.state || [])
|
||||
const statesSelectors = applicableStates.map(state => {
|
||||
const selector = states[state] || ''
|
||||
let arraySelector = Array.isArray(selector) ? selector : [selector]
|
||||
if (ignoreOutOfTreeSelector || liteMode) arraySelector = [arraySelector[0]]
|
||||
arraySelector
|
||||
.sort((a) => {
|
||||
if (a.startsWith(':')) return 1
|
||||
if (/^[a-z]/.exec(a)) return -1
|
||||
else return 0
|
||||
})
|
||||
.join('')
|
||||
return arraySelector
|
||||
})
|
||||
|
||||
const statesSelectorsFlat = statesSelectors.reduce((acc, s) => {
|
||||
return expand(acc, s).map(st => st.join(''))
|
||||
}, [])
|
||||
|
||||
const componentVariant = expand(componentSelectors, variantSelectors).map(cv => cv.join(''))
|
||||
const componentVariantStates = expand(componentVariant, statesSelectorsFlat).map(cvs => cvs.join(''))
|
||||
const selectors = expand(componentVariantStates, children).map(cvsc => cvsc.join(' '))
|
||||
/*
|
||||
*/
|
||||
|
||||
if (rule.parent) {
|
||||
return genericRuleToSelector(components)(rule.parent, ignoreOutOfTreeSelector, liteMode, selectors)
|
||||
}
|
||||
|
||||
return selectors.join(', ').trim()
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if combination matches
|
||||
|
|
@ -151,8 +174,8 @@ export const combinationsMatch = (criteria, subject, strict) => {
|
|||
const criteriaStatesSet = new Set(criteria.state)
|
||||
|
||||
const setsAreEqual =
|
||||
[...criteriaStatesSet].every(state => subjectStatesSet.has(state)) &&
|
||||
[...subjectStatesSet].every(state => criteriaStatesSet.has(state))
|
||||
[...criteriaStatesSet].every((state) => subjectStatesSet.has(state)) &&
|
||||
[...subjectStatesSet].every((state) => criteriaStatesSet.has(state))
|
||||
|
||||
if (!setsAreEqual) return false
|
||||
}
|
||||
|
|
@ -168,7 +191,7 @@ export const combinationsMatch = (criteria, subject, strict) => {
|
|||
*
|
||||
* @return function that returns true/false if subject matches
|
||||
*/
|
||||
export const findRules = (criteria, strict) => subject => {
|
||||
export const findRules = (criteria, strict) => (subject) => {
|
||||
// If we searching for "general" rules - ignore "specific" ones
|
||||
if (criteria.parent === null && !!subject.parent) return false
|
||||
if (!combinationsMatch(criteria, subject, strict)) return false
|
||||
|
|
@ -186,14 +209,15 @@ export const findRules = (criteria, strict) => subject => {
|
|||
const criteriaParent = pathCriteria[i]
|
||||
const subjectParent = pathSubject[i]
|
||||
if (!subjectParent) return true
|
||||
if (!combinationsMatch(criteriaParent, subjectParent, strict)) return false
|
||||
if (!combinationsMatch(criteriaParent, subjectParent, strict))
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Pre-fills 'normal' state/variant if missing
|
||||
export const normalizeCombination = rule => {
|
||||
export const normalizeCombination = (rule) => {
|
||||
rule.variant = rule.variant ?? 'normal'
|
||||
rule.state = [...new Set(['normal', ...(rule.state || [])])]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue