postcss-convert-values 4.0.0-rc.2 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.js +25 -30
- package/dist/lib/convert.js +18 -26
- package/package.json +6 -6
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -18,20 +18,20 @@ var _convert2 = _interopRequireDefault(_convert);
|
|
|
18
18
|
|
|
19
19
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
const LENGTH_UNITS = ['em', 'ex', 'ch', 'rem', 'vw', 'vh', 'vmin', 'vmax', 'cm', 'mm', 'q', 'in', 'pt', 'pc', 'px'];
|
|
22
22
|
|
|
23
23
|
function parseWord(node, opts, keepZeroUnit) {
|
|
24
|
-
|
|
24
|
+
const pair = (0, _postcssValueParser.unit)(node.value);
|
|
25
25
|
if (pair) {
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const num = Number(pair.number);
|
|
27
|
+
const u = pair.unit;
|
|
28
28
|
if (num === 0) {
|
|
29
|
-
node.value = keepZeroUnit || !~LENGTH_UNITS.indexOf(u) && u !== '%' ? 0 + u : 0;
|
|
29
|
+
node.value = keepZeroUnit || !~LENGTH_UNITS.indexOf(u.toLowerCase()) && u !== '%' ? 0 + u : 0;
|
|
30
30
|
} else {
|
|
31
31
|
node.value = (0, _convert2.default)(num, u, opts);
|
|
32
32
|
|
|
33
|
-
if (typeof opts.precision === 'number' && u === 'px' && ~pair.number.indexOf('.')) {
|
|
34
|
-
|
|
33
|
+
if (typeof opts.precision === 'number' && u.toLowerCase() === 'px' && ~pair.number.indexOf('.')) {
|
|
34
|
+
const precision = Math.pow(10, opts.precision);
|
|
35
35
|
node.value = Math.round(parseFloat(node.value) * precision) / precision + u;
|
|
36
36
|
}
|
|
37
37
|
}
|
|
@@ -39,11 +39,11 @@ function parseWord(node, opts, keepZeroUnit) {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
function clampOpacity(node) {
|
|
42
|
-
|
|
42
|
+
const pair = (0, _postcssValueParser.unit)(node.value);
|
|
43
43
|
if (!pair) {
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
let num = Number(pair.number);
|
|
47
47
|
if (num > 1) {
|
|
48
48
|
node.value = 1 + pair.unit;
|
|
49
49
|
} else if (num < 0) {
|
|
@@ -51,50 +51,45 @@ function clampOpacity(node) {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
function shouldStripPercent(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return ~value.indexOf('%') && (prop === 'max-height' || prop === 'height') || parent.parent && parent.parent.name === 'keyframes' && prop === 'stroke-dasharray' || prop === 'stroke-dashoffset' || prop === 'stroke-width';
|
|
54
|
+
function shouldStripPercent(decl) {
|
|
55
|
+
const { parent } = decl;
|
|
56
|
+
const lowerCasedProp = decl.prop.toLowerCase();
|
|
57
|
+
return ~decl.value.indexOf('%') && (lowerCasedProp === 'max-height' || lowerCasedProp === 'height') || parent.parent && parent.parent.name && parent.parent.name.toLowerCase() === 'keyframes' && lowerCasedProp === 'stroke-dasharray' || lowerCasedProp === 'stroke-dashoffset' || lowerCasedProp === 'stroke-width';
|
|
60
58
|
}
|
|
61
59
|
|
|
62
60
|
function transform(opts, decl) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (~prop.indexOf('flex') || prop.indexOf('--') === 0) {
|
|
61
|
+
const lowerCasedProp = decl.prop.toLowerCase();
|
|
62
|
+
if (~lowerCasedProp.indexOf('flex') || lowerCasedProp.indexOf('--') === 0) {
|
|
66
63
|
return;
|
|
67
64
|
}
|
|
68
65
|
|
|
69
|
-
decl.value = (0, _postcssValueParser2.default)(decl.value).walk(
|
|
66
|
+
decl.value = (0, _postcssValueParser2.default)(decl.value).walk(node => {
|
|
67
|
+
const lowerCasedValue = node.value.toLowerCase();
|
|
68
|
+
|
|
70
69
|
if (node.type === 'word') {
|
|
71
70
|
parseWord(node, opts, shouldStripPercent(decl));
|
|
72
|
-
if (
|
|
71
|
+
if (lowerCasedProp === 'opacity' || lowerCasedProp === 'shape-image-threshold') {
|
|
73
72
|
clampOpacity(node);
|
|
74
73
|
}
|
|
75
74
|
} else if (node.type === 'function') {
|
|
76
|
-
if (
|
|
77
|
-
(0, _postcssValueParser.walk)(node.nodes,
|
|
75
|
+
if (lowerCasedValue === 'calc' || lowerCasedValue === 'hsl' || lowerCasedValue === 'hsla') {
|
|
76
|
+
(0, _postcssValueParser.walk)(node.nodes, n => {
|
|
78
77
|
if (n.type === 'word') {
|
|
79
78
|
parseWord(n, opts, true);
|
|
80
79
|
}
|
|
81
80
|
});
|
|
82
81
|
return false;
|
|
83
82
|
}
|
|
84
|
-
if (
|
|
83
|
+
if (lowerCasedValue === 'url') {
|
|
85
84
|
return false;
|
|
86
85
|
}
|
|
87
86
|
}
|
|
88
87
|
}).toString();
|
|
89
88
|
}
|
|
90
89
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
exports.default = _postcss2.default.plugin(plugin, function () {
|
|
94
|
-
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { precision: false };
|
|
90
|
+
const plugin = 'postcss-convert-values';
|
|
95
91
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
};
|
|
92
|
+
exports.default = _postcss2.default.plugin(plugin, (opts = { precision: false }) => {
|
|
93
|
+
return css => css.walkDecls(transform.bind(null, opts));
|
|
99
94
|
});
|
|
100
95
|
module.exports = exports['default'];
|
package/dist/lib/convert.js
CHANGED
|
@@ -4,23 +4,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
|
|
7
|
-
exports.default = function (number, unit,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
angle = _ref.angle;
|
|
7
|
+
exports.default = function (number, unit, { time, length, angle }) {
|
|
8
|
+
let value = dropLeadingZero(number) + (unit ? unit : '');
|
|
9
|
+
let converted;
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
var converted = void 0;
|
|
14
|
-
|
|
15
|
-
if (length !== false && unit in lengthConv) {
|
|
11
|
+
if (length !== false && unit.toLowerCase() in lengthConv) {
|
|
16
12
|
converted = transform(number, unit, lengthConv);
|
|
17
13
|
}
|
|
18
14
|
|
|
19
|
-
if (time !== false && unit in timeConv) {
|
|
15
|
+
if (time !== false && unit.toLowerCase() in timeConv) {
|
|
20
16
|
converted = transform(number, unit, timeConv);
|
|
21
17
|
}
|
|
22
18
|
|
|
23
|
-
if (angle !== false && unit in angleConv) {
|
|
19
|
+
if (angle !== false && unit.toLowerCase() in angleConv) {
|
|
24
20
|
converted = transform(number, unit, angleConv);
|
|
25
21
|
}
|
|
26
22
|
|
|
@@ -31,25 +27,25 @@ exports.default = function (number, unit, _ref) {
|
|
|
31
27
|
return value;
|
|
32
28
|
};
|
|
33
29
|
|
|
34
|
-
|
|
30
|
+
const lengthConv = {
|
|
35
31
|
in: 96,
|
|
36
32
|
px: 1,
|
|
37
33
|
pt: 4 / 3,
|
|
38
34
|
pc: 16
|
|
39
35
|
};
|
|
40
36
|
|
|
41
|
-
|
|
37
|
+
const timeConv = {
|
|
42
38
|
s: 1000,
|
|
43
39
|
ms: 1
|
|
44
40
|
};
|
|
45
41
|
|
|
46
|
-
|
|
42
|
+
const angleConv = {
|
|
47
43
|
turn: 360,
|
|
48
44
|
deg: 1
|
|
49
45
|
};
|
|
50
46
|
|
|
51
47
|
function dropLeadingZero(number) {
|
|
52
|
-
|
|
48
|
+
const value = String(number);
|
|
53
49
|
|
|
54
50
|
if (number % 1) {
|
|
55
51
|
if (value[0] === '0') {
|
|
@@ -65,26 +61,22 @@ function dropLeadingZero(number) {
|
|
|
65
61
|
}
|
|
66
62
|
|
|
67
63
|
function transform(number, unit, conversion) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
const lowerCasedUnit = unit.toLowerCase();
|
|
65
|
+
let one, base;
|
|
66
|
+
let convertionUnits = Object.keys(conversion).filter(u => {
|
|
71
67
|
if (conversion[u] === 1) {
|
|
72
68
|
one = u;
|
|
73
69
|
}
|
|
74
|
-
return
|
|
70
|
+
return lowerCasedUnit !== u;
|
|
75
71
|
});
|
|
76
72
|
|
|
77
|
-
if (
|
|
78
|
-
base = number / conversion[
|
|
73
|
+
if (lowerCasedUnit === one) {
|
|
74
|
+
base = number / conversion[lowerCasedUnit];
|
|
79
75
|
} else {
|
|
80
|
-
base = number * conversion[
|
|
76
|
+
base = number * conversion[lowerCasedUnit];
|
|
81
77
|
}
|
|
82
78
|
|
|
83
|
-
return convertionUnits.map(
|
|
84
|
-
return dropLeadingZero(base / conversion[u]) + u;
|
|
85
|
-
}).reduce(function (a, b) {
|
|
86
|
-
return a.length < b.length ? a : b;
|
|
87
|
-
});
|
|
79
|
+
return convertionUnits.map(u => dropLeadingZero(base / conversion[u]) + u).reduce((a, b) => a.length < b.length ? a : b);
|
|
88
80
|
}
|
|
89
81
|
|
|
90
82
|
module.exports = exports['default'];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-convert-values",
|
|
3
|
-
"version": "4.0.0
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Convert values with PostCSS (e.g. ms -> s)",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -19,23 +19,23 @@
|
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"babel-cli": "^6.0.0",
|
|
22
|
-
"cross-env": "^
|
|
22
|
+
"cross-env": "^5.0.0"
|
|
23
23
|
},
|
|
24
|
-
"homepage": "https://github.com/
|
|
24
|
+
"homepage": "https://github.com/cssnano/cssnano",
|
|
25
25
|
"author": {
|
|
26
26
|
"name": "Ben Briggs",
|
|
27
27
|
"email": "beneb.info@gmail.com",
|
|
28
28
|
"url": "http://beneb.info"
|
|
29
29
|
},
|
|
30
|
-
"repository": "
|
|
30
|
+
"repository": "cssnano/cssnano",
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"postcss": "^6.0.0",
|
|
33
33
|
"postcss-value-parser": "^3.0.0"
|
|
34
34
|
},
|
|
35
35
|
"bugs": {
|
|
36
|
-
"url": "https://github.com/
|
|
36
|
+
"url": "https://github.com/cssnano/cssnano/issues"
|
|
37
37
|
},
|
|
38
38
|
"engines": {
|
|
39
|
-
"node": ">=
|
|
39
|
+
"node": ">=6.9.0"
|
|
40
40
|
}
|
|
41
41
|
}
|