postcss-convert-values
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -0,5 +1,9 @@ | ||
# 1.2.0 | ||
* Adds support for slash/comma separated values (thanks to @TrySound). | ||
# 1.1.1 | ||
* Fixes an issue where trailing zeroes were not being removed in | ||
values that were not `0`. | ||
values that were not `0`. (thanks to @TrySound) | ||
@@ -9,2 +13,3 @@ # 1.1.0 | ||
* Adds support for removing leading zeroes from `rem` values. | ||
(thanks to @tunnckoCore) | ||
@@ -11,0 +16,0 @@ # 1.0.3 |
73
index.js
'use strict'; | ||
var postcss = require('postcss'); | ||
var list = postcss.list; | ||
var converter = require('./lib/converter'); | ||
var reduce = require('reduce-function-call'); | ||
var convert = require('./lib/convert'); | ||
var eachValue = require('./lib/eachValue'); | ||
var parseUnit = require('./lib/parseUnit'); | ||
var duration = /^((?:[-+]?[\d]+?)(?:\.?(?:[\d]+?))?)(s|ms)/; | ||
var length = /^((?:[-+]?[\d]+?)(?:\.?(?:[\d]+?))?)?(%|em|rem|ex|in|cm|mm|pt|pc|px)?$/; | ||
var tidyValue = /^([\d]*)?\.([^1-9]*)?$/; | ||
module.exports = postcss.plugin('postcss-convert-values', function () { | ||
return function (css) { | ||
css.eachDecl(function (decl) { | ||
decl.value = eachValue(decl.value, function (value) { | ||
var number, unit; | ||
function durationOptimiser (value) { | ||
if (duration.test(value)) { | ||
return value.replace(duration, function (_, num, unit) { | ||
return converter(num, unit); | ||
}); | ||
} | ||
return value; | ||
} | ||
if (!~decl.prop.indexOf('flex') && !isNaN(number = parseFloat(value))) { | ||
unit = parseUnit(value); | ||
function lengthOptimiser (value, prop) { | ||
if (length.test(value)) { | ||
var match = value.match(length), | ||
num = match[1], | ||
integer = parseInt(num), | ||
unit = match[2]; | ||
if (parseFloat(num) === integer) { | ||
num = integer; | ||
} | ||
if (num !== 0) { | ||
num = converter(num, unit); | ||
} else if (~prop.indexOf('flex')) { | ||
return value; | ||
} | ||
return num; | ||
} | ||
return value; | ||
} | ||
if (number === 0) { | ||
value = (unit === 'ms' || unit === 's') ? 0 + unit : 0; | ||
} else { | ||
value = convert(number, unit); | ||
} | ||
function eachValue (decl) { | ||
return function (value) { | ||
var match = tidyValue.exec(value); | ||
if (match) { | ||
if (!/\d/.test(match[2])) { | ||
value = value.replace(tidyValue, '$1$2'); | ||
} else if (!match[1]) { | ||
value = value.replace(tidyValue, '0'); | ||
} | ||
} | ||
value = durationOptimiser(value); | ||
value = lengthOptimiser(value, decl.prop); | ||
return value; | ||
}; | ||
} | ||
module.exports = postcss.plugin('postcss-convert-values', function () { | ||
return function (css) { | ||
css.eachDecl(function (decl) { | ||
var each = eachValue(decl); | ||
decl.value = list.space(decl.value).map(each).join(' '); | ||
decl.value = reduce(decl.value, 'calc', function (body, fn) { | ||
var transform = list.space(body).map(each).join(' '); | ||
return fn + '(' + transform + ')'; | ||
return value; | ||
} | ||
}); | ||
@@ -65,0 +26,0 @@ }); |
{ | ||
"name": "postcss-convert-values", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Convert values with PostCSS (e.g. ms -> s)", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape test.js | tap-spec" | ||
"test": "tape test.js | tap-spec", | ||
"jscs": "jscs index.js test.js lib/*.js" | ||
}, | ||
@@ -28,5 +29,6 @@ "keywords": [ | ||
"dependencies": { | ||
"postcss": "^4.1.4", | ||
"reduce-function-call": "^1.0.1" | ||
"balanced-match": "^0.2.0", | ||
"css-list": "0.0.3", | ||
"postcss": "^4.1.4" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6931
10
3
97
+ Addedbalanced-match@^0.2.0
+ Addedcss-list@0.0.3
+ Addedbalanced-match@0.2.1(transitive)
+ Addedcss-list@0.0.3(transitive)
- Removedreduce-function-call@^1.0.1
- Removedbalanced-match@1.0.2(transitive)
- Removedreduce-function-call@1.0.3(transitive)