shelving 1.83.2 → 1.83.4
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/entry.d.ts +3 -3
- package/util/entry.js +9 -6
- package/util/units.d.ts +21 -22
- package/util/units.js +20 -23
package/package.json
CHANGED
package/util/entry.d.ts
CHANGED
|
@@ -20,6 +20,6 @@ export declare function getEntryKeys<K, T>(input: Iterable<Entry<K, T>>): Iterab
|
|
|
20
20
|
/** Yield the values of an iterable set of entries. */
|
|
21
21
|
export declare function getEntryValues<K, T>(input: Iterable<Entry<K, T>>): Iterable<T>;
|
|
22
22
|
/** Yield the entries in something that can yield entries. */
|
|
23
|
-
export declare function getEntries<K extends
|
|
24
|
-
export declare function getEntries<K extends
|
|
25
|
-
export declare function getEntries<K, T = K>(
|
|
23
|
+
export declare function getEntries<K extends string, T = K>(...input: (ImmutableSet<K & T> | Partial<ImmutableObject<K, T>> | Iterable<Entry<K, T>>)[]): Iterable<Entry<K, T>>;
|
|
24
|
+
export declare function getEntries<K extends number, T = K>(...input: (ImmutableArray<T> | ImmutableSet<K & T> | Iterable<Entry<K, T>>)[]): Iterable<Entry<K, T>>;
|
|
25
|
+
export declare function getEntries<K, T = K>(...input: (ImmutableSet<K & T> | Iterable<Entry<K, T>>)[]): Iterable<Entry<K, T>>;
|
package/util/entry.js
CHANGED
|
@@ -15,10 +15,13 @@ export function* getEntryValues(input) {
|
|
|
15
15
|
for (const [, v] of input)
|
|
16
16
|
yield v;
|
|
17
17
|
}
|
|
18
|
-
export function getEntries(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
export function* getEntries(...input) {
|
|
19
|
+
for (const entries of input) {
|
|
20
|
+
if (isArray(entries) || isSet(entries))
|
|
21
|
+
yield* entries.entries();
|
|
22
|
+
else if (isIterable(entries))
|
|
23
|
+
yield* entries;
|
|
24
|
+
else
|
|
25
|
+
yield* Object.entries(entries);
|
|
26
|
+
}
|
|
24
27
|
}
|
package/util/units.d.ts
CHANGED
|
@@ -8,11 +8,11 @@ declare type Conversions<T extends string> = {
|
|
|
8
8
|
readonly [K in T]?: Conversion;
|
|
9
9
|
};
|
|
10
10
|
declare type UnitProps<T extends string> = {
|
|
11
|
-
/** Short abbreviation for this unit, e.g. `km` (defaults to first letter of `
|
|
11
|
+
/** Short abbreviation for this unit, e.g. `km` (defaults to first letter of `id`). */
|
|
12
12
|
readonly abbr?: string;
|
|
13
|
-
/** Singular name for this unit, e.g. `kilometer` (defaults to `
|
|
13
|
+
/** Singular name for this unit, e.g. `kilometer` (defaults to `id` + "s"). */
|
|
14
14
|
readonly singular?: string;
|
|
15
|
-
/** Plural name for this unit, e.g. `kilometers` (defaults to `
|
|
15
|
+
/** Plural name for this unit, e.g. `kilometers` (defaults to `id`). */
|
|
16
16
|
readonly plural?: string;
|
|
17
17
|
/** Conversions to other units (typically needs at least the base conversion, unless it's already the base unit). */
|
|
18
18
|
readonly to?: Conversions<T>;
|
|
@@ -22,11 +22,11 @@ export declare class Unit<T extends string> {
|
|
|
22
22
|
/** `UnitList` this unit belongs to. */
|
|
23
23
|
readonly list: UnitList<T>;
|
|
24
24
|
private readonly _to;
|
|
25
|
-
/**
|
|
26
|
-
readonly
|
|
27
|
-
/** Short abbreviation for this unit, e.g. `km` (defaults to first letter of `
|
|
25
|
+
/** Identifier for this unit, e.g. `kilometer` */
|
|
26
|
+
readonly id: T;
|
|
27
|
+
/** Short abbreviation for this unit, e.g. `km` (defaults to first letter of `id`). */
|
|
28
28
|
readonly abbr: string;
|
|
29
|
-
/** Singular name for this unit, e.g. `kilometer` (defaults to `
|
|
29
|
+
/** Singular name for this unit, e.g. `kilometer` (defaults to `id`). */
|
|
30
30
|
readonly singular: string;
|
|
31
31
|
/** Plural name for this unit, e.g. `kilometers` (defaults to `singular` + "s"). */
|
|
32
32
|
readonly plural: string;
|
|
@@ -35,16 +35,14 @@ export declare class Unit<T extends string> {
|
|
|
35
35
|
constructor(
|
|
36
36
|
/** `UnitList` this unit belongs to. */
|
|
37
37
|
list: UnitList<T>,
|
|
38
|
-
/**
|
|
39
|
-
|
|
38
|
+
/** Key for this unit, e.g. `kilometer` */
|
|
39
|
+
id: T,
|
|
40
40
|
/** Props to configure this unit. */
|
|
41
41
|
{ abbr, singular, plural, to }: UnitProps<T>);
|
|
42
42
|
/** Convert an amount from this unit to another unit. */
|
|
43
|
-
to(amount: number,
|
|
43
|
+
to(amount: number, id?: T | Unit<T>): number;
|
|
44
44
|
/** Convert an amount from another unit to this unit. */
|
|
45
|
-
from(amount: number,
|
|
46
|
-
/** Get a unit from a unit or unit reference. */
|
|
47
|
-
private _unit;
|
|
45
|
+
from(amount: number, id?: T | Unit<T>): number;
|
|
48
46
|
/** Convert an amount from this unit to another unit (must specify another `Unit` instance). */
|
|
49
47
|
private _toUnit;
|
|
50
48
|
/** Format a number with a given unit of measure, e.g. `12 kg` or `29.5 l` */
|
|
@@ -59,33 +57,34 @@ export declare class UnitList<T extends string> extends ImmutableRequiredMap<T,
|
|
|
59
57
|
}
|
|
60
58
|
/** Percentage units. */
|
|
61
59
|
export declare const PERCENTAGE_UNITS: UnitList<"percent">;
|
|
62
|
-
export declare type
|
|
60
|
+
export declare type PercentageUnitIdentifier = MapKey<typeof PERCENTAGE_UNITS>;
|
|
63
61
|
/** Point units. */
|
|
64
62
|
export declare const POINT_UNITS: UnitList<"basis-point" | "percentage-point">;
|
|
65
|
-
export declare type
|
|
63
|
+
export declare type PointUnitIdentifier = MapKey<typeof POINT_UNITS>;
|
|
66
64
|
/** Angle units. */
|
|
67
65
|
export declare const ANGLE_UNITS: UnitList<"degree" | "radian" | "gradian">;
|
|
68
|
-
export declare type
|
|
66
|
+
export declare type AngleUnitIdentifier = MapKey<typeof ANGLE_UNITS>;
|
|
69
67
|
/** Mass units. */
|
|
70
68
|
export declare const MASS_UNITS: UnitList<"milligram" | "gram" | "kilogram" | "ounce" | "pound" | "stone">;
|
|
71
|
-
export declare type
|
|
69
|
+
export declare type MassUnitIdentifier = MapKey<typeof MASS_UNITS>;
|
|
72
70
|
/** Time units. */
|
|
73
71
|
export declare const TIME_UNITS: UnitList<"millisecond" | "second" | "minute" | "hour" | "day" | "week" | "month" | "year">;
|
|
74
|
-
export declare type
|
|
72
|
+
export declare type TimeUnitIdentifier = MapKey<typeof TIME_UNITS>;
|
|
75
73
|
/** Length units. */
|
|
76
74
|
export declare const LENGTH_UNITS: UnitList<"millimeter" | "centimeter" | "meter" | "kilometer" | "inch" | "foot" | "yard" | "furlong" | "mile">;
|
|
77
|
-
export declare type
|
|
75
|
+
export declare type LengthUnitIdentifier = MapKey<typeof LENGTH_UNITS>;
|
|
78
76
|
/** Speed units. */
|
|
79
77
|
export declare const SPEED_UNITS: UnitList<"meter-per-second" | "kilometer-per-hour" | "mile-per-hour">;
|
|
80
|
-
export declare type
|
|
78
|
+
export declare type SpeedUnitIdentifier = MapKey<typeof SPEED_UNITS>;
|
|
81
79
|
/** Area units. */
|
|
82
80
|
export declare const AREA_UNITS: UnitList<"square-millimeter" | "square-centimeter" | "square-meter" | "square-kilometer" | "hectare" | "square-inch" | "square-foot" | "square-yard" | "acre">;
|
|
81
|
+
export declare type AreaUnitIdentifier = MapKey<typeof AREA_UNITS>;
|
|
83
82
|
/** Volume units. */
|
|
84
83
|
export declare const VOLUME_UNITS: UnitList<"milliliter" | "liter" | "cubic-centimeter" | "cubic-meter" | "us-fluid-ounce" | "us-pint" | "us-quart" | "us-gallon" | "imperial-fluid-ounce" | "imperial-pint" | "imperial-quart" | "imperial-gallon" | "cubic-inch" | "cubic-foot" | "cubic-yard">;
|
|
85
|
-
export declare type
|
|
84
|
+
export declare type VolumeUnitIdentifier = MapKey<typeof VOLUME_UNITS>;
|
|
86
85
|
/** Temperature units. */
|
|
87
86
|
export declare const TEMPERATURE_UNITS: UnitList<"celsius" | "fahrenheit" | "kelvin">;
|
|
88
|
-
export declare type
|
|
87
|
+
export declare type TemperatureUnitIdentifier = MapKey<typeof TEMPERATURE_UNITS>;
|
|
89
88
|
/** Format a percentage (combines `getPercent()` and `formatUnits()` for convenience). */
|
|
90
89
|
export declare const formatPercent: (numerator: number, denumerator: number, maxPrecision?: number, minPrecision?: number) => string;
|
|
91
90
|
/** 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`. */
|
package/util/units.js
CHANGED
|
@@ -11,12 +11,12 @@ export class Unit {
|
|
|
11
11
|
constructor(
|
|
12
12
|
/** `UnitList` this unit belongs to. */
|
|
13
13
|
list,
|
|
14
|
-
/**
|
|
15
|
-
|
|
14
|
+
/** Key for this unit, e.g. `kilometer` */
|
|
15
|
+
id,
|
|
16
16
|
/** Props to configure this unit. */
|
|
17
|
-
{ abbr =
|
|
17
|
+
{ abbr = id.slice(0, 1), singular = id.replace(/-/, " "), plural = `${singular}s`, to }) {
|
|
18
18
|
this.list = list;
|
|
19
|
-
this.
|
|
19
|
+
this.id = id;
|
|
20
20
|
this.abbr = abbr;
|
|
21
21
|
this.singular = singular;
|
|
22
22
|
this.plural = plural;
|
|
@@ -27,16 +27,12 @@ export class Unit {
|
|
|
27
27
|
return `${this.abbr} (${this.plural})`;
|
|
28
28
|
}
|
|
29
29
|
/** Convert an amount from this unit to another unit. */
|
|
30
|
-
to(amount,
|
|
31
|
-
return this._toUnit(amount, this.
|
|
30
|
+
to(amount, id = this.list.base) {
|
|
31
|
+
return this._toUnit(amount, _getUnit(this.list, id));
|
|
32
32
|
}
|
|
33
33
|
/** Convert an amount from another unit to this unit. */
|
|
34
|
-
from(amount,
|
|
35
|
-
return this.
|
|
36
|
-
}
|
|
37
|
-
/** Get a unit from a unit or unit reference. */
|
|
38
|
-
_unit(ref) {
|
|
39
|
-
return typeof ref === "string" ? this.list.get(ref) : ref;
|
|
34
|
+
from(amount, id = this.list.base) {
|
|
35
|
+
return _getUnit(this.list, id)._toUnit(amount, this);
|
|
40
36
|
}
|
|
41
37
|
/** Convert an amount from this unit to another unit (must specify another `Unit` instance). */
|
|
42
38
|
_toUnit(amount, unit) {
|
|
@@ -45,20 +41,20 @@ export class Unit {
|
|
|
45
41
|
if (unit === this)
|
|
46
42
|
return amount;
|
|
47
43
|
// Exact conversion.
|
|
48
|
-
const thisToUnit = (_a = this._to) === null || _a === void 0 ? void 0 : _a[unit.
|
|
44
|
+
const thisToUnit = (_a = this._to) === null || _a === void 0 ? void 0 : _a[unit.id];
|
|
49
45
|
if (thisToUnit)
|
|
50
46
|
return _convert(amount, thisToUnit);
|
|
51
47
|
// Invert number conversion (can't do this for function conversions).
|
|
52
|
-
const unitToThis = (_b = unit._to) === null || _b === void 0 ? void 0 : _b[this.
|
|
48
|
+
const unitToThis = (_b = unit._to) === null || _b === void 0 ? void 0 : _b[this.id];
|
|
53
49
|
if (typeof unitToThis === "number")
|
|
54
50
|
return amount / unitToThis;
|
|
55
51
|
// Two step conversion via base.
|
|
56
52
|
const base = this.list.base;
|
|
57
|
-
const thisToBase = (_c = this._to) === null || _c === void 0 ? void 0 : _c[base.
|
|
53
|
+
const thisToBase = (_c = this._to) === null || _c === void 0 ? void 0 : _c[base.id];
|
|
58
54
|
if (thisToBase)
|
|
59
55
|
return base._toUnit(_convert(amount, thisToBase), unit);
|
|
60
56
|
// Cannot convert.
|
|
61
|
-
throw new ConditionError(`Cannot convert "${this.
|
|
57
|
+
throw new ConditionError(`Cannot convert "${this.id}" to "${unit.id}"`);
|
|
62
58
|
}
|
|
63
59
|
/** Format a number with a given unit of measure, e.g. `12 kg` or `29.5 l` */
|
|
64
60
|
format(amount, maxPrecision, minPrecision) {
|
|
@@ -69,15 +65,16 @@ export class Unit {
|
|
|
69
65
|
return formatFullQuantity(amount, this.singular, this.plural, maxPrecision, minPrecision);
|
|
70
66
|
}
|
|
71
67
|
}
|
|
68
|
+
const _getUnit = (list, id) => (typeof id === "string" ? list.get(id) : id);
|
|
72
69
|
/** Represent a list of units. */
|
|
73
70
|
export class UnitList extends ImmutableRequiredMap {
|
|
74
71
|
constructor(units) {
|
|
75
72
|
super();
|
|
76
|
-
for (const [
|
|
77
|
-
const unit = new Unit(this,
|
|
73
|
+
for (const [id, props] of getProps(units)) {
|
|
74
|
+
const unit = new Unit(this, id, props);
|
|
78
75
|
if (!this.base)
|
|
79
76
|
this.base = unit;
|
|
80
|
-
Map.prototype.set.call(this,
|
|
77
|
+
Map.prototype.set.call(this, id, unit);
|
|
81
78
|
}
|
|
82
79
|
}
|
|
83
80
|
}
|
|
@@ -210,8 +207,8 @@ export const TEMPERATURE_UNITS = new UnitList({
|
|
|
210
207
|
});
|
|
211
208
|
/** Format a percentage (combines `getPercent()` and `formatUnits()` for convenience). */
|
|
212
209
|
export const formatPercent = (numerator, denumerator, maxPrecision, minPrecision) => formatQuantity(getPercent(numerator, denumerator), "%", maxPrecision, minPrecision);
|
|
213
|
-
/**
|
|
214
|
-
function
|
|
210
|
+
/** Get the ID for a time unit based on the amount in milliseconds. */
|
|
211
|
+
function _getTimeUnitIdentifier(ms) {
|
|
215
212
|
const abs = Math.abs(ms);
|
|
216
213
|
if (abs > 18 * MONTH)
|
|
217
214
|
return "year";
|
|
@@ -231,12 +228,12 @@ function _getTimeUnitReference(ms) {
|
|
|
231
228
|
}
|
|
232
229
|
/** 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`. */
|
|
233
230
|
export function formatFullDuration(ms, maxPrecision, minPrecision) {
|
|
234
|
-
const unit = TIME_UNITS.get(
|
|
231
|
+
const unit = TIME_UNITS.get(_getTimeUnitIdentifier(ms));
|
|
235
232
|
return unit.formatFull(unit.from(ms), maxPrecision, minPrecision);
|
|
236
233
|
}
|
|
237
234
|
/** Format a description of a duration of time using the most reasonable units e.g. `5y` or `4m` or `12ms`. */
|
|
238
235
|
export function formatDuration(ms, maxPrecision, minPrecision) {
|
|
239
|
-
const unit = TIME_UNITS.get(
|
|
236
|
+
const unit = TIME_UNITS.get(_getTimeUnitIdentifier(ms));
|
|
240
237
|
return unit.format(unit.from(ms), maxPrecision, minPrecision);
|
|
241
238
|
}
|
|
242
239
|
/** format when a data happens/happened. */
|