@thi.ng/units 0.4.45 → 0.5.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/CHANGELOG.md +11 -1
- package/README.md +24 -20
- package/constants/paper-sizes.d.ts +4 -0
- package/constants/paper-sizes.js +9 -0
- package/package.json +11 -11
- package/unit.d.ts +14 -6
- package/unit.js +10 -20
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-
|
|
3
|
+
- **Last updated**: 2024-06-21T19:34:38Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,16 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [0.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/units@0.5.0) (2024-06-21)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add DPI constants/presets ([5fe2f78](https://github.com/thi-ng/umbrella/commit/5fe2f78))
|
|
17
|
+
|
|
18
|
+
#### 🩹 Bug fixes
|
|
19
|
+
|
|
20
|
+
- update DPI constants ([0b36809](https://github.com/thi-ng/umbrella/commit/0b36809))
|
|
21
|
+
|
|
12
22
|
### [0.4.14](https://github.com/thi-ng/umbrella/tree/@thi.ng/units@0.4.14) (2023-11-09)
|
|
13
23
|
|
|
14
24
|
#### ♻️ Refactoring
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://mastodon.thi.ng/@toxi)
|
|
8
8
|
|
|
9
9
|
> [!NOTE]
|
|
10
|
-
> This is one of
|
|
10
|
+
> This is one of 193 standalone projects, maintained as part
|
|
11
11
|
> of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
|
|
12
12
|
> and anti-framework.
|
|
13
13
|
>
|
|
@@ -540,6 +540,8 @@ import { convert, mul, quantity, NONE } from "@thi.ng/units";
|
|
|
540
540
|
|
|
541
541
|
// DIN A4 paper size (also available as preset)
|
|
542
542
|
const A4 = quantity([210, 297], "mm");
|
|
543
|
+
// (also available as preset)
|
|
544
|
+
const DPI_300 = quantity(300, "dpi");
|
|
543
545
|
|
|
544
546
|
// convert paper size to inches
|
|
545
547
|
convert(A4, "in");
|
|
@@ -548,15 +550,16 @@ convert(A4, "in");
|
|
|
548
550
|
// or calculate pixel dimensions @ 300 dpi
|
|
549
551
|
// the result of this product is dimensionless,
|
|
550
552
|
// so we use the NONE preset as target unit...
|
|
551
|
-
convert(mul(A4,
|
|
553
|
+
convert(mul(A4, DPI_300), NONE)
|
|
552
554
|
// [ 2480.314960629921, 3507.8740157480315 ]
|
|
553
555
|
|
|
554
556
|
// alternatively, dimensionless units can be deref'd directly
|
|
555
|
-
mul(A4,
|
|
557
|
+
mul(A4, DPI_300).deref()
|
|
556
558
|
// [ 2480.314960629921, 3507.8740157480315 ]
|
|
557
559
|
```
|
|
558
560
|
|
|
559
|
-
When combining different quantities, their units do not need to be the same
|
|
561
|
+
When combining different quantities, their units do not need to be the same (but
|
|
562
|
+
compatible):
|
|
560
563
|
|
|
561
564
|
```ts
|
|
562
565
|
import { convert, mul, quantity } from "@thi.ng/units";
|
|
@@ -570,21 +573,22 @@ convert(mul(quantity(10, "mm"), quantity(2, "in")), "cm2")
|
|
|
570
573
|
|
|
571
574
|
The following constants are provided (more to come):
|
|
572
575
|
|
|
573
|
-
| Var name
|
|
574
|
-
|
|
575
|
-
| `DIN_A0` ... `DIN_A8`
|
|
576
|
-
| `
|
|
577
|
-
| `
|
|
578
|
-
| `
|
|
579
|
-
| `
|
|
580
|
-
| `
|
|
581
|
-
| `
|
|
582
|
-
| `
|
|
583
|
-
| `
|
|
584
|
-
| `
|
|
585
|
-
| `
|
|
586
|
-
| `
|
|
587
|
-
| `
|
|
576
|
+
| Var name | Unit | Comment |
|
|
577
|
+
|----------------------------------------------|---------------------|---------------------------|
|
|
578
|
+
| `DIN_A0` ... `DIN_A8` | 2d vector of `mm` | Paper sizes<sup>(1)</sup> |
|
|
579
|
+
| `DPI_72` / `DPI_150` / `DPI_300` / `DPI_600` | dots per inch | Screen/print resolutions |
|
|
580
|
+
| `EARTH_GRAVITY` | `m/s` | |
|
|
581
|
+
| `EARTH_CIRCUMFERENCE` | `m` | |
|
|
582
|
+
| `EARTH_MASS` | `kg` | |
|
|
583
|
+
| `EARTH_RADIUS` | `m` | |
|
|
584
|
+
| `GRAVITATION` | `kg-1·m3·s-2` | Gravitational constant |
|
|
585
|
+
| `SPEED_OF_LIGHT` | `m/s` | |
|
|
586
|
+
| `SPEED_OF_SOUND_IN_AIR` | `m/s` | at 20 ℃ |
|
|
587
|
+
| `SPEED_OF_SOUND_IN_WATER` | `m/s` | at 20 ℃ |
|
|
588
|
+
| `US_ANSI_A` ... `US_ANSI_E` | 2d vector of `inch` | Paper sizes<sup>(1)</sup> |
|
|
589
|
+
| `US_ARCH_A` ... `US_ARCH_E` | 2d vector of `inch` | Paper sizes<sup>(1)</sup> |
|
|
590
|
+
| `US_LETTER` / `US_HALF_LETTER` | 2d vector of `inch` | Paper sizes<sup>(1)</sup> |
|
|
591
|
+
| `US_LEGAL` / `US_JUNIOR_LEGAL` | 2d vector of `inch` | Paper sizes<sup>(1)</sup> |
|
|
588
592
|
|
|
589
593
|
- <sup>(1)</sup> - all paper sizes are also available as landscape presets
|
|
590
594
|
(using `_LANDSCAPE` as suffix).
|
|
@@ -646,7 +650,7 @@ For Node.js REPL:
|
|
|
646
650
|
const units = await import("@thi.ng/units");
|
|
647
651
|
```
|
|
648
652
|
|
|
649
|
-
Package sizes (brotli'd, pre-treeshake): ESM: 4.
|
|
653
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 4.78 KB
|
|
650
654
|
|
|
651
655
|
## Dependencies
|
|
652
656
|
|
|
@@ -51,4 +51,8 @@ export declare const US_ARCH_E_LANDSCAPE: Quantity<number[]>;
|
|
|
51
51
|
export declare const US_ARCH_E1_LANDSCAPE: Quantity<number[]>;
|
|
52
52
|
export declare const US_ARCH_E2_LANDSCAPE: Quantity<number[]>;
|
|
53
53
|
export declare const US_ARCH_E3_LANDSCAPE: Quantity<number[]>;
|
|
54
|
+
export declare const DPI_72: Quantity<72>;
|
|
55
|
+
export declare const DPI_150: Quantity<150>;
|
|
56
|
+
export declare const DPI_300: Quantity<300>;
|
|
57
|
+
export declare const DPI_600: Quantity<600>;
|
|
54
58
|
//# sourceMappingURL=paper-sizes.d.ts.map
|
package/constants/paper-sizes.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Quantity, quantity } from "../unit.js";
|
|
2
|
+
import { dpi } from "../units/density.js";
|
|
2
3
|
import { inch, mm } from "../units/length.js";
|
|
3
4
|
const DIN_A0 = quantity([841, 1189], mm);
|
|
4
5
|
const DIN_A1 = quantity([594, 841], mm);
|
|
@@ -53,6 +54,10 @@ const US_ARCH_E_LANDSCAPE = $(US_ARCH_E);
|
|
|
53
54
|
const US_ARCH_E1_LANDSCAPE = $(US_ARCH_E1);
|
|
54
55
|
const US_ARCH_E2_LANDSCAPE = $(US_ARCH_E2);
|
|
55
56
|
const US_ARCH_E3_LANDSCAPE = $(US_ARCH_E3);
|
|
57
|
+
const DPI_72 = quantity(72, dpi);
|
|
58
|
+
const DPI_150 = quantity(150, dpi);
|
|
59
|
+
const DPI_300 = quantity(300, dpi);
|
|
60
|
+
const DPI_600 = quantity(600, dpi);
|
|
56
61
|
export {
|
|
57
62
|
ANSI_A,
|
|
58
63
|
ANSI_A_LANDSCAPE,
|
|
@@ -82,6 +87,10 @@ export {
|
|
|
82
87
|
DIN_A7_LANDSCAPE,
|
|
83
88
|
DIN_A8,
|
|
84
89
|
DIN_A8_LANDSCAPE,
|
|
90
|
+
DPI_150,
|
|
91
|
+
DPI_300,
|
|
92
|
+
DPI_600,
|
|
93
|
+
DPI_72,
|
|
85
94
|
US_ARCH_A,
|
|
86
95
|
US_ARCH_A_LANDSCAPE,
|
|
87
96
|
US_ARCH_B,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/units",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Extensible SI unit creation, conversions, quantities & calculations (incl. ~170 predefined units & constants)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "https://github.com/thi-ng/umbrella.git"
|
|
12
12
|
},
|
|
13
|
-
"homepage": "https://
|
|
13
|
+
"homepage": "https://thi.ng/units",
|
|
14
14
|
"funding": [
|
|
15
15
|
{
|
|
16
16
|
"type": "github",
|
|
@@ -36,16 +36,16 @@
|
|
|
36
36
|
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@thi.ng/api": "^8.11.
|
|
40
|
-
"@thi.ng/checks": "^3.6.
|
|
41
|
-
"@thi.ng/equiv": "^2.1.
|
|
42
|
-
"@thi.ng/errors": "^2.5.
|
|
39
|
+
"@thi.ng/api": "^8.11.3",
|
|
40
|
+
"@thi.ng/checks": "^3.6.5",
|
|
41
|
+
"@thi.ng/equiv": "^2.1.59",
|
|
42
|
+
"@thi.ng/errors": "^2.5.8"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@microsoft/api-extractor": "^7.
|
|
46
|
-
"esbuild": "^0.
|
|
47
|
-
"typedoc": "^0.25.
|
|
48
|
-
"typescript": "^5.
|
|
45
|
+
"@microsoft/api-extractor": "^7.47.0",
|
|
46
|
+
"esbuild": "^0.21.5",
|
|
47
|
+
"typedoc": "^0.25.13",
|
|
48
|
+
"typescript": "^5.5.2"
|
|
49
49
|
},
|
|
50
50
|
"keywords": [
|
|
51
51
|
"acceleration",
|
|
@@ -179,5 +179,5 @@
|
|
|
179
179
|
"status": "beta",
|
|
180
180
|
"year": 2021
|
|
181
181
|
},
|
|
182
|
-
"gitHead": "
|
|
182
|
+
"gitHead": "154c95cf9d6bab32174498ec3b5b5d87e42be7f9\n"
|
|
183
183
|
}
|
package/unit.d.ts
CHANGED
|
@@ -94,29 +94,37 @@ export declare class Quantity<T extends number | number[]> implements IDeref<T>
|
|
|
94
94
|
* dimensionless quantities). Use {@link convert} otherwise!
|
|
95
95
|
*
|
|
96
96
|
* @example
|
|
97
|
-
* ```ts
|
|
98
|
-
* import { convert, div, quantity, NONE } from "@thi.ng/units";
|
|
97
|
+
* ```ts tangle:../export/quantity.ts
|
|
98
|
+
* import { convert, div, mul, quantity, NONE } from "@thi.ng/units";
|
|
99
99
|
*
|
|
100
100
|
* const speedOfLight = quantity(299792458, "m/s");
|
|
101
101
|
*
|
|
102
102
|
* // compute wavelength of a WiFi signal in millimeters
|
|
103
|
-
*
|
|
103
|
+
* console.log(
|
|
104
|
+
* convert(div(speedOfLight, quantity(2.4,"GHz")), "mm")
|
|
105
|
+
* );
|
|
104
106
|
* // 124.9135
|
|
105
107
|
*
|
|
106
108
|
* // DIN A4 paper size
|
|
107
109
|
* const A4 = quantity([210, 297], "mm");
|
|
108
110
|
*
|
|
109
111
|
* // convert paper size to inches
|
|
110
|
-
*
|
|
112
|
+
* console.log(
|
|
113
|
+
* convert(A4, "in")
|
|
114
|
+
* );
|
|
111
115
|
* // [ 8.2677, 11.6929 ]
|
|
112
116
|
*
|
|
113
117
|
* // or calculate pixel dimensions @ 300 dpi
|
|
114
118
|
* // the result of the product is dimensionless so we use NONE as target unit
|
|
115
|
-
*
|
|
119
|
+
* console.log(
|
|
120
|
+
* convert(mul(A4, quantity(300, "dpi")), NONE)
|
|
121
|
+
* );
|
|
116
122
|
* // [ 2480.314960629921, 3507.8740157480315 ]
|
|
117
123
|
*
|
|
118
124
|
* // alternatively dimensionless units can be deref'd directly
|
|
119
|
-
*
|
|
125
|
+
* console.log(
|
|
126
|
+
* mul(A4, quantity(300, "dpi")).deref()
|
|
127
|
+
* );
|
|
120
128
|
* // [ 2480.314960629921, 3507.8740157480315 ]
|
|
121
129
|
* ```
|
|
122
130
|
*
|
package/unit.js
CHANGED
|
@@ -18,8 +18,7 @@ const unit = (dim, scale, offset = 0, coherent2 = false) => ({
|
|
|
18
18
|
const coherent = (dim) => unit(dim, 1, 0, true);
|
|
19
19
|
const dimensionless = (scale, offset = 0, coherent2 = false) => unit(NONE.dim, scale, offset, coherent2);
|
|
20
20
|
const defUnit = (sym, name, unit2, force = false) => {
|
|
21
|
-
if (UNITS[sym] && !force)
|
|
22
|
-
illegalArgs(`attempt to override unit: ${sym}`);
|
|
21
|
+
if (UNITS[sym] && !force) illegalArgs(`attempt to override unit: ${sym}`);
|
|
23
22
|
return UNITS[sym] = { ...unit2, sym, name };
|
|
24
23
|
};
|
|
25
24
|
const asUnit = (id) => {
|
|
@@ -31,8 +30,7 @@ const asUnit = (id) => {
|
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
32
|
for (let u in UNITS) {
|
|
34
|
-
if (UNITS[u].name === id)
|
|
35
|
-
return UNITS[u];
|
|
33
|
+
if (UNITS[u].name === id) return UNITS[u];
|
|
36
34
|
}
|
|
37
35
|
illegalArgs(`unknown unit: ${id}`);
|
|
38
36
|
};
|
|
@@ -52,11 +50,9 @@ const quantity = (value, unit2) => new Quantity(
|
|
|
52
50
|
isNumber(value) ? mul(unit2, value) : value.map((x) => mul(unit2, x))
|
|
53
51
|
);
|
|
54
52
|
function mul(a, b, coherent2 = false) {
|
|
55
|
-
if (a instanceof Quantity)
|
|
56
|
-
return __combineQ(mul, a, b);
|
|
53
|
+
if (a instanceof Quantity) return __combineQ(mul, a, b);
|
|
57
54
|
const $a = __ensureUnit(a);
|
|
58
|
-
if (isNumber(b))
|
|
59
|
-
return unit($a.dim, $a.scale * b, $a.offset, coherent2);
|
|
55
|
+
if (isNumber(b)) return unit($a.dim, $a.scale * b, $a.offset, coherent2);
|
|
60
56
|
const $b = __ensureUnit(b);
|
|
61
57
|
return unit(
|
|
62
58
|
$a.dim.map((x, i) => x + $b.dim[i]),
|
|
@@ -66,8 +62,7 @@ function mul(a, b, coherent2 = false) {
|
|
|
66
62
|
);
|
|
67
63
|
}
|
|
68
64
|
function div(a, b, coherent2 = false) {
|
|
69
|
-
if (a instanceof Quantity)
|
|
70
|
-
return __combineQ(div, a, b);
|
|
65
|
+
if (a instanceof Quantity) return __combineQ(div, a, b);
|
|
71
66
|
const $a = __ensureUnit(a);
|
|
72
67
|
if (isNumber(b)) {
|
|
73
68
|
return unit($a.dim, $a.scale / b, $a.offset, coherent2);
|
|
@@ -107,8 +102,7 @@ function convert(x, a, b) {
|
|
|
107
102
|
return (xnorm - $dest.offset) / $dest.scale;
|
|
108
103
|
}
|
|
109
104
|
const isConvertible = (src, dest) => {
|
|
110
|
-
if (src instanceof Quantity)
|
|
111
|
-
return isConvertible(__qunit(src), dest);
|
|
105
|
+
if (src instanceof Quantity) return isConvertible(__qunit(src), dest);
|
|
112
106
|
const $src = __ensureUnit(src);
|
|
113
107
|
const $dest = __ensureUnit(dest);
|
|
114
108
|
return isReciprocal($src, $dest) || equivArrayLike($src.dim, $dest.dim);
|
|
@@ -121,24 +115,20 @@ const isReciprocal = (a, b) => {
|
|
|
121
115
|
for (let i = 0; i < 7; i++) {
|
|
122
116
|
const xa = $a[i];
|
|
123
117
|
const xb = $b[i];
|
|
124
|
-
if (xa === 0 && xb === 0)
|
|
125
|
-
|
|
126
|
-
if (xa !== -xb)
|
|
127
|
-
return false;
|
|
118
|
+
if (xa === 0 && xb === 0) continue;
|
|
119
|
+
if (xa !== -xb) return false;
|
|
128
120
|
ok = true;
|
|
129
121
|
}
|
|
130
122
|
return ok;
|
|
131
123
|
};
|
|
132
124
|
const formatSI = (u) => {
|
|
133
|
-
if (u instanceof Quantity)
|
|
134
|
-
return formatSI(__qunit(u));
|
|
125
|
+
if (u instanceof Quantity) return formatSI(__qunit(u));
|
|
135
126
|
const { dim } = __ensureUnit(u);
|
|
136
127
|
const SI = ["kg", "m", "s", "A", "K", "mol", "cd"];
|
|
137
128
|
const acc = [];
|
|
138
129
|
for (let i = 0; i < 7; i++) {
|
|
139
130
|
const x = dim[i];
|
|
140
|
-
if (x !== 0)
|
|
141
|
-
acc.push(SI[i] + (x !== 1 ? x : ""));
|
|
131
|
+
if (x !== 0) acc.push(SI[i] + (x !== 1 ? x : ""));
|
|
142
132
|
}
|
|
143
133
|
return acc.length ? acc.join("\xB7") : "<dimensionless>";
|
|
144
134
|
};
|