postcss-calc 8.2.0 → 8.2.4

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 CHANGED
@@ -1,12 +1,9 @@
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
4
  [![Support Chat][git-img]][git-url]
6
5
 
7
- [PostCSS Calc] lets you reduce `calc()` references whenever it's possible. This
8
- can be particularly useful with the [PostCSS Custom Properties] plugin.
9
-
6
+ [PostCSS Calc] lets you reduce `calc()` references whenever it's possible.
10
7
  When multiple units are mixed together in the same expression, the `calc()`
11
8
  statement is left as is, to fallback to the [W3C calc() implementation].
12
9
 
@@ -34,61 +31,27 @@ var output = postcss()
34
31
  .css
35
32
  ```
36
33
 
37
- **Example** (with [PostCSS Custom Properties] enabled as well):
38
-
39
- ```js
40
- // dependencies
41
- var fs = require("fs")
42
- var postcss = require("postcss")
43
- var customProperties = require("postcss-custom-properties")
44
- var calc = require("postcss-calc")
45
-
46
- // css to be processed
47
- var css = fs.readFileSync("input.css", "utf8")
48
-
49
- // process css
50
- var output = postcss()
51
- .use(customProperties())
52
- .use(calc())
53
- .process(css)
54
- .css
55
- ```
56
-
57
34
  Using this `input.css`:
58
35
 
59
36
  ```css
60
- :root {
61
- --main-font-size: 16px;
62
- }
63
-
64
- body {
65
- font-size: var(--main-font-size);
66
- }
67
-
68
37
  h1 {
69
- font-size: calc(var(--main-font-size) * 2);
38
+ font-size: calc(16px * 2);
70
39
  height: calc(100px - 2em);
71
- margin-bottom: calc(
72
- var(--main-font-size)
73
- * 1.5
74
- )
40
+ width: calc(2*var(--base-width));
41
+ margin-bottom: calc(16px * 1.5);
75
42
  }
76
43
  ```
77
44
 
78
45
  you will get:
79
46
 
80
47
  ```css
81
- body {
82
- font-size: 16px
83
- }
84
-
85
48
  h1 {
86
49
  font-size: 32px;
87
50
  height: calc(100px - 2em);
51
+ width: calc(2*var(--base-width));
88
52
  margin-bottom: 24px
89
53
  }
90
54
  ```
91
-
92
55
  Checkout [tests] for more examples.
93
56
 
94
57
  ### Options
@@ -159,6 +122,9 @@ div[data-size="calc(3*3)"] {
159
122
 
160
123
  ---
161
124
 
125
+ ## Related PostCSS plugins
126
+ To replace the value of CSS custom properties at build time, try [PostCSS Custom Properties].
127
+
162
128
  ## Contributing
163
129
 
164
130
  Work on a branch, install dev-dependencies, respect coding style & run tests
@@ -175,8 +141,6 @@ npm test
175
141
 
176
142
  ## [License](LICENSE)
177
143
 
178
- [cli-img]: https://img.shields.io/travis/postcss/postcss-calc/master.svg
179
- [cli-url]: https://travis-ci.org/postcss/postcss-calc
180
144
  [git-img]: https://img.shields.io/badge/support-chat-blue.svg
181
145
  [git-url]: https://gitter.im/postcss/postcss
182
146
  [npm-img]: https://img.shields.io/npm/v/postcss-calc.svg
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-calc",
3
- "version": "8.2.0",
3
+ "version": "8.2.4",
4
4
  "description": "PostCSS plugin to reduce calc()",
5
5
  "keywords": [
6
6
  "css",
@@ -9,10 +9,10 @@
9
9
  "calculation",
10
10
  "calc"
11
11
  ],
12
- "main": "dist/index.js",
12
+ "main": "src/index.js",
13
13
  "types": "types/index.d.ts",
14
14
  "files": [
15
- "dist",
15
+ "src",
16
16
  "types",
17
17
  "LICENSE"
18
18
  ],
@@ -22,38 +22,41 @@
22
22
  "eslintConfig": {
23
23
  "extends": [
24
24
  "eslint:recommended",
25
- "plugin:import/recommended"
25
+ "prettier"
26
+ ],
27
+ "env": {
28
+ "node": true,
29
+ "es2017": true
30
+ },
31
+ "ignorePatterns": [
32
+ "src/parser.js"
26
33
  ],
27
34
  "rules": {
28
35
  "curly": "error"
29
36
  }
30
37
  },
31
38
  "devDependencies": {
32
- "@babel/cli": "^7.16.0",
33
- "@babel/core": "^7.16.5",
34
- "@babel/plugin-transform-modules-commonjs": "^7.16.5",
35
- "@babel/register": "^7.16.7",
36
- "babel-plugin-add-module-exports": "^1.0.0",
37
- "eslint": "^8.5.0",
38
- "eslint-plugin-import": "^2.25.3",
39
+ "@types/node": "^17.0.15",
40
+ "eslint": "^8.8.0",
41
+ "eslint-config-prettier": "^8.3.0",
39
42
  "jison-gho": "^0.6.1-216",
40
43
  "postcss": "^8.2.2",
41
- "rimraf": "^3.0.2",
42
- "typescript": "^4.5.4",
43
- "uvu": "^0.5.2"
44
+ "prettier": "^2.5.1",
45
+ "typescript": "^4.5.5",
46
+ "uvu": "^0.5.3"
44
47
  },
45
48
  "dependencies": {
46
- "postcss-selector-parser": "^6.0.2",
47
- "postcss-value-parser": "^4.0.2"
49
+ "postcss-selector-parser": "^6.0.9",
50
+ "postcss-value-parser": "^4.2.0"
48
51
  },
49
52
  "peerDependencies": {
50
53
  "postcss": "^8.2.2"
51
54
  },
52
55
  "scripts": {
53
- "build": "rimraf dist && babel src --out-dir dist --ignore src/__tests__/**/*.js && jison src/parser.jison -o dist/parser.js",
56
+ "build": "jison src/parser.jison -o src/parser.js",
54
57
  "lint": "eslint src && tsc",
55
58
  "pretest": "pnpm run build",
56
- "test": "uvu -r @babel/register src/__tests__"
59
+ "test": "uvu src/__tests__"
57
60
  },
58
- "readme": "# PostCSS Calc [<img src=\"https://postcss.github.io/postcss/logo.svg\" alt=\"PostCSS\" width=\"90\" height=\"90\" align=\"right\">][PostCSS]\n\n[![NPM Version][npm-img]][npm-url]\n[![Build Status][cli-img]][cli-url]\n[![Support Chat][git-img]][git-url]\n\n[PostCSS Calc] lets you reduce `calc()` references whenever it's possible. This\ncan be particularly useful with the [PostCSS Custom Properties] plugin.\n\nWhen multiple units are mixed together in the same expression, the `calc()`\nstatement is left as is, to fallback to the [W3C calc() implementation].\n\n## Installation\n\n```bash\nnpm install postcss-calc\n```\n\n## Usage\n\n```js\n// dependencies\nvar fs = require(\"fs\")\nvar postcss = require(\"postcss\")\nvar calc = require(\"postcss-calc\")\n\n// css to be processed\nvar css = fs.readFileSync(\"input.css\", \"utf8\")\n\n// process css\nvar output = postcss()\n .use(calc())\n .process(css)\n .css\n```\n\n**Example** (with [PostCSS Custom Properties] enabled as well):\n\n```js\n// dependencies\nvar fs = require(\"fs\")\nvar postcss = require(\"postcss\")\nvar customProperties = require(\"postcss-custom-properties\")\nvar calc = require(\"postcss-calc\")\n\n// css to be processed\nvar css = fs.readFileSync(\"input.css\", \"utf8\")\n\n// process css\nvar output = postcss()\n .use(customProperties())\n .use(calc())\n .process(css)\n .css\n```\n\nUsing this `input.css`:\n\n```css\n:root {\n --main-font-size: 16px;\n}\n\nbody {\n font-size: var(--main-font-size);\n}\n\nh1 {\n font-size: calc(var(--main-font-size) * 2);\n height: calc(100px - 2em);\n margin-bottom: calc(\n var(--main-font-size)\n * 1.5\n )\n}\n```\n\nyou will get:\n\n```css\nbody {\n font-size: 16px\n}\n\nh1 {\n font-size: 32px;\n height: calc(100px - 2em);\n margin-bottom: 24px\n}\n```\n\nCheckout [tests] for more examples.\n\n### Options\n\n#### `precision` (default: `5`)\n\nAllow you to define the precision for decimal numbers.\n\n```js\nvar out = postcss()\n .use(calc({precision: 10}))\n .process(css)\n .css\n```\n\n#### `preserve` (default: `false`)\n\nAllow you to preserve calc() usage in output so browsers will handle decimal\nprecision themselves.\n\n```js\nvar out = postcss()\n .use(calc({preserve: true}))\n .process(css)\n .css\n```\n\n#### `warnWhenCannotResolve` (default: `false`)\n\nAdds warnings when calc() are not reduced to a single value.\n\n```js\nvar out = postcss()\n .use(calc({warnWhenCannotResolve: true}))\n .process(css)\n .css\n```\n\n#### `mediaQueries` (default: `false`)\n\nAllows calc() usage as part of media query declarations.\n\n```js\nvar out = postcss()\n .use(calc({mediaQueries: true}))\n .process(css)\n .css\n```\n\n#### `selectors` (default: `false`)\n\nAllows calc() usage as part of selectors.\n\n```js\nvar out = postcss()\n .use(calc({selectors: true}))\n .process(css)\n .css\n```\n\nExample:\n\n```css\ndiv[data-size=\"calc(3*3)\"] {\n width: 100px;\n}\n```\n\n---\n\n## Contributing\n\nWork on a branch, install dev-dependencies, respect coding style & run tests\nbefore submitting a bug fix or a feature.\n\n```bash\ngit clone git@github.com:postcss/postcss-calc.git\ngit checkout -b patch-1\nnpm install\nnpm test\n```\n\n## [Changelog](CHANGELOG.md)\n\n## [License](LICENSE)\n\n[cli-img]: https://img.shields.io/travis/postcss/postcss-calc/master.svg\n[cli-url]: https://travis-ci.org/postcss/postcss-calc\n[git-img]: https://img.shields.io/badge/support-chat-blue.svg\n[git-url]: https://gitter.im/postcss/postcss\n[npm-img]: https://img.shields.io/npm/v/postcss-calc.svg\n[npm-url]: https://www.npmjs.com/package/postcss-calc\n\n[PostCSS]: https://github.com/postcss\n[PostCSS Calc]: https://github.com/postcss/postcss-calc\n[PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties\n[tests]: src/__tests__/index.js\n[W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation\n"
61
+ "readme": "# PostCSS Calc [<img src=\"https://postcss.github.io/postcss/logo.svg\" alt=\"PostCSS\" width=\"90\" height=\"90\" align=\"right\">][PostCSS]\n\n[![NPM Version][npm-img]][npm-url]\n[![Support Chat][git-img]][git-url]\n\n[PostCSS Calc] lets you reduce `calc()` references whenever it's possible.\nWhen multiple units are mixed together in the same expression, the `calc()`\nstatement is left as is, to fallback to the [W3C calc() implementation].\n\n## Installation\n\n```bash\nnpm install postcss-calc\n```\n\n## Usage\n\n```js\n// dependencies\nvar fs = require(\"fs\")\nvar postcss = require(\"postcss\")\nvar calc = require(\"postcss-calc\")\n\n// css to be processed\nvar css = fs.readFileSync(\"input.css\", \"utf8\")\n\n// process css\nvar output = postcss()\n .use(calc())\n .process(css)\n .css\n```\n\nUsing this `input.css`:\n\n```css\nh1 {\n font-size: calc(16px * 2);\n height: calc(100px - 2em);\n width: calc(2*var(--base-width));\n margin-bottom: calc(16px * 1.5);\n}\n```\n\nyou will get:\n\n```css\nh1 {\n font-size: 32px;\n height: calc(100px - 2em);\n width: calc(2*var(--base-width));\n margin-bottom: 24px\n}\n```\nCheckout [tests] for more examples.\n\n### Options\n\n#### `precision` (default: `5`)\n\nAllow you to define the precision for decimal numbers.\n\n```js\nvar out = postcss()\n .use(calc({precision: 10}))\n .process(css)\n .css\n```\n\n#### `preserve` (default: `false`)\n\nAllow you to preserve calc() usage in output so browsers will handle decimal\nprecision themselves.\n\n```js\nvar out = postcss()\n .use(calc({preserve: true}))\n .process(css)\n .css\n```\n\n#### `warnWhenCannotResolve` (default: `false`)\n\nAdds warnings when calc() are not reduced to a single value.\n\n```js\nvar out = postcss()\n .use(calc({warnWhenCannotResolve: true}))\n .process(css)\n .css\n```\n\n#### `mediaQueries` (default: `false`)\n\nAllows calc() usage as part of media query declarations.\n\n```js\nvar out = postcss()\n .use(calc({mediaQueries: true}))\n .process(css)\n .css\n```\n\n#### `selectors` (default: `false`)\n\nAllows calc() usage as part of selectors.\n\n```js\nvar out = postcss()\n .use(calc({selectors: true}))\n .process(css)\n .css\n```\n\nExample:\n\n```css\ndiv[data-size=\"calc(3*3)\"] {\n width: 100px;\n}\n```\n\n---\n\n## Related PostCSS plugins\nTo replace the value of CSS custom properties at build time, try [PostCSS Custom Properties].\n\n## Contributing\n\nWork on a branch, install dev-dependencies, respect coding style & run tests\nbefore submitting a bug fix or a feature.\n\n```bash\ngit clone git@github.com:postcss/postcss-calc.git\ngit checkout -b patch-1\nnpm install\nnpm test\n```\n\n## [Changelog](CHANGELOG.md)\n\n## [License](LICENSE)\n\n[git-img]: https://img.shields.io/badge/support-chat-blue.svg\n[git-url]: https://gitter.im/postcss/postcss\n[npm-img]: https://img.shields.io/npm/v/postcss-calc.svg\n[npm-url]: https://www.npmjs.com/package/postcss-calc\n\n[PostCSS]: https://github.com/postcss\n[PostCSS Calc]: https://github.com/postcss/postcss-calc\n[PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties\n[tests]: src/__tests__/index.js\n[W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation\n"
59
62
  }
@@ -0,0 +1,421 @@
1
+ 'use strict';
2
+ const { test } = require('uvu');
3
+ const assert = require('uvu/assert');
4
+
5
+ const convertUnit = require('../lib/convertUnit.js');
6
+
7
+ test('valid conversions', () => {
8
+ const conversions = [
9
+ // source value, source unit, expected value, target unit
10
+ [10, 'px', 10, 'px'],
11
+ [10, 'px', 0.26458, 'cm'],
12
+ [10, 'px', 2.64583, 'mm'],
13
+ [10, 'px', 10.58333, 'q'],
14
+ [10, 'px', 0.10417, 'in'],
15
+ [10, 'px', 7.5, 'pt'],
16
+ [10, 'px', 0.625, 'pc'],
17
+ [10, 'cm', 377.95276, 'px'],
18
+ [10, 'cm', 10, 'cm'],
19
+ [10, 'cm', 100, 'mm'],
20
+ [10, 'cm', 400, 'q'],
21
+ [10, 'cm', 3.93701, 'in'],
22
+ [10, 'cm', 283.46457, 'pt'],
23
+ [10, 'cm', 23.62205, 'pc'],
24
+ [10, 'mm', 37.79528, 'px'],
25
+ [10, 'mm', 1, 'cm'],
26
+ [10, 'mm', 10, 'mm'],
27
+ [10, 'mm', 40, 'q'],
28
+ [10, 'mm', 0.3937, 'in'],
29
+ [10, 'mm', 28.34646, 'pt'],
30
+ [10, 'mm', 2.3622, 'pc'],
31
+ [10, 'q', 9.44882, 'px'],
32
+ [10, 'q', 0.25, 'cm'],
33
+ [10, 'q', 2.5, 'mm'],
34
+ [10, 'q', 0.09843, 'in'],
35
+ [10, 'q', 7.08661, 'pt'],
36
+ [10, 'q', 0.59055, 'pc'],
37
+ [10, 'in', 960, 'px'],
38
+ [10, 'in', 25.4, 'cm'],
39
+ [10, 'in', 254, 'mm'],
40
+ [10, 'in', 1016, 'q'],
41
+ [10, 'in', 10, 'in'],
42
+ [10, 'in', 720, 'pt'],
43
+ [10, 'in', 60, 'pc'],
44
+ [10, 'pt', 13.33333, 'px'],
45
+ [10, 'pt', 0.35278, 'cm'],
46
+ [10, 'pt', 3.52778, 'mm'],
47
+ [10, 'pt', 14.11111, 'q'],
48
+ [10, 'pt', 0.13889, 'in'],
49
+ [10, 'pt', 10, 'pt'],
50
+ [10, 'pt', 0.83333, 'pc'],
51
+ [10, 'pc', 160, 'px'],
52
+ [10, 'pc', 4.23333, 'cm'],
53
+ [10, 'pc', 42.33333, 'mm'],
54
+ [10, 'pc', 169.33333, 'q'],
55
+ [10, 'pc', 1.66667, 'in'],
56
+ [10, 'pc', 120, 'pt'],
57
+ [10, 'pc', 10, 'pc'],
58
+ [10, 'deg', 10, 'deg'],
59
+ [10, 'deg', 11.11111, 'grad'],
60
+ [10, 'deg', 0.17453, 'rad'],
61
+ [10, 'deg', 0.02778, 'turn'],
62
+ [10, 'grad', 9, 'deg'],
63
+ [10, 'grad', 10, 'grad'],
64
+ [10, 'grad', 0.15708, 'rad'],
65
+ [10, 'grad', 0.025, 'turn'],
66
+ [10, 'rad', 572.9578, 'deg'],
67
+ [10, 'rad', 636.61977, 'grad'],
68
+ [10, 'rad', 10, 'rad'],
69
+ [10, 'rad', 1.59155, 'turn'],
70
+ [10, 'turn', 3600, 'deg'],
71
+ [10, 'turn', 4000, 'grad'],
72
+ [10, 'turn', 62.83185, 'rad'],
73
+ [10, 'turn', 10, 'turn'],
74
+ [10, 's', 10, 's'],
75
+ [10, 's', 10000, 'ms'],
76
+ [10, 'ms', 0.01, 's'],
77
+ [10, 'ms', 10, 'ms'],
78
+ [10, 'Hz', 10, 'Hz'],
79
+ [10, 'Hz', 0.01, 'kHz'],
80
+ [10, 'kHz', 10000, 'Hz'],
81
+ [10, 'kHz', 10, 'kHz'],
82
+ [10, 'dpi', 10, 'dpi'],
83
+ [10, 'dpi', 25.4, 'dpcm'],
84
+ [10, 'dpi', 960, 'dppx'],
85
+ [10, 'dpcm', 3.93701, 'dpi'],
86
+ [10, 'dpcm', 10, 'dpcm'],
87
+ [10, 'dpcm', 377.95276, 'dppx'],
88
+ [10, 'dppx', 0.10417, 'dpi'],
89
+ [10, 'dppx', 0.26458, 'dpcm'],
90
+ [10, 'dppx', 10, 'dppx'],
91
+ ];
92
+
93
+ conversions.forEach(function (e) {
94
+ const value = e[0];
95
+ const unit = e[1];
96
+ const expected = e[2];
97
+ const targetUnit = e[3];
98
+
99
+ assert.is(
100
+ convertUnit(value, unit, targetUnit),
101
+ expected,
102
+ unit + ' -> ' + targetUnit
103
+ );
104
+ });
105
+ });
106
+
107
+ test('invalid conversions', () => {
108
+ const invalid_units = {
109
+ px: [
110
+ 'deg',
111
+ 'grad',
112
+ 'rad',
113
+ 'turn',
114
+ 's',
115
+ 'ms',
116
+ 'Hz',
117
+ 'kHz',
118
+ 'dpi',
119
+ 'dpcm',
120
+ 'dppx',
121
+ ],
122
+ cm: [
123
+ 'deg',
124
+ 'grad',
125
+ 'rad',
126
+ 'turn',
127
+ 's',
128
+ 'ms',
129
+ 'Hz',
130
+ 'kHz',
131
+ 'dpi',
132
+ 'dpcm',
133
+ 'dppx',
134
+ ],
135
+ mm: [
136
+ 'deg',
137
+ 'grad',
138
+ 'rad',
139
+ 'turn',
140
+ 's',
141
+ 'ms',
142
+ 'Hz',
143
+ 'kHz',
144
+ 'dpi',
145
+ 'dpcm',
146
+ 'dppx',
147
+ ],
148
+ q: [
149
+ 'deg',
150
+ 'grad',
151
+ 'rad',
152
+ 'turn',
153
+ 's',
154
+ 'ms',
155
+ 'Hz',
156
+ 'kHz',
157
+ 'dpi',
158
+ 'dpcm',
159
+ 'dppx',
160
+ ],
161
+ in: [
162
+ 'deg',
163
+ 'grad',
164
+ 'rad',
165
+ 'turn',
166
+ 's',
167
+ 'ms',
168
+ 'Hz',
169
+ 'kHz',
170
+ 'dpi',
171
+ 'dpcm',
172
+ 'dppx',
173
+ ],
174
+ pt: [
175
+ 'deg',
176
+ 'grad',
177
+ 'rad',
178
+ 'turn',
179
+ 's',
180
+ 'ms',
181
+ 'Hz',
182
+ 'kHz',
183
+ 'dpi',
184
+ 'dpcm',
185
+ 'dppx',
186
+ ],
187
+ pc: [
188
+ 'deg',
189
+ 'grad',
190
+ 'rad',
191
+ 'turn',
192
+ 's',
193
+ 'ms',
194
+ 'Hz',
195
+ 'kHz',
196
+ 'dpi',
197
+ 'dpcm',
198
+ 'dppx',
199
+ ],
200
+ deg: [
201
+ 'px',
202
+ 'cm',
203
+ 'mm',
204
+ 'in',
205
+ 'pt',
206
+ 'pc',
207
+ 's',
208
+ 'ms',
209
+ 'Hz',
210
+ 'kHz',
211
+ 'dpi',
212
+ 'dpcm',
213
+ 'dppx',
214
+ ],
215
+ grad: [
216
+ 'px',
217
+ 'cm',
218
+ 'mm',
219
+ 'in',
220
+ 'pt',
221
+ 'pc',
222
+ 's',
223
+ 'ms',
224
+ 'Hz',
225
+ 'kHz',
226
+ 'dpi',
227
+ 'dpcm',
228
+ 'dppx',
229
+ ],
230
+ rad: [
231
+ 'px',
232
+ 'cm',
233
+ 'mm',
234
+ 'in',
235
+ 'pt',
236
+ 'pc',
237
+ 's',
238
+ 'ms',
239
+ 'Hz',
240
+ 'kHz',
241
+ 'dpi',
242
+ 'dpcm',
243
+ 'dppx',
244
+ ],
245
+ turn: [
246
+ 'px',
247
+ 'cm',
248
+ 'mm',
249
+ 'in',
250
+ 'pt',
251
+ 'pc',
252
+ 's',
253
+ 'ms',
254
+ 'Hz',
255
+ 'kHz',
256
+ 'dpi',
257
+ 'dpcm',
258
+ 'dppx',
259
+ ],
260
+ s: [
261
+ 'px',
262
+ 'cm',
263
+ 'mm',
264
+ 'in',
265
+ 'pt',
266
+ 'pc',
267
+ 'deg',
268
+ 'grad',
269
+ 'rad',
270
+ 'turn',
271
+ 'Hz',
272
+ 'kHz',
273
+ 'dpi',
274
+ 'dpcm',
275
+ 'dppx',
276
+ ],
277
+ ms: [
278
+ 'px',
279
+ 'cm',
280
+ 'mm',
281
+ 'in',
282
+ 'pt',
283
+ 'pc',
284
+ 'deg',
285
+ 'grad',
286
+ 'rad',
287
+ 'turn',
288
+ 'Hz',
289
+ 'kHz',
290
+ 'dpi',
291
+ 'dpcm',
292
+ 'dppx',
293
+ ],
294
+ Hz: [
295
+ 'px',
296
+ 'cm',
297
+ 'mm',
298
+ 'in',
299
+ 'pt',
300
+ 'pc',
301
+ 'deg',
302
+ 'grad',
303
+ 'rad',
304
+ 'turn',
305
+ 's',
306
+ 'ms',
307
+ 'dpi',
308
+ 'dpcm',
309
+ 'dppx',
310
+ ],
311
+ kHz: [
312
+ 'px',
313
+ 'cm',
314
+ 'mm',
315
+ 'in',
316
+ 'pt',
317
+ 'pc',
318
+ 'deg',
319
+ 'grad',
320
+ 'rad',
321
+ 'turn',
322
+ 's',
323
+ 'ms',
324
+ 'dpi',
325
+ 'dpcm',
326
+ 'dppx',
327
+ ],
328
+ dpi: [
329
+ 'px',
330
+ 'cm',
331
+ 'mm',
332
+ 'in',
333
+ 'pt',
334
+ 'pc',
335
+ 'deg',
336
+ 'grad',
337
+ 'rad',
338
+ 'turn',
339
+ 's',
340
+ 'ms',
341
+ 'Hz',
342
+ 'kHz',
343
+ ],
344
+ dpcm: [
345
+ 'px',
346
+ 'cm',
347
+ 'mm',
348
+ 'in',
349
+ 'pt',
350
+ 'pc',
351
+ 'deg',
352
+ 'grad',
353
+ 'rad',
354
+ 'turn',
355
+ 's',
356
+ 'ms',
357
+ 'Hz',
358
+ 'kHz',
359
+ ],
360
+ dppx: [
361
+ 'px',
362
+ 'cm',
363
+ 'mm',
364
+ 'in',
365
+ 'pt',
366
+ 'pc',
367
+ 'deg',
368
+ 'grad',
369
+ 'rad',
370
+ 'turn',
371
+ 's',
372
+ 'ms',
373
+ 'Hz',
374
+ 'kHz',
375
+ ],
376
+ };
377
+
378
+ for (const unit in invalid_units) {
379
+ invalid_units[unit].forEach((targetUnit) => {
380
+ let failed = false;
381
+
382
+ try {
383
+ convertUnit(10, unit, targetUnit);
384
+ } catch (e) {
385
+ failed = true;
386
+ }
387
+
388
+ assert.ok(failed, unit + ' -> ' + targetUnit);
389
+ });
390
+ }
391
+ });
392
+
393
+ test('precision', () => {
394
+ const precision = 10;
395
+ const conversions = [
396
+ // source value, source unit, expected value, target unit
397
+ [10, 'px', 0.2645833333, 'cm'],
398
+ [10, 'px', 2.6458333333, 'mm'],
399
+ [10, 'px', 0.1041666667, 'in'],
400
+ [10, 'cm', 377.9527559055, 'px'],
401
+ ];
402
+
403
+ conversions.forEach((e) => {
404
+ const value = e[0];
405
+ const unit = e[1];
406
+ const expected = e[2];
407
+ const targetUnit = e[3];
408
+
409
+ assert.is(
410
+ convertUnit(value, unit, targetUnit, precision),
411
+ expected,
412
+ unit + ' -> ' + targetUnit
413
+ );
414
+ });
415
+ });
416
+
417
+ test('falsey precision', () => {
418
+ assert.is(convertUnit(10, 'px', 'cm', false), 0.26458333333333334);
419
+ });
420
+
421
+ test.run();