@thi.ng/units 0.4.34 → 0.4.36
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 +1 -1
- package/README.md +20 -0
- package/package.json +8 -7
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -93,6 +93,8 @@ most dimensions' base units usually using a factor of 1 and no offset.
|
|
|
93
93
|
For example, here's how we can define kilograms and meters:
|
|
94
94
|
|
|
95
95
|
```ts
|
|
96
|
+
import { coherent, unit } from "@thi.ng/units";
|
|
97
|
+
|
|
96
98
|
// kilogram, SI dimension 0
|
|
97
99
|
const KG = coherent(0);
|
|
98
100
|
// { dim: [ 1, 0, 0, 0, 0, 0, 0 ], scale: 1, offset: 0, coherent: true }
|
|
@@ -114,6 +116,8 @@ More complex units like electrical resistance (e.g. ohm) are based on more than
|
|
|
114
116
|
a single dimension:
|
|
115
117
|
|
|
116
118
|
```ts
|
|
119
|
+
import { div, A, V } from "@thi.ng/units";
|
|
120
|
+
|
|
117
121
|
// ohm = volt / ampere
|
|
118
122
|
div(V, A)
|
|
119
123
|
// { dim: [ 1, 2, -3, -2, 0, 0, 0 ], scale: 1, offset: 0, coherent: true }
|
|
@@ -129,6 +133,8 @@ Btw. The
|
|
|
129
133
|
function can be used to format a unit's dimension vector:
|
|
130
134
|
|
|
131
135
|
```ts
|
|
136
|
+
import { div, formatSI, A, V } from "@thi.ng/units";
|
|
137
|
+
|
|
132
138
|
formatSI(div(V, A));
|
|
133
139
|
// "kg·m2·s-3·A-2"
|
|
134
140
|
```
|
|
@@ -411,6 +417,8 @@ Existing coherent units can be
|
|
|
411
417
|
produce derived versions:
|
|
412
418
|
|
|
413
419
|
```ts
|
|
420
|
+
import { prefix, Hz } from "@thi.ng/units";
|
|
421
|
+
|
|
414
422
|
// define micrometer (also available as preset)
|
|
415
423
|
prefix("µ", "m")
|
|
416
424
|
// { dim: [ 0, 1, 0, 0, 0, 0, 0 ], scale: 0.000001, offset: 0, coherent: false }
|
|
@@ -435,6 +443,8 @@ The following combinators can be used to derive scaled and/or more complex units
|
|
|
435
443
|
Creates reciprocal of given unit (e.g. Hz ⇒ 1/second)
|
|
436
444
|
|
|
437
445
|
```ts
|
|
446
|
+
import { div, mul, pow, prefix, reciprocal, bit, m, s } from "@thi.ng/units";
|
|
447
|
+
|
|
438
448
|
// acceleration (meter per second squared)
|
|
439
449
|
const m_s2 = div(m, pow(s, 2));
|
|
440
450
|
// { dim: [ 0, 1, -2, 0, 0, 0, 0 ], scale: 1, offset: 0, coherent: false }
|
|
@@ -464,6 +474,8 @@ nonsense).
|
|
|
464
474
|
Units can be specified in various ways:
|
|
465
475
|
|
|
466
476
|
```ts
|
|
477
|
+
import { convert, div, reciprocal, h, km_h, mph, yd } from "@thi.ng/units";
|
|
478
|
+
|
|
467
479
|
// convert from km/h to mph using unit names
|
|
468
480
|
convert(100, "km/h", "mph");
|
|
469
481
|
// 62.13711922373341
|
|
@@ -485,6 +497,8 @@ Another example using dimensionless units (here angles, arc second ⇒ radian) t
|
|
|
485
497
|
compute the distance of 10 arcsec on the earth surface (in meters):
|
|
486
498
|
|
|
487
499
|
```ts
|
|
500
|
+
import { convert, R } from "@thi.ng/units";
|
|
501
|
+
|
|
488
502
|
// earth radius in meters
|
|
489
503
|
// (also available as quantity EARTH_RADIUS, see section below)
|
|
490
504
|
const R = 6371000;
|
|
@@ -509,6 +523,8 @@ Quantities are created via
|
|
|
509
523
|
quantities). Use [`convert()`](#unit-conversions) otherwise!
|
|
510
524
|
|
|
511
525
|
```ts
|
|
526
|
+
import { convert, div, quantity } from "@thi.ng/units";
|
|
527
|
+
|
|
512
528
|
// (also available as preset)
|
|
513
529
|
const speedOfLight = quantity(299792458, "m/s");
|
|
514
530
|
|
|
@@ -520,6 +536,8 @@ convert(div(speedOfLight, quantity(2.4,"GHz")), "mm");
|
|
|
520
536
|
Some examples using vector quantities:
|
|
521
537
|
|
|
522
538
|
```ts
|
|
539
|
+
import { convert, mul, quantity, NONE } from "@thi.ng/units";
|
|
540
|
+
|
|
523
541
|
// DIN A4 paper size (also available as preset)
|
|
524
542
|
const A4 = quantity([210, 297], "mm");
|
|
525
543
|
|
|
@@ -541,6 +559,8 @@ mul(A4, quantity(300, "dpi")).deref()
|
|
|
541
559
|
When combining different quantities, their units do not need to be the same:
|
|
542
560
|
|
|
543
561
|
```ts
|
|
562
|
+
import { convert, mul, quantity } from "@thi.ng/units";
|
|
563
|
+
|
|
544
564
|
// compute 10 mm x 2 inch and convert to square centimeter
|
|
545
565
|
convert(mul(quantity(10, "mm"), quantity(2, "in")), "cm2")
|
|
546
566
|
// 5.08
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/units",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.36",
|
|
4
4
|
"description": "Extensible SI unit creation, conversions, quantities & calculations (incl. ~170 predefined units & constants)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -32,13 +32,14 @@
|
|
|
32
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
33
33
|
"doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
|
|
34
34
|
"pub": "yarn npm publish --access public",
|
|
35
|
-
"test": "bun test"
|
|
35
|
+
"test": "bun test",
|
|
36
|
+
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
|
-
"@thi.ng/api": "^8.9.
|
|
39
|
-
"@thi.ng/checks": "^3.5.
|
|
40
|
-
"@thi.ng/equiv": "^2.1.
|
|
41
|
-
"@thi.ng/errors": "^2.4.
|
|
39
|
+
"@thi.ng/api": "^8.9.28",
|
|
40
|
+
"@thi.ng/checks": "^3.5.2",
|
|
41
|
+
"@thi.ng/equiv": "^2.1.51",
|
|
42
|
+
"@thi.ng/errors": "^2.4.20"
|
|
42
43
|
},
|
|
43
44
|
"devDependencies": {
|
|
44
45
|
"@microsoft/api-extractor": "^7.40.1",
|
|
@@ -179,5 +180,5 @@
|
|
|
179
180
|
"status": "beta",
|
|
180
181
|
"year": 2021
|
|
181
182
|
},
|
|
182
|
-
"gitHead": "
|
|
183
|
+
"gitHead": "a421058a65ba76608d94129ac29451bfedaf201c\n"
|
|
183
184
|
}
|