shelving 1.83.0 → 1.83.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.
@@ -14,7 +14,7 @@ export declare abstract class Constraints<T extends Data, C extends Constraint<P
14
14
  /** Clone this set of constraints but add additional constraints. */
15
15
  with(...constraints: C[]): this;
16
16
  /** Clone this set of constraints but remove specific constraints. */
17
- without(...constraints: C[]): this;
17
+ omit(...constraints: C[]): this;
18
18
  /** Iterate over the constraints. */
19
19
  [Symbol.iterator](): Iterator<C, void>;
20
20
  }
@@ -1,4 +1,4 @@
1
- import { withArrayItems, withoutArrayItems } from "../util/array.js";
1
+ import { withArrayItems, omitArrayItems } from "../util/array.js";
2
2
  import { Constraint } from "./Constraint.js";
3
3
  /** Type of Rule that is powered by several sub-constraints (e.g. `Filters` and `Sorts` and `Query` itself extend this). */
4
4
  export class Constraints extends Constraint {
@@ -24,8 +24,8 @@ export class Constraints extends Constraint {
24
24
  return _constraints !== this._constraints ? { __proto__: Object.getPrototypeOf(this), ...this, _constraints: _constraints } : this;
25
25
  }
26
26
  /** Clone this set of constraints but remove specific constraints. */
27
- without(...constraints) {
28
- const _constraints = withoutArrayItems(this._constraints, ...constraints);
27
+ omit(...constraints) {
28
+ const _constraints = omitArrayItems(this._constraints, ...constraints);
29
29
  return _constraints !== this._constraints ? { __proto__: Object.getPrototypeOf(this), ...this, _constraints: _constraints } : this;
30
30
  }
31
31
  /** Iterate over the constraints. */
@@ -1,6 +1,6 @@
1
1
  import { getProp } from "../util/object.js";
2
2
  import { assert } from "../util/assert.js";
3
- import { limitItems } from "../util/iterate.js";
3
+ import { limitArray } from "../util/array.js";
4
4
  import { FilterConstraints } from "./FilterConstraints.js";
5
5
  import { SortConstraints } from "./SortConstraints.js";
6
6
  import { Constraint } from "./Constraint.js";
@@ -83,7 +83,7 @@ export class QueryConstraints extends Constraint {
83
83
  // Implement `Rule`
84
84
  transform(items) {
85
85
  const sorted = this.sorts.transform(this.filters.transform(items));
86
- return typeof this.limit === "number" ? limitItems(sorted, this.limit) : sorted;
86
+ return typeof this.limit === "number" ? limitArray(sorted, this.limit) : sorted;
87
87
  }
88
88
  // Implement toString()
89
89
  toString() {
package/db/Query.js CHANGED
@@ -1,5 +1,4 @@
1
- import { getFirstItem, getLastItem, getOptionalFirstItem, getOptionalLastItem } from "../util/array.js";
2
- import { countItems, hasItems } from "../util/iterate.js";
1
+ import { getFirstItem, getLastItem, getOptionalFirstItem, getOptionalLastItem, isArrayMin, countArray } from "../util/array.js";
3
2
  import { runSequence } from "../util/sequence.js";
4
3
  import { QueryConstraints } from "../constraint/QueryConstraints.js";
5
4
  /** Reference to a set of items in a sync or async provider. */
@@ -31,7 +30,7 @@ export class Query extends BaseQuery {
31
30
  return this.value.length;
32
31
  }
33
32
  get exists() {
34
- return hasItems(this.max(1).value);
33
+ return !!this.max(1).value.length;
35
34
  }
36
35
  get firstValue() {
37
36
  return getOptionalFirstItem(this.max(1).value);
@@ -66,10 +65,10 @@ export class AsyncQuery extends BaseQuery {
66
65
  return this.db.provider.getQuery(this.collection, this);
67
66
  }
68
67
  get count() {
69
- return this.value.then(countItems);
68
+ return this.value.then(countArray);
70
69
  }
71
70
  get exists() {
72
- return this.max(1).value.then(hasItems);
71
+ return this.max(1).value.then(isArrayMin);
73
72
  }
74
73
  get firstValue() {
75
74
  return this.max(1).value.then(getOptionalFirstItem);
@@ -1,4 +1,4 @@
1
- import type { ImmutableDictionary } from "../util/dictionary.js";
1
+ import { Entry } from "../util/entry.js";
2
2
  /**
3
3
  * The `Feedback` class represents a feedback message that should be shown to the user.
4
4
  * - Basic `Feedback` is neither good nor bad, `SuccessFeedback` indicates good news, and `ErrorFeedback` indicates bad news.
@@ -21,7 +21,7 @@ export declare class Feedback<T = unknown> {
21
21
  /** Is an unknown value a `Feedback` object. */
22
22
  export declare const isFeedback: <T extends Feedback<unknown>>(v: unknown) => v is Feedback<unknown>;
23
23
  /**
24
- * Get an object of sub-messages in `{ key: message }` format from a feedback's value.
24
+ * Yield the sub-messages in `[key: string, message: string]` format from a feedback's value.
25
25
  * - Takes the `.value` property from a `Feedback` instance and looks for keyed `Feedback` instances in either `{ key: Feedback }`. `Feedback[]`, or `Map<key, Feedback>` formats.
26
26
  */
27
- export declare const getFeedbackMessages: ({ value }: Feedback) => ImmutableDictionary<string>;
27
+ export declare function getFeedbackMessages({ value }: Feedback): Iterable<Entry<string, string>>;
@@ -31,11 +31,10 @@ __decorate([
31
31
  /** Is an unknown value a `Feedback` object. */
32
32
  export const isFeedback = (v) => isObject(v) && typeof v.name === "string" && v.name.endsWith("Feedback") && typeof v.message === "string";
33
33
  /**
34
- * Get an object of sub-messages in `{ key: message }` format from a feedback's value.
34
+ * Yield the sub-messages in `[key: string, message: string]` format from a feedback's value.
35
35
  * - Takes the `.value` property from a `Feedback` instance and looks for keyed `Feedback` instances in either `{ key: Feedback }`. `Feedback[]`, or `Map<key, Feedback>` formats.
36
36
  */
37
- export const getFeedbackMessages = ({ value }) => Object.fromEntries(_yieldFeedbackMessages(value));
38
- function* _yieldFeedbackMessages(value) {
37
+ export function* getFeedbackMessages({ value }) {
39
38
  if (isObject(value))
40
39
  for (const [k, v] of getEntries(value))
41
40
  if (isFeedback(v))
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.83.0",
14
+ "version": "1.83.2",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -15,13 +15,13 @@ export declare type SchemaOptions = {
15
15
  */
16
16
  export declare abstract class Schema<T extends unknown = unknown> implements Validatable<T> {
17
17
  /** Title, e.g. for showing in fields. */
18
- readonly title: string;
18
+ readonly title?: string;
19
19
  /** Description, e.g. for showing in fields. */
20
- readonly description: string;
20
+ readonly description?: string;
21
21
  /** Placeholder, e.g. for showing in fields. */
22
- readonly placeholder: string;
22
+ readonly placeholder?: string;
23
23
  /** Default value. */
24
- readonly value: unknown;
24
+ readonly value?: unknown;
25
25
  constructor({ title, description, placeholder }: SchemaOptions);
26
26
  /** Every schema must implement a `validate()` method. */
27
27
  abstract validate(unsafeValue: unknown): T;
@@ -1,4 +1,4 @@
1
- import { toggleArrayItems, withArrayItems, withoutArrayItems } from "../util/array.js";
1
+ import { toggleArrayItems, withArrayItems, omitArrayItems } from "../util/array.js";
2
2
  import { State } from "./State.js";
3
3
  /** State that stores an array and has additional methods to help with that. */
4
4
  export class ArrayState extends State {
@@ -15,7 +15,7 @@ export class ArrayState extends State {
15
15
  }
16
16
  /** Remove items from this array. */
17
17
  delete(...items) {
18
- this.set(withoutArrayItems(this.value, ...items));
18
+ this.set(omitArrayItems(this.value, ...items));
19
19
  }
20
20
  /** Toggle items in this array. */
21
21
  toggle(...items) {
@@ -1,4 +1,4 @@
1
- import { withoutDictionaryItems } from "../util/dictionary.js";
1
+ import { omitDictionaryItems } from "../util/dictionary.js";
2
2
  import { transformDictionary } from "../util/transform.js";
3
3
  import { State } from "./State.js";
4
4
  /** State that stores a dictionary object and has additional methods to help with that. */
@@ -16,7 +16,7 @@ export class DictionaryState extends State {
16
16
  }
17
17
  /** Remove a named entry from this object. */
18
18
  delete(...keys) {
19
- this.set(withoutDictionaryItems(this.value, ...keys));
19
+ this.set(omitDictionaryItems(this.value, ...keys));
20
20
  }
21
21
  /** Iterate over the entries of the object. */
22
22
  [Symbol.iterator]() {
@@ -4,16 +4,16 @@ import { Update } from "./Update.js";
4
4
  /** Update that can be applied to an array to add/remove items. */
5
5
  export declare class ArrayUpdate<T> extends Update<ImmutableArray<T>> {
6
6
  /** Return an array update with an item marked for addition. */
7
- static with<X>(...adds: X[]): ArrayUpdate<X>;
7
+ static add<X>(...adds: X[]): ArrayUpdate<X>;
8
8
  /** Return an array update with an item marked for deletion. */
9
- static without<X>(...deletes: X[]): ArrayUpdate<X>;
9
+ static delete<X>(...deletes: X[]): ArrayUpdate<X>;
10
10
  readonly adds: ImmutableArray<T>;
11
11
  readonly deletes: ImmutableArray<T>;
12
12
  constructor(adds?: ImmutableArray<T>, deletes?: ImmutableArray<T>);
13
- transform(arr?: ImmutableArray<T>): ImmutableArray<T>;
14
- validate(validator: Validator<ImmutableArray<T>>): this;
15
13
  /** Return an array update with an additional item marked for addition. */
16
- with(...adds: T[]): this;
14
+ add(...adds: T[]): this;
17
15
  /** Return an array update with an additional item marked for deletion. */
18
- without(...deletes: T[]): this;
16
+ delete(...deletes: T[]): this;
17
+ transform(arr?: ImmutableArray<T>): ImmutableArray<T>;
18
+ validate(validator: Validator<ImmutableArray<T>>): this;
19
19
  }
@@ -1,5 +1,5 @@
1
1
  import { ArraySchema } from "../schema/ArraySchema.js";
2
- import { withArrayItems, withoutArrayItems } from "../util/array.js";
2
+ import { withArrayItems, omitArrayItems } from "../util/array.js";
3
3
  import { validateArray } from "../util/validate.js";
4
4
  import { Update } from "./Update.js";
5
5
  /** Update that can be applied to an array to add/remove items. */
@@ -10,40 +10,42 @@ export class ArrayUpdate extends Update {
10
10
  this.deletes = deletes;
11
11
  }
12
12
  /** Return an array update with an item marked for addition. */
13
- static with(...adds) {
13
+ static add(...adds) {
14
14
  return new ArrayUpdate(adds);
15
15
  }
16
16
  /** Return an array update with an item marked for deletion. */
17
- static without(...deletes) {
17
+ static delete(...deletes) {
18
18
  return new ArrayUpdate([], deletes);
19
19
  }
20
- transform(arr = []) {
21
- return withoutArrayItems(withArrayItems(arr, ...this.adds), ...this.deletes);
22
- }
23
- validate(validator) {
24
- if (!(validator instanceof ArraySchema))
25
- return super.validate(validator);
20
+ /** Return an array update with an additional item marked for addition. */
21
+ add(...adds) {
26
22
  return {
27
23
  __proto__: Object.getPrototypeOf(this),
28
24
  ...this,
29
- adds: validateArray(this.adds, validator.items),
30
- deletes: validateArray(this.deletes, validator.items),
25
+ adds: [...this.adds, ...adds],
31
26
  };
32
27
  }
33
- /** Return an array update with an additional item marked for addition. */
34
- with(...adds) {
28
+ /** Return an array update with an additional item marked for deletion. */
29
+ delete(...deletes) {
35
30
  return {
36
31
  __proto__: Object.getPrototypeOf(this),
37
32
  ...this,
38
- adds: [...this.adds, ...adds],
33
+ deletes: [...this.deletes, ...deletes],
39
34
  };
40
35
  }
41
- /** Return an array update with an additional item marked for deletion. */
42
- without(...deletes) {
36
+ // Implement `Transformable`
37
+ transform(arr = []) {
38
+ return omitArrayItems(withArrayItems(arr, ...this.adds), ...this.deletes);
39
+ }
40
+ // Implement `Validatable`
41
+ validate(validator) {
42
+ if (!(validator instanceof ArraySchema))
43
+ return super.validate(validator);
43
44
  return {
44
45
  __proto__: Object.getPrototypeOf(this),
45
46
  ...this,
46
- deletes: [...this.deletes, ...deletes],
47
+ adds: validateArray(this.adds, validator.items),
48
+ deletes: validateArray(this.deletes, validator.items),
47
49
  };
48
50
  }
49
51
  }
@@ -23,13 +23,12 @@ export declare const updateData: <T extends Data>(data: T, updates: Updates<T>)
23
23
  */
24
24
  export declare class DataUpdate<T extends Data = Data> extends Update<T> implements Iterable<DataProp<Updates<T>>>, Transformable<T, T> {
25
25
  /** Return a data update with a specific prop marked for update. */
26
- static with<X extends Data, K extends DataKey<X>>(key: Nullish<K>, value: X[K] | Update<X[K]>): DataUpdate<X>;
26
+ static update<X extends Data, K extends DataKey<X>>(key: Nullish<K>, value: X[K] | Update<X[K]>): DataUpdate<X>;
27
27
  readonly updates: Updates<T>;
28
28
  constructor(props: Updates<T>);
29
+ /** Return a data update with a specific prop marked for update. */
30
+ update<K extends DataKey<T>>(key: Nullish<K>, value: T[K] | Update<T[K]>): this;
29
31
  transform(data: T): T;
30
32
  validate(validator: Validator<T>): this;
31
- /** Return a data update with a specific prop marked for update. */
32
- with<K extends DataKey<T>>(key: Nullish<K>, value: T[K] | Update<T[K]>): this;
33
- /** Iterate over the transforms in this object. */
34
33
  [Symbol.iterator](): Iterator<DataProp<Updates<T>>, void>;
35
34
  }
@@ -41,12 +41,24 @@ export class DataUpdate extends Update {
41
41
  this.updates = props;
42
42
  }
43
43
  /** Return a data update with a specific prop marked for update. */
44
- static with(key, value) {
44
+ static update(key, value) {
45
45
  return new DataUpdate(!isNullish(key) ? { [key]: value } : {});
46
46
  }
47
+ /** Return a data update with a specific prop marked for update. */
48
+ update(key, value) {
49
+ if (isNullish(key))
50
+ return this;
51
+ return {
52
+ __proto__: Object.getPrototypeOf(this),
53
+ ...this,
54
+ updates: { ...this.updates, [key]: value },
55
+ };
56
+ }
57
+ // Implement `Transformable`
47
58
  transform(data) {
48
59
  return updateData(data, this.updates);
49
60
  }
61
+ // Implement `Validatable`
50
62
  validate(validator) {
51
63
  if (!(validator instanceof DataSchema))
52
64
  return super.validate(validator);
@@ -56,17 +68,7 @@ export class DataUpdate extends Update {
56
68
  updates: validateUpdates(this.updates, validator.props),
57
69
  };
58
70
  }
59
- /** Return a data update with a specific prop marked for update. */
60
- with(key, value) {
61
- if (isNullish(key))
62
- return this;
63
- return {
64
- __proto__: Object.getPrototypeOf(this),
65
- ...this,
66
- updates: { ...this.updates, [key]: value },
67
- };
68
- }
69
- /** Iterate over the transforms in this object. */
71
+ // Implement `Iterable`
70
72
  [Symbol.iterator]() {
71
73
  return Object.entries(this.updates).values();
72
74
  }
@@ -9,21 +9,16 @@ export declare type DictionaryUpdates<T> = ImmutableDictionary<T | Update<T> | D
9
9
  /** Update that can be applied to a dictionary object to add/remove/update its entries. */
10
10
  export declare class DictionaryUpdate<T> extends Update<ImmutableDictionary<T>> implements Iterable<Entry<string, T | Update<T> | Delete>> {
11
11
  /** Return a dictionary update with a specific entry marked for update. */
12
- static with<X>(key: Nullish<string>, value: X | Update<X> | Delete): DictionaryUpdate<X>;
12
+ static update<X>(key: Nullish<string>, value: X | Update<X> | Delete): DictionaryUpdate<X>;
13
13
  /** Return a dictionary update with a specific entry marked for deletion. */
14
- static without<X>(key: Nullish<string>): DictionaryUpdate<X>;
14
+ static delete<X>(key: Nullish<string>): DictionaryUpdate<X>;
15
15
  readonly updates: DictionaryUpdates<T>;
16
16
  constructor(updates?: DictionaryUpdates<T>);
17
- transform(obj?: ImmutableDictionary<T>): ImmutableDictionary<T>;
18
- validate(validator: Validator<ImmutableDictionary<T>>): this;
19
17
  /** Return a dictionary update with a specific entry marked for update. */
20
- with(key: Nullish<string>, value: T | Update<T>): this;
18
+ update(key: Nullish<string>, value: T | Update<T>): this;
21
19
  /** Return a dictionary update with a specific entry marked for deletion. */
22
- without(key: Nullish<string>): this;
23
- /**
24
- * Iterate over the changes in this object.
25
- * - Updates are yielded first, then deletes.
26
- * - Entries whose value is `undefined` indicate deletion.
27
- */
20
+ delete(key: Nullish<string>): this;
21
+ transform(obj?: ImmutableDictionary<T>): ImmutableDictionary<T>;
22
+ validate(validator: Validator<ImmutableDictionary<T>>): this;
28
23
  [Symbol.iterator](): Iterator<Entry<string, T | Update<T> | Delete>, void>;
29
24
  }
@@ -13,27 +13,15 @@ export class DictionaryUpdate extends Update {
13
13
  this.updates = updates;
14
14
  }
15
15
  /** Return a dictionary update with a specific entry marked for update. */
16
- static with(key, value) {
16
+ static update(key, value) {
17
17
  return new DictionaryUpdate(isNullish(key) ? {} : { [key]: value });
18
18
  }
19
19
  /** Return a dictionary update with a specific entry marked for deletion. */
20
- static without(key) {
20
+ static delete(key) {
21
21
  return new DictionaryUpdate(isNullish(key) ? {} : { [key]: DELETE });
22
22
  }
23
- transform(obj = {}) {
24
- return Object.fromEntries(_transformDictionaryEntries(obj, this.updates));
25
- }
26
- validate(validator) {
27
- if (!(validator instanceof DictionarySchema))
28
- return super.validate(validator);
29
- return {
30
- __proto__: Object.getPrototypeOf(this),
31
- ...this,
32
- updates: Object.fromEntries(_validateDictionaryUpdates(this.updates, validator.items)),
33
- };
34
- }
35
23
  /** Return a dictionary update with a specific entry marked for update. */
36
- with(key, value) {
24
+ update(key, value) {
37
25
  if (isNullish(key))
38
26
  return this;
39
27
  return {
@@ -43,7 +31,7 @@ export class DictionaryUpdate extends Update {
43
31
  };
44
32
  }
45
33
  /** Return a dictionary update with a specific entry marked for deletion. */
46
- without(key) {
34
+ delete(key) {
47
35
  if (isNullish(key))
48
36
  return this;
49
37
  return {
@@ -52,11 +40,21 @@ export class DictionaryUpdate extends Update {
52
40
  updates: { ...this.updates, [key]: DELETE },
53
41
  };
54
42
  }
55
- /**
56
- * Iterate over the changes in this object.
57
- * - Updates are yielded first, then deletes.
58
- * - Entries whose value is `undefined` indicate deletion.
59
- */
43
+ // Implement `Transformable`
44
+ transform(obj = {}) {
45
+ return Object.fromEntries(_transformDictionaryEntries(obj, this.updates));
46
+ }
47
+ // Implement `Validatable`
48
+ validate(validator) {
49
+ if (!(validator instanceof DictionarySchema))
50
+ return super.validate(validator);
51
+ return {
52
+ __proto__: Object.getPrototypeOf(this),
53
+ ...this,
54
+ updates: Object.fromEntries(_validateDictionaryUpdates(this.updates, validator.items)),
55
+ };
56
+ }
57
+ // Implement `Iterable`
60
58
  *[Symbol.iterator]() {
61
59
  for (const entry of Object.entries(this.updates))
62
60
  yield entry;
@@ -8,6 +8,7 @@ export class Increment extends Update {
8
8
  super();
9
9
  this.amount = amount;
10
10
  }
11
+ // Implement `Transformable`
11
12
  transform(existing) {
12
13
  return typeof existing === "number" ? existing + this.amount : this.amount;
13
14
  }
@@ -5,8 +5,6 @@ import { Validator, Validatable } from "../util/validate.js";
5
5
  * - Implements `Transformable` for applying that update with its `transform()` method.
6
6
  */
7
7
  export declare abstract class Update<T> implements Transformable<T, T>, Validatable<Update<T>> {
8
- /** Apply this update to a value. */
9
8
  abstract transform(value?: unknown): T;
10
- /** Validate this update's values against a validator. */
11
9
  validate(validator: Validator<T>): this;
12
10
  }
package/update/Update.js CHANGED
@@ -4,7 +4,7 @@ import { validate } from "../util/validate.js";
4
4
  * - Implements `Transformable` for applying that update with its `transform()` method.
5
5
  */
6
6
  export class Update {
7
- /** Validate this update's values against a validator. */
7
+ // Implement `Validatable`
8
8
  validate(validator) {
9
9
  validate(this.transform(), validator);
10
10
  return this;
package/util/array.d.ts CHANGED
@@ -10,6 +10,8 @@ export declare type MutableArray<T = unknown> = T[];
10
10
  export declare type ImmutableArray<T = unknown> = readonly T[];
11
11
  /** Get the type of the _items_ in an array. */
12
12
  export declare type ArrayItem<T extends ImmutableArray> = T[number];
13
+ /** Things that can be converted to arrays. */
14
+ export declare type PossibleArray<T> = ImmutableArray<T> | Iterable<T>;
13
15
  /** Is an unknown value an array? */
14
16
  export declare const isArray: <T extends ImmutableArray<unknown>>(v: unknown) => v is T;
15
17
  /** Assert that a value is an array. */
@@ -17,14 +19,14 @@ export declare function assertArray<T>(arr: ImmutableArray<T> | unknown): assert
17
19
  /** Is an unknown value an item in a specified array? */
18
20
  export declare const isArrayItem: <T>(arr: ImmutableArray<T>, item: unknown) => item is T;
19
21
  export declare const isItem: <T>(arr: ImmutableArray<T>, item: unknown) => item is T;
20
- /** Things that can be converted to arrays. */
21
- export declare type PossibleArray<T> = ImmutableArray<T> | Iterable<T>;
22
22
  /** Convert an iterable to an array (if its not already an array). */
23
23
  export declare function getArray<T>(items: PossibleArray<T>): ImmutableArray<T>;
24
24
  /** Add multiple items to an array (immutably) and return a new array with those items (or the same array if no changes were made). */
25
25
  export declare function withArrayItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
26
+ /** Pick multiple items from an array (immutably) and return a new array without those items (or the same array if no changes were made). */
27
+ export declare function pickArrayItems<T>(input: ImmutableArray<T> | Iterable<T>, ...pick: T[]): ImmutableArray<T>;
26
28
  /** Remove multiple items from an array (immutably) and return a new array without those items (or the same array if no changes were made). */
27
- export declare function withoutArrayItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
29
+ export declare function omitArrayItems<T>(input: ImmutableArray<T> | Iterable<T>, ...omit: T[]): ImmutableArray<T>;
28
30
  /** 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). */
29
31
  export declare function toggleArrayItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
30
32
  /** Get the first item from an array or iterable, or `null` if it didn't exist. */
@@ -46,11 +48,13 @@ export declare function addArrayItem<T>(arr: MutableArray<T>, item: T): T;
46
48
  /** Add multiple items to an array (by reference). */
47
49
  export declare function addArrayItems<T>(arr: MutableArray<T>, ...items: T[]): void;
48
50
  /** Remove multiple items from an array (by reference). */
49
- export declare function removeArrayItems<T>(arr: MutableArray<T>, ...items: T[]): void;
51
+ export declare function deleteArrayItems<T>(arr: MutableArray<T>, ...items: T[]): void;
50
52
  /** Return an array of the unique items in an array. */
51
53
  export declare function uniqueArray<T>(input: Iterable<T>): ImmutableArray<T>;
52
54
  /** Apply a limit to an array. */
53
- export declare function limitArray<T>(arr: ImmutableArray<T>, limit: number): ImmutableArray<T>;
55
+ export declare function limitArray<T>(items: Iterable<T>, limit: number): ImmutableArray<T>;
56
+ /** Count the items in an array. */
57
+ export declare function countArray<T>(arr: ImmutableArray<T>): number;
54
58
  /** Does an array have the specified minimum length. */
55
59
  export declare function isArrayMin<T>(arr: ImmutableArray<T>, min?: 1): arr is [T, ...T[]];
56
60
  export declare function isArrayMin<T>(arr: ImmutableArray<T>, min: 2): arr is [T, T, ...T[]];
package/util/array.js CHANGED
@@ -1,5 +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
4
  /** Is an unknown value an array? */
4
5
  export const isArray = (v) => Array.isArray(v);
5
6
  /** Assert that a value is an array. */
@@ -19,14 +20,19 @@ export function withArrayItems(input, ...items) {
19
20
  const extras = items.filter(_doesNotInclude, input);
20
21
  return extras.length ? [...input, ...extras] : input;
21
22
  }
22
- /** Remove multiple items from an array (immutably) and return a new array without those items (or the same array if no changes were made). */
23
- export function withoutArrayItems(input, ...items) {
24
- const output = input.filter(_doesNotInclude, items);
25
- return output.length === input.length ? input : output;
26
- }
27
23
  function _doesNotInclude(value) {
28
24
  return !this.includes(value);
29
25
  }
26
+ /** Pick multiple items from an array (immutably) and return a new array without those items (or the same array if no changes were made). */
27
+ export function pickArrayItems(input, ...pick) {
28
+ const output = Array.from(pickItems(input, ...pick));
29
+ return isArray(input) && output.length === input.length ? input : output;
30
+ }
31
+ /** Remove multiple items from an array (immutably) and return a new array without those items (or the same array if no changes were made). */
32
+ export function omitArrayItems(input, ...omit) {
33
+ const output = Array.from(omitItems(input, ...omit));
34
+ return isArray(input) && output.length === input.length ? input : output;
35
+ }
30
36
  /** 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
37
  export function toggleArrayItems(input, ...items) {
32
38
  const extras = items.filter(_doesNotInclude, input);
@@ -102,7 +108,7 @@ export function addArrayItems(arr, ...items) {
102
108
  arr.push(item);
103
109
  }
104
110
  /** Remove multiple items from an array (by reference). */
105
- export function removeArrayItems(arr, ...items) {
111
+ export function deleteArrayItems(arr, ...items) {
106
112
  for (let i = arr.length - 1; i >= 0; i--)
107
113
  if (i in arr && items.includes(arr[i]))
108
114
  arr.splice(i, 1);
@@ -116,9 +122,14 @@ export function uniqueArray(input) {
116
122
  return output;
117
123
  }
118
124
  /** Apply a limit to an array. */
119
- export function limitArray(arr, limit) {
125
+ export function limitArray(items, limit) {
126
+ const arr = getArray(items);
120
127
  return limit > arr.length ? arr : arr.slice(0, limit);
121
128
  }
129
+ /** Count the items in an array. */
130
+ export function countArray(arr) {
131
+ return arr.length;
132
+ }
122
133
  export function isArrayMin(arr, min = 1) {
123
134
  return arr.length >= min;
124
135
  }
@@ -28,12 +28,12 @@ export declare const withDictionaryItem: <T>(input: ImmutableDictionary<T>, key:
28
28
  /** Set several props on a dictionary object (immutably) and return a new object including those props. */
29
29
  export declare const withDictionaryItems: <T>(input: ImmutableDictionary<T>, props: PossibleDictionary<T>) => ImmutableDictionary<T>;
30
30
  /** Remove several key/value entries from a dictionary object (immutably) and return a new object without those props. */
31
- export declare const withoutDictionaryItems: <T>(input: ImmutableDictionary<T>, ...keys: string[]) => ImmutableDictionary<T>;
31
+ export declare const omitDictionaryItems: <T>(input: ImmutableDictionary<T>, ...keys: string[]) => ImmutableDictionary<T>;
32
32
  /** Pick several props from a dictionary object and return a new object with only thos props. */
33
33
  export declare const pickDictionaryItems: <T>(input: ImmutableDictionary<T>, ...keys: string[]) => ImmutableDictionary<T>;
34
34
  /** Set a single named prop on a dictionary object (by reference) and return its value. */
35
- export declare const setDictionaryEntry: <T>(dict: MutableDictionary<T>, key: string, value: T) => T;
35
+ export declare const setDictionaryItem: <T>(dict: MutableDictionary<T>, key: string, value: T) => T;
36
36
  /** Set several named props on a dictionary object (by reference). */
37
- export declare const setDictionaryEntries: <T>(dict: MutableDictionary<T>, entries: PossibleDictionary<T>) => void;
37
+ export declare const setDictionaryItems: <T>(dict: MutableDictionary<T>, entries: PossibleDictionary<T>) => void;
38
38
  /** Remove several key/value entries from a dictionary object (by reference). */
39
- export declare const deleteDictionaryEntries: <T extends MutableDictionary>(dict: T, ...keys: string[]) => void;
39
+ export declare const deleteDictionaryItems: <T extends MutableDictionary>(dict: T, ...keys: string[]) => void;
@@ -22,12 +22,12 @@ export const withDictionaryItem = withProp;
22
22
  /** Set several props on a dictionary object (immutably) and return a new object including those props. */
23
23
  export const withDictionaryItems = withProps;
24
24
  /** Remove several key/value entries from a dictionary object (immutably) and return a new object without those props. */
25
- export const withoutDictionaryItems = omitProps;
25
+ export const omitDictionaryItems = omitProps;
26
26
  /** Pick several props from a dictionary object and return a new object with only thos props. */
27
27
  export const pickDictionaryItems = pickProps;
28
28
  /** Set a single named prop on a dictionary object (by reference) and return its value. */
29
- export const setDictionaryEntry = setProp;
29
+ export const setDictionaryItem = setProp;
30
30
  /** Set several named props on a dictionary object (by reference). */
31
- export const setDictionaryEntries = setProps;
31
+ export const setDictionaryItems = setProps;
32
32
  /** Remove several key/value entries from a dictionary object (by reference). */
33
- export const deleteDictionaryEntries = deleteProps;
33
+ export const deleteDictionaryItems = deleteProps;
package/util/iterate.d.ts CHANGED
@@ -1,15 +1,9 @@
1
- import type { ImmutableArray } from "./array.js";
2
1
  /**
3
2
  * Is a value an iterable object?
4
3
  * - Any object with a `Symbol.iterator` property is iterable.
5
4
  * - Note: Array and Map instances etc will return true because they implement `Symbol.iterator`
6
5
  */
7
6
  export declare const isIterable: <T extends Iterable<unknown>>(value: unknown) => value is T;
8
- /**
9
- * Count the number items of an iterable.
10
- * - Checks `items.size` or `items.length` first, or consumes the iterable and counts its iterations.
11
- */
12
- export declare function countItems(items: Iterable<unknown>): number;
13
7
  /** An iterable containing items or nested iterables of items. */
14
8
  export declare type DeepIterable<T> = T | Iterable<DeepIterable<T>>;
15
9
  /** Flatten one or more iterables. */
@@ -19,6 +13,8 @@ export declare function flattenItems<T>(items: DeepIterable<T>): Iterable<T>;
19
13
  * - Checks `items.size` or `items.length` first, or consumes the iterable and counts its iterations.
20
14
  */
21
15
  export declare function hasItems(items: Iterable<unknown>): boolean;
16
+ /** Count the number of items in an iterable. */
17
+ export declare function countItems(items: Iterable<unknown>): number;
22
18
  /**
23
19
  * Yield a range of numbers from `start` to `end`
24
20
  * - Yields in descending order if `end` is lower than `start`
@@ -29,6 +25,10 @@ export declare function getRange(start: number, end: number): Iterable<number>;
29
25
  * - Checks `items.size` or `items.length` first to see if the limit is necessary.
30
26
  */
31
27
  export declare function limitItems<T>(items: Iterable<T>, limit: number): Iterable<T>;
28
+ /** Pick items from an iterable set of items. */
29
+ export declare function pickItems<T>(items: Iterable<T>, ...pick: T[]): Iterable<T>;
30
+ /** Omit items from an iterable set of items. */
31
+ export declare function omitItems<T>(items: Iterable<T>, ...omit: T[]): Iterable<T>;
32
32
  /** Reduce an iterable set of items using a reducer function. */
33
33
  export declare function reduceItems<T>(items: Iterable<T>, reducer: (previous: T, item: T) => T, initial: T): T;
34
34
  export declare function reduceItems<T>(items: Iterable<T>, reducer: (previous: T | undefined, item: T) => T, initial?: T): T | undefined;
@@ -37,6 +37,6 @@ export declare function getChunks<T>(input: Iterable<T>, size: 1): Iterable<read
37
37
  export declare function getChunks<T>(input: Iterable<T>, size: 2): Iterable<readonly [T, T]>;
38
38
  export declare function getChunks<T>(input: Iterable<T>, size: 3): Iterable<readonly [T, T, T]>;
39
39
  export declare function getChunks<T>(input: Iterable<T>, size: 4): Iterable<readonly [T, T, T, T]>;
40
- export declare function getChunks<T>(input: Iterable<T>, size: number): Iterable<ImmutableArray<T>>;
40
+ export declare function getChunks<T>(input: Iterable<T>, size: number): Iterable<readonly T[]>;
41
41
  /** Merge two or more iterables into a single iterable set. */
42
42
  export declare function mergeItems<T>(...inputs: [Iterable<T>, Iterable<T>, ...Iterable<T>[]]): Iterable<T>;
package/util/iterate.js CHANGED
@@ -4,27 +4,6 @@
4
4
  * - Note: Array and Map instances etc will return true because they implement `Symbol.iterator`
5
5
  */
6
6
  export const isIterable = (value) => typeof value === "object" && !!value && Symbol.iterator in value;
7
- /** Get the known size or length of an object (e.g. `Array`, `Map`, and `Set` have known size), or return `undefined` if the size cannot be established. */
8
- function _getKnownSize(obj) {
9
- if ("size" in obj && typeof obj.size === "number")
10
- return obj.size;
11
- if ("length" in obj && typeof obj.length === "number")
12
- return obj.length;
13
- }
14
- /**
15
- * Count the number items of an iterable.
16
- * - Checks `items.size` or `items.length` first, or consumes the iterable and counts its iterations.
17
- */
18
- export function countItems(items) {
19
- var _a;
20
- return (_a = _getKnownSize(items)) !== null && _a !== void 0 ? _a : _countItems(items);
21
- }
22
- function _countItems(items) {
23
- let count = 0;
24
- for (const unused of items)
25
- count++;
26
- return count;
27
- }
28
7
  /** Flatten one or more iterables. */
29
8
  export function* flattenItems(items) {
30
9
  if (isIterable(items))
@@ -38,14 +17,17 @@ export function* flattenItems(items) {
38
17
  * - Checks `items.size` or `items.length` first, or consumes the iterable and counts its iterations.
39
18
  */
40
19
  export function hasItems(items) {
41
- var _a;
42
- return !!((_a = _getKnownSize(items)) !== null && _a !== void 0 ? _a : _hasItems(items));
43
- }
44
- function _hasItems(items) {
45
20
  for (const unused of items)
46
21
  return true;
47
22
  return false;
48
23
  }
24
+ /** Count the number of items in an iterable. */
25
+ export function countItems(items) {
26
+ let count = 0;
27
+ for (const unused of items)
28
+ count++;
29
+ return count;
30
+ }
49
31
  /**
50
32
  * Yield a range of numbers from `start` to `end`
51
33
  * - Yields in descending order if `end` is lower than `start`
@@ -62,22 +44,29 @@ export function* getRange(start, end) {
62
44
  * Apply a limit to an iterable set of items.
63
45
  * - Checks `items.size` or `items.length` first to see if the limit is necessary.
64
46
  */
65
- export function limitItems(items, limit) {
66
- var _a;
67
- const size = (_a = _getKnownSize(items)) !== null && _a !== void 0 ? _a : Infinity;
68
- return size <= limit ? items : _limitItems(items, limit);
69
- }
70
- function* _limitItems(source, limit) {
47
+ export function* limitItems(items, limit) {
71
48
  let count = 0;
72
49
  if (count >= limit)
73
50
  return;
74
- for (const item of source) {
51
+ for (const item of items) {
75
52
  yield item;
76
53
  count++;
77
54
  if (count >= limit)
78
- break;
55
+ return;
79
56
  }
80
57
  }
58
+ /** Pick items from an iterable set of items. */
59
+ export function* pickItems(items, ...pick) {
60
+ for (const item of items)
61
+ if (pick.includes(item))
62
+ yield item;
63
+ }
64
+ /** Omit items from an iterable set of items. */
65
+ export function* omitItems(items, ...omit) {
66
+ for (const item of items)
67
+ if (!omit.includes(item))
68
+ yield item;
69
+ }
81
70
  export function reduceItems(items, reducer, initial) {
82
71
  let current = initial;
83
72
  for (const item of items)
package/util/object.d.ts CHANGED
@@ -88,7 +88,7 @@ export declare function withProps<T extends ImmutableObject>(input: T, props: T
88
88
  /** Remove several props from an object (immutably) and return a new object without those props. */
89
89
  export declare function omitProps<T extends ImmutableObject, K extends keyof T>(input: T, ...keys: K[]): Omit<T, K>;
90
90
  /** Pick several props from an object and return a new object with only thos props. */
91
- export declare function pickProps<T extends ImmutableObject, K extends keyof T>(input: T, ...keys: K[]): Pick<T, K>;
91
+ export declare function pickProps<T extends ImmutableObject, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>;
92
92
  /** Set a single named prop on an object (by reference) and return its value. */
93
93
  export declare function setProp<T extends MutableObject, K extends keyof T>(obj: T, key: K, value: T[K]): T[K];
94
94
  /** Set several named props on an object (by reference). */
package/util/set.d.ts CHANGED
@@ -19,6 +19,6 @@ export declare function addSetItem<T>(set: MutableSet<T>, item: T): T;
19
19
  /** Add multiple items to a set (by reference). */
20
20
  export declare function addSetItems<T>(set: MutableSet<T>, ...items: T[]): void;
21
21
  /** Remove multiple items from a set (by reference). */
22
- export declare function removeSetItems<T>(set: MutableSet<T>, ...items: T[]): void;
22
+ export declare function deleteSetItems<T>(set: MutableSet<T>, ...items: T[]): void;
23
23
  /** Apply a limit to a set. */
24
24
  export declare function limitSet<T>(set: ImmutableSet<T>, limit: number): ImmutableSet<T>;
package/util/set.js CHANGED
@@ -24,7 +24,7 @@ export function addSetItems(set, ...items) {
24
24
  set.add(item);
25
25
  }
26
26
  /** Remove multiple items from a set (by reference). */
27
- export function removeSetItems(set, ...items) {
27
+ export function deleteSetItems(set, ...items) {
28
28
  for (const item of items)
29
29
  set.delete(item);
30
30
  }
@@ -24,11 +24,11 @@ export declare type Validator<T = unknown> = Validatable<T> | Validate<T>;
24
24
  export declare type ValidatorType<X> = X extends Validator<infer Y> ? Y : never;
25
25
  /** A set of named validators in `{ name: Validator }` format. */
26
26
  export declare type Validators<T extends Data = Data> = {
27
- [K in keyof T]: Validator<T[K]>;
27
+ readonly [K in keyof T]: Validator<T[K]>;
28
28
  };
29
29
  /** Extract the type from a set of validators. */
30
30
  export declare type ValidatorsType<T> = {
31
- [K in keyof T]: ValidatorType<T[K]>;
31
+ readonly [K in keyof T]: ValidatorType<T[K]>;
32
32
  };
33
33
  /** Validate an unknown value with a validator. */
34
34
  export declare function validate<T>(unsafeValue: unknown, validator: Validator<T>): T;