@mark1russell7/vex-domains 0.19.0 → 0.19.1
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 +18 -0
- package/build/quantity.d.ts +53 -0
- package/build/quantity.d.ts.map +1 -0
- package/build/quantity.js +177 -0
- package/build/quantity.js.map +1 -0
- package/package.json +9 -3
- package/src/quantity.ts +124 -0
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ npm install @mark1russell7/vex @mark1russell7/vex-domains
|
|
|
24
24
|
| `JsonDomain` | JSON data: texts, numbers, booleans, arrays and plain objects. Put it last. | `kind`, `kinds`, `fields`, `field`, `leaves` |
|
|
25
25
|
| `ColorDomain` | `Color`: an RGB color | `mix`, `add`, `multiply`, `luminance`, `relativeLuminance`, `contrast` |
|
|
26
26
|
| `AngleDomain` | `Angle`: an angle | `add`, `normalize`, `sin`, `cos`, `degrees` |
|
|
27
|
+
| `QuantityDomain` | `{ value, unit }`: an amount in a unit of `@mark1russell7/quantity`. Import it from `@mark1russell7/vex-domains/quantity` | `add`, `lte`, `max`, `ratio`, `convert`, `valueIn` |
|
|
27
28
|
| `FnDomain`, `MaybeDomain` | Two example domains: function wrappers and `Maybe` | `invoke`, `map`, `chain`, `valueOr` |
|
|
28
29
|
|
|
29
30
|
## Example
|
|
@@ -51,6 +52,23 @@ const cell = items.start(0).each((t) => t.from("w")._.vec2(t.from("h"))).reduce(
|
|
|
51
52
|
cell.at("0"); // => { tag: "some", value: new Vec2(30, 20) }
|
|
52
53
|
```
|
|
53
54
|
|
|
55
|
+
## Amounts in units
|
|
56
|
+
|
|
57
|
+
`QuantityDomain` of the entry `@mark1russell7/vex-domains/quantity` reads amounts in units, for example `{ value: 155, unit: "mm" }`. The package `@mark1russell7/quantity` converts each amount exactly. Thus a program compares 155 mm with 16 cm, and a sum of 0.1 m and 0.2 m is 0.3 m. Two amounts of different kinds give `#CALC!` with the reason.
|
|
58
|
+
|
|
59
|
+
```ts doctest
|
|
60
|
+
import { space, vex } from "@mark1russell7/vex";
|
|
61
|
+
import { NumDomain } from "@mark1russell7/vex-domains";
|
|
62
|
+
import { QuantityDomain } from "@mark1russell7/vex-domains/quantity";
|
|
63
|
+
|
|
64
|
+
const cases = space.record({ tower: { cooler: { value: 155, unit: "mm" }, room: { value: 16, unit: "cm" } } });
|
|
65
|
+
const fits = vex(NumDomain, QuantityDomain).over(cases).from("cooler")._.lte("room");
|
|
66
|
+
|
|
67
|
+
fits.at("tower"); // => { tag: "some", value: true }
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The entry is apart from the main entry, so a bundle without amounts does not get the unit tables.
|
|
71
|
+
|
|
54
72
|
## The schema of JSON documents
|
|
55
73
|
|
|
56
74
|
`SCHEMA_PROGRAM` is a Vex program that finds the schema of a list of JSON documents. It reads the documents from the name `docs`, and `SCHEMA_DOMAINS` gives its domains. It gives one `JsonFieldSchema` record for each field path, in the order that the paths first occur. A record has the path, the distinct kinds and the count of the documents with the path.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The domain of amounts in units: the entry `@mark1russell7/vex-domains/quantity`. A value is a quantity of
|
|
3
|
+
* `@mark1russell7/quantity`, as plain data: `{ value: 155, unit: "mm" }`. That package does the unit arithmetic. It
|
|
4
|
+
* converts each amount exactly, so 155 mm and 16 cm compare without a rounding of doubles.
|
|
5
|
+
*/
|
|
6
|
+
import { Domain, FnOp } from "@mark1russell7/vex";
|
|
7
|
+
import { Quantity, Quantity as Quantity$1 } from "@mark1russell7/quantity";
|
|
8
|
+
type QFn2 = (a: Quantity$1, b: Quantity$1) => Quantity$1;
|
|
9
|
+
type QTest = (a: Quantity$1, b: Quantity$1) => boolean;
|
|
10
|
+
/** The ops of `QuantityDomain`. */
|
|
11
|
+
export type QuantityOps = {
|
|
12
|
+
readonly value: FnOp<Quantity$1, (a: Quantity$1) => number>;
|
|
13
|
+
readonly unit: FnOp<Quantity$1, (a: Quantity$1) => string>;
|
|
14
|
+
readonly kind: FnOp<Quantity$1, (a: Quantity$1) => string>;
|
|
15
|
+
readonly convert: FnOp<Quantity$1, (a: Quantity$1, unit: string) => Quantity$1>;
|
|
16
|
+
readonly valueIn: FnOp<Quantity$1, (a: Quantity$1, unit: string) => number>;
|
|
17
|
+
readonly add: FnOp<Quantity$1, QFn2>;
|
|
18
|
+
readonly subtract: FnOp<Quantity$1, QFn2>;
|
|
19
|
+
readonly multiply: FnOp<Quantity$1, (a: Quantity$1, k: number) => Quantity$1>;
|
|
20
|
+
readonly divide: FnOp<Quantity$1, (a: Quantity$1, k: number) => Quantity$1>;
|
|
21
|
+
readonly ratio: FnOp<Quantity$1, (a: Quantity$1, b: Quantity$1) => number>;
|
|
22
|
+
readonly negate: FnOp<Quantity$1, (a: Quantity$1) => Quantity$1>;
|
|
23
|
+
readonly abs: FnOp<Quantity$1, (a: Quantity$1) => Quantity$1>;
|
|
24
|
+
readonly min: FnOp<Quantity$1, QFn2>;
|
|
25
|
+
readonly max: FnOp<Quantity$1, QFn2>;
|
|
26
|
+
readonly compare: FnOp<Quantity$1, (a: Quantity$1, b: Quantity$1) => number>;
|
|
27
|
+
readonly lt: FnOp<Quantity$1, QTest>;
|
|
28
|
+
readonly lte: FnOp<Quantity$1, QTest>;
|
|
29
|
+
readonly gt: FnOp<Quantity$1, QTest>;
|
|
30
|
+
readonly gte: FnOp<Quantity$1, QTest>;
|
|
31
|
+
readonly eq: FnOp<Quantity$1, QTest>;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* This function tells if a value is a quantity of the domain. A quantity is a plain object with only the fields `value`
|
|
35
|
+
* and `unit`. Its value is a number, and its unit is canonical. The check `valid` of the domain refuses a number that
|
|
36
|
+
* is not finite.
|
|
37
|
+
*/
|
|
38
|
+
export declare const isQuantityValue: (u: unknown) => u is Quantity$1;
|
|
39
|
+
/**
|
|
40
|
+
* The domain of amounts in units. Each op does its unit arithmetic with `@mark1russell7/quantity`.
|
|
41
|
+
*
|
|
42
|
+
* - `add`, `subtract`, `min` and `max` give the result in the unit of the receiver. They convert the argument exactly.
|
|
43
|
+
* - The comparisons convert exactly too. Thus `lte` of 155 mm and 16 cm is `true`.
|
|
44
|
+
* - Two amounts of different kinds, for example a length and a mass, give an error value with the reason.
|
|
45
|
+
* - `multiply` and `divide` take a number. `ratio` divides two amounts of one kind, and it gives a number.
|
|
46
|
+
* - No op lifts a number: a number has no unit.
|
|
47
|
+
* - A value that is not finite is not valid, so an op that gives one gives `#NUM!`.
|
|
48
|
+
*
|
|
49
|
+
* Put `QuantityDomain` before `NumRecordDomain` and `JsonDomain`, because they accept the same objects.
|
|
50
|
+
*/
|
|
51
|
+
export declare const QuantityDomain: Domain<"Quantity", Quantity$1, QuantityOps>;
|
|
52
|
+
export type { Quantity };
|
|
53
|
+
//# sourceMappingURL=quantity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quantity.d.ts","names":["Quantity"],"sources":["../src/quantity.ts"],"mappings":";;;;;;;KAyBK,QAAQ,GAAGA,YAAU,GAAGA,eAAaA;KACrC,SAAS,GAAGA,YAAU,GAAGA;;YAGlB;WACD,OAAO,KAAKA,aAAW,GAAGA;WAC1B,MAAM,KAAKA,aAAW,GAAGA;WACzB,MAAM,KAAKA,aAAW,GAAGA;WACzB,SAAS,KAAKA,aAAW,GAAGA,YAAU,iBAAiBA;WACvD,SAAS,KAAKA,aAAW,GAAGA,YAAU;WACtC,KAAK,KAAKA,YAAU;WACpB,UAAU,KAAKA,YAAU;WACzB,UAAU,KAAKA,aAAW,GAAGA,YAAU,cAAcA;WACrD,QAAQ,KAAKA,aAAW,GAAGA,YAAU,cAAcA;WACnD,OAAO,KAAKA,aAAW,GAAGA,YAAU,GAAGA;WACvC,QAAQ,KAAKA,aAAW,GAAGA,eAAaA;WACxC,KAAK,KAAKA,aAAW,GAAGA,eAAaA;WACrC,KAAK,KAAKA,YAAU;WACpB,KAAK,KAAKA,YAAU;WACpB,SAAS,KAAKA,aAAW,GAAGA,YAAU,GAAGA;WACzC,IAAI,KAAKA,YAAU;WACnB,KAAK,KAAKA,YAAU;WACpB,IAAI,KAAKA,YAAU;WACnB,KAAK,KAAKA,YAAU;WACpB,IAAI,KAAKA,YAAU;;;;;;;qBAYjB,kBAAmB,eAAa,KAAKA;;;;;;;;;;;;;qBAmBrC,gBAAgB,mBAAmBA,YAAU"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { defineDomain } from "@mark1russell7/vex";
|
|
2
|
+
import { add, canonicalUnit, compare, convert, convertExact, isCanonicalUnit, kindOfQuantity, max, min, rationals, scale, sub, valueIn } from "@mark1russell7/quantity";
|
|
3
|
+
// src/quantity.ts
|
|
4
|
+
/**
|
|
5
|
+
* The domain of amounts in units: the entry `@mark1russell7/vex-domains/quantity`. A value is a quantity of
|
|
6
|
+
* `@mark1russell7/quantity`, as plain data: `{ value: 155, unit: "mm" }`. That package does the unit arithmetic. It
|
|
7
|
+
* converts each amount exactly, so 155 mm and 16 cm compare without a rounding of doubles.
|
|
8
|
+
*/
|
|
9
|
+
/** This function tells if a value is a plain object: its prototype is `Object.prototype` or `null`. */
|
|
10
|
+
const plain = (u) => typeof u === "object" && u !== null && [Object.prototype, null].includes(Object.getPrototypeOf(u));
|
|
11
|
+
/**
|
|
12
|
+
* This function tells if a value is a quantity of the domain. A quantity is a plain object with only the fields `value`
|
|
13
|
+
* and `unit`. Its value is a number, and its unit is canonical. The check `valid` of the domain refuses a number that
|
|
14
|
+
* is not finite.
|
|
15
|
+
*/
|
|
16
|
+
const isQuantityValue = (u) => plain(u) && Object.keys(u).length === 2 && typeof u.value === "number" && typeof u.unit === "string" && isCanonicalUnit(u.unit);
|
|
17
|
+
/** This function gives the ratio a / b of two amounts of one kind, exactly. Each amount is in its own unit. */
|
|
18
|
+
const ratioOf = (a, b) => rationals.toNumber(rationals.div(convertExact(rationals.fromNumber(a.value), a.unit, b.unit), rationals.fromNumber(b.value)));
|
|
19
|
+
/**
|
|
20
|
+
* The domain of amounts in units. Each op does its unit arithmetic with `@mark1russell7/quantity`.
|
|
21
|
+
*
|
|
22
|
+
* - `add`, `subtract`, `min` and `max` give the result in the unit of the receiver. They convert the argument exactly.
|
|
23
|
+
* - The comparisons convert exactly too. Thus `lte` of 155 mm and 16 cm is `true`.
|
|
24
|
+
* - Two amounts of different kinds, for example a length and a mass, give an error value with the reason.
|
|
25
|
+
* - `multiply` and `divide` take a number. `ratio` divides two amounts of one kind, and it gives a number.
|
|
26
|
+
* - No op lifts a number: a number has no unit.
|
|
27
|
+
* - A value that is not finite is not valid, so an op that gives one gives `#NUM!`.
|
|
28
|
+
*
|
|
29
|
+
* Put `QuantityDomain` before `NumRecordDomain` and `JsonDomain`, because they accept the same objects.
|
|
30
|
+
*/
|
|
31
|
+
const QuantityDomain = defineDomain({
|
|
32
|
+
name: "Quantity",
|
|
33
|
+
size: "fixed",
|
|
34
|
+
fields: ["value", "unit"],
|
|
35
|
+
is: isQuantityValue,
|
|
36
|
+
valid: (a) => Number.isFinite(a.value) && isCanonicalUnit(a.unit),
|
|
37
|
+
show: (a) => `${String(a.value)} ${a.unit}`,
|
|
38
|
+
encode: (a) => ({
|
|
39
|
+
value: a.value,
|
|
40
|
+
unit: a.unit
|
|
41
|
+
}),
|
|
42
|
+
decode: (u) => {
|
|
43
|
+
if (!plain(u) || typeof u.value !== "number" || typeof u.unit !== "string") throw new TypeError("a quantity is { value: number, unit: string }");
|
|
44
|
+
return {
|
|
45
|
+
value: u.value,
|
|
46
|
+
unit: canonicalUnit(u.unit)
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
samples: () => [
|
|
50
|
+
{
|
|
51
|
+
value: 155,
|
|
52
|
+
unit: "mm"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
value: 16,
|
|
56
|
+
unit: "cm"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
value: 2.5,
|
|
60
|
+
unit: "kg"
|
|
61
|
+
}
|
|
62
|
+
],
|
|
63
|
+
ops: {
|
|
64
|
+
value: {
|
|
65
|
+
fn: (a) => a.value,
|
|
66
|
+
params: [],
|
|
67
|
+
returns: "number"
|
|
68
|
+
},
|
|
69
|
+
unit: {
|
|
70
|
+
fn: (a) => a.unit,
|
|
71
|
+
params: [],
|
|
72
|
+
returns: "string"
|
|
73
|
+
},
|
|
74
|
+
kind: {
|
|
75
|
+
fn: (a) => kindOfQuantity(a),
|
|
76
|
+
params: [],
|
|
77
|
+
returns: "string"
|
|
78
|
+
},
|
|
79
|
+
convert: {
|
|
80
|
+
fn: (a, unit) => convert(a, unit),
|
|
81
|
+
params: ["string"],
|
|
82
|
+
returns: "domain"
|
|
83
|
+
},
|
|
84
|
+
valueIn: {
|
|
85
|
+
fn: (a, unit) => valueIn(a, unit),
|
|
86
|
+
params: ["string"],
|
|
87
|
+
returns: "number"
|
|
88
|
+
},
|
|
89
|
+
add: {
|
|
90
|
+
fn: (a, b) => add(a, b),
|
|
91
|
+
params: ["domain"],
|
|
92
|
+
returns: "domain"
|
|
93
|
+
},
|
|
94
|
+
subtract: {
|
|
95
|
+
fn: (a, b) => sub(a, b),
|
|
96
|
+
params: ["domain"],
|
|
97
|
+
returns: "domain"
|
|
98
|
+
},
|
|
99
|
+
multiply: {
|
|
100
|
+
fn: (a, k) => scale(a, k),
|
|
101
|
+
params: ["number"],
|
|
102
|
+
returns: "domain"
|
|
103
|
+
},
|
|
104
|
+
divide: {
|
|
105
|
+
fn: (a, k) => ({
|
|
106
|
+
value: a.value / k,
|
|
107
|
+
unit: a.unit
|
|
108
|
+
}),
|
|
109
|
+
params: ["number"],
|
|
110
|
+
returns: "domain"
|
|
111
|
+
},
|
|
112
|
+
ratio: {
|
|
113
|
+
fn: (a, b) => b.value === 0 ? NaN : ratioOf(a, b),
|
|
114
|
+
params: ["domain"],
|
|
115
|
+
returns: "number"
|
|
116
|
+
},
|
|
117
|
+
negate: {
|
|
118
|
+
fn: (a) => ({
|
|
119
|
+
value: -a.value,
|
|
120
|
+
unit: a.unit
|
|
121
|
+
}),
|
|
122
|
+
params: [],
|
|
123
|
+
returns: "domain"
|
|
124
|
+
},
|
|
125
|
+
abs: {
|
|
126
|
+
fn: (a) => ({
|
|
127
|
+
value: Math.abs(a.value),
|
|
128
|
+
unit: a.unit
|
|
129
|
+
}),
|
|
130
|
+
params: [],
|
|
131
|
+
returns: "domain"
|
|
132
|
+
},
|
|
133
|
+
min: {
|
|
134
|
+
fn: (a, b) => convert(min(a, b), a.unit),
|
|
135
|
+
params: ["domain"],
|
|
136
|
+
returns: "domain"
|
|
137
|
+
},
|
|
138
|
+
max: {
|
|
139
|
+
fn: (a, b) => convert(max(a, b), a.unit),
|
|
140
|
+
params: ["domain"],
|
|
141
|
+
returns: "domain"
|
|
142
|
+
},
|
|
143
|
+
compare: {
|
|
144
|
+
fn: (a, b) => compare(a, b),
|
|
145
|
+
params: ["domain"],
|
|
146
|
+
returns: "number"
|
|
147
|
+
},
|
|
148
|
+
lt: {
|
|
149
|
+
fn: (a, b) => compare(a, b) < 0,
|
|
150
|
+
params: ["domain"],
|
|
151
|
+
returns: "boolean"
|
|
152
|
+
},
|
|
153
|
+
lte: {
|
|
154
|
+
fn: (a, b) => compare(a, b) <= 0,
|
|
155
|
+
params: ["domain"],
|
|
156
|
+
returns: "boolean"
|
|
157
|
+
},
|
|
158
|
+
gt: {
|
|
159
|
+
fn: (a, b) => compare(a, b) > 0,
|
|
160
|
+
params: ["domain"],
|
|
161
|
+
returns: "boolean"
|
|
162
|
+
},
|
|
163
|
+
gte: {
|
|
164
|
+
fn: (a, b) => compare(a, b) >= 0,
|
|
165
|
+
params: ["domain"],
|
|
166
|
+
returns: "boolean"
|
|
167
|
+
},
|
|
168
|
+
eq: {
|
|
169
|
+
fn: (a, b) => compare(a, b) === 0,
|
|
170
|
+
params: ["domain"],
|
|
171
|
+
returns: "boolean"
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
export { QuantityDomain, isQuantityValue };
|
|
176
|
+
|
|
177
|
+
//# sourceMappingURL=quantity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quantity.js","names":[],"sources":["../src/quantity.ts"],"sourcesContent":["/**\n * The domain of amounts in units: the entry `@mark1russell7/vex-domains/quantity`. A value is a quantity of\n * `@mark1russell7/quantity`, as plain data: `{ value: 155, unit: \"mm\" }`. That package does the unit arithmetic. It\n * converts each amount exactly, so 155 mm and 16 cm compare without a rounding of doubles.\n */\nimport { defineDomain, type Domain, type FnOp } from \"@mark1russell7/vex\";\nimport {\n add,\n canonicalUnit,\n compare,\n convert,\n convertExact,\n isCanonicalUnit,\n kindOfQuantity,\n max,\n min,\n rationals,\n scale,\n sub,\n valueIn,\n type Quantity,\n} from \"@mark1russell7/quantity\";\n\nexport type { Quantity } from \"@mark1russell7/quantity\";\n\ntype QFn2 = (a: Quantity, b: Quantity) => Quantity;\ntype QTest = (a: Quantity, b: Quantity) => boolean;\n\n/** The ops of `QuantityDomain`. */\nexport type QuantityOps = {\n readonly value: FnOp<Quantity, (a: Quantity) => number>;\n readonly unit: FnOp<Quantity, (a: Quantity) => string>;\n readonly kind: FnOp<Quantity, (a: Quantity) => string>;\n readonly convert: FnOp<Quantity, (a: Quantity, unit: string) => Quantity>;\n readonly valueIn: FnOp<Quantity, (a: Quantity, unit: string) => number>;\n readonly add: FnOp<Quantity, QFn2>;\n readonly subtract: FnOp<Quantity, QFn2>;\n readonly multiply: FnOp<Quantity, (a: Quantity, k: number) => Quantity>;\n readonly divide: FnOp<Quantity, (a: Quantity, k: number) => Quantity>;\n readonly ratio: FnOp<Quantity, (a: Quantity, b: Quantity) => number>;\n readonly negate: FnOp<Quantity, (a: Quantity) => Quantity>;\n readonly abs: FnOp<Quantity, (a: Quantity) => Quantity>;\n readonly min: FnOp<Quantity, QFn2>;\n readonly max: FnOp<Quantity, QFn2>;\n readonly compare: FnOp<Quantity, (a: Quantity, b: Quantity) => number>;\n readonly lt: FnOp<Quantity, QTest>;\n readonly lte: FnOp<Quantity, QTest>;\n readonly gt: FnOp<Quantity, QTest>;\n readonly gte: FnOp<Quantity, QTest>;\n readonly eq: FnOp<Quantity, QTest>;\n};\n\n/** This function tells if a value is a plain object: its prototype is `Object.prototype` or `null`. */\nconst plain = (u: unknown): u is { readonly value?: unknown; readonly unit?: unknown } =>\n typeof u === \"object\" && u !== null && [Object.prototype, null].includes(Object.getPrototypeOf(u) as object | null);\n\n/**\n * This function tells if a value is a quantity of the domain. A quantity is a plain object with only the fields `value`\n * and `unit`. Its value is a number, and its unit is canonical. The check `valid` of the domain refuses a number that\n * is not finite.\n */\nexport const isQuantityValue = (u: unknown): u is Quantity =>\n plain(u) && Object.keys(u).length === 2 && typeof u.value === \"number\" && typeof u.unit === \"string\" && isCanonicalUnit(u.unit);\n\n/** This function gives the ratio a / b of two amounts of one kind, exactly. Each amount is in its own unit. */\nconst ratioOf = (a: Quantity, b: Quantity): number =>\n rationals.toNumber(rationals.div(convertExact(rationals.fromNumber(a.value), a.unit, b.unit), rationals.fromNumber(b.value)));\n\n/**\n * The domain of amounts in units. Each op does its unit arithmetic with `@mark1russell7/quantity`.\n *\n * - `add`, `subtract`, `min` and `max` give the result in the unit of the receiver. They convert the argument exactly.\n * - The comparisons convert exactly too. Thus `lte` of 155 mm and 16 cm is `true`.\n * - Two amounts of different kinds, for example a length and a mass, give an error value with the reason.\n * - `multiply` and `divide` take a number. `ratio` divides two amounts of one kind, and it gives a number.\n * - No op lifts a number: a number has no unit.\n * - A value that is not finite is not valid, so an op that gives one gives `#NUM!`.\n *\n * Put `QuantityDomain` before `NumRecordDomain` and `JsonDomain`, because they accept the same objects.\n */\nexport const QuantityDomain: Domain<\"Quantity\", Quantity, QuantityOps> = defineDomain<\"Quantity\", Quantity, QuantityOps>({\n name: \"Quantity\",\n // LOOP.SIZE.OPS: an op keeps the unit of its receiver, or it takes the unit of its argument.\n size: \"fixed\",\n fields: [\"value\", \"unit\"],\n is: isQuantityValue,\n valid: (a: Quantity): boolean => Number.isFinite(a.value) && isCanonicalUnit(a.unit),\n show: (a: Quantity): string => `${String(a.value)} ${a.unit}`,\n encode: (a: Quantity): unknown => ({ value: a.value, unit: a.unit }),\n // A value from JSON can spell its unit in another way, for example \"hr\". The domain keeps the canonical spelling. A\n // unit that does not exist throws a UnitError, and the reader of the document gives #CALC!.\n decode: (u: unknown): Quantity => {\n if (!plain(u) || typeof u.value !== \"number\" || typeof u.unit !== \"string\") throw new TypeError(\"a quantity is { value: number, unit: string }\");\n return { value: u.value, unit: canonicalUnit(u.unit) };\n },\n samples: (): readonly Quantity[] => [\n { value: 155, unit: \"mm\" },\n { value: 16, unit: \"cm\" },\n { value: 2.5, unit: \"kg\" },\n ],\n ops: {\n value: { fn: (a: Quantity): number => a.value, params: [], returns: \"number\" },\n unit: { fn: (a: Quantity): string => a.unit, params: [], returns: \"string\" },\n kind: { fn: (a: Quantity): string => kindOfQuantity(a), params: [], returns: \"string\" },\n convert: { fn: (a: Quantity, unit: string): Quantity => convert(a, unit), params: [\"string\"], returns: \"domain\" },\n valueIn: { fn: (a: Quantity, unit: string): number => valueIn(a, unit), params: [\"string\"], returns: \"number\" },\n add: { fn: (a: Quantity, b: Quantity): Quantity => add(a, b), params: [\"domain\"], returns: \"domain\" },\n subtract: { fn: (a: Quantity, b: Quantity): Quantity => sub(a, b), params: [\"domain\"], returns: \"domain\" },\n multiply: { fn: (a: Quantity, k: number): Quantity => scale(a, k), params: [\"number\"], returns: \"domain\" },\n // A division by 0 gives a value that is not finite, so the check of the result gives #NUM!.\n divide: { fn: (a: Quantity, k: number): Quantity => ({ value: a.value / k, unit: a.unit }), params: [\"number\"], returns: \"domain\" },\n ratio: { fn: (a: Quantity, b: Quantity): number => (b.value === 0 ? Number.NaN : ratioOf(a, b)), params: [\"domain\"], returns: \"number\" },\n negate: { fn: (a: Quantity): Quantity => ({ value: -a.value, unit: a.unit }), params: [], returns: \"domain\" },\n abs: { fn: (a: Quantity): Quantity => ({ value: Math.abs(a.value), unit: a.unit }), params: [], returns: \"domain\" },\n min: { fn: (a: Quantity, b: Quantity): Quantity => convert(min(a, b), a.unit), params: [\"domain\"], returns: \"domain\" },\n max: { fn: (a: Quantity, b: Quantity): Quantity => convert(max(a, b), a.unit), params: [\"domain\"], returns: \"domain\" },\n compare: { fn: (a: Quantity, b: Quantity): number => compare(a, b), params: [\"domain\"], returns: \"number\" },\n lt: { fn: (a: Quantity, b: Quantity): boolean => compare(a, b) < 0, params: [\"domain\"], returns: \"boolean\" },\n lte: { fn: (a: Quantity, b: Quantity): boolean => compare(a, b) <= 0, params: [\"domain\"], returns: \"boolean\" },\n gt: { fn: (a: Quantity, b: Quantity): boolean => compare(a, b) > 0, params: [\"domain\"], returns: \"boolean\" },\n gte: { fn: (a: Quantity, b: Quantity): boolean => compare(a, b) >= 0, params: [\"domain\"], returns: \"boolean\" },\n eq: { fn: (a: Quantity, b: Quantity): boolean => compare(a, b) === 0, params: [\"domain\"], returns: \"boolean\" },\n },\n});\n"],"mappings":";;;;;;;;;AAqDA,MAAM,SAAS,MACb,OAAO,MAAM,YAAY,MAAM,QAAQ,CAAC,OAAO,WAAW,IAAI,CAAC,CAAC,SAAS,OAAO,eAAe,CAAC,CAAkB;;;;;;AAOpH,MAAa,mBAAmB,MAC9B,MAAM,CAAC,KAAK,OAAO,KAAK,CAAC,CAAC,CAAC,WAAW,KAAK,OAAO,EAAE,UAAU,YAAY,OAAO,EAAE,SAAS,YAAY,gBAAgB,EAAE,IAAI;;AAGhI,MAAM,WAAW,GAAa,MAC5B,UAAU,SAAS,UAAU,IAAI,aAAa,UAAU,WAAW,EAAE,KAAK,GAAG,EAAE,MAAM,EAAE,IAAI,GAAG,UAAU,WAAW,EAAE,KAAK,CAAC,CAAC;;;;;;;;;;;;;AAc9H,MAAa,iBAA4D,aAAgD;CACvH,MAAM;CAEN,MAAM;CACN,QAAQ,CAAC,SAAS,MAAM;CACxB,IAAI;CACJ,QAAQ,MAAyB,OAAO,SAA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AQ,GAAG,CAAC,KAAK;GAAG,QAAQ,CAAC,QAAQ;GAAG,SAAS;EAAU;EAC7G,IAAI;GAAE,KAAK,GAAa,MAAyB,QAAQ,GAAG,CAAC,MAAM;GAAG,QAAQ,CAAC,QAAQ;GAAG,SAAS;EAAU;CAC/G;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/vex-domains",
|
|
4
|
-
"version": "0.19.
|
|
5
|
-
"description": "Vex domains: 2D vectors, named-component vectors, colors, angles, numbers and
|
|
4
|
+
"version": "0.19.1",
|
|
5
|
+
"description": "Vex domains: 2D vectors, named-component vectors, colors, angles, numbers, booleans and amounts in units.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"vex",
|
|
8
8
|
"vector",
|
|
9
9
|
"vec2",
|
|
10
10
|
"color",
|
|
11
11
|
"angle",
|
|
12
|
+
"units",
|
|
12
13
|
"domain",
|
|
13
14
|
"typescript"
|
|
14
15
|
],
|
|
@@ -32,6 +33,10 @@
|
|
|
32
33
|
"types": "./build/index.d.ts",
|
|
33
34
|
"default": "./build/index.js"
|
|
34
35
|
},
|
|
36
|
+
"./quantity": {
|
|
37
|
+
"types": "./build/quantity.d.ts",
|
|
38
|
+
"default": "./build/quantity.js"
|
|
39
|
+
},
|
|
35
40
|
"./package.json": "./package.json"
|
|
36
41
|
},
|
|
37
42
|
"files": [
|
|
@@ -52,7 +57,8 @@
|
|
|
52
57
|
"vitest": "^5.0.3"
|
|
53
58
|
},
|
|
54
59
|
"dependencies": {
|
|
55
|
-
"@mark1russell7/
|
|
60
|
+
"@mark1russell7/quantity": "^0.4.0",
|
|
61
|
+
"@mark1russell7/vex": "0.19.1"
|
|
56
62
|
},
|
|
57
63
|
"scripts": {
|
|
58
64
|
"build": "tsdown",
|
package/src/quantity.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The domain of amounts in units: the entry `@mark1russell7/vex-domains/quantity`. A value is a quantity of
|
|
3
|
+
* `@mark1russell7/quantity`, as plain data: `{ value: 155, unit: "mm" }`. That package does the unit arithmetic. It
|
|
4
|
+
* converts each amount exactly, so 155 mm and 16 cm compare without a rounding of doubles.
|
|
5
|
+
*/
|
|
6
|
+
import { defineDomain, type Domain, type FnOp } from "@mark1russell7/vex";
|
|
7
|
+
import {
|
|
8
|
+
add,
|
|
9
|
+
canonicalUnit,
|
|
10
|
+
compare,
|
|
11
|
+
convert,
|
|
12
|
+
convertExact,
|
|
13
|
+
isCanonicalUnit,
|
|
14
|
+
kindOfQuantity,
|
|
15
|
+
max,
|
|
16
|
+
min,
|
|
17
|
+
rationals,
|
|
18
|
+
scale,
|
|
19
|
+
sub,
|
|
20
|
+
valueIn,
|
|
21
|
+
type Quantity,
|
|
22
|
+
} from "@mark1russell7/quantity";
|
|
23
|
+
|
|
24
|
+
export type { Quantity } from "@mark1russell7/quantity";
|
|
25
|
+
|
|
26
|
+
type QFn2 = (a: Quantity, b: Quantity) => Quantity;
|
|
27
|
+
type QTest = (a: Quantity, b: Quantity) => boolean;
|
|
28
|
+
|
|
29
|
+
/** The ops of `QuantityDomain`. */
|
|
30
|
+
export type QuantityOps = {
|
|
31
|
+
readonly value: FnOp<Quantity, (a: Quantity) => number>;
|
|
32
|
+
readonly unit: FnOp<Quantity, (a: Quantity) => string>;
|
|
33
|
+
readonly kind: FnOp<Quantity, (a: Quantity) => string>;
|
|
34
|
+
readonly convert: FnOp<Quantity, (a: Quantity, unit: string) => Quantity>;
|
|
35
|
+
readonly valueIn: FnOp<Quantity, (a: Quantity, unit: string) => number>;
|
|
36
|
+
readonly add: FnOp<Quantity, QFn2>;
|
|
37
|
+
readonly subtract: FnOp<Quantity, QFn2>;
|
|
38
|
+
readonly multiply: FnOp<Quantity, (a: Quantity, k: number) => Quantity>;
|
|
39
|
+
readonly divide: FnOp<Quantity, (a: Quantity, k: number) => Quantity>;
|
|
40
|
+
readonly ratio: FnOp<Quantity, (a: Quantity, b: Quantity) => number>;
|
|
41
|
+
readonly negate: FnOp<Quantity, (a: Quantity) => Quantity>;
|
|
42
|
+
readonly abs: FnOp<Quantity, (a: Quantity) => Quantity>;
|
|
43
|
+
readonly min: FnOp<Quantity, QFn2>;
|
|
44
|
+
readonly max: FnOp<Quantity, QFn2>;
|
|
45
|
+
readonly compare: FnOp<Quantity, (a: Quantity, b: Quantity) => number>;
|
|
46
|
+
readonly lt: FnOp<Quantity, QTest>;
|
|
47
|
+
readonly lte: FnOp<Quantity, QTest>;
|
|
48
|
+
readonly gt: FnOp<Quantity, QTest>;
|
|
49
|
+
readonly gte: FnOp<Quantity, QTest>;
|
|
50
|
+
readonly eq: FnOp<Quantity, QTest>;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** This function tells if a value is a plain object: its prototype is `Object.prototype` or `null`. */
|
|
54
|
+
const plain = (u: unknown): u is { readonly value?: unknown; readonly unit?: unknown } =>
|
|
55
|
+
typeof u === "object" && u !== null && [Object.prototype, null].includes(Object.getPrototypeOf(u) as object | null);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* This function tells if a value is a quantity of the domain. A quantity is a plain object with only the fields `value`
|
|
59
|
+
* and `unit`. Its value is a number, and its unit is canonical. The check `valid` of the domain refuses a number that
|
|
60
|
+
* is not finite.
|
|
61
|
+
*/
|
|
62
|
+
export const isQuantityValue = (u: unknown): u is Quantity =>
|
|
63
|
+
plain(u) && Object.keys(u).length === 2 && typeof u.value === "number" && typeof u.unit === "string" && isCanonicalUnit(u.unit);
|
|
64
|
+
|
|
65
|
+
/** This function gives the ratio a / b of two amounts of one kind, exactly. Each amount is in its own unit. */
|
|
66
|
+
const ratioOf = (a: Quantity, b: Quantity): number =>
|
|
67
|
+
rationals.toNumber(rationals.div(convertExact(rationals.fromNumber(a.value), a.unit, b.unit), rationals.fromNumber(b.value)));
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The domain of amounts in units. Each op does its unit arithmetic with `@mark1russell7/quantity`.
|
|
71
|
+
*
|
|
72
|
+
* - `add`, `subtract`, `min` and `max` give the result in the unit of the receiver. They convert the argument exactly.
|
|
73
|
+
* - The comparisons convert exactly too. Thus `lte` of 155 mm and 16 cm is `true`.
|
|
74
|
+
* - Two amounts of different kinds, for example a length and a mass, give an error value with the reason.
|
|
75
|
+
* - `multiply` and `divide` take a number. `ratio` divides two amounts of one kind, and it gives a number.
|
|
76
|
+
* - No op lifts a number: a number has no unit.
|
|
77
|
+
* - A value that is not finite is not valid, so an op that gives one gives `#NUM!`.
|
|
78
|
+
*
|
|
79
|
+
* Put `QuantityDomain` before `NumRecordDomain` and `JsonDomain`, because they accept the same objects.
|
|
80
|
+
*/
|
|
81
|
+
export const QuantityDomain: Domain<"Quantity", Quantity, QuantityOps> = defineDomain<"Quantity", Quantity, QuantityOps>({
|
|
82
|
+
name: "Quantity",
|
|
83
|
+
// LOOP.SIZE.OPS: an op keeps the unit of its receiver, or it takes the unit of its argument.
|
|
84
|
+
size: "fixed",
|
|
85
|
+
fields: ["value", "unit"],
|
|
86
|
+
is: isQuantityValue,
|
|
87
|
+
valid: (a: Quantity): boolean => Number.isFinite(a.value) && isCanonicalUnit(a.unit),
|
|
88
|
+
show: (a: Quantity): string => `${String(a.value)} ${a.unit}`,
|
|
89
|
+
encode: (a: Quantity): unknown => ({ value: a.value, unit: a.unit }),
|
|
90
|
+
// A value from JSON can spell its unit in another way, for example "hr". The domain keeps the canonical spelling. A
|
|
91
|
+
// unit that does not exist throws a UnitError, and the reader of the document gives #CALC!.
|
|
92
|
+
decode: (u: unknown): Quantity => {
|
|
93
|
+
if (!plain(u) || typeof u.value !== "number" || typeof u.unit !== "string") throw new TypeError("a quantity is { value: number, unit: string }");
|
|
94
|
+
return { value: u.value, unit: canonicalUnit(u.unit) };
|
|
95
|
+
},
|
|
96
|
+
samples: (): readonly Quantity[] => [
|
|
97
|
+
{ value: 155, unit: "mm" },
|
|
98
|
+
{ value: 16, unit: "cm" },
|
|
99
|
+
{ value: 2.5, unit: "kg" },
|
|
100
|
+
],
|
|
101
|
+
ops: {
|
|
102
|
+
value: { fn: (a: Quantity): number => a.value, params: [], returns: "number" },
|
|
103
|
+
unit: { fn: (a: Quantity): string => a.unit, params: [], returns: "string" },
|
|
104
|
+
kind: { fn: (a: Quantity): string => kindOfQuantity(a), params: [], returns: "string" },
|
|
105
|
+
convert: { fn: (a: Quantity, unit: string): Quantity => convert(a, unit), params: ["string"], returns: "domain" },
|
|
106
|
+
valueIn: { fn: (a: Quantity, unit: string): number => valueIn(a, unit), params: ["string"], returns: "number" },
|
|
107
|
+
add: { fn: (a: Quantity, b: Quantity): Quantity => add(a, b), params: ["domain"], returns: "domain" },
|
|
108
|
+
subtract: { fn: (a: Quantity, b: Quantity): Quantity => sub(a, b), params: ["domain"], returns: "domain" },
|
|
109
|
+
multiply: { fn: (a: Quantity, k: number): Quantity => scale(a, k), params: ["number"], returns: "domain" },
|
|
110
|
+
// A division by 0 gives a value that is not finite, so the check of the result gives #NUM!.
|
|
111
|
+
divide: { fn: (a: Quantity, k: number): Quantity => ({ value: a.value / k, unit: a.unit }), params: ["number"], returns: "domain" },
|
|
112
|
+
ratio: { fn: (a: Quantity, b: Quantity): number => (b.value === 0 ? Number.NaN : ratioOf(a, b)), params: ["domain"], returns: "number" },
|
|
113
|
+
negate: { fn: (a: Quantity): Quantity => ({ value: -a.value, unit: a.unit }), params: [], returns: "domain" },
|
|
114
|
+
abs: { fn: (a: Quantity): Quantity => ({ value: Math.abs(a.value), unit: a.unit }), params: [], returns: "domain" },
|
|
115
|
+
min: { fn: (a: Quantity, b: Quantity): Quantity => convert(min(a, b), a.unit), params: ["domain"], returns: "domain" },
|
|
116
|
+
max: { fn: (a: Quantity, b: Quantity): Quantity => convert(max(a, b), a.unit), params: ["domain"], returns: "domain" },
|
|
117
|
+
compare: { fn: (a: Quantity, b: Quantity): number => compare(a, b), params: ["domain"], returns: "number" },
|
|
118
|
+
lt: { fn: (a: Quantity, b: Quantity): boolean => compare(a, b) < 0, params: ["domain"], returns: "boolean" },
|
|
119
|
+
lte: { fn: (a: Quantity, b: Quantity): boolean => compare(a, b) <= 0, params: ["domain"], returns: "boolean" },
|
|
120
|
+
gt: { fn: (a: Quantity, b: Quantity): boolean => compare(a, b) > 0, params: ["domain"], returns: "boolean" },
|
|
121
|
+
gte: { fn: (a: Quantity, b: Quantity): boolean => compare(a, b) >= 0, params: ["domain"], returns: "boolean" },
|
|
122
|
+
eq: { fn: (a: Quantity, b: Quantity): boolean => compare(a, b) === 0, params: ["domain"], returns: "boolean" },
|
|
123
|
+
},
|
|
124
|
+
});
|