shelving 1.86.4 → 1.86.5
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/db/Query.js +2 -2
- package/package.json +1 -1
- package/util/array.d.ts +27 -15
- package/util/array.js +8 -7
- package/util/number.d.ts +2 -0
- package/util/number.js +7 -1
- package/util/string.d.ts +17 -7
- package/util/string.js +18 -6
package/db/Query.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getFirstItem, getLastItem, getOptionalFirstItem, getOptionalLastItem,
|
|
1
|
+
import { getFirstItem, getLastItem, getOptionalFirstItem, getOptionalLastItem, isArrayLength, countArray } from "../util/array.js";
|
|
2
2
|
import { runSequence } from "../util/sequence.js";
|
|
3
3
|
import { QueryConstraints } from "../constraint/QueryConstraints.js";
|
|
4
4
|
/** Reference to a set of items in a sync or async provider. */
|
|
@@ -68,7 +68,7 @@ export class AsyncQuery extends BaseQuery {
|
|
|
68
68
|
return this.value.then(countArray);
|
|
69
69
|
}
|
|
70
70
|
get exists() {
|
|
71
|
-
return this.max(1).value.then(
|
|
71
|
+
return this.max(1).value.then(isArrayLength);
|
|
72
72
|
}
|
|
73
73
|
get firstValue() {
|
|
74
74
|
return this.max(1).value.then(getOptionalFirstItem);
|
package/package.json
CHANGED
package/util/array.d.ts
CHANGED
|
@@ -60,20 +60,32 @@ export declare function limitArray<T>(items: Iterable<T>, limit: number): Immuta
|
|
|
60
60
|
/** Count the items in an array. */
|
|
61
61
|
export declare function countArray<T>(arr: ImmutableArray<T>): number;
|
|
62
62
|
/** Does an array have the specified minimum length. */
|
|
63
|
-
export declare function
|
|
64
|
-
export declare function
|
|
65
|
-
export declare function
|
|
66
|
-
export declare function
|
|
67
|
-
export declare function
|
|
63
|
+
export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 1, max: 1): arr is readonly [T];
|
|
64
|
+
export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 2, max: 2): arr is readonly [T, T];
|
|
65
|
+
export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 3, max: 3): arr is readonly [T, T, T];
|
|
66
|
+
export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 4, max: 4): arr is readonly [T, T, T, T];
|
|
67
|
+
export declare function isArrayLength<T>(arr: ImmutableArray<T>, min?: 1, max?: number): arr is readonly [T, ...T[]];
|
|
68
|
+
export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 2, max?: number): arr is readonly [T, T, ...T[]];
|
|
69
|
+
export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 3, max?: number): arr is readonly [T, T, T, ...T[]];
|
|
70
|
+
export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 4, max?: number): arr is readonly [T, T, T, T, ...T[]];
|
|
71
|
+
export declare function isArrayLength<T>(arr: ImmutableArray<T>, min?: number, max?: number): boolean;
|
|
68
72
|
/** Assert that a value has a specific length (or length is in a specific range). */
|
|
69
|
-
export declare function
|
|
70
|
-
export declare function
|
|
71
|
-
export declare function
|
|
72
|
-
export declare function
|
|
73
|
-
export declare function
|
|
73
|
+
export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 1, max: 1): asserts arr is readonly [T];
|
|
74
|
+
export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 2, max: 2): asserts arr is readonly [T, T];
|
|
75
|
+
export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 3, max: 3): asserts arr is readonly [T, T, T];
|
|
76
|
+
export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 4, max: 4): asserts arr is readonly [T, T, T, T];
|
|
77
|
+
export declare function assertArrayLength<T>(arr: ImmutableArray<T> | unknown, min?: 1, max?: number): asserts arr is readonly [T, ...T[]];
|
|
78
|
+
export declare function assertArrayLength<T>(arr: ImmutableArray<T> | unknown, min: 2, max?: number): asserts arr is readonly [T, T, ...T[]];
|
|
79
|
+
export declare function assertArrayLength<T>(arr: ImmutableArray<T> | unknown, min: 3, max?: number): asserts arr is readonly [T, T, T, ...T[]];
|
|
80
|
+
export declare function assertArrayLength<T>(arr: ImmutableArray<T> | unknown, min: 4, max?: number): asserts arr is readonly [T, T, T, T, ...T[]];
|
|
81
|
+
export declare function assertArrayLength<T>(arr: ImmutableArray<T> | unknown, min: number, max?: number): asserts arr is ImmutableArray<T>;
|
|
74
82
|
/** Get an array if it has the specified minimum length. */
|
|
75
|
-
export declare function
|
|
76
|
-
export declare function
|
|
77
|
-
export declare function
|
|
78
|
-
export declare function
|
|
79
|
-
export declare function
|
|
83
|
+
export declare function getArrayLength<T>(arr: ImmutableArray<T>, min: 1, max: 1): readonly [T];
|
|
84
|
+
export declare function getArrayLength<T>(arr: ImmutableArray<T>, min: 2, max: 2): readonly [T, T];
|
|
85
|
+
export declare function getArrayLength<T>(arr: ImmutableArray<T>, min: 3, max: 3): readonly [T, T, T];
|
|
86
|
+
export declare function getArrayLength<T>(arr: ImmutableArray<T>, min: 4, max: 4): readonly [T, T, T, T];
|
|
87
|
+
export declare function getArrayLength<T>(arr: ImmutableArray<T>, min?: 1, max?: number): readonly [T, ...T[]];
|
|
88
|
+
export declare function getArrayLength<T>(arr: ImmutableArray<T>, min: 2, max?: number): readonly [T, T, ...T[]];
|
|
89
|
+
export declare function getArrayLength<T>(arr: ImmutableArray<T>, min: 3, max?: number): readonly [T, T, T, ...T[]];
|
|
90
|
+
export declare function getArrayLength<T>(arr: ImmutableArray<T>, min: 4, max?: number): readonly [T, T, T, T, ...T[]];
|
|
91
|
+
export declare function getArrayLength<T>(arr: ImmutableArray<T>, min?: number, max?: number): ImmutableArray<T>;
|
package/util/array.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AssertionError } from "../error/AssertionError.js";
|
|
2
2
|
import { RequiredError } from "../error/RequiredError.js";
|
|
3
3
|
import { filterItems, omitItems, pickItems } from "./iterate.js";
|
|
4
|
+
import { formatRange } from "./number.js";
|
|
4
5
|
/** Is an unknown value an array? */
|
|
5
6
|
export const isArray = (v) => Array.isArray(v);
|
|
6
7
|
/** Assert that a value is an array. */
|
|
@@ -137,14 +138,14 @@ export function limitArray(items, limit) {
|
|
|
137
138
|
export function countArray(arr) {
|
|
138
139
|
return arr.length;
|
|
139
140
|
}
|
|
140
|
-
export function
|
|
141
|
-
return arr.length >= min;
|
|
141
|
+
export function isArrayLength(arr, min = 1, max = Infinity) {
|
|
142
|
+
return arr.length >= min && arr.length <= max;
|
|
142
143
|
}
|
|
143
|
-
export function
|
|
144
|
-
if (!isArray(arr) || !
|
|
145
|
-
throw new AssertionError(`Must be array with
|
|
144
|
+
export function assertArrayLength(arr, min = 1, max = Infinity) {
|
|
145
|
+
if (!isArray(arr) || !isArrayLength(arr, min, max))
|
|
146
|
+
throw new AssertionError(`Must be array with length ${formatRange(min, max)}`, arr);
|
|
146
147
|
}
|
|
147
|
-
export function
|
|
148
|
-
|
|
148
|
+
export function getArrayLength(arr, min = 1, max = Infinity) {
|
|
149
|
+
assertArrayLength(arr, min, max);
|
|
149
150
|
return arr;
|
|
150
151
|
}
|
package/util/number.d.ts
CHANGED
|
@@ -70,6 +70,8 @@ export declare function boundNumber(num: number, min: number, max: number): numb
|
|
|
70
70
|
export declare function wrapNumber(num: number, min: number, max: number): number;
|
|
71
71
|
/** Format a number (based on the user's browser language settings). */
|
|
72
72
|
export declare function formatNumber(num: number, precision?: number | null): string;
|
|
73
|
+
/** Format a number range (based on the user's browser language settings). */
|
|
74
|
+
export declare function formatRange(min: number, max: number, precision?: number | null): string;
|
|
73
75
|
/** Format a number with a short suffix, e.g. `1,000 kg` */
|
|
74
76
|
export declare const formatQuantity: (num: number, abbr: string, precision?: number | null) => string;
|
|
75
77
|
/** Format a number with a longer full-word suffix. */
|
package/util/number.js
CHANGED
|
@@ -110,10 +110,16 @@ export function wrapNumber(num, min, max) {
|
|
|
110
110
|
}
|
|
111
111
|
/** Format a number (based on the user's browser language settings). */
|
|
112
112
|
export function formatNumber(num, precision = null) {
|
|
113
|
+
if (Number.isNaN(num))
|
|
114
|
+
return "None";
|
|
113
115
|
if (!Number.isFinite(num))
|
|
114
|
-
return
|
|
116
|
+
return "∞";
|
|
115
117
|
return new Intl.NumberFormat(undefined, { minimumFractionDigits: precision !== null && precision !== void 0 ? precision : undefined, maximumFractionDigits: precision !== null && precision !== void 0 ? precision : 20 }).format(num);
|
|
116
118
|
}
|
|
119
|
+
/** Format a number range (based on the user's browser language settings). */
|
|
120
|
+
export function formatRange(min, max, precision = null) {
|
|
121
|
+
return `${formatNumber(min, precision)}–${formatNumber(max, precision)}`;
|
|
122
|
+
}
|
|
117
123
|
/** Format a number with a short suffix, e.g. `1,000 kg` */
|
|
118
124
|
export const formatQuantity = (num, abbr, precision) => `${formatNumber(num, precision)}${NNBSP}${abbr}`;
|
|
119
125
|
/** Format a number with a longer full-word suffix. */
|
package/util/string.d.ts
CHANGED
|
@@ -27,6 +27,12 @@ export declare function assertString(value: unknown): asserts value is string;
|
|
|
27
27
|
* - Everything else returns `"Unknown"`
|
|
28
28
|
*/
|
|
29
29
|
export declare function getString(value: unknown): string;
|
|
30
|
+
/** Does a string have the specified minimum length. */
|
|
31
|
+
export declare const isStringLength: (str: string, min?: number, max?: number) => boolean;
|
|
32
|
+
/** Assert that a value has a specific length (or length is in a specific range). */
|
|
33
|
+
export declare function assertStringLength(str: string | unknown, min?: number, max?: number): asserts str is string;
|
|
34
|
+
/** Get a string if it has the specified minimum length. */
|
|
35
|
+
export declare function getStringLength(arr: string, min?: number, max?: number): string;
|
|
30
36
|
/** Concatenate an iterable set of strings together. */
|
|
31
37
|
export declare const joinStrings: (strs: Iterable<string> & NotString, joiner?: string) => string;
|
|
32
38
|
/**
|
|
@@ -89,13 +95,17 @@ export declare function limitString(str: string, maxLength: number, append?: str
|
|
|
89
95
|
* Divide a string into parts based on a separator.
|
|
90
96
|
* - Like `String.prototype.split()` but with more useful arguments.
|
|
91
97
|
* - Excess segments in `String.prototype.split()` is counterintuitive because further parts are thrown away.
|
|
92
|
-
* - Excess segments in `splitString()` are concatenated onto the last segment (set `
|
|
98
|
+
* - Excess segments in `splitString()` are concatenated onto the last segment (set `max` to `null` if you want infinite segments).
|
|
93
99
|
*
|
|
94
|
-
* @throws AssertionError if `
|
|
100
|
+
* @throws AssertionError if `min` isn't met.
|
|
95
101
|
* @throws AssertionError if any of the segments are empty.
|
|
96
102
|
*/
|
|
97
|
-
export declare function splitString(str: string, separator: string,
|
|
98
|
-
export declare function splitString(str: string, separator: string,
|
|
99
|
-
export declare function splitString(str: string, separator: string,
|
|
100
|
-
export declare function splitString(str: string, separator: string,
|
|
101
|
-
export declare function splitString(str: string, separator: string,
|
|
103
|
+
export declare function splitString(str: string, separator: string, min: 1, max: 1): readonly [string];
|
|
104
|
+
export declare function splitString(str: string, separator: string, min: 2, max: 2): readonly [string, string];
|
|
105
|
+
export declare function splitString(str: string, separator: string, min: 3, max: 3): readonly [string, string, string];
|
|
106
|
+
export declare function splitString(str: string, separator: string, min: 4, max: 4): readonly [string, string, string, string];
|
|
107
|
+
export declare function splitString(str: string, separator: string, min: 1, max?: number): readonly [string, ...string[]];
|
|
108
|
+
export declare function splitString(str: string, separator: string, min: 2, max?: number): readonly [string, string, ...string[]];
|
|
109
|
+
export declare function splitString(str: string, separator: string, min: 3, max?: number): readonly [string, string, string, ...string[]];
|
|
110
|
+
export declare function splitString(str: string, separator: string, min: 4, max?: number): readonly [string, string, string, string, ...string[]];
|
|
111
|
+
export declare function splitString(str: string, separator: string, min?: number, max?: number): ImmutableArray<string>;
|
package/util/string.js
CHANGED
|
@@ -3,7 +3,7 @@ import { AssertionError } from "../error/AssertionError.js";
|
|
|
3
3
|
import { formatDate, isDate } from "./date.js";
|
|
4
4
|
import { formatObject, isObject } from "./object.js";
|
|
5
5
|
import { getArray, isArray } from "./array.js";
|
|
6
|
-
import { formatNumber, isBetween } from "./number.js";
|
|
6
|
+
import { formatNumber, formatRange, isBetween } from "./number.js";
|
|
7
7
|
/** Is a value a string? */
|
|
8
8
|
export const isString = (v) => typeof v === "string";
|
|
9
9
|
/** Assert that a value is a string. */
|
|
@@ -45,6 +45,18 @@ export function getString(value) {
|
|
|
45
45
|
return formatObject(value);
|
|
46
46
|
return "Unknown";
|
|
47
47
|
}
|
|
48
|
+
/** Does a string have the specified minimum length. */
|
|
49
|
+
export const isStringLength = (str, min = 1, max = Infinity) => str.length >= min && str.length <= max;
|
|
50
|
+
/** Assert that a value has a specific length (or length is in a specific range). */
|
|
51
|
+
export function assertStringLength(str, min = 1, max = Infinity) {
|
|
52
|
+
if (!isString(str) || !isStringLength(str, min, max))
|
|
53
|
+
throw new AssertionError(`Must be string with length ${formatRange(min, max)}`, str);
|
|
54
|
+
}
|
|
55
|
+
/** Get a string if it has the specified minimum length. */
|
|
56
|
+
export function getStringLength(arr, min = 1, max = Infinity) {
|
|
57
|
+
assertStringLength(arr, min, max);
|
|
58
|
+
return arr;
|
|
59
|
+
}
|
|
48
60
|
/** Concatenate an iterable set of strings together. */
|
|
49
61
|
export const joinStrings = (strs, joiner = "") => getArray(strs).join(joiner);
|
|
50
62
|
/**
|
|
@@ -133,11 +145,11 @@ export function limitString(str, maxLength, append = "…") {
|
|
|
133
145
|
const lastSpace = str.lastIndexOf(" ", maxLength);
|
|
134
146
|
return `${str.slice(0, lastSpace > 0 ? lastSpace : maxLength).trimEnd()}${append}`;
|
|
135
147
|
}
|
|
136
|
-
export function splitString(str, separator,
|
|
148
|
+
export function splitString(str, separator, min = 1, max = Infinity) {
|
|
137
149
|
const segments = str.split(separator);
|
|
138
|
-
if (
|
|
139
|
-
segments.splice(
|
|
140
|
-
if (segments.length <
|
|
141
|
-
throw new AssertionError(`Must be string with ${
|
|
150
|
+
if (segments.length > max)
|
|
151
|
+
segments.splice(max - 1, segments.length, segments.slice(max - 1).join(separator));
|
|
152
|
+
if (segments.length < min || !segments.every(Boolean))
|
|
153
|
+
throw new AssertionError(`Must be string with ${formatRange(min, max)} non-empty segments separated by "${separator}"`, str);
|
|
142
154
|
return segments;
|
|
143
155
|
}
|