shelving 1.86.7 → 1.86.8
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 +3 -3
- package/state/State.js +2 -1
- package/util/async.js +2 -1
- package/util/duration.d.ts +2 -2
- package/util/duration.js +16 -17
- package/util/units.d.ts +7 -0
- package/util/units.js +17 -6
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"state-management",
|
|
12
12
|
"query-builder"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.86.
|
|
14
|
+
"version": "1.86.8",
|
|
15
15
|
"repository": "https://github.com/dhoulb/shelving",
|
|
16
16
|
"author": "Dave Houlbrooke <dave@shax.com>",
|
|
17
17
|
"license": "0BSD",
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"@typescript-eslint/eslint-plugin": "^5.49.0",
|
|
72
72
|
"@typescript-eslint/parser": "^5.49.0",
|
|
73
73
|
"dpdm": "^3.11.0",
|
|
74
|
-
"esbuild": "^0.
|
|
74
|
+
"esbuild": "^0.17.12",
|
|
75
75
|
"esbuild-jest": "^0.5.0",
|
|
76
76
|
"eslint": "^8.33.0",
|
|
77
77
|
"eslint-config-prettier": "^8.6.0",
|
|
@@ -83,7 +83,7 @@
|
|
|
83
83
|
"prettier": "^2.8.3",
|
|
84
84
|
"react": "^18.1.0",
|
|
85
85
|
"react-dom": "^18.1.0",
|
|
86
|
-
"typescript": "^
|
|
86
|
+
"typescript": "^5.0.2"
|
|
87
87
|
},
|
|
88
88
|
"peerDependencies": {
|
|
89
89
|
"@google-cloud/firestore": ">=4.0.0",
|
package/state/State.js
CHANGED
|
@@ -10,7 +10,7 @@ import { DeferredSequence } from "../sequence/DeferredSequence.js";
|
|
|
10
10
|
* - To set the state to be loading, use the `State.NOVALUE` constant or a `Promise` value.
|
|
11
11
|
* - To set the state to an explicit value, use that value or another `State` instance with a value.
|
|
12
12
|
* */
|
|
13
|
-
|
|
13
|
+
class State {
|
|
14
14
|
/** Most recently dispatched value (or throw `Promise` that resolves to the next value). */
|
|
15
15
|
get value() {
|
|
16
16
|
if (this._value === State.NOVALUE)
|
|
@@ -79,5 +79,6 @@ export class State {
|
|
|
79
79
|
}
|
|
80
80
|
/** The `NOVALUE` symbol indicates no value has been received by a `State` instance. */
|
|
81
81
|
State.NOVALUE = Symbol("shelving/State.NOVALUE");
|
|
82
|
+
export { State };
|
|
82
83
|
/** Is an unknown value a `State` instance. */
|
|
83
84
|
export const isState = (v) => v instanceof State;
|
package/util/async.js
CHANGED
|
@@ -78,7 +78,7 @@ export class Delay extends AbstractPromise {
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
/** Resolve to `SIGNAL` on a specific signal. */
|
|
81
|
-
|
|
81
|
+
class Signal extends AbstractPromise {
|
|
82
82
|
constructor() {
|
|
83
83
|
super(...arguments);
|
|
84
84
|
/** Send this signal now. */
|
|
@@ -87,3 +87,4 @@ export class Signal extends AbstractPromise {
|
|
|
87
87
|
}
|
|
88
88
|
/** The `SIGNAL` symbol indicates a signal. */
|
|
89
89
|
Signal.SIGNAL = SIGNAL;
|
|
90
|
+
export { Signal };
|
package/util/duration.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { PossibleDate } from "./date.js";
|
|
2
2
|
/** Format a full format of a duration of time using the most reasonable units e.g. `5 years` or `1 week` or `4 minutes` or `12 milliseconds`. */
|
|
3
|
-
export declare function formatFullDuration(ms: number
|
|
3
|
+
export declare function formatFullDuration(ms: number): string;
|
|
4
4
|
/** Format a description of a duration of time using the most reasonable units e.g. `5y` or `4m` or `12ms`. */
|
|
5
|
-
export declare function formatDuration(ms: number
|
|
5
|
+
export declare function formatDuration(ms: number): string;
|
|
6
6
|
/** Full when a date happens/happened, e.g. `in 10 days` or `2 hours ago` */
|
|
7
7
|
export declare const formatFullWhen: (target: PossibleDate, current?: PossibleDate) => string;
|
|
8
8
|
/** Compact when a date happens/happened, e.g. `in 10d` or `2h ago` or `in 1w` */
|
package/util/duration.js
CHANGED
|
@@ -1,35 +1,34 @@
|
|
|
1
1
|
import { MONTH, WEEK, DAY, HOUR, SECOND } from "./constants.js";
|
|
2
2
|
import { getDuration } from "./date.js";
|
|
3
|
-
import { getMapItem } from "./map.js";
|
|
4
3
|
import { TIME_UNITS } from "./units.js";
|
|
5
|
-
/** Get
|
|
6
|
-
function
|
|
4
|
+
/** Get an appropriate time unit based on an amount in milliseconds. */
|
|
5
|
+
function _getTimeUnit(ms) {
|
|
7
6
|
const abs = Math.abs(ms);
|
|
8
7
|
if (abs > 18 * MONTH)
|
|
9
|
-
return "year";
|
|
8
|
+
return TIME_UNITS.unit("year");
|
|
10
9
|
if (abs > 10 * WEEK)
|
|
11
|
-
return "month";
|
|
10
|
+
return TIME_UNITS.unit("month");
|
|
12
11
|
if (abs > 2 * WEEK)
|
|
13
|
-
return "week";
|
|
12
|
+
return TIME_UNITS.unit("week");
|
|
14
13
|
if (abs > DAY)
|
|
15
|
-
return "day";
|
|
14
|
+
return TIME_UNITS.unit("day");
|
|
16
15
|
if (abs > HOUR)
|
|
17
|
-
return "hour";
|
|
16
|
+
return TIME_UNITS.unit("hour");
|
|
18
17
|
if (abs > 9949)
|
|
19
|
-
return "minute";
|
|
18
|
+
return TIME_UNITS.unit("minute");
|
|
20
19
|
if (abs > SECOND)
|
|
21
|
-
return "second";
|
|
22
|
-
return "millisecond";
|
|
20
|
+
return TIME_UNITS.unit("second");
|
|
21
|
+
return TIME_UNITS.unit("millisecond");
|
|
23
22
|
}
|
|
24
23
|
/** Format a full format of a duration of time using the most reasonable units e.g. `5 years` or `1 week` or `4 minutes` or `12 milliseconds`. */
|
|
25
|
-
export function formatFullDuration(ms
|
|
26
|
-
const unit =
|
|
27
|
-
return unit.formatFull(unit.from(ms),
|
|
24
|
+
export function formatFullDuration(ms) {
|
|
25
|
+
const unit = _getTimeUnit(ms);
|
|
26
|
+
return unit.formatFull(unit.from(ms), 0);
|
|
28
27
|
}
|
|
29
28
|
/** Format a description of a duration of time using the most reasonable units e.g. `5y` or `4m` or `12ms`. */
|
|
30
|
-
export function formatDuration(ms
|
|
31
|
-
const unit =
|
|
32
|
-
return unit.format(unit.from(ms),
|
|
29
|
+
export function formatDuration(ms) {
|
|
30
|
+
const unit = _getTimeUnit(ms);
|
|
31
|
+
return unit.format(unit.from(ms), 0);
|
|
33
32
|
}
|
|
34
33
|
/** format when a data happens/happened. */
|
|
35
34
|
function _formatWhen(formatter, target, current) {
|
package/util/units.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ type Conversion = number | ((num: number) => number);
|
|
|
6
6
|
type Conversions<T extends string> = {
|
|
7
7
|
readonly [K in T]?: Conversion;
|
|
8
8
|
};
|
|
9
|
+
/** Get a `Unit` instance from a `UnitList` instance */
|
|
9
10
|
type UnitProps<T extends string> = {
|
|
10
11
|
/** Short abbreviation for this unit, e.g. `km` (defaults to first letter of `id`). */
|
|
11
12
|
readonly abbr?: string;
|
|
@@ -58,6 +59,7 @@ export declare class Unit<K extends string> {
|
|
|
58
59
|
/**
|
|
59
60
|
* Represent a list of units.
|
|
60
61
|
* - Has a known base unit at `.base`
|
|
62
|
+
* - Can get required units from `.unit()`
|
|
61
63
|
* - Cannot have additional units added after it is created.
|
|
62
64
|
*/
|
|
63
65
|
export declare class UnitList<K extends string> extends ImmutableMap<K, Unit<K>> {
|
|
@@ -65,6 +67,11 @@ export declare class UnitList<K extends string> extends ImmutableMap<K, Unit<K>>
|
|
|
65
67
|
constructor(units: ImmutableObject<K, UnitProps<K>>);
|
|
66
68
|
/** Convert an amount from a unit to another unit. */
|
|
67
69
|
convert(amount: number, sourceUnit: K | Unit<K>, targetUnit: K | Unit<K>): number;
|
|
70
|
+
/**
|
|
71
|
+
* Get a unit from this list.
|
|
72
|
+
* @throws RequiredError if the unit is not found.
|
|
73
|
+
*/
|
|
74
|
+
unit(unit: K | Unit<K>): Unit<K>;
|
|
68
75
|
}
|
|
69
76
|
/** Percentage units. */
|
|
70
77
|
export declare const PERCENTAGE_UNITS: UnitList<"percent">;
|
package/util/units.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { ConditionError } from "../error/ConditionError.js";
|
|
2
|
+
import { RequiredError } from "../error/RequiredError.js";
|
|
2
3
|
import { DAY, HOUR, MILLION, MINUTE, MONTH, NNBSP, SECOND, WEEK, YEAR } from "./constants.js";
|
|
3
4
|
import { getProps } from "./object.js";
|
|
4
|
-
import { ImmutableMap
|
|
5
|
+
import { ImmutableMap } from "./map.js";
|
|
5
6
|
import { cramQuantity, formatFullQuantity, formatQuantity } from "./number.js";
|
|
6
7
|
/** Convert an amount using a `Conversion. */
|
|
7
8
|
const _convert = (amount, conversion) => (typeof conversion === "function" ? conversion(amount) : conversion === 1 ? amount : amount * conversion);
|
|
8
|
-
/** Get a `Unit` instance from a `UnitList` instance */
|
|
9
|
-
const _getUnit = (list, unit) => (typeof unit === "string" ? getMapItem(list, unit) : unit);
|
|
10
9
|
/** Represent a unit. */
|
|
11
10
|
export class Unit {
|
|
12
11
|
/** Title for this unit (uses format `abbr (plural)`, e.g. `fl oz (US fluid ounces)`) */
|
|
@@ -30,11 +29,11 @@ export class Unit {
|
|
|
30
29
|
}
|
|
31
30
|
/** Convert an amount from this unit to another unit. */
|
|
32
31
|
to(amount, unit = this.list.base) {
|
|
33
|
-
return this._toUnit(amount,
|
|
32
|
+
return this._toUnit(amount, this.list.unit(unit));
|
|
34
33
|
}
|
|
35
34
|
/** Convert an amount from another unit to this unit. */
|
|
36
35
|
from(amount, unit = this.list.base) {
|
|
37
|
-
return
|
|
36
|
+
return this.list.unit(unit)._toUnit(amount, this);
|
|
38
37
|
}
|
|
39
38
|
/** Convert an amount from this unit to another unit (must specify another `Unit` instance). */
|
|
40
39
|
_toUnit(amount, unit) {
|
|
@@ -74,6 +73,7 @@ export class Unit {
|
|
|
74
73
|
/**
|
|
75
74
|
* Represent a list of units.
|
|
76
75
|
* - Has a known base unit at `.base`
|
|
76
|
+
* - Can get required units from `.unit()`
|
|
77
77
|
* - Cannot have additional units added after it is created.
|
|
78
78
|
*/
|
|
79
79
|
export class UnitList extends ImmutableMap {
|
|
@@ -88,7 +88,18 @@ export class UnitList extends ImmutableMap {
|
|
|
88
88
|
}
|
|
89
89
|
/** Convert an amount from a unit to another unit. */
|
|
90
90
|
convert(amount, sourceUnit, targetUnit) {
|
|
91
|
-
return
|
|
91
|
+
return this.unit(sourceUnit).to(amount, targetUnit);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Get a unit from this list.
|
|
95
|
+
* @throws RequiredError if the unit is not found.
|
|
96
|
+
*/
|
|
97
|
+
unit(unit) {
|
|
98
|
+
if (typeof unit !== "string")
|
|
99
|
+
return unit;
|
|
100
|
+
if (!this.has(unit))
|
|
101
|
+
throw new RequiredError(`Unit "${unit}" not found`);
|
|
102
|
+
return this.get(unit);
|
|
92
103
|
}
|
|
93
104
|
}
|
|
94
105
|
// Distance constants.
|