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 CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.104.0",
14
+ "version": "1.105.1",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
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.unit("year");
8
+ return TIME_UNITS.getUnit("year");
9
9
  if (abs > 10 * WEEK)
10
- return TIME_UNITS.unit("month");
10
+ return TIME_UNITS.getUnit("month");
11
11
  if (abs > 2 * WEEK)
12
- return TIME_UNITS.unit("week");
12
+ return TIME_UNITS.getUnit("week");
13
13
  if (abs > DAY)
14
- return TIME_UNITS.unit("day");
14
+ return TIME_UNITS.getUnit("day");
15
15
  if (abs > HOUR)
16
- return TIME_UNITS.unit("hour");
16
+ return TIME_UNITS.getUnit("hour");
17
17
  if (abs > 9949)
18
- return TIME_UNITS.unit("minute");
18
+ return TIME_UNITS.getUnit("minute");
19
19
  if (abs > SECOND)
20
- return TIME_UNITS.unit("second");
21
- return TIME_UNITS.unit("millisecond");
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, unit?: K | Unit<K>): number;
44
+ to(amount: number, units?: K): number;
45
45
  /** Convert an amount from another unit to this unit. */
46
- from(amount: number, unit?: K | Unit<K>): 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, sourceUnit: K | Unit<K>, targetUnit: K | Unit<K>): 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
- unit(unit: K | Unit<K>): Unit<K>;
69
+ getUnit(units: K): Unit<K>;
70
70
  }
71
71
  /** Percentage units. */
72
- export declare const PERCENTAGE_UNITS: UnitList<"percent">;
73
- export type PercentageUnitKey = MapKey<typeof PERCENTAGE_UNITS>;
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, unit = this.list.base) {
41
- return this._toUnit(amount, this.list.unit(unit));
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, unit = this.list.base) {
45
- return this.list.unit(unit)._toUnit(amount, this);
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, sourceUnit, targetUnit) {
96
- return this.unit(sourceUnit).to(amount, targetUnit);
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
- unit(unit) {
103
- if (typeof unit !== "string")
104
- return unit;
105
- if (!this.has(unit))
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 PERCENTAGE_UNITS = new UnitList({
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
- if (!isObject(oldValue))
46
- throw new AssertionError(`Prop "${keys.slice(0, i + 1).join(".")}" is not an object`);
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
  }