Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Jun 17, 2019
1 parent dff3c80 commit 11ef79f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
19 changes: 11 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function chain(instance) {
get: function() {
return Object.keys(this.__style).reduce((acc, key) => {
if (typeof this.__style[key] === 'number' || typeof this.__style[key] === 'string')
acc[key.replace(/^!/, '')] = this.__style[key]
acc[key.charAt(0) === '!' ? key.slice(1) : key] = this.__style[key]
return acc
}, {})
}
Expand Down Expand Up @@ -144,12 +144,11 @@ function $media(value, style) {
return b
}

const hasUrl = /^('|"|url\('|url\(")/i
function $import(value) {
if (value && !/^('|"|url\('|url\(")/.test(value))
value = '"' + value + '"'

if (value)
insert('@import ' + value + ';', 0)
value && insert('@import '
+ (hasUrl.test(value) ? value : '"' + value + '"')
+ ';', 0)

return chain(this)
}
Expand Down Expand Up @@ -255,15 +254,19 @@ function short(prop) {
return short
}

const blockEndMatch = /;(?![^("]*[)"])|\n/
const commentsMatch = /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*(?![^("]*[)"])/g
const propSeperator = /[ :]+/

const stringToObject = memoize(string => {
let last = ''
, prev

return string.trim().replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*(?![^("]*[)"])/g, '').split(/;(?![^("]*[)"])|\n/).reduce((acc, line) => {
return string.trim().replace(commentsMatch, '').split(blockEndMatch).reduce((acc, line) => {
if (!line)
return acc
line = last + line.trim()
const [key, ...tokens] = line.replace(/[ :]+/, ' ').split(' ')
const [key, ...tokens] = line.replace(propSeperator, ' ').split(' ')

last = line.charAt(line.length - 1) === ',' ? line : ''
if (last)
Expand Down
14 changes: 9 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,24 @@ export function assign(obj, obj2) {
return obj
}

const hyphenSeparator = /-([a-z])/g
export function hyphenToCamelCase(hyphen) {
return hyphen.slice(hyphen.charAt(0) === '-' ? 1 : 0).replace(/-([a-z])/g, function(match) {
return hyphen.slice(hyphen.charAt(0) === '-' ? 1 : 0).replace(hyphenSeparator, function(match) {
return match[1].toUpperCase()
})
}

const camelSeparator = /(\B[A-Z])/g
export function camelCaseToHyphen(camelCase) {
return camelCase.replace(/(\B[A-Z])/g, '-$1').toLowerCase()
return camelCase.replace(camelSeparator, '-$1').toLowerCase()
}

const initialMatch = /([A-Z])/g
export function initials(camelCase) {
return camelCase.charAt(0) + (camelCase.match(/([A-Z])/g) || []).join('').toLowerCase()
return camelCase.charAt(0) + (camelCase.match(initialMatch) || []).join('').toLowerCase()
}

const ampersandMatch = /&/g
export function objectToRules(style, selector, suffix = '', single) {
const base = {}
const extra = suffix.indexOf('&') > -1 && suffix.indexOf(',') === -1 ? '' : '&'
Expand All @@ -96,7 +100,7 @@ export function objectToRules(style, selector, suffix = '', single) {

if (Object.keys(base).length) {
rules.unshift(
((single || (suffix.charAt(0) === ' ') ? '' : '&') + extra + suffix).replace(/&/g, selector).trim() +
((single || (suffix.charAt(0) === ' ') ? '' : '&') + extra + suffix).replace(ampersandMatch, selector).trim() +
'{' + stylesToCss(base) + '}'
)
}
Expand All @@ -108,7 +112,7 @@ export const selectorSplit = /,(?=(?:(?:[^"]*"){2})*[^"]*$)/

export function stylesToCss(style) {
return Object.keys(style).reduce((acc, prop) =>
acc + propToString(prop.replace(/!/g, ''), style[prop])
acc + propToString(prop.charAt(0) === '!' ? prop.slice(1) : prop, style[prop])
, '')
}

Expand Down

0 comments on commit 11ef79f

Please sign in to comment.