shelving 1.84.0 → 1.84.2
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/constraint/FilterConstraint.d.ts +1 -1
- package/constraint/FilterConstraint.js +1 -2
- package/constraint/FilterConstraints.d.ts +1 -1
- package/constraint/FilterConstraints.js +1 -1
- package/package.json +1 -1
- package/schema/AllowSchema.d.ts +5 -7
- package/schema/DateSchema.d.ts +2 -2
- package/schema/DateSchema.js +8 -10
- package/schema/NumberSchema.d.ts +6 -2
- package/schema/NumberSchema.js +6 -2
- package/schema/TimeSchema.d.ts +12 -2
- package/schema/TimeSchema.js +14 -5
- package/util/array.d.ts +4 -0
- package/util/array.js +8 -1
- package/util/date.d.ts +9 -9
- package/util/date.js +23 -24
- package/util/index.d.ts +0 -1
- package/util/index.js +0 -1
- package/util/iterate.d.ts +4 -0
- package/util/iterate.js +7 -5
- package/util/match.d.ts +15 -15
- package/util/match.js +3 -2
- package/util/regexp.d.ts +2 -2
- package/util/time.d.ts +2 -2
- package/util/time.js +0 -2
- package/util/filter.d.ts +0 -8
- package/util/filter.js +0 -12
|
@@ -24,7 +24,7 @@ export declare type FilterList<T extends Data> = Nullish<FilterProps<T> | Filter
|
|
|
24
24
|
* @param operator FilterOperator, e.g. `IS` or `CONTAINS`
|
|
25
25
|
* @param value Value the specified property should be matched against.
|
|
26
26
|
*/
|
|
27
|
-
export declare class FilterConstraint<T extends Data = Data> implements Constraint<T>, Matchable<T
|
|
27
|
+
export declare class FilterConstraint<T extends Data = Data> implements Constraint<T>, Matchable<[T]> {
|
|
28
28
|
readonly key: string;
|
|
29
29
|
readonly operator: FilterOperator;
|
|
30
30
|
readonly value: unknown;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { isArray } from "../util/array.js";
|
|
2
2
|
import { isArrayWith, isEqual, isEqualGreater, isEqualLess, isGreater, isInArray, isLess, notEqual, notInArray } from "../util/match.js";
|
|
3
|
-
import { filterItems } from "../util/
|
|
4
|
-
import { isIterable } from "../util/iterate.js";
|
|
3
|
+
import { filterItems, isIterable } from "../util/iterate.js";
|
|
5
4
|
/** Map `FilterOperator` to its corresponding `Match` function. */
|
|
6
5
|
const MATCHERS = {
|
|
7
6
|
IS: isEqual,
|
|
@@ -6,7 +6,7 @@ import { Constraints } from "./Constraints.js";
|
|
|
6
6
|
* Interface to make sure an object implements all matchers.
|
|
7
7
|
* - Extends `Matchable` so this object itself can be directly be used in `filterItems()` and `filterEntries()`
|
|
8
8
|
*/
|
|
9
|
-
export interface Filterable<T extends Data> extends Matchable<T
|
|
9
|
+
export interface Filterable<T extends Data> extends Matchable<[T]> {
|
|
10
10
|
/** Add a filter to this filterable. */
|
|
11
11
|
filter(...filters: FilterList<Partial<T>>[]): this;
|
|
12
12
|
/** Match an item against the filters specified for this object. */
|
package/package.json
CHANGED
package/schema/AllowSchema.d.ts
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import type { Entry } from "../util/entry.js";
|
|
2
2
|
import { ImmutableRequiredMap, PossibleMap, PossibleStringMap } from "../util/map.js";
|
|
3
3
|
import { Schema, SchemaOptions } from "./Schema.js";
|
|
4
|
-
/** Options for an `AllowSchema` instance. */
|
|
5
|
-
declare type AllowSchemaOptions<K, T> = SchemaOptions & {
|
|
6
|
-
allow: PossibleMap<K, T>;
|
|
7
|
-
value?: K | null;
|
|
8
|
-
};
|
|
9
4
|
/** Define a valid value from an allowed set of values. */
|
|
10
5
|
export declare class AllowSchema<K, T> extends Schema<K> implements Iterable<Entry<K, T>> {
|
|
11
6
|
readonly value: K | null;
|
|
12
7
|
readonly allow: ImmutableRequiredMap<K, T>;
|
|
13
|
-
constructor({ allow, value, ...options }:
|
|
8
|
+
constructor({ allow, value, ...options }: SchemaOptions & {
|
|
9
|
+
allow: PossibleMap<K, T>;
|
|
10
|
+
value?: K | null;
|
|
11
|
+
});
|
|
14
12
|
validate(value?: unknown): K;
|
|
15
13
|
/** Iterate over the the allowed options in `[key, value]` format. */
|
|
16
14
|
[Symbol.iterator](): Iterator<Entry<K, T>>;
|
|
@@ -19,6 +17,7 @@ export declare class AllowSchema<K, T> extends Schema<K> implements Iterable<Ent
|
|
|
19
17
|
export declare class AllowStringSchema<K extends string, T> extends AllowSchema<K, T> {
|
|
20
18
|
constructor({ allow, ...options }: SchemaOptions & {
|
|
21
19
|
allow: PossibleStringMap<K, T>;
|
|
20
|
+
value?: K | null;
|
|
22
21
|
});
|
|
23
22
|
validator(value?: unknown): K;
|
|
24
23
|
}
|
|
@@ -26,4 +25,3 @@ export declare class AllowStringSchema<K extends string, T> extends AllowSchema<
|
|
|
26
25
|
export declare function ALLOW<K, T>(allow: PossibleMap<K, T>): AllowSchema<K, T>;
|
|
27
26
|
/** Valid string from an allowed set of values. */
|
|
28
27
|
export declare function ALLOW_STRING<K extends string, T>(allow: PossibleStringMap<K, T>): AllowSchema<K, T>;
|
|
29
|
-
export {};
|
package/schema/DateSchema.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { Schema, SchemaOptions } from "./Schema.js";
|
|
|
3
3
|
/** Define a valid date in YMD format, e.g. `2005-09-12` */
|
|
4
4
|
export declare class DateSchema extends Schema<string> {
|
|
5
5
|
readonly value: PossibleDate;
|
|
6
|
-
readonly min:
|
|
7
|
-
readonly max:
|
|
6
|
+
readonly min: Date | null;
|
|
7
|
+
readonly max: Date | null;
|
|
8
8
|
constructor({ value, min, max, ...options }: SchemaOptions & {
|
|
9
9
|
readonly value?: PossibleDate;
|
|
10
10
|
readonly min?: PossibleDate | null;
|
package/schema/DateSchema.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getOptionalDate,
|
|
1
|
+
import { formatDate, getDate, getOptionalDate, getYMD } from "../util/date.js";
|
|
2
2
|
import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
3
3
|
import { Schema } from "./Schema.js";
|
|
4
4
|
import { OPTIONAL } from "./OptionalSchema.js";
|
|
@@ -7,20 +7,18 @@ export class DateSchema extends Schema {
|
|
|
7
7
|
constructor({ value = "now", min = null, max = null, ...options }) {
|
|
8
8
|
super(options);
|
|
9
9
|
this.value = value;
|
|
10
|
-
this.min = min;
|
|
11
|
-
this.max = max;
|
|
10
|
+
this.min = min !== null ? getDate(min) : null;
|
|
11
|
+
this.max = max !== null ? getDate(max) : null;
|
|
12
12
|
}
|
|
13
13
|
validate(unsafeValue = this.value) {
|
|
14
14
|
const date = getOptionalDate(unsafeValue);
|
|
15
15
|
if (!date)
|
|
16
16
|
throw new InvalidFeedback(unsafeValue ? "Invalid date" : "Required", { value: unsafeValue });
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
throw new InvalidFeedback(`Maximum ${maxDate.toLocaleDateString()}`, { value: date });
|
|
23
|
-
return getYmd(date);
|
|
17
|
+
if (this.min && date < this.min)
|
|
18
|
+
throw new InvalidFeedback(`Minimum ${formatDate(this.min)}`, { value: date });
|
|
19
|
+
if (this.max && date > this.max)
|
|
20
|
+
throw new InvalidFeedback(`Maximum ${formatDate(this.max)}`, { value: date });
|
|
21
|
+
return getYMD(date);
|
|
24
22
|
}
|
|
25
23
|
}
|
|
26
24
|
/** Valid date, e.g. `2005-09-12` (required because falsy values are invalid). */
|
package/schema/NumberSchema.d.ts
CHANGED
|
@@ -13,7 +13,11 @@ export declare class NumberSchema extends Schema<number> {
|
|
|
13
13
|
});
|
|
14
14
|
validate(unsafeValue?: unknown): number;
|
|
15
15
|
}
|
|
16
|
-
/** Valid number, e.g. `2048` or `0` zero. */
|
|
16
|
+
/** Valid number, e.g. `2048.12345` or `0` zero. */
|
|
17
17
|
export declare const NUMBER: NumberSchema;
|
|
18
|
-
/** Valid number, e.g.
|
|
18
|
+
/** Valid number, e.g. `2048.12345` or `0` zero, or `null` */
|
|
19
19
|
export declare const OPTIONAL_NUMBER: import("./OptionalSchema.js").OptionalSchema<number>;
|
|
20
|
+
/** Valid integer number, e.g. `2048` or `0` zero. */
|
|
21
|
+
export declare const INTEGER: NumberSchema;
|
|
22
|
+
/** Valid integer number, e.g. `2048` or `0` zero, or `null` */
|
|
23
|
+
export declare const OPTIONAL_INTEGER: import("./OptionalSchema.js").OptionalSchema<number>;
|
package/schema/NumberSchema.js
CHANGED
|
@@ -23,7 +23,11 @@ export class NumberSchema extends Schema {
|
|
|
23
23
|
return safeNumber;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
-
/** Valid number, e.g. `2048` or `0` zero. */
|
|
26
|
+
/** Valid number, e.g. `2048.12345` or `0` zero. */
|
|
27
27
|
export const NUMBER = new NumberSchema({});
|
|
28
|
-
/** Valid number, e.g.
|
|
28
|
+
/** Valid number, e.g. `2048.12345` or `0` zero, or `null` */
|
|
29
29
|
export const OPTIONAL_NUMBER = OPTIONAL(NUMBER);
|
|
30
|
+
/** Valid integer number, e.g. `2048` or `0` zero. */
|
|
31
|
+
export const INTEGER = new NumberSchema({ step: 1, min: Number.MIN_SAFE_INTEGER, max: Number.MIN_SAFE_INTEGER });
|
|
32
|
+
/** Valid integer number, e.g. `2048` or `0` zero, or `null` */
|
|
33
|
+
export const OPTIONAL_INTEGER = OPTIONAL(INTEGER);
|
package/schema/TimeSchema.d.ts
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
|
-
import { PossibleTime } from "../util/time.js";
|
|
1
|
+
import { PossibleTime, Time } from "../util/time.js";
|
|
2
2
|
import { Schema, SchemaOptions } from "./Schema.js";
|
|
3
3
|
/** Define a valid time in 24h hh:mm:ss.fff format, e.g. `23:59` or `24:00 */
|
|
4
4
|
export declare class TimeSchema extends Schema<string> {
|
|
5
5
|
readonly value: PossibleTime;
|
|
6
|
-
|
|
6
|
+
readonly min: Time | null;
|
|
7
|
+
readonly max: Time | null;
|
|
8
|
+
/**
|
|
9
|
+
* Rounding step (in milliseconds, because that's the base unit for time), e.g. `60000` will round to the nearest second.
|
|
10
|
+
* - Note: `<input type="time">` elements expect `step=""` to be in _seconds_ so you need to multiply this by `1000`
|
|
11
|
+
*/
|
|
12
|
+
readonly step: number | null;
|
|
13
|
+
constructor({ value, min, max, step, ...options }: SchemaOptions & {
|
|
7
14
|
readonly value?: PossibleTime;
|
|
15
|
+
readonly min?: PossibleTime | null;
|
|
16
|
+
readonly max?: PossibleTime | null;
|
|
17
|
+
readonly step?: number | null;
|
|
8
18
|
});
|
|
9
19
|
validate(unsafeValue?: unknown): string;
|
|
10
20
|
}
|
package/schema/TimeSchema.js
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
|
|
2
|
-
import {
|
|
2
|
+
import { roundStep } from "../util/number.js";
|
|
3
|
+
import { getOptionalTime, Time, getTime } from "../util/time.js";
|
|
3
4
|
import { OPTIONAL } from "./OptionalSchema.js";
|
|
4
5
|
import { Schema } from "./Schema.js";
|
|
5
6
|
/** Define a valid time in 24h hh:mm:ss.fff format, e.g. `23:59` or `24:00 */
|
|
6
7
|
export class TimeSchema extends Schema {
|
|
7
|
-
constructor({ value = "now", ...options }) {
|
|
8
|
+
constructor({ value = "now", min = null, max = null, step = 60, ...options }) {
|
|
8
9
|
super(options);
|
|
9
10
|
this.value = value;
|
|
11
|
+
this.min = min !== null ? getTime(min) : null;
|
|
12
|
+
this.max = max !== null ? getTime(max) : null;
|
|
13
|
+
this.step = step;
|
|
10
14
|
}
|
|
11
15
|
validate(unsafeValue = this.value) {
|
|
12
|
-
const
|
|
13
|
-
if (!
|
|
16
|
+
const unsafeTime = getOptionalTime(unsafeValue);
|
|
17
|
+
if (!unsafeTime)
|
|
14
18
|
throw new InvalidFeedback(unsafeValue ? "Invalid time" : "Required", { value: unsafeValue });
|
|
15
|
-
|
|
19
|
+
const safeTime = typeof this.step === "number" ? new Time(roundStep(unsafeTime.time, this.step)) : unsafeTime;
|
|
20
|
+
if (this.max && safeTime > this.max)
|
|
21
|
+
throw new InvalidFeedback(`Maximum ${this.max.format()}`, { value: safeTime });
|
|
22
|
+
if (this.min && safeTime < this.min)
|
|
23
|
+
throw new InvalidFeedback(`Minimum ${this.min.format()}`, { value: safeTime });
|
|
24
|
+
return safeTime.long;
|
|
16
25
|
}
|
|
17
26
|
}
|
|
18
27
|
/** Valid time, e.g. `2005-09-12` (required because falsy values are invalid). */
|
package/util/array.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Arguments } from "./function.js";
|
|
2
|
+
import { Matcher } from "./match.js";
|
|
1
3
|
/**
|
|
2
4
|
* Mutable array: an array that can be changed.
|
|
3
5
|
* - Consistency with `MutableObject<T>` and `ImmutableArray<T>`
|
|
@@ -27,6 +29,8 @@ export declare function withArrayItems<T>(input: ImmutableArray<T>, ...items: T[
|
|
|
27
29
|
export declare function pickArrayItems<T>(input: ImmutableArray<T> | Iterable<T>, ...pick: T[]): ImmutableArray<T>;
|
|
28
30
|
/** Remove multiple items from an array (immutably) and return a new array without those items (or the same array if no changes were made). */
|
|
29
31
|
export declare function omitArrayItems<T>(input: ImmutableArray<T> | Iterable<T>, ...omit: T[]): ImmutableArray<T>;
|
|
32
|
+
/** Filter an array using a matcher (and optionally a target value). */
|
|
33
|
+
export declare function filterArray<T, A extends Arguments = []>(input: ImmutableArray<T>, matcher: Matcher<[T, ...A]>, ...args: A): ImmutableArray<T>;
|
|
30
34
|
/** Toggle an item in and out of an array (immutably) and return a new array with or without the specified items (or the same array if no changes were made). */
|
|
31
35
|
export declare function toggleArrayItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
|
|
32
36
|
/** Get the first item from an array or iterable, or `null` if it didn't exist. */
|
package/util/array.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AssertionError } from "../error/AssertionError.js";
|
|
2
2
|
import { RequiredError } from "../error/RequiredError.js";
|
|
3
|
-
import { omitItems, pickItems } from "./iterate.js";
|
|
3
|
+
import { filterItems, omitItems, pickItems } from "./iterate.js";
|
|
4
4
|
/** Is an unknown value an array? */
|
|
5
5
|
export const isArray = (v) => Array.isArray(v);
|
|
6
6
|
/** Assert that a value is an array. */
|
|
@@ -33,6 +33,13 @@ export function omitArrayItems(input, ...omit) {
|
|
|
33
33
|
const output = Array.from(omitItems(input, ...omit));
|
|
34
34
|
return isArray(input) && output.length === input.length ? input : output;
|
|
35
35
|
}
|
|
36
|
+
/** Filter an array using a matcher (and optionally a target value). */
|
|
37
|
+
export function filterArray(input, matcher, ...args) {
|
|
38
|
+
if (!input.length)
|
|
39
|
+
return input;
|
|
40
|
+
const output = Array.from(filterItems(input, matcher, ...args));
|
|
41
|
+
return output.length === input.length ? input : output;
|
|
42
|
+
}
|
|
36
43
|
/** Toggle an item in and out of an array (immutably) and return a new array with or without the specified items (or the same array if no changes were made). */
|
|
37
44
|
export function toggleArrayItems(input, ...items) {
|
|
38
45
|
const extras = items.filter(_doesNotInclude, input);
|
package/util/date.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/** Things that converted to dates. */
|
|
2
|
-
export declare type PossibleDate = Date | number | string
|
|
2
|
+
export declare type PossibleDate = Date | number | string;
|
|
3
3
|
/** Things that converted to dates or `null` */
|
|
4
|
-
export declare type PossibleOptionalDate = Date | number | string | null
|
|
4
|
+
export declare type PossibleOptionalDate = Date | number | string | null;
|
|
5
5
|
/** Is a value a date? */
|
|
6
6
|
export declare const isDate: (v: Date | unknown) => v is Date;
|
|
7
7
|
/** Assert that a value is a `Date` instance. */
|
|
8
8
|
export declare function assertDate(v: Date | unknown): asserts v is Date;
|
|
9
9
|
/**
|
|
10
|
-
* Convert an unknown value to a `Date` instance, or `null` if it couldn't be converted.
|
|
11
|
-
|
|
10
|
+
* Convert an unknown value to a valid `Date` instance, or `null` if it couldn't be converted.
|
|
11
|
+
* - Note: `Date` instances can be invalid (e.g. `new Date("blah blah").getTime()` returns `NaN`). These are detected and will always return `null`
|
|
12
12
|
*
|
|
13
13
|
* Conversion rules:
|
|
14
14
|
* - `Date` instance returns unchanged (BUT if the date isn't valid, `null` is returned).
|
|
@@ -22,16 +22,16 @@ export declare function assertDate(v: Date | unknown): asserts v is Date;
|
|
|
22
22
|
* - Numbers are return the corresponding date (using `new Date(number)`, i.e. milliseconds since 01/01/1970).
|
|
23
23
|
* - Anything else is converted to `null`
|
|
24
24
|
*
|
|
25
|
-
* @param
|
|
25
|
+
* @param possible Any value that we want to parse as a valid date.
|
|
26
26
|
* @returns `Date` instance if the value could be converted to a valid date, and `null` if not.
|
|
27
27
|
*/
|
|
28
|
-
export declare function getOptionalDate(
|
|
28
|
+
export declare function getOptionalDate(possible: unknown): Date | null;
|
|
29
29
|
/** Convert a possible date to a `Date` instance, or throw `AssertionError` if it couldn't be converted. */
|
|
30
|
-
export declare function getDate(
|
|
30
|
+
export declare function getDate(possible?: PossibleDate): Date;
|
|
31
31
|
/** Convert an unknown value to a YMD date string like "2015-09-12", or `null` if it couldn't be converted. */
|
|
32
|
-
export declare function
|
|
32
|
+
export declare function getOptionalYMD(possible: unknown): string | null;
|
|
33
33
|
/** Convert a `Date` instance to a YMD string like "2015-09-12", or throw `AssertionError` if it couldn't be converted. */
|
|
34
|
-
export declare
|
|
34
|
+
export declare const getYMD: (possible?: PossibleDate) => string;
|
|
35
35
|
/** List of day-of-week strings. */
|
|
36
36
|
export declare const days: readonly ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
|
|
37
37
|
/** Type listing day-of-week strings. */
|
package/util/date.js
CHANGED
|
@@ -7,8 +7,8 @@ export function assertDate(v) {
|
|
|
7
7
|
throw new AssertionError(`Must be date`, v);
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
|
-
* Convert an unknown value to a `Date` instance, or `null` if it couldn't be converted.
|
|
11
|
-
|
|
10
|
+
* Convert an unknown value to a valid `Date` instance, or `null` if it couldn't be converted.
|
|
11
|
+
* - Note: `Date` instances can be invalid (e.g. `new Date("blah blah").getTime()` returns `NaN`). These are detected and will always return `null`
|
|
12
12
|
*
|
|
13
13
|
* Conversion rules:
|
|
14
14
|
* - `Date` instance returns unchanged (BUT if the date isn't valid, `null` is returned).
|
|
@@ -22,48 +22,47 @@ export function assertDate(v) {
|
|
|
22
22
|
* - Numbers are return the corresponding date (using `new Date(number)`, i.e. milliseconds since 01/01/1970).
|
|
23
23
|
* - Anything else is converted to `null`
|
|
24
24
|
*
|
|
25
|
-
* @param
|
|
25
|
+
* @param possible Any value that we want to parse as a valid date.
|
|
26
26
|
* @returns `Date` instance if the value could be converted to a valid date, and `null` if not.
|
|
27
27
|
*/
|
|
28
|
-
export function getOptionalDate(
|
|
29
|
-
if (
|
|
28
|
+
export function getOptionalDate(possible) {
|
|
29
|
+
if (possible === undefined || possible === "now")
|
|
30
30
|
return new Date();
|
|
31
|
-
if (isDate(
|
|
32
|
-
return
|
|
33
|
-
if (
|
|
31
|
+
if (isDate(possible))
|
|
32
|
+
return _getValidDate(possible);
|
|
33
|
+
if (possible === "today")
|
|
34
34
|
return getMidnight();
|
|
35
|
-
if (
|
|
35
|
+
if (possible === "tomorrow")
|
|
36
36
|
return addDays(1, getMidnight());
|
|
37
|
-
if (
|
|
37
|
+
if (possible === "yesterday")
|
|
38
38
|
return addDays(-1, getMidnight());
|
|
39
|
-
if (
|
|
39
|
+
if (possible === null || possible === "")
|
|
40
40
|
return null; // We know empty string is always an invalid date.
|
|
41
|
-
if (typeof
|
|
42
|
-
return
|
|
43
|
-
if (typeof possibleDate === "function")
|
|
44
|
-
return getOptionalDate(possibleDate());
|
|
41
|
+
if (typeof possible === "string" || typeof possible === "number")
|
|
42
|
+
return _getValidDate(new Date(possible));
|
|
45
43
|
return null;
|
|
46
44
|
}
|
|
45
|
+
const _getValidDate = (date) => (Number.isFinite(date.getTime()) ? date : null);
|
|
47
46
|
/** Convert a possible date to a `Date` instance, or throw `AssertionError` if it couldn't be converted. */
|
|
48
|
-
export function getDate(
|
|
49
|
-
const date = getOptionalDate(
|
|
47
|
+
export function getDate(possible = "now") {
|
|
48
|
+
const date = getOptionalDate(possible);
|
|
50
49
|
if (!date)
|
|
51
|
-
throw new AssertionError(`Must be date`,
|
|
50
|
+
throw new AssertionError(`Must be date`, possible);
|
|
52
51
|
return date;
|
|
53
52
|
}
|
|
54
53
|
/** Convert an unknown value to a YMD date string like "2015-09-12", or `null` if it couldn't be converted. */
|
|
55
|
-
export function
|
|
56
|
-
const date = getOptionalDate(
|
|
57
|
-
return date ? date
|
|
54
|
+
export function getOptionalYMD(possible) {
|
|
55
|
+
const date = getOptionalDate(possible);
|
|
56
|
+
return date ? _ymd(date) : null;
|
|
58
57
|
}
|
|
59
58
|
/** Convert a `Date` instance to a YMD string like "2015-09-12", or throw `AssertionError` if it couldn't be converted. */
|
|
60
|
-
export
|
|
61
|
-
|
|
59
|
+
export const getYMD = (possible = "now") => _ymd(getDate(possible));
|
|
60
|
+
const _ymd = (date) => {
|
|
62
61
|
const y = _pad(date.getUTCFullYear(), 4);
|
|
63
62
|
const m = _pad(date.getUTCMonth() + 1, 2);
|
|
64
63
|
const d = _pad(date.getUTCDate(), 2);
|
|
65
64
|
return `${y}-${m}-${d}`;
|
|
66
|
-
}
|
|
65
|
+
};
|
|
67
66
|
const _pad = (num, size) => num.toString(10).padStart(size, "0000");
|
|
68
67
|
/** List of day-of-week strings. */
|
|
69
68
|
export const days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
|
package/util/index.d.ts
CHANGED
package/util/index.js
CHANGED
package/util/iterate.d.ts
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
* - Any object with a `Symbol.iterator` property is iterable.
|
|
4
4
|
* - Note: Array and Map instances etc will return true because they implement `Symbol.iterator`
|
|
5
5
|
*/
|
|
6
|
+
import { Arguments } from "./function.js";
|
|
7
|
+
import { Matcher } from "./match.js";
|
|
6
8
|
export declare const isIterable: <T extends Iterable<unknown>>(value: unknown) => value is T;
|
|
7
9
|
/** An iterable containing items or nested iterables of items. */
|
|
8
10
|
export declare type DeepIterable<T> = T | Iterable<DeepIterable<T>>;
|
|
@@ -32,6 +34,8 @@ export declare function omitItems<T>(items: Iterable<T>, ...omit: T[]): Iterable
|
|
|
32
34
|
/** Reduce an iterable set of items using a reducer function. */
|
|
33
35
|
export declare function reduceItems<T>(items: Iterable<T>, reducer: (previous: T, item: T) => T, initial: T): T;
|
|
34
36
|
export declare function reduceItems<T>(items: Iterable<T>, reducer: (previous: T | undefined, item: T) => T, initial?: T): T | undefined;
|
|
37
|
+
/** Filter an iterable set of items using a matcher (and optionally a target value). */
|
|
38
|
+
export declare function filterItems<T, A extends Arguments = []>(input: Iterable<T>, matcher: Matcher<[T, ...A]>, ...args: A): Iterable<T>;
|
|
35
39
|
/** Yield chunks of a given size. */
|
|
36
40
|
export declare function getChunks<T>(input: Iterable<T>, size: 1): Iterable<readonly [T]>;
|
|
37
41
|
export declare function getChunks<T>(input: Iterable<T>, size: 2): Iterable<readonly [T, T]>;
|
package/util/iterate.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Is a value an iterable object?
|
|
3
|
-
* - Any object with a `Symbol.iterator` property is iterable.
|
|
4
|
-
* - Note: Array and Map instances etc will return true because they implement `Symbol.iterator`
|
|
5
|
-
*/
|
|
1
|
+
import { match } from "./match.js";
|
|
6
2
|
export const isIterable = (value) => typeof value === "object" && !!value && Symbol.iterator in value;
|
|
7
3
|
/** Flatten one or more iterables. */
|
|
8
4
|
export function* flattenItems(items) {
|
|
@@ -73,6 +69,12 @@ export function reduceItems(items, reducer, initial) {
|
|
|
73
69
|
current = reducer(current, item);
|
|
74
70
|
return current;
|
|
75
71
|
}
|
|
72
|
+
/** Filter an iterable set of items using a matcher (and optionally a target value). */
|
|
73
|
+
export function* filterItems(input, matcher, ...args) {
|
|
74
|
+
for (const item of input)
|
|
75
|
+
if (match(matcher, item, ...args))
|
|
76
|
+
yield item;
|
|
77
|
+
}
|
|
76
78
|
export function* getChunks(input, size) {
|
|
77
79
|
let chunk = [];
|
|
78
80
|
for (const item of input) {
|
package/util/match.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
+
import { Arguments } from "./function.js";
|
|
1
2
|
/** Object that can match an item against a target with its `match()` function. */
|
|
2
|
-
export interface Matchable<
|
|
3
|
-
match(
|
|
3
|
+
export interface Matchable<A extends Arguments = unknown[]> {
|
|
4
|
+
match(...args: A): boolean;
|
|
4
5
|
}
|
|
5
6
|
/** Function that can match an item against a target. */
|
|
6
|
-
export declare type Match<
|
|
7
|
+
export declare type Match<A extends Arguments = unknown[]> = (...args: A) => boolean;
|
|
7
8
|
/** Match is either a `Matcherable` object, or matcher function. */
|
|
8
|
-
export declare type Matcher<
|
|
9
|
+
export declare type Matcher<A extends Arguments = unknown[]> = Matchable<A> | Match<A>;
|
|
9
10
|
/** Match two values using a `Matcher`. */
|
|
10
|
-
export declare function match<
|
|
11
|
-
export declare
|
|
12
|
-
export declare const
|
|
13
|
-
export declare const
|
|
14
|
-
export declare const
|
|
15
|
-
export declare const
|
|
16
|
-
export declare const
|
|
17
|
-
export declare const
|
|
18
|
-
export declare const
|
|
19
|
-
export declare const
|
|
20
|
-
export declare const isEqualGreater: Match<unknown, unknown>;
|
|
11
|
+
export declare function match<A extends Arguments>(matcher: Matcher<A>, ...args: A): boolean;
|
|
12
|
+
export declare const isEqual: Match<[item: unknown, target: unknown]>;
|
|
13
|
+
export declare const notEqual: Match<[item: unknown, target: unknown]>;
|
|
14
|
+
export declare const isInArray: Match<[item: unknown, target: unknown]>;
|
|
15
|
+
export declare const notInArray: Match<[item: unknown, target: unknown]>;
|
|
16
|
+
export declare const isArrayWith: Match<[item: unknown, target: unknown]>;
|
|
17
|
+
export declare const isLess: Match<[item: unknown, target: unknown]>;
|
|
18
|
+
export declare const isEqualLess: Match<[item: unknown, target: unknown]>;
|
|
19
|
+
export declare const isGreater: Match<[item: unknown, target: unknown]>;
|
|
20
|
+
export declare const isEqualGreater: Match<[item: unknown, target: unknown]>;
|
package/util/match.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { isArray } from "./array.js";
|
|
2
2
|
import { rankAsc } from "./sort.js";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/** Match two values using a `Matcher`. */
|
|
4
|
+
export function match(matcher, ...args) {
|
|
5
|
+
return typeof matcher === "function" ? matcher(...args) : matcher.match(...args);
|
|
5
6
|
}
|
|
6
7
|
// Regular matchers.
|
|
7
8
|
export const isEqual = (item, target) => item === target;
|
package/util/regexp.d.ts
CHANGED
|
@@ -34,6 +34,6 @@ export declare function getAnyRegExp(patterns: Iterable<PossibleRegExp> & NotStr
|
|
|
34
34
|
/** Create regular expression that matches all of a list of other expressions. */
|
|
35
35
|
export declare function getAllRegExp(patterns: Iterable<PossibleRegExp> & NotString, flags?: string): RegExp;
|
|
36
36
|
/** Match function for finding strings that match against regular expressions (use with `filter()` to positively filter iterable sets of items). */
|
|
37
|
-
export declare const isRegExpMatch: Match<string, RegExp>;
|
|
37
|
+
export declare const isRegExpMatch: Match<[item: string, target: RegExp]>;
|
|
38
38
|
/** Match function for finding strings that match against regular expressions (use with `filter()` to negatively filter iterable sets of items). */
|
|
39
|
-
export declare const notRegExpMatch: Match<string, RegExp>;
|
|
39
|
+
export declare const notRegExpMatch: Match<[item: string, target: RegExp]>;
|
package/util/time.d.ts
CHANGED
|
@@ -33,9 +33,9 @@ export declare class Time {
|
|
|
33
33
|
/** Regular expression that matches a time in ISO 8601 format. */
|
|
34
34
|
export declare const TIME_REGEXP: RegExp;
|
|
35
35
|
/** Things that converted to times. */
|
|
36
|
-
export declare type PossibleTime = Time | Date | number | string
|
|
36
|
+
export declare type PossibleTime = Time | Date | number | string;
|
|
37
37
|
/** Things that converted to times or `null` */
|
|
38
|
-
export declare type PossibleOptionalTime = Time | Date | number | string | null
|
|
38
|
+
export declare type PossibleOptionalTime = Time | Date | number | string | null;
|
|
39
39
|
/** Is an unknown value a `Time` instance. */
|
|
40
40
|
export declare const isTime: (v: Time | unknown) => v is Time;
|
|
41
41
|
/**
|
package/util/time.js
CHANGED
|
@@ -88,8 +88,6 @@ export const isTime = (v) => v instanceof Time;
|
|
|
88
88
|
export function getOptionalTime(possible) {
|
|
89
89
|
if (isTime(possible))
|
|
90
90
|
return possible;
|
|
91
|
-
if (typeof possible === "function")
|
|
92
|
-
return getOptionalTime(possible());
|
|
93
91
|
return (typeof possible === "string" && Time.fromString(possible)) || Time.fromDate(possible) || null;
|
|
94
92
|
}
|
|
95
93
|
/** Convert a possible time to a `Time` instance, or throw `AssertionError` if it couldn't be converted. */
|
package/util/filter.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { ImmutableArray } from "./array.js";
|
|
2
|
-
import { Matcher } from "./match.js";
|
|
3
|
-
/** Filter an iterable set of items using a matcher (and optionally a target value). */
|
|
4
|
-
export declare function filterItems<L>(input: Iterable<L>, matcher: Matcher<L, void>): Iterable<L>;
|
|
5
|
-
export declare function filterItems<L, R>(input: Iterable<L>, matcher: Matcher<L, R>, target: R): Iterable<L>;
|
|
6
|
-
/** Filter an array using a matcher (and optionally a target value). */
|
|
7
|
-
export declare function filterArray<L>(input: ImmutableArray<L>, matcher: Matcher<L, void>): ImmutableArray<L>;
|
|
8
|
-
export declare function filterArray<L, R>(input: ImmutableArray<L>, matcher: Matcher<L, R>, target: R): ImmutableArray<L>;
|
package/util/filter.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { match } from "./match.js";
|
|
2
|
-
export function* filterItems(input, matcher, target) {
|
|
3
|
-
for (const item of input)
|
|
4
|
-
if (match(item, matcher, target))
|
|
5
|
-
yield item;
|
|
6
|
-
}
|
|
7
|
-
export function filterArray(input, matcher, target) {
|
|
8
|
-
if (!input.length)
|
|
9
|
-
return input;
|
|
10
|
-
const output = Array.from(filterItems(input, matcher, target));
|
|
11
|
-
return output.length === input.length ? input : output;
|
|
12
|
-
}
|