postcss-convert-values 5.0.4 → 5.1.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/package.json +4 -2
- package/src/index.js +37 -7
- package/src/lib/convert.js +21 -4
- package/types/index.d.ts +19 -0
- package/types/lib/convert.d.ts +6 -0
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-convert-values",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Convert values with PostCSS (e.g. ms -> s)",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
+
"types": "types/index.d.ts",
|
|
6
7
|
"files": [
|
|
7
8
|
"LICENSE-MIT",
|
|
8
|
-
"src"
|
|
9
|
+
"src",
|
|
10
|
+
"types"
|
|
9
11
|
],
|
|
10
12
|
"keywords": [
|
|
11
13
|
"css",
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
const valueParser = require('postcss-value-parser');
|
|
3
|
-
const convert = require('./lib/convert');
|
|
3
|
+
const convert = require('./lib/convert.js');
|
|
4
4
|
|
|
5
5
|
const LENGTH_UNITS = new Set([
|
|
6
6
|
'em',
|
|
@@ -36,10 +36,13 @@ const keepWhenZero = new Set([
|
|
|
36
36
|
'line-height',
|
|
37
37
|
]);
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
/**
|
|
40
40
|
* Numbers without digits after the dot are technically invalid,
|
|
41
41
|
* but in that case css-value-parser returns the dot as part of the unit,
|
|
42
42
|
* so we use this to remove the dot.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} item
|
|
45
|
+
* @return {string}
|
|
43
46
|
*/
|
|
44
47
|
function stripLeadingDot(item) {
|
|
45
48
|
if (item.charCodeAt(0) === '.'.charCodeAt(0)) {
|
|
@@ -49,6 +52,12 @@ function stripLeadingDot(item) {
|
|
|
49
52
|
}
|
|
50
53
|
}
|
|
51
54
|
|
|
55
|
+
/**
|
|
56
|
+
* @param {valueParser.Node} node
|
|
57
|
+
* @param {Options} opts
|
|
58
|
+
* @param {boolean} keepZeroUnit
|
|
59
|
+
* @return {void}
|
|
60
|
+
*/
|
|
52
61
|
function parseWord(node, opts, keepZeroUnit) {
|
|
53
62
|
const pair = valueParser.unit(node.value);
|
|
54
63
|
if (pair) {
|
|
@@ -76,6 +85,10 @@ function parseWord(node, opts, keepZeroUnit) {
|
|
|
76
85
|
}
|
|
77
86
|
}
|
|
78
87
|
|
|
88
|
+
/**
|
|
89
|
+
* @param {valueParser.WordNode} node
|
|
90
|
+
* @return {void}
|
|
91
|
+
*/
|
|
79
92
|
function clampOpacity(node) {
|
|
80
93
|
const pair = valueParser.unit(node.value);
|
|
81
94
|
if (!pair) {
|
|
@@ -89,20 +102,31 @@ function clampOpacity(node) {
|
|
|
89
102
|
}
|
|
90
103
|
}
|
|
91
104
|
|
|
105
|
+
/**
|
|
106
|
+
* @param {import('postcss').Declaration} decl
|
|
107
|
+
* @return {boolean}
|
|
108
|
+
*/
|
|
92
109
|
function shouldKeepZeroUnit(decl) {
|
|
93
110
|
const { parent } = decl;
|
|
94
111
|
const lowerCasedProp = decl.prop.toLowerCase();
|
|
95
112
|
return (
|
|
96
113
|
(decl.value.includes('%') &&
|
|
97
114
|
(lowerCasedProp === 'max-height' || lowerCasedProp === 'height')) ||
|
|
98
|
-
(parent
|
|
99
|
-
parent.parent
|
|
100
|
-
parent.parent.
|
|
115
|
+
(parent &&
|
|
116
|
+
parent.parent &&
|
|
117
|
+
parent.parent.type === 'atrule' &&
|
|
118
|
+
/** @type {import('postcss').AtRule} */ (
|
|
119
|
+
parent.parent
|
|
120
|
+
).name.toLowerCase() === 'keyframes' &&
|
|
101
121
|
lowerCasedProp === 'stroke-dasharray') ||
|
|
102
122
|
keepWhenZero.has(lowerCasedProp)
|
|
103
123
|
);
|
|
104
124
|
}
|
|
105
|
-
|
|
125
|
+
/**
|
|
126
|
+
* @param {Options} opts
|
|
127
|
+
* @param {import('postcss').Declaration} decl
|
|
128
|
+
* @return {void}
|
|
129
|
+
*/
|
|
106
130
|
function transform(opts, decl) {
|
|
107
131
|
const lowerCasedProp = decl.prop.toLowerCase();
|
|
108
132
|
if (
|
|
@@ -150,7 +174,13 @@ function transform(opts, decl) {
|
|
|
150
174
|
}
|
|
151
175
|
|
|
152
176
|
const plugin = 'postcss-convert-values';
|
|
153
|
-
|
|
177
|
+
/**
|
|
178
|
+
* @typedef {{precision: boolean | number, angle?: boolean, time?: boolean, length?: boolean}} Options */
|
|
179
|
+
/**
|
|
180
|
+
* @type {import('postcss').PluginCreator<Options>}
|
|
181
|
+
* @param {Options} opts
|
|
182
|
+
* @return {import('postcss').Plugin}
|
|
183
|
+
*/
|
|
154
184
|
function pluginCreator(opts = { precision: false }) {
|
|
155
185
|
return {
|
|
156
186
|
postcssPlugin: plugin,
|
package/src/lib/convert.js
CHANGED
|
@@ -15,7 +15,10 @@ const angleConv = new Map([
|
|
|
15
15
|
['turn', 360],
|
|
16
16
|
['deg', 1],
|
|
17
17
|
]);
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* @param {number} number
|
|
20
|
+
* @return {string}
|
|
21
|
+
*/
|
|
19
22
|
function dropLeadingZero(number) {
|
|
20
23
|
const value = String(number);
|
|
21
24
|
|
|
@@ -31,19 +34,33 @@ function dropLeadingZero(number) {
|
|
|
31
34
|
|
|
32
35
|
return value;
|
|
33
36
|
}
|
|
34
|
-
|
|
37
|
+
/**
|
|
38
|
+
* @param {number} number
|
|
39
|
+
* @param {string} originalUnit
|
|
40
|
+
* @param {Map<string, number>} conversions
|
|
41
|
+
* @return {string}
|
|
42
|
+
*/
|
|
35
43
|
function transform(number, originalUnit, conversions) {
|
|
36
44
|
let conversionUnits = [...conversions.keys()].filter((u) => {
|
|
37
45
|
return originalUnit !== u;
|
|
38
46
|
});
|
|
39
47
|
|
|
40
|
-
const base = number * conversions.get(originalUnit);
|
|
48
|
+
const base = number * /** @type {number} */ (conversions.get(originalUnit));
|
|
41
49
|
|
|
42
50
|
return conversionUnits
|
|
43
|
-
.map(
|
|
51
|
+
.map(
|
|
52
|
+
(u) =>
|
|
53
|
+
dropLeadingZero(base / /** @type {number} */ (conversions.get(u))) + u
|
|
54
|
+
)
|
|
44
55
|
.reduce((a, b) => (a.length < b.length ? a : b));
|
|
45
56
|
}
|
|
46
57
|
|
|
58
|
+
/**
|
|
59
|
+
* @param {number} number
|
|
60
|
+
* @param {string} unit
|
|
61
|
+
* @param {{time?: boolean, length?: boolean, angle?: boolean}} options
|
|
62
|
+
* @return {string}
|
|
63
|
+
*/
|
|
47
64
|
module.exports = function (number, unit, { time, length, angle }) {
|
|
48
65
|
let value = dropLeadingZero(number) + (unit ? unit : '');
|
|
49
66
|
let converted;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export = pluginCreator;
|
|
2
|
+
/**
|
|
3
|
+
* @typedef {{precision: boolean | number, angle?: boolean, time?: boolean, length?: boolean}} Options */
|
|
4
|
+
/**
|
|
5
|
+
* @type {import('postcss').PluginCreator<Options>}
|
|
6
|
+
* @param {Options} opts
|
|
7
|
+
* @return {import('postcss').Plugin}
|
|
8
|
+
*/
|
|
9
|
+
declare function pluginCreator(opts?: Options): import('postcss').Plugin;
|
|
10
|
+
declare namespace pluginCreator {
|
|
11
|
+
export { postcss, Options };
|
|
12
|
+
}
|
|
13
|
+
type Options = {
|
|
14
|
+
precision: boolean | number;
|
|
15
|
+
angle?: boolean;
|
|
16
|
+
time?: boolean;
|
|
17
|
+
length?: boolean;
|
|
18
|
+
};
|
|
19
|
+
declare var postcss: true;
|