shelving 1.104.0 → 1.105.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/package.json +1 -1
- package/util/duration.js +8 -8
- package/util/units.d.ts +6 -6
- package/util/units.js +11 -13
- package/util/update.js +3 -4
package/package.json
CHANGED
package/util/duration.js
CHANGED
|
@@ -5,20 +5,20 @@ import { TIME_UNITS } from "./units.js";
|
|
|
5
5
|
function _getTimeUnit(ms) {
|
|
6
6
|
const abs = Math.abs(ms);
|
|
7
7
|
if (abs > 18 * MONTH)
|
|
8
|
-
return TIME_UNITS.
|
|
8
|
+
return TIME_UNITS.getUnit("year");
|
|
9
9
|
if (abs > 10 * WEEK)
|
|
10
|
-
return TIME_UNITS.
|
|
10
|
+
return TIME_UNITS.getUnit("month");
|
|
11
11
|
if (abs > 2 * WEEK)
|
|
12
|
-
return TIME_UNITS.
|
|
12
|
+
return TIME_UNITS.getUnit("week");
|
|
13
13
|
if (abs > DAY)
|
|
14
|
-
return TIME_UNITS.
|
|
14
|
+
return TIME_UNITS.getUnit("day");
|
|
15
15
|
if (abs > HOUR)
|
|
16
|
-
return TIME_UNITS.
|
|
16
|
+
return TIME_UNITS.getUnit("hour");
|
|
17
17
|
if (abs > 9949)
|
|
18
|
-
return TIME_UNITS.
|
|
18
|
+
return TIME_UNITS.getUnit("minute");
|
|
19
19
|
if (abs > SECOND)
|
|
20
|
-
return TIME_UNITS.
|
|
21
|
-
return TIME_UNITS.
|
|
20
|
+
return TIME_UNITS.getUnit("second");
|
|
21
|
+
return TIME_UNITS.getUnit("millisecond");
|
|
22
22
|
}
|
|
23
23
|
/** Default number options for duration (no decimal places and rounding down). */
|
|
24
24
|
const NUMBER_OPTIONS = { maximumFractionDigits: 0, roundingMode: "trunc" };
|
package/util/units.d.ts
CHANGED
|
@@ -41,9 +41,9 @@ export declare class Unit<K extends string> {
|
|
|
41
41
|
/** Props to configure this unit. */
|
|
42
42
|
{ abbr, singular, plural, to }: UnitProps<K>);
|
|
43
43
|
/** Convert an amount from this unit to another unit. */
|
|
44
|
-
to(amount: number,
|
|
44
|
+
to(amount: number, units?: K): number;
|
|
45
45
|
/** Convert an amount from another unit to this unit. */
|
|
46
|
-
from(amount: number,
|
|
46
|
+
from(amount: number, units?: K): number;
|
|
47
47
|
/** Convert an amount from this unit to another unit (must specify another `Unit` instance). */
|
|
48
48
|
private _toUnit;
|
|
49
49
|
/** Format an amount with a given unit of measure, e.g. `12 kg` or `29.5 l` */
|
|
@@ -61,16 +61,16 @@ export declare class UnitList<K extends string> extends ImmutableMap<K, Unit<K>>
|
|
|
61
61
|
readonly base: Unit<K>;
|
|
62
62
|
constructor(units: ImmutableObject<K, UnitProps<K>>);
|
|
63
63
|
/** Convert an amount from a unit to another unit. */
|
|
64
|
-
convert(amount: number,
|
|
64
|
+
convert(amount: number, sourceUnits: K, targetUnits: K): number;
|
|
65
65
|
/**
|
|
66
66
|
* Get a unit from this list.
|
|
67
67
|
* @throws RequiredError if the unit is not found.
|
|
68
68
|
*/
|
|
69
|
-
|
|
69
|
+
getUnit(units: K): Unit<K>;
|
|
70
70
|
}
|
|
71
71
|
/** Percentage units. */
|
|
72
|
-
export declare const
|
|
73
|
-
export type
|
|
72
|
+
export declare const PERCENT_UNITS: UnitList<"percent">;
|
|
73
|
+
export type PercentUnitKey = MapKey<typeof PERCENT_UNITS>;
|
|
74
74
|
/** Point units. */
|
|
75
75
|
export declare const POINT_UNITS: UnitList<"basis-point" | "percentage-point">;
|
|
76
76
|
export type PointUnitKey = MapKey<typeof POINT_UNITS>;
|
package/util/units.js
CHANGED
|
@@ -37,12 +37,12 @@ export class Unit {
|
|
|
37
37
|
this._to = to;
|
|
38
38
|
}
|
|
39
39
|
/** Convert an amount from this unit to another unit. */
|
|
40
|
-
to(amount,
|
|
41
|
-
return this._toUnit(amount, this.list.
|
|
40
|
+
to(amount, units) {
|
|
41
|
+
return this._toUnit(amount, units ? this.list.getUnit(units) : this.list.base);
|
|
42
42
|
}
|
|
43
43
|
/** Convert an amount from another unit to this unit. */
|
|
44
|
-
from(amount,
|
|
45
|
-
return this.list.
|
|
44
|
+
from(amount, units) {
|
|
45
|
+
return (units ? this.list.getUnit(units) : this.list.base)._toUnit(amount, this);
|
|
46
46
|
}
|
|
47
47
|
/** Convert an amount from this unit to another unit (must specify another `Unit` instance). */
|
|
48
48
|
_toUnit(amount, unit) {
|
|
@@ -92,19 +92,17 @@ export class UnitList extends ImmutableMap {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
/** Convert an amount from a unit to another unit. */
|
|
95
|
-
convert(amount,
|
|
96
|
-
return this.
|
|
95
|
+
convert(amount, sourceUnits, targetUnits) {
|
|
96
|
+
return this.getUnit(sourceUnits).to(amount, targetUnits);
|
|
97
97
|
}
|
|
98
98
|
/**
|
|
99
99
|
* Get a unit from this list.
|
|
100
100
|
* @throws RequiredError if the unit is not found.
|
|
101
101
|
*/
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
return
|
|
105
|
-
|
|
106
|
-
throw new RequiredError(`Unit "${unit}" not found`);
|
|
107
|
-
return this.get(unit);
|
|
102
|
+
getUnit(units) {
|
|
103
|
+
if (this.has(units))
|
|
104
|
+
return this.get(units);
|
|
105
|
+
throw new RequiredError(`Unit "${units}" not found`);
|
|
108
106
|
}
|
|
109
107
|
}
|
|
110
108
|
// Distance constants.
|
|
@@ -134,7 +132,7 @@ const ML_PER_IN3 = MM3_PER_IN3 / 1000;
|
|
|
134
132
|
const US_IN3_PER_GAL = 231;
|
|
135
133
|
const IMP_ML_PER_GAL = 4546090 / 1000;
|
|
136
134
|
/** Percentage units. */
|
|
137
|
-
export const
|
|
135
|
+
export const PERCENT_UNITS = new UnitList({
|
|
138
136
|
percent: { abbr: "%", plural: "percent" },
|
|
139
137
|
});
|
|
140
138
|
/** Point units. */
|
package/util/update.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { AssertionError } from "../error/AssertionError.js";
|
|
2
1
|
import { reduceItems } from "./iterate.js";
|
|
3
2
|
import { getNumber } from "./number.js";
|
|
4
3
|
import { getProps, isObject } from "./object.js";
|
|
@@ -34,6 +33,7 @@ function _updatePropDeep(obj, update, keys, i) {
|
|
|
34
33
|
const oldValue = obj[k];
|
|
35
34
|
let newValue = oldValue;
|
|
36
35
|
if (i === keys.length - 1) {
|
|
36
|
+
// Final key.
|
|
37
37
|
if (action === "sum")
|
|
38
38
|
newValue = typeof oldValue === "number" ? oldValue + value : value;
|
|
39
39
|
else if (action === "set")
|
|
@@ -42,9 +42,8 @@ function _updatePropDeep(obj, update, keys, i) {
|
|
|
42
42
|
return action; // Never happens.
|
|
43
43
|
}
|
|
44
44
|
else {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
newValue = _updatePropDeep(oldValue, update, keys, i + 1);
|
|
45
|
+
// Subkeys.
|
|
46
|
+
newValue = _updatePropDeep(isObject(oldValue) ? oldValue : {}, update, keys, i + 1);
|
|
48
47
|
}
|
|
49
48
|
return oldValue === newValue ? obj : { ...obj, [k]: newValue };
|
|
50
49
|
}
|