blob: f3bd52694432cf07652886db6892993d7bf53a29 [file] [log] [blame]
Tim van der Lippe706ec962021-06-04 13:24:42 +01001var OMIT_PLUSSIGN = /^(?:\+|(-))?0*(\d*)(?:\.0*|(\.\d*?)0*)?$/;
2var KEEP_PLUSSIGN = /^([\+\-])?0*(\d*)(?:\.0*|(\.\d*?)0*)?$/;
3var unsafeToRemovePlusSignAfter = {
4 Dimension: true,
5 Hash: true,
6 Identifier: true,
7 Number: true,
8 Raw: true,
9 UnicodeRange: true
10};
11
12function packNumber(value, item) {
13 // omit plus sign only if no prev or prev is safe type
14 var regexp = item && item.prev !== null && unsafeToRemovePlusSignAfter.hasOwnProperty(item.prev.data.type)
15 ? KEEP_PLUSSIGN
16 : OMIT_PLUSSIGN;
17
18 // 100 -> '100'
19 // 00100 -> '100'
20 // +100 -> '100' (only when safe, e.g. omitting plus sign for 1px+1px leads to single dimension instead of two)
21 // -100 -> '-100'
22 // 0.123 -> '.123'
23 // 0.12300 -> '.123'
24 // 0.0 -> ''
25 // 0 -> ''
26 // -0 -> '-'
27 value = String(value).replace(regexp, '$1$2$3');
28
29 if (value === '' || value === '-') {
30 value = '0';
31 }
32
33 return value;
34}
35
36module.exports = function(node, item) {
37 node.value = packNumber(node.value, item);
38};
39module.exports.pack = packNumber;