Skip to content

Commit

Permalink
Merge pull request #9 from helpscout/add-compiler-function
Browse files Browse the repository at this point in the history
Add compiler function
  • Loading branch information
ItsJonQ committed May 7, 2018
2 parents aaed7e8 + 77287e3 commit fd34597
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/utilities/classNames.js
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

0 comments on commit fd34597

Please sign in to comment.