diff --git a/async.js b/async.js index 1ba6b3b..99dbae1 100644 --- a/async.js +++ b/async.js @@ -1,15 +1,12 @@ let postcss = require('postcss') -let parse = require('./parser') +let parser = require('./parser') let processResult = require('./process-result') module.exports = function async(plugins) { let processor = postcss(plugins) return async input => { - let result = await processor.process(input, { - parser: parse, - from: undefined - }) + let result = await processor.process(input, { parser, from: undefined }) return processResult(result) } } diff --git a/objectifier.js b/objectifier.js index bd47fd2..6cb9d88 100644 --- a/objectifier.js +++ b/objectifier.js @@ -24,11 +24,7 @@ let UNITLESS = { } function atRule(node) { - if (typeof node.nodes === 'undefined') { - return true - } else { - return process(node) - } + return node.nodes === undefined ? true : process(node) } // From https://github.com/hyperz111/fast-camelcase-css @@ -43,7 +39,7 @@ function camelcase(property) { // Microsoft vendor-prefixes are uniquely cased if (property.startsWith('-ms-')) { - property = property.substring(1) + property = property.slice(1) index = property.indexOf('-') } @@ -51,24 +47,23 @@ function camelcase(property) { let result = '' do { - result += property.substring(cursor, index) + property[index + 1].toUpperCase() + result += property.slice(cursor, index) + property[index + 1].toUpperCase() cursor = index + 2 index = property.indexOf('-', cursor) } while (index !== -1) - return result + property.substring(cursor) + return result + property.slice(cursor) } function process(node, options = {}) { let name let result = {} - let { stringifyImportant } = options node.each(child => { if (child.type === 'atrule') { name = '@' + child.name if (child.params) name += ' ' + child.params - if (typeof result[name] === 'undefined') { + if (result[name] === undefined) { result[name] = atRule(child) } else if (Array.isArray(result[name])) { result[name].push(atRule(child)) @@ -81,7 +76,7 @@ function process(node, options = {}) { for (let i in body) { let object = result[child.selector] if ( - stringifyImportant && + options.stringifyImportant && typeof object[i] === 'string' && object[i].endsWith('!important') ) { @@ -96,7 +91,7 @@ function process(node, options = {}) { result[child.selector] = body } } else if (child.type === 'decl') { - if (child.prop[0] === '-' && child.prop[1] === '-') { + if (child.prop.startsWith('--')) { name = child.prop } else if (child.parent && child.parent.selector === ':export') { name = child.prop @@ -104,11 +99,9 @@ function process(node, options = {}) { name = camelcase(child.prop) } let value = child.value - if (!isNaN(child.value) && UNITLESS[name]) { - value = parseFloat(child.value) - } + if (!isNaN(child.value) && UNITLESS[name]) value = parseFloat(child.value) if (child.important) value += ' !important' - if (typeof result[name] === 'undefined') { + if (result[name] === undefined) { result[name] = value } else if (Array.isArray(result[name])) { result[name].push(value) diff --git a/parser.js b/parser.js index 3cac15d..e6df6e8 100644 --- a/parser.js +++ b/parser.js @@ -27,28 +27,30 @@ let UNITLESS = { 'stroke-width': true } -let { fromCharCode } = String; +let { fromCharCode } = String function dashify(str) { - let result = ''; - let i = 0; - let len = str.length; - let code; + if (str === 'cssFloat') return 'float' - if (str[0] === 'm' && str[1] === 's') result += fromCharCode(45); // '-' + let result = '' + let i = 0 + let len = str.length + let code + + if (str.startsWith('ms')) result += fromCharCode(45) // '-' for (; i < len; i++) { - code = str[i].charCodeAt(0); + code = str[i].charCodeAt(0) if (code > 64 && code < 91) { - result += fromCharCode(45) + fromCharCode(code + 32); - continue; + result += fromCharCode(45) + fromCharCode(code + 32) + continue } - result += fromCharCode(code); + result += fromCharCode(code) } - return result; + return result } function decl(parent, name, value) { @@ -59,15 +61,10 @@ function decl(parent, name, value) { } if (typeof value === 'number') { - if (value === 0 || UNITLESS[name]) { - value = value.toString() - } else { - value += 'px' - } + value = value.toString() + if (value !== '0' && !UNITLESS[name]) value += 'px' } - if (name === 'css-float') name = 'float' - if (IMPORTANT.test(value)) { value = value.replace(IMPORTANT, '') parent.push(postcss.decl({ prop: name, value, important: true })) @@ -89,7 +86,7 @@ function parse(obj, parent) { let name, node, value for (name in obj) { value = obj[name] - if (value === null || typeof value === 'undefined') { + if (value == null) { continue } else if (name[0] === '@') { let parts = name.match(/@(\S+)(\s+([\W\w]*)\s*)?/) diff --git a/process-result.js b/process-result.js index 215a95c..4128876 100644 --- a/process-result.js +++ b/process-result.js @@ -3,8 +3,7 @@ let objectify = require('./objectifier') module.exports = function processResult(result) { if (console && console.warn) { result.warnings().forEach(warn => { - let source = warn.plugin || 'PostCSS' - console.warn(source + ': ' + warn.text) + console.warn((warn.plugin || 'PostCSS') + ': ' + warn.text) }) } return objectify(result.root) diff --git a/sync.js b/sync.js index 668a9ef..90c6337 100644 --- a/sync.js +++ b/sync.js @@ -1,12 +1,12 @@ let postcss = require('postcss') -let parse = require('./parser') +let parser = require('./parser') let processResult = require('./process-result') module.exports = function (plugins) { let processor = postcss(plugins) return input => { - let result = processor.process(input, { parser: parse, from: undefined }) + let result = processor.process(input, { parser, from: undefined }) return processResult(result) } }