Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compiler function #9

Merged
merged 1 commit into from
May 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/utilities/classNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ export const makeUniqFirstClassName = (uuid, id) => (item, index) => {
return index === 0 ? `${item}__${uuid}-${id}` : item
}

/**
* Compiles cssRules from token points.
*
* @param {string} rule
* @param {string} token
* @param {function} compiler
* @returns {string}
*/
export const compileRule = (rule, token, compiler) => {
return rule.split(token).map(compiler).join(token)
}

/**
* Creates a unique namespaced className selector.
*
Expand All @@ -51,20 +63,17 @@ export const makeUniqClassName = (selector, uuid, id) => {

if (index === 0) {
if (item.indexOf(':') >= 0) {
className = item.split(':').map(generateClassName).join(':')
className = compileRule(item, ':', generateClassName)
}
if (item.indexOf(',') >= 0) {
className = item.split(',').map(generateClassName).join(',')
className = compileRule(item, ',', generateClassName)
}
}

return className
}

return selector
.split(' ')
.map(generate)
.join(' ')
return compileRule(selector, ' ', generate)
}

/**
Expand All @@ -77,10 +86,8 @@ export const makeUniqClassName = (selector, uuid, id) => {
* @returns {string}
*/
export const makeUniqSelectorForCombinator = (combinator, selector, uuid, id) => {
return selector
.split(combinator)
.map(s => makeUniqClassName(s.trim(), uuid, id))
.join(combinator)
const compiler = s => makeUniqClassName(s.trim(), uuid, id)
return compileRule(selector, combinator, compiler)
}

/**
Expand Down