postcss-calc 6.0.1 → 7.0.2
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/CHANGELOG.md +38 -0
- package/README.md +34 -16
- package/dist/index.js +19 -11
- package/dist/lib/convertUnit.js +157 -0
- package/dist/lib/reducer.js +327 -0
- package/dist/lib/stringifier.js +81 -0
- package/dist/lib/transform.js +45 -31
- package/dist/parser.js +3804 -0
- package/package.json +32 -25
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,41 @@
|
|
|
1
|
+
# 7.0.2
|
|
2
|
+
|
|
3
|
+
- Fixed: incorrect reduction of subtraction from zero ([#88](https://github.com/postcss/postcss-calc/issues/88))
|
|
4
|
+
- Fixed: doesn't remove calc for single function
|
|
5
|
+
- Fixed: relax parser on unknown units ([#76](https://github.com/postcss/postcss-calc/issues/76))
|
|
6
|
+
- Fixed: handle numbers with exponen composed ([#83](https://github.com/postcss/postcss-calc/pull/83))
|
|
7
|
+
- Fixed: handle plus sign before value ([#79](https://github.com/postcss/postcss-calc/pull/79))
|
|
8
|
+
- Fixed: better handle precision for nested calc ([#75](https://github.com/postcss/postcss-calc/pull/75))
|
|
9
|
+
- Fixed: properly handle nested add and sub expression inside sub expression ([#64](https://github.com/postcss/postcss-calc/issues/64))
|
|
10
|
+
- Fixed: handle uppercase units and functions ([#71](https://github.com/postcss/postcss-calc/pull/71))
|
|
11
|
+
- Fixed: do not break `calc` with single var ([cssnano/cssnano#725](https://github.com/cssnano/cssnano/issues/725))
|
|
12
|
+
- Updated: `postcss` to 7.0.27 (patch)
|
|
13
|
+
- Updated: `postcss-selector-parser` to 6.0.2
|
|
14
|
+
- Updated: `postcss-value-parser` to 4.0.2
|
|
15
|
+
|
|
16
|
+
# 7.0.1
|
|
17
|
+
|
|
18
|
+
- Updated: `postcss` to 7.0.2 (patch)
|
|
19
|
+
- Updated: `postcss-selector-parser` to 5.0.0-rc.4 (patch)
|
|
20
|
+
- Updated: `postcss-value-parser` to 3.3.1 (patch)
|
|
21
|
+
|
|
22
|
+
# 7.0.0
|
|
23
|
+
|
|
24
|
+
- Changed: Updated postcss-selector-parser to version 5.0.0-rc.3
|
|
25
|
+
- Changed: Dropped reduce-css-calc as a dependency
|
|
26
|
+
- Fixed: Support constant() and env() ([#42](https://github.com/postcss/postcss-calc/issues/42), [#48](https://github.com/postcss/postcss-calc/issues/48))
|
|
27
|
+
- Fixed: Support custom properties with "calc" in its name ([#50](https://github.com/postcss/postcss-calc/issues/50))
|
|
28
|
+
- Fixed: Remove unnecessary whitespace around `*` and `/` ([cssnano#625](https://github.com/cssnano/cssnano/issues/625))
|
|
29
|
+
- Fixed: Arithmetic bugs around subtraction ([#49](https://github.com/postcss/postcss-calc/issues/49))
|
|
30
|
+
- Fixed: Handling of nested calc statements ([reduce-css-calc#49](https://github.com/MoOx/reduce-css-calc/issues/49))
|
|
31
|
+
- Fixed: Bugs regarding complex calculations ([reduce-cs-calc#45](https://github.com/MoOx/reduce-css-calc/issues/45))
|
|
32
|
+
- Fixed: `100%` incorrectly being transformed to `1` ([reduce-css-calc#44](https://github.com/MoOx/reduce-css-calc/issues/44))
|
|
33
|
+
- Added: support for case-insensitive calc statements
|
|
34
|
+
|
|
35
|
+
# 6.0.2 - 2018-09-25
|
|
36
|
+
|
|
37
|
+
- Fixed: use PostCSS 7 (thanks to @douglasduteil)
|
|
38
|
+
|
|
1
39
|
# 6.0.1 - 2017-10-10
|
|
2
40
|
|
|
3
41
|
- Fixed: throwing error for attribute selectors without a value
|
package/README.md
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# PostCSS Calc [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][PostCSS]
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[![NPM Version][npm-img]][npm-url]
|
|
4
|
+
[![Build Status][cli-img]][cli-url]
|
|
5
|
+
[![Support Chat][git-img]][git-url]
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
[PostCSS Calc] lets you reduce `calc()` references whenever it's possible. This
|
|
8
|
+
can be particularly useful with the [PostCSS Custom Properties] plugin.
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
When multiple units are mixed together in the same expression, the `calc()`
|
|
11
|
+
statement is left as is, to fallback to the [W3C calc() implementation].
|
|
9
12
|
|
|
10
13
|
## Installation
|
|
11
14
|
|
|
12
|
-
```
|
|
13
|
-
|
|
15
|
+
```bash
|
|
16
|
+
npm install postcss-calc
|
|
14
17
|
```
|
|
15
18
|
|
|
16
19
|
## Usage
|
|
@@ -31,7 +34,7 @@ var output = postcss()
|
|
|
31
34
|
.css
|
|
32
35
|
```
|
|
33
36
|
|
|
34
|
-
**Example** (with [
|
|
37
|
+
**Example** (with [PostCSS Custom Properties] enabled as well):
|
|
35
38
|
|
|
36
39
|
```js
|
|
37
40
|
// dependencies
|
|
@@ -86,7 +89,7 @@ h1 {
|
|
|
86
89
|
}
|
|
87
90
|
```
|
|
88
91
|
|
|
89
|
-
Checkout [tests]
|
|
92
|
+
Checkout [tests] for more examples.
|
|
90
93
|
|
|
91
94
|
### Options
|
|
92
95
|
|
|
@@ -103,7 +106,8 @@ var out = postcss()
|
|
|
103
106
|
|
|
104
107
|
#### `preserve` (default: `false`)
|
|
105
108
|
|
|
106
|
-
Allow you to preserve calc() usage in output so browsers will handle decimal
|
|
109
|
+
Allow you to preserve calc() usage in output so browsers will handle decimal
|
|
110
|
+
precision themselves.
|
|
107
111
|
|
|
108
112
|
```js
|
|
109
113
|
var out = postcss()
|
|
@@ -157,15 +161,29 @@ div[data-size="calc(3*3)"] {
|
|
|
157
161
|
|
|
158
162
|
## Contributing
|
|
159
163
|
|
|
160
|
-
Work on a branch, install dev-dependencies, respect coding style & run tests
|
|
164
|
+
Work on a branch, install dev-dependencies, respect coding style & run tests
|
|
165
|
+
before submitting a bug fix or a feature.
|
|
161
166
|
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
+
```bash
|
|
168
|
+
git clone git@github.com:postcss/postcss-calc.git
|
|
169
|
+
git checkout -b patch-1
|
|
170
|
+
npm install
|
|
171
|
+
npm test
|
|
167
172
|
```
|
|
168
173
|
|
|
169
174
|
## [Changelog](CHANGELOG.md)
|
|
170
175
|
|
|
171
176
|
## [License](LICENSE)
|
|
177
|
+
|
|
178
|
+
[cli-img]: https://img.shields.io/travis/postcss/postcss-calc/master.svg
|
|
179
|
+
[cli-url]: https://travis-ci.org/postcss/postcss-calc
|
|
180
|
+
[git-img]: https://img.shields.io/badge/support-chat-blue.svg
|
|
181
|
+
[git-url]: https://gitter.im/postcss/postcss
|
|
182
|
+
[npm-img]: https://img.shields.io/npm/v/postcss-calc.svg
|
|
183
|
+
[npm-url]: https://www.npmjs.com/package/postcss-calc
|
|
184
|
+
|
|
185
|
+
[PostCSS]: https://github.com/postcss
|
|
186
|
+
[PostCSS Calc]: https://github.com/postcss/postcss-calc
|
|
187
|
+
[PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties
|
|
188
|
+
[tests]: src/__tests__/index.js
|
|
189
|
+
[W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.default = void 0;
|
|
6
7
|
|
|
7
|
-
var _postcss = require(
|
|
8
|
+
var _postcss = require("postcss");
|
|
8
9
|
|
|
9
|
-
var _transform = require(
|
|
10
|
-
|
|
11
|
-
var _transform2 = _interopRequireDefault(_transform);
|
|
10
|
+
var _transform = _interopRequireDefault(require("./lib/transform"));
|
|
12
11
|
|
|
13
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
var _default = (0, _postcss.plugin)('postcss-calc', function (opts) {
|
|
16
15
|
var options = Object.assign({
|
|
17
16
|
precision: 5,
|
|
18
17
|
preserve: false,
|
|
@@ -20,15 +19,24 @@ exports.default = (0, _postcss.plugin)('postcss-calc', function (opts) {
|
|
|
20
19
|
mediaQueries: false,
|
|
21
20
|
selectors: false
|
|
22
21
|
}, opts);
|
|
23
|
-
|
|
24
22
|
return function (css, result) {
|
|
25
23
|
css.walk(function (node) {
|
|
26
24
|
var type = node.type;
|
|
27
25
|
|
|
28
|
-
if (type === 'decl')
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
if (type === 'decl') {
|
|
27
|
+
(0, _transform.default)(node, "value", options, result);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (type === 'atrule' && options.mediaQueries) {
|
|
31
|
+
(0, _transform.default)(node, "params", options, result);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (type === 'rule' && options.selectors) {
|
|
35
|
+
(0, _transform.default)(node, "selector", options, result);
|
|
36
|
+
}
|
|
31
37
|
});
|
|
32
38
|
};
|
|
33
39
|
});
|
|
34
|
-
|
|
40
|
+
|
|
41
|
+
exports.default = _default;
|
|
42
|
+
module.exports = exports.default;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var conversions = {
|
|
8
|
+
// Absolute length units
|
|
9
|
+
'px': {
|
|
10
|
+
'px': 1,
|
|
11
|
+
'cm': 96 / 2.54,
|
|
12
|
+
'mm': 96 / 25.4,
|
|
13
|
+
'q': 96 / 101.6,
|
|
14
|
+
'in': 96,
|
|
15
|
+
'pt': 96 / 72,
|
|
16
|
+
'pc': 16
|
|
17
|
+
},
|
|
18
|
+
'cm': {
|
|
19
|
+
'px': 2.54 / 96,
|
|
20
|
+
'cm': 1,
|
|
21
|
+
'mm': 0.1,
|
|
22
|
+
'q': 0.025,
|
|
23
|
+
'in': 2.54,
|
|
24
|
+
'pt': 2.54 / 72,
|
|
25
|
+
'pc': 2.54 / 6
|
|
26
|
+
},
|
|
27
|
+
'mm': {
|
|
28
|
+
'px': 25.4 / 96,
|
|
29
|
+
'cm': 10,
|
|
30
|
+
'mm': 1,
|
|
31
|
+
'q': 0.25,
|
|
32
|
+
'in': 25.4,
|
|
33
|
+
'pt': 25.4 / 72,
|
|
34
|
+
'pc': 25.4 / 6
|
|
35
|
+
},
|
|
36
|
+
'q': {
|
|
37
|
+
'px': 101.6 / 96,
|
|
38
|
+
'cm': 40,
|
|
39
|
+
'mm': 4,
|
|
40
|
+
'q': 1,
|
|
41
|
+
'in': 101.6,
|
|
42
|
+
'pt': 101.6 / 72,
|
|
43
|
+
'pc': 101.6 / 6
|
|
44
|
+
},
|
|
45
|
+
'in': {
|
|
46
|
+
'px': 1 / 96,
|
|
47
|
+
'cm': 1 / 2.54,
|
|
48
|
+
'mm': 1 / 25.4,
|
|
49
|
+
'q': 1 / 101.6,
|
|
50
|
+
'in': 1,
|
|
51
|
+
'pt': 1 / 72,
|
|
52
|
+
'pc': 1 / 6
|
|
53
|
+
},
|
|
54
|
+
'pt': {
|
|
55
|
+
'px': 0.75,
|
|
56
|
+
'cm': 72 / 2.54,
|
|
57
|
+
'mm': 72 / 25.4,
|
|
58
|
+
'q': 72 / 101.6,
|
|
59
|
+
'in': 72,
|
|
60
|
+
'pt': 1,
|
|
61
|
+
'pc': 12
|
|
62
|
+
},
|
|
63
|
+
'pc': {
|
|
64
|
+
'px': 0.0625,
|
|
65
|
+
'cm': 6 / 2.54,
|
|
66
|
+
'mm': 6 / 25.4,
|
|
67
|
+
'q': 6 / 101.6,
|
|
68
|
+
'in': 6,
|
|
69
|
+
'pt': 6 / 72,
|
|
70
|
+
'pc': 1
|
|
71
|
+
},
|
|
72
|
+
// Angle units
|
|
73
|
+
'deg': {
|
|
74
|
+
'deg': 1,
|
|
75
|
+
'grad': 0.9,
|
|
76
|
+
'rad': 180 / Math.PI,
|
|
77
|
+
'turn': 360
|
|
78
|
+
},
|
|
79
|
+
'grad': {
|
|
80
|
+
'deg': 400 / 360,
|
|
81
|
+
'grad': 1,
|
|
82
|
+
'rad': 200 / Math.PI,
|
|
83
|
+
'turn': 400
|
|
84
|
+
},
|
|
85
|
+
'rad': {
|
|
86
|
+
'deg': Math.PI / 180,
|
|
87
|
+
'grad': Math.PI / 200,
|
|
88
|
+
'rad': 1,
|
|
89
|
+
'turn': Math.PI * 2
|
|
90
|
+
},
|
|
91
|
+
'turn': {
|
|
92
|
+
'deg': 1 / 360,
|
|
93
|
+
'grad': 0.0025,
|
|
94
|
+
'rad': 0.5 / Math.PI,
|
|
95
|
+
'turn': 1
|
|
96
|
+
},
|
|
97
|
+
// Duration units
|
|
98
|
+
's': {
|
|
99
|
+
's': 1,
|
|
100
|
+
'ms': 0.001
|
|
101
|
+
},
|
|
102
|
+
'ms': {
|
|
103
|
+
's': 1000,
|
|
104
|
+
'ms': 1
|
|
105
|
+
},
|
|
106
|
+
// Frequency units
|
|
107
|
+
'hz': {
|
|
108
|
+
'hz': 1,
|
|
109
|
+
'khz': 1000
|
|
110
|
+
},
|
|
111
|
+
'khz': {
|
|
112
|
+
'hz': 0.001,
|
|
113
|
+
'khz': 1
|
|
114
|
+
},
|
|
115
|
+
// Resolution units
|
|
116
|
+
'dpi': {
|
|
117
|
+
'dpi': 1,
|
|
118
|
+
'dpcm': 1 / 2.54,
|
|
119
|
+
'dppx': 1 / 96
|
|
120
|
+
},
|
|
121
|
+
'dpcm': {
|
|
122
|
+
'dpi': 2.54,
|
|
123
|
+
'dpcm': 1,
|
|
124
|
+
'dppx': 2.54 / 96
|
|
125
|
+
},
|
|
126
|
+
'dppx': {
|
|
127
|
+
'dpi': 96,
|
|
128
|
+
'dpcm': 96 / 2.54,
|
|
129
|
+
'dppx': 1
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
function convertUnit(value, sourceUnit, targetUnit, precision) {
|
|
134
|
+
var sourceUnitNormalized = sourceUnit.toLowerCase();
|
|
135
|
+
var targetUnitNormalized = targetUnit.toLowerCase();
|
|
136
|
+
|
|
137
|
+
if (!conversions[targetUnitNormalized]) {
|
|
138
|
+
throw new Error("Cannot convert to " + targetUnit);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!conversions[targetUnitNormalized][sourceUnitNormalized]) {
|
|
142
|
+
throw new Error("Cannot convert from " + sourceUnit + " to " + targetUnit);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
var converted = conversions[targetUnitNormalized][sourceUnitNormalized] * value;
|
|
146
|
+
|
|
147
|
+
if (precision !== false) {
|
|
148
|
+
precision = Math.pow(10, parseInt(precision) || 5);
|
|
149
|
+
return Math.round(converted * precision) / precision;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return converted;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
var _default = convertUnit;
|
|
156
|
+
exports.default = _default;
|
|
157
|
+
module.exports = exports.default;
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _convertUnit = _interopRequireDefault(require("./convertUnit"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
function isEqual(left, right) {
|
|
13
|
+
return left.type === right.type && left.value === right.value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isValueType(type) {
|
|
17
|
+
switch (type) {
|
|
18
|
+
case 'LengthValue':
|
|
19
|
+
case 'AngleValue':
|
|
20
|
+
case 'TimeValue':
|
|
21
|
+
case 'FrequencyValue':
|
|
22
|
+
case 'ResolutionValue':
|
|
23
|
+
case 'EmValue':
|
|
24
|
+
case 'ExValue':
|
|
25
|
+
case 'ChValue':
|
|
26
|
+
case 'RemValue':
|
|
27
|
+
case 'VhValue':
|
|
28
|
+
case 'VwValue':
|
|
29
|
+
case 'VminValue':
|
|
30
|
+
case 'VmaxValue':
|
|
31
|
+
case 'PercentageValue':
|
|
32
|
+
case 'Number':
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function flip(operator) {
|
|
40
|
+
return operator === '+' ? '-' : '+';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function flipValue(node) {
|
|
44
|
+
if (isValueType(node.type)) {
|
|
45
|
+
node.value = -node.value;
|
|
46
|
+
} else if (node.type === 'MathExpression') {
|
|
47
|
+
if (node.operator === '*' || node.operator === '/') {
|
|
48
|
+
node.left = flipValue(node.left);
|
|
49
|
+
} else {
|
|
50
|
+
node.left = flipValue(node.left);
|
|
51
|
+
node.right = flipValue(node.right);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return node;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function reduceAddSubExpression(node, precision) {
|
|
59
|
+
// something + 0 => something
|
|
60
|
+
// something - 0 => something
|
|
61
|
+
if (isValueType(node.right.type) && node.right.value === 0) {
|
|
62
|
+
return node.left;
|
|
63
|
+
} // 0 + something => something
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if (isValueType(node.left.type) && node.left.value === 0 && node.operator === "+") {
|
|
67
|
+
return node.right;
|
|
68
|
+
} // 0 - something => -something
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
if (isValueType(node.left.type) && node.left.value === 0 && node.operator === "-" && node.right.type !== "Function") {
|
|
72
|
+
return flipValue(node.right);
|
|
73
|
+
} // value + value
|
|
74
|
+
// value - value
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
if (isValueType(node.left.type) && node.left.type === node.right.type) {
|
|
78
|
+
var operator = node.operator;
|
|
79
|
+
|
|
80
|
+
var _covertNodesUnits = covertNodesUnits(node.left, node.right, precision),
|
|
81
|
+
left = _covertNodesUnits.left,
|
|
82
|
+
right = _covertNodesUnits.right;
|
|
83
|
+
|
|
84
|
+
if (operator === "+") {
|
|
85
|
+
left.value += right.value;
|
|
86
|
+
} else {
|
|
87
|
+
left.value -= right.value;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return left;
|
|
91
|
+
} // value <op> (expr)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
if (node.right.type === 'MathExpression' && (node.right.operator === '+' || node.right.operator === '-')) {
|
|
95
|
+
// something - (something + something) => something - something - something
|
|
96
|
+
// something - (something - something) => something - something + something
|
|
97
|
+
if ((node.right.operator === '+' || node.right.operator === '-') && node.operator === '-') {
|
|
98
|
+
node.right.operator = flip(node.right.operator);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (isValueType(node.left.type)) {
|
|
102
|
+
// value + (value + something) => value + something
|
|
103
|
+
// value + (value - something) => value - something
|
|
104
|
+
// value - (value + something) => value - something
|
|
105
|
+
// value - (value - something) => value + something
|
|
106
|
+
if (node.left.type === node.right.left.type) {
|
|
107
|
+
var _left = node.left,
|
|
108
|
+
_operator = node.operator,
|
|
109
|
+
_right = node.right;
|
|
110
|
+
node.left = reduce({
|
|
111
|
+
type: 'MathExpression',
|
|
112
|
+
operator: _operator,
|
|
113
|
+
left: _left,
|
|
114
|
+
right: _right.left
|
|
115
|
+
});
|
|
116
|
+
node.operator = _right.operator;
|
|
117
|
+
node.right = _right.right;
|
|
118
|
+
return reduce(node, precision);
|
|
119
|
+
} // something + (something + value) => dimension + something
|
|
120
|
+
// something + (something - value) => dimension + something
|
|
121
|
+
// something - (something + value) => dimension - something
|
|
122
|
+
// something - (something - value) => dimension - something
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
if (node.left.type === node.right.right.type) {
|
|
126
|
+
var _left2 = node.left,
|
|
127
|
+
_right2 = node.right;
|
|
128
|
+
node.left = reduce({
|
|
129
|
+
type: 'MathExpression',
|
|
130
|
+
operator: _right2.operator,
|
|
131
|
+
left: _left2,
|
|
132
|
+
right: _right2.right
|
|
133
|
+
});
|
|
134
|
+
node.right = _right2.left;
|
|
135
|
+
return reduce(node, precision);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
} // (expr) <op> value
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
if (node.left.type === 'MathExpression' && (node.left.operator === '+' || node.left.operator === '-') && isValueType(node.right.type)) {
|
|
142
|
+
// (value + something) + value => value + something
|
|
143
|
+
// (value - something) + value => value - something
|
|
144
|
+
// (value + something) - value => value + something
|
|
145
|
+
// (value - something) - value => value - something
|
|
146
|
+
if (node.right.type === node.left.left.type) {
|
|
147
|
+
var _left3 = node.left,
|
|
148
|
+
_operator2 = node.operator,
|
|
149
|
+
_right3 = node.right;
|
|
150
|
+
_left3.left = reduce({
|
|
151
|
+
type: 'MathExpression',
|
|
152
|
+
operator: _operator2,
|
|
153
|
+
left: _left3.left,
|
|
154
|
+
right: _right3
|
|
155
|
+
}, precision);
|
|
156
|
+
return reduce(_left3, precision);
|
|
157
|
+
} // (something + dimension) + dimension => something + dimension
|
|
158
|
+
// (something - dimension) + dimension => something - dimension
|
|
159
|
+
// (something + dimension) - dimension => something + dimension
|
|
160
|
+
// (something - dimension) - dimension => something - dimension
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
if (node.right.type === node.left.right.type) {
|
|
164
|
+
var _left4 = node.left,
|
|
165
|
+
_operator3 = node.operator,
|
|
166
|
+
_right4 = node.right;
|
|
167
|
+
|
|
168
|
+
if (_left4.operator === '-') {
|
|
169
|
+
_left4.operator = _operator3 === '-' ? '-' : '+';
|
|
170
|
+
_left4.right = reduce({
|
|
171
|
+
type: 'MathExpression',
|
|
172
|
+
operator: _operator3 === '-' ? '+' : '-',
|
|
173
|
+
left: _right4,
|
|
174
|
+
right: _left4.right
|
|
175
|
+
}, precision);
|
|
176
|
+
} else {
|
|
177
|
+
_left4.right = reduce({
|
|
178
|
+
type: 'MathExpression',
|
|
179
|
+
operator: _operator3,
|
|
180
|
+
left: _left4.right,
|
|
181
|
+
right: _right4
|
|
182
|
+
}, precision);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (_left4.right.value < 0) {
|
|
186
|
+
_left4.right.value *= -1;
|
|
187
|
+
_left4.operator = _left4.operator === '-' ? '+' : '-';
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
_left4.parenthesized = node.parenthesized;
|
|
191
|
+
return reduce(_left4, precision);
|
|
192
|
+
}
|
|
193
|
+
} // (expr) + (expr) => number
|
|
194
|
+
// (expr) - (expr) => number
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
if (node.right.type === 'MathExpression' && node.left.type === 'MathExpression') {
|
|
198
|
+
if (isEqual(node.left.right, node.right.right)) {
|
|
199
|
+
var newNodes = covertNodesUnits(node.left.left, node.right.left, precision);
|
|
200
|
+
node.left = newNodes.left;
|
|
201
|
+
node.right = newNodes.right;
|
|
202
|
+
return reduce(node);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (isEqual(node.left.right, node.right.left)) {
|
|
206
|
+
var _newNodes = covertNodesUnits(node.left.left, node.right.right, precision);
|
|
207
|
+
|
|
208
|
+
node.left = _newNodes.left;
|
|
209
|
+
node.right = _newNodes.right;
|
|
210
|
+
return reduce(node);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return node;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function reduceDivisionExpression(node) {
|
|
218
|
+
if (!isValueType(node.right.type)) {
|
|
219
|
+
return node;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (node.right.type !== 'Number') {
|
|
223
|
+
throw new Error(`Cannot divide by "${node.right.unit}", number expected`);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (node.right.value === 0) {
|
|
227
|
+
throw new Error('Cannot divide by zero');
|
|
228
|
+
} // something / value
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
if (isValueType(node.left.type)) {
|
|
232
|
+
node.left.value /= node.right.value;
|
|
233
|
+
return node.left;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return node;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function reduceMultiplicationExpression(node) {
|
|
240
|
+
// (expr) * number
|
|
241
|
+
if (node.left.type === 'MathExpression' && node.right.type === 'Number') {
|
|
242
|
+
if (isValueType(node.left.left.type) && isValueType(node.left.right.type)) {
|
|
243
|
+
node.left.left.value *= node.right.value;
|
|
244
|
+
node.left.right.value *= node.right.value;
|
|
245
|
+
return node.left;
|
|
246
|
+
}
|
|
247
|
+
} // something * number
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
if (isValueType(node.left.type) && node.right.type === 'Number') {
|
|
251
|
+
node.left.value *= node.right.value;
|
|
252
|
+
return node.left;
|
|
253
|
+
} // number * (expr)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
if (node.left.type === 'Number' && node.right.type === 'MathExpression') {
|
|
257
|
+
if (isValueType(node.right.left.type) && isValueType(node.right.right.type)) {
|
|
258
|
+
node.right.left.value *= node.left.value;
|
|
259
|
+
node.right.right.value *= node.left.value;
|
|
260
|
+
return node.right;
|
|
261
|
+
}
|
|
262
|
+
} // number * something
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
if (node.left.type === 'Number' && isValueType(node.right.type)) {
|
|
266
|
+
node.right.value *= node.left.value;
|
|
267
|
+
return node.right;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return node;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function covertNodesUnits(left, right, precision) {
|
|
274
|
+
switch (left.type) {
|
|
275
|
+
case 'LengthValue':
|
|
276
|
+
case 'AngleValue':
|
|
277
|
+
case 'TimeValue':
|
|
278
|
+
case 'FrequencyValue':
|
|
279
|
+
case 'ResolutionValue':
|
|
280
|
+
if (right.type === left.type && right.unit && left.unit) {
|
|
281
|
+
var converted = (0, _convertUnit.default)(right.value, right.unit, left.unit, precision);
|
|
282
|
+
right = {
|
|
283
|
+
type: left.type,
|
|
284
|
+
value: converted,
|
|
285
|
+
unit: left.unit
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return {
|
|
290
|
+
left,
|
|
291
|
+
right
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
default:
|
|
295
|
+
return {
|
|
296
|
+
left,
|
|
297
|
+
right
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function reduce(node, precision) {
|
|
303
|
+
if (node.type === "MathExpression") {
|
|
304
|
+
node.left = reduce(node.left, precision);
|
|
305
|
+
node.right = reduce(node.right, precision);
|
|
306
|
+
|
|
307
|
+
switch (node.operator) {
|
|
308
|
+
case "+":
|
|
309
|
+
case "-":
|
|
310
|
+
return reduceAddSubExpression(node, precision);
|
|
311
|
+
|
|
312
|
+
case "/":
|
|
313
|
+
return reduceDivisionExpression(node, precision);
|
|
314
|
+
|
|
315
|
+
case "*":
|
|
316
|
+
return reduceMultiplicationExpression(node, precision);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return node;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return node;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
var _default = reduce;
|
|
326
|
+
exports.default = _default;
|
|
327
|
+
module.exports = exports.default;
|