shelving 1.82.0 → 1.83.0

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.
Files changed (91) hide show
  1. package/constraint/Constraints.js +3 -3
  2. package/constraint/FilterConstraint.d.ts +5 -5
  3. package/constraint/SortConstraint.d.ts +2 -2
  4. package/db/Change.d.ts +12 -12
  5. package/db/Collection.d.ts +4 -4
  6. package/db/Database.d.ts +29 -29
  7. package/db/Item.d.ts +4 -4
  8. package/db/Query.d.ts +4 -4
  9. package/feedback/Feedback.d.ts +2 -2
  10. package/feedback/Feedback.js +3 -2
  11. package/firestore/client/FirestoreClientProvider.js +2 -2
  12. package/firestore/lite/FirestoreLiteProvider.js +2 -2
  13. package/firestore/server/FirestoreServerProvider.js +2 -2
  14. package/package.json +1 -1
  15. package/provider/CacheProvider.d.ts +12 -12
  16. package/provider/DebugProvider.d.ts +21 -21
  17. package/provider/MemoryProvider.d.ts +16 -16
  18. package/provider/MemoryProvider.js +3 -3
  19. package/provider/Provider.d.ts +30 -30
  20. package/provider/ThroughProvider.d.ts +23 -23
  21. package/provider/ValidationProvider.d.ts +22 -22
  22. package/provider/ValidationProvider.js +2 -2
  23. package/react/useItem.d.ts +4 -4
  24. package/react/useQuery.d.ts +4 -4
  25. package/schema/AllowSchema.d.ts +25 -24
  26. package/schema/AllowSchema.js +25 -29
  27. package/schema/ArraySchema.d.ts +2 -2
  28. package/schema/BooleanSchema.d.ts +2 -2
  29. package/schema/DataSchema.d.ts +2 -2
  30. package/schema/DataSchema.js +2 -2
  31. package/schema/DateSchema.d.ts +2 -2
  32. package/schema/DictionarySchema.d.ts +19 -0
  33. package/schema/{ObjectSchema.js → DictionarySchema.js} +6 -6
  34. package/schema/LinkSchema.d.ts +2 -2
  35. package/schema/NumberSchema.d.ts +2 -2
  36. package/schema/Schema.d.ts +10 -8
  37. package/schema/Schema.js +1 -1
  38. package/schema/StringSchema.d.ts +12 -10
  39. package/schema/index.d.ts +1 -1
  40. package/schema/index.js +1 -1
  41. package/state/ArrayState.js +4 -4
  42. package/state/DataState.js +3 -3
  43. package/state/DictionaryState.d.ts +15 -0
  44. package/state/{ObjectState.js → DictionaryState.js} +6 -6
  45. package/state/index.d.ts +2 -2
  46. package/state/index.js +2 -2
  47. package/test/basics.js +1 -1
  48. package/update/ArrayUpdate.js +2 -2
  49. package/update/DataUpdate.d.ts +10 -9
  50. package/update/DataUpdate.js +6 -4
  51. package/update/DictionaryUpdate.d.ts +29 -0
  52. package/update/{ObjectUpdate.js → DictionaryUpdate.js} +14 -14
  53. package/update/hydrations.js +2 -2
  54. package/update/index.d.ts +1 -1
  55. package/update/index.js +1 -1
  56. package/util/array.d.ts +40 -111
  57. package/util/array.js +50 -114
  58. package/util/clone.d.ts +2 -2
  59. package/util/clone.js +5 -5
  60. package/util/data.d.ts +20 -53
  61. package/util/data.js +11 -1
  62. package/util/dictionary.d.ts +39 -0
  63. package/util/dictionary.js +33 -0
  64. package/util/diff.d.ts +3 -3
  65. package/util/entry.d.ts +7 -5
  66. package/util/entry.js +8 -6
  67. package/util/filter.d.ts +0 -5
  68. package/util/filter.js +0 -3
  69. package/util/hydrate.d.ts +2 -2
  70. package/util/hydrate.js +7 -8
  71. package/util/index.d.ts +1 -0
  72. package/util/index.js +1 -0
  73. package/util/map.d.ts +28 -6
  74. package/util/map.js +22 -6
  75. package/util/merge.d.ts +3 -6
  76. package/util/merge.js +4 -6
  77. package/util/object.d.ts +77 -90
  78. package/util/object.js +23 -76
  79. package/util/set.d.ts +10 -0
  80. package/util/set.js +17 -0
  81. package/util/template.d.ts +3 -3
  82. package/util/template.js +3 -3
  83. package/util/transform.d.ts +22 -45
  84. package/util/transform.js +20 -37
  85. package/util/units.d.ts +6 -12
  86. package/util/units.js +8 -4
  87. package/util/validate.d.ts +6 -5
  88. package/util/validate.js +5 -4
  89. package/schema/ObjectSchema.d.ts +0 -19
  90. package/state/ObjectState.d.ts +0 -16
  91. package/update/ObjectUpdate.d.ts +0 -29
package/util/array.d.ts CHANGED
@@ -8,135 +8,64 @@ export declare type MutableArray<T = unknown> = T[];
8
8
  * - Consistency with `ImmutableObject<T>` and `MutableArray<T>`
9
9
  */
10
10
  export declare type ImmutableArray<T = unknown> = readonly T[];
11
- /**
12
- * Array type: extract the type for the items of an array or readonly array.
13
- * - Consistency with builtin `ReturnType<T>` and `ObjectType<T>`
14
- */
15
- export declare type ArrayType<T extends ImmutableArray> = T[number];
11
+ /** Get the type of the _items_ in an array. */
12
+ export declare type ArrayItem<T extends ImmutableArray> = T[number];
16
13
  /** Is an unknown value an array? */
17
14
  export declare const isArray: <T extends ImmutableArray<unknown>>(v: unknown) => v is T;
18
15
  /** Assert that a value is an array. */
19
16
  export declare function assertArray<T>(arr: ImmutableArray<T> | unknown): asserts arr is ImmutableArray<T>;
20
17
  /** Is an unknown value an item in a specified array? */
18
+ export declare const isArrayItem: <T>(arr: ImmutableArray<T>, item: unknown) => item is T;
21
19
  export declare const isItem: <T>(arr: ImmutableArray<T>, item: unknown) => item is T;
22
20
  /** Things that can be converted to arrays. */
23
21
  export declare type PossibleArray<T> = ImmutableArray<T> | Iterable<T>;
24
22
  /** Convert an iterable to an array (if its not already an array). */
25
23
  export declare function getArray<T>(items: PossibleArray<T>): ImmutableArray<T>;
26
- /**
27
- * Add multiple items to an array (immutably).
28
- * - Returns an array that definitely contains the specified item.
29
- *
30
- * @param input The input array to add items to.
31
- * @param items The array of items to add.
32
- *
33
- * @return New array with the specified item (or same array if no items were added).
34
- */
35
- export declare function withItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
36
- /**
37
- * Remove multiple items from an array (immutably).
38
- * - Finds all instances of a number of items from an array and returns an array that definitely does not contain them.
39
- *
40
- * @param input The input array to remove items from.
41
- * @param items The array of items to add.
42
- *
43
- * @return New array without the specified items (or same array if no items were removed).
44
- */
45
- export declare function withoutItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
46
- /**
47
- * Toggle an item in and out of an array (immutably).
48
- * - If an item is being removed from the array ALL instances of that item in the array will be removed.
49
- *
50
- * @param input The input array to toggle items from.
51
- * @param items The array of items to toggle.
52
- *
53
- * @return New array with or without the specified items (or same array if no items were toggled).
54
- */
55
- export declare function toggleItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
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
+ export declare function withArrayItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
26
+ /** 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>;
28
+ /** 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
+ export declare function toggleArrayItems<T>(input: ImmutableArray<T>, ...items: T[]): ImmutableArray<T>;
56
30
  /** Get the first item from an array or iterable, or `null` if it didn't exist. */
57
- export declare function getOptionalFirstItem<T>(items: ImmutableArray<T> | Iterable<T>): T | null;
31
+ export declare function getOptionalFirstItem<T>(items: PossibleArray<T>): T | null;
58
32
  /** Get the first item from an array or iterable. */
59
- export declare function getFirstItem<T>(items: ImmutableArray<T> | Iterable<T>): T;
33
+ export declare function getFirstItem<T>(items: PossibleArray<T>): T;
60
34
  /** Get the last item from an array or iterable, or `null` if it didn't exist. */
61
- export declare function getOptionalLastItem<T>(items: ImmutableArray<T> | Iterable<T>): T | null;
35
+ export declare function getOptionalLastItem<T>(items: PossibleArray<T>): T | null;
62
36
  /** Get the last item from an array or iterable. */
63
- export declare function getLastItem<T>(items: ImmutableArray<T> | Iterable<T>): T;
64
- /**
65
- * Get the next item in an array.
66
- *
67
- * @param arr The target array.
68
- * @param value The value of the target item.
69
- * @return The item after the target item, or `undefined` if that item does not exist in the array.
70
- */
71
- export declare function getNextItem<T>(arr: ImmutableArray<T>, value: T): T | undefined;
72
- /**
73
- * Get the previous item in an array.
74
- *
75
- * @param arr The target array.
76
- * @param value The value of the target item.
77
- *
78
- * @return The item before the target item, or `undefined` if that item does not exist in the array.
79
- */
80
- export declare function getPrevItem<T>(arr: ImmutableArray<T>, value: T): T | undefined;
81
- /**
82
- * Return a shuffled version of an array or iterable.
83
- * - Uses Fisher Yates algorithm.
84
- *
85
- * @param input The input array or iterable to shuffle.
86
- * @return Copy of in a random order.
87
- */
88
- export declare function shuffleArray<T>(input: Iterable<T>): ImmutableArray<T>;
89
- /**
90
- * Add an item to an array (by reference).
91
- * - If the item already exists in the array (using `indexOf()`) then it won't be added again.
92
- *
93
- * @param arr The target array to add items to.
94
- * @param item The item to add.
95
- */
96
- export declare function addItem<T>(arr: MutableArray<T>, item: T): T;
97
- /**
98
- * Add multiple items to an array (by reference).
99
- * - If an item already exists in the array (using `indexOf()`) then it won't be added again.
100
- *
101
- * @param arr The target array to add items to.
102
- * @param items The array of items to add.
103
- */
104
- export declare function addItems<T>(arr: MutableArray<T>, items: Iterable<T>): void;
105
- /**
106
- * Remove an item from an array (by reference).
107
- * - Deletes all instances of an item from an array by reference, and returns void.
108
- *
109
- * @param arr The target array to remove items from.
110
- * @param item The item to remove.
111
- */
112
- export declare function removeItem<T>(arr: MutableArray<T>, item: T): void;
113
- /**
114
- * Remove multiple items from an array (by reference).
115
- * - Deletes all instances of an item from an array by reference, and returns void.
116
- *
117
- * @param arr The target array to remove items from.
118
- * @param items The array of items to remove.
119
- */
120
- export declare function removeItems<T>(arr: MutableArray<T>, items: Iterable<T>): void;
37
+ export declare function getLastItem<T>(items: PossibleArray<T>): T;
38
+ /** Get the next item in an array or iterable. */
39
+ export declare function getNextItem<T>(items: PossibleArray<T>, value: T): T | null;
40
+ /** Get the previous item in an array or iterable. */
41
+ export declare function getPrevItem<T>(items: PossibleArray<T>, value: T): T | null;
42
+ /** Return a shuffled version of an array or iterable. */
43
+ export declare function shuffleArray<T>(input: PossibleArray<T>): ImmutableArray<T>;
44
+ /** Add an item to an array (by reference) and return the set item. */
45
+ export declare function addArrayItem<T>(arr: MutableArray<T>, item: T): T;
46
+ /** Add multiple items to an array (by reference). */
47
+ export declare function addArrayItems<T>(arr: MutableArray<T>, ...items: T[]): void;
48
+ /** Remove multiple items from an array (by reference). */
49
+ export declare function removeArrayItems<T>(arr: MutableArray<T>, ...items: T[]): void;
121
50
  /** Return an array of the unique items in an array. */
122
51
  export declare function uniqueArray<T>(input: Iterable<T>): ImmutableArray<T>;
123
52
  /** Apply a limit to an array. */
124
53
  export declare function limitArray<T>(arr: ImmutableArray<T>, limit: number): ImmutableArray<T>;
125
54
  /** Does an array have the specified minimum length. */
126
- export declare function isMinLength<T>(arr: ImmutableArray<T>, min?: 1): arr is [T, ...T[]];
127
- export declare function isMinLength<T>(arr: ImmutableArray<T>, min: 2): arr is [T, T, ...T[]];
128
- export declare function isMinLength<T>(arr: ImmutableArray<T>, min: 3): arr is [T, T, T, ...T[]];
129
- export declare function isMinLength<T>(arr: ImmutableArray<T>, min: 4): arr is [T, T, T, T, ...T[]];
130
- export declare function isMinLength<T>(arr: ImmutableArray<T>, min: number): boolean;
55
+ export declare function isArrayMin<T>(arr: ImmutableArray<T>, min?: 1): arr is [T, ...T[]];
56
+ export declare function isArrayMin<T>(arr: ImmutableArray<T>, min: 2): arr is [T, T, ...T[]];
57
+ export declare function isArrayMin<T>(arr: ImmutableArray<T>, min: 3): arr is [T, T, T, ...T[]];
58
+ export declare function isArrayMin<T>(arr: ImmutableArray<T>, min: 4): arr is [T, T, T, T, ...T[]];
59
+ export declare function isArrayMin<T>(arr: ImmutableArray<T>, min: number): boolean;
131
60
  /** Assert that a value has a specific length (or length is in a specific range). */
132
- export declare function assertMinLength<T>(arr: ImmutableArray<T> | unknown, min?: 1): asserts arr is [T, ...T[]];
133
- export declare function assertMinLength<T>(arr: ImmutableArray<T> | unknown, min: 2): asserts arr is [T, T, ...T[]];
134
- export declare function assertMinLength<T>(arr: ImmutableArray<T> | unknown, min: 3): asserts arr is [T, T, T, ...T[]];
135
- export declare function assertMinLength<T>(arr: ImmutableArray<T> | unknown, min: 4): asserts arr is [T, T, T, T, ...T[]];
136
- export declare function assertMinLength<T>(arr: ImmutableArray<T> | unknown, min: number): asserts arr is ImmutableArray<T>;
61
+ export declare function assertArrayMin<T>(arr: ImmutableArray<T> | unknown, min?: 1): asserts arr is [T, ...T[]];
62
+ export declare function assertArrayMin<T>(arr: ImmutableArray<T> | unknown, min: 2): asserts arr is [T, T, ...T[]];
63
+ export declare function assertArrayMin<T>(arr: ImmutableArray<T> | unknown, min: 3): asserts arr is [T, T, T, ...T[]];
64
+ export declare function assertArrayMin<T>(arr: ImmutableArray<T> | unknown, min: 4): asserts arr is [T, T, T, T, ...T[]];
65
+ export declare function assertArrayMin<T>(arr: ImmutableArray<T> | unknown, min: number): asserts arr is ImmutableArray<T>;
137
66
  /** Get an array if it has the specified minimum length. */
138
- export declare function getMinLength<T>(arr: ImmutableArray<T>, min?: 1): [T, ...T[]];
139
- export declare function getMinLength<T>(arr: ImmutableArray<T>, min: 2): [T, T, ...T[]];
140
- export declare function getMinLength<T>(arr: ImmutableArray<T>, min: 3): [T, T, T, ...T[]];
141
- export declare function getMinLength<T>(arr: ImmutableArray<T>, min: 4): [T, T, T, T, ...T[]];
142
- export declare function getMinLength<T>(arr: ImmutableArray<T>, min: number): ImmutableArray<T>;
67
+ export declare function getArrayMin<T>(arr: ImmutableArray<T>, min?: 1): [T, ...T[]];
68
+ export declare function getArrayMin<T>(arr: ImmutableArray<T>, min: 2): [T, T, ...T[]];
69
+ export declare function getArrayMin<T>(arr: ImmutableArray<T>, min: 3): [T, T, T, ...T[]];
70
+ export declare function getArrayMin<T>(arr: ImmutableArray<T>, min: 4): [T, T, T, T, ...T[]];
71
+ export declare function getArrayMin<T>(arr: ImmutableArray<T>, min: number): ImmutableArray<T>;
package/util/array.js CHANGED
@@ -8,61 +8,35 @@ export function assertArray(arr) {
8
8
  throw new AssertionError(`Must be array`, arr);
9
9
  }
10
10
  /** Is an unknown value an item in a specified array? */
11
- export const isItem = (arr, item) => arr.includes(item);
11
+ export const isArrayItem = (arr, item) => arr.includes(item);
12
+ export const isItem = isArrayItem;
12
13
  /** Convert an iterable to an array (if its not already an array). */
13
14
  export function getArray(items) {
14
15
  return isArray(items) ? items : Array.from(items);
15
16
  }
16
- /**
17
- * Add multiple items to an array (immutably).
18
- * - Returns an array that definitely contains the specified item.
19
- *
20
- * @param input The input array to add items to.
21
- * @param items The array of items to add.
22
- *
23
- * @return New array with the specified item (or same array if no items were added).
24
- */
25
- export function withItems(input, ...items) {
17
+ /** Add multiple items to an array (immutably) and return a new array with those items (or the same array if no changes were made). */
18
+ export function withArrayItems(input, ...items) {
26
19
  const extras = items.filter(_doesNotInclude, input);
27
20
  return extras.length ? [...input, ...extras] : input;
28
21
  }
29
- /**
30
- * Remove multiple items from an array (immutably).
31
- * - Finds all instances of a number of items from an array and returns an array that definitely does not contain them.
32
- *
33
- * @param input The input array to remove items from.
34
- * @param items The array of items to add.
35
- *
36
- * @return New array without the specified items (or same array if no items were removed).
37
- */
38
- export function withoutItems(input, ...items) {
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) {
39
24
  const output = input.filter(_doesNotInclude, items);
40
25
  return output.length === input.length ? input : output;
41
26
  }
42
27
  function _doesNotInclude(value) {
43
28
  return !this.includes(value);
44
29
  }
45
- /**
46
- * Toggle an item in and out of an array (immutably).
47
- * - If an item is being removed from the array ALL instances of that item in the array will be removed.
48
- *
49
- * @param input The input array to toggle items from.
50
- * @param items The array of items to toggle.
51
- *
52
- * @return New array with or without the specified items (or same array if no items were toggled).
53
- */
54
- export function toggleItems(input, ...items) {
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). */
31
+ export function toggleArrayItems(input, ...items) {
55
32
  const extras = items.filter(_doesNotInclude, input);
56
33
  const output = input.filter(_doesNotInclude, items);
57
34
  return extras.length ? [...output, ...extras] : output.length !== input.length ? output : input;
58
35
  }
59
36
  /** Get the first item from an array or iterable, or `null` if it didn't exist. */
60
37
  export function getOptionalFirstItem(items) {
61
- var _a;
62
- if (isArray(items))
63
- return (_a = items[1]) !== null && _a !== void 0 ? _a : null;
64
- const [item] = items;
65
- return item !== null && item !== void 0 ? item : null;
38
+ const arr = getArray(items);
39
+ return 0 in arr ? arr[0] : null;
66
40
  }
67
41
  /** Get the first item from an array or iterable. */
68
42
  export function getFirstItem(items) {
@@ -73,9 +47,9 @@ export function getFirstItem(items) {
73
47
  }
74
48
  /** Get the last item from an array or iterable, or `null` if it didn't exist. */
75
49
  export function getOptionalLastItem(items) {
76
- var _a;
77
50
  const arr = getArray(items);
78
- return (_a = arr[arr.length - 1]) !== null && _a !== void 0 ? _a : null;
51
+ const j = arr.length - 1;
52
+ return j in arr ? arr[j] : null;
79
53
  }
80
54
  /** Get the last item from an array or iterable. */
81
55
  export function getLastItem(items) {
@@ -84,40 +58,29 @@ export function getLastItem(items) {
84
58
  throw new RequiredError("Last item is required");
85
59
  return item;
86
60
  }
87
- /**
88
- * Get the next item in an array.
89
- *
90
- * @param arr The target array.
91
- * @param value The value of the target item.
92
- * @return The item after the target item, or `undefined` if that item does not exist in the array.
93
- */
94
- export function getNextItem(arr, value) {
61
+ /** Get the next item in an array or iterable. */
62
+ export function getNextItem(items, value) {
63
+ const arr = getArray(items);
95
64
  const i = arr.indexOf(value);
96
- if (i >= 0)
97
- return arr[i + 1];
98
- return undefined;
99
- }
100
- /**
101
- * Get the previous item in an array.
102
- *
103
- * @param arr The target array.
104
- * @param value The value of the target item.
105
- *
106
- * @return The item before the target item, or `undefined` if that item does not exist in the array.
107
- */
108
- export function getPrevItem(arr, value) {
65
+ if (i >= 0) {
66
+ const j = i + 1;
67
+ if (j in arr)
68
+ return arr[j];
69
+ }
70
+ return null;
71
+ }
72
+ /** Get the previous item in an array or iterable. */
73
+ export function getPrevItem(items, value) {
74
+ const arr = getArray(items);
109
75
  const i = arr.indexOf(value);
110
- if (i >= 1)
111
- return arr[i - 1];
112
- return undefined;
113
- }
114
- /**
115
- * Return a shuffled version of an array or iterable.
116
- * - Uses Fisher Yates algorithm.
117
- *
118
- * @param input The input array or iterable to shuffle.
119
- * @return Copy of in a random order.
120
- */
76
+ if (i >= 1) {
77
+ const j = i - 1;
78
+ if (j in arr)
79
+ return arr[j];
80
+ }
81
+ return null;
82
+ }
83
+ /** Return a shuffled version of an array or iterable. */
121
84
  export function shuffleArray(input) {
122
85
  const output = Array.from(input);
123
86
  for (let i = output.length - 1; i > 0; i--) {
@@ -126,50 +89,23 @@ export function shuffleArray(input) {
126
89
  }
127
90
  return output;
128
91
  }
129
- /**
130
- * Add an item to an array (by reference).
131
- * - If the item already exists in the array (using `indexOf()`) then it won't be added again.
132
- *
133
- * @param arr The target array to add items to.
134
- * @param item The item to add.
135
- */
136
- export function addItem(arr, item) {
92
+ /** Add an item to an array (by reference) and return the set item. */
93
+ export function addArrayItem(arr, item) {
137
94
  if (arr.indexOf(item) < 0)
138
95
  arr.push(item);
139
96
  return item;
140
97
  }
141
- /**
142
- * Add multiple items to an array (by reference).
143
- * - If an item already exists in the array (using `indexOf()`) then it won't be added again.
144
- *
145
- * @param arr The target array to add items to.
146
- * @param items The array of items to add.
147
- */
148
- export function addItems(arr, items) {
98
+ /** Add multiple items to an array (by reference). */
99
+ export function addArrayItems(arr, ...items) {
149
100
  for (const item of items)
150
- addItem(arr, item);
151
- }
152
- /**
153
- * Remove an item from an array (by reference).
154
- * - Deletes all instances of an item from an array by reference, and returns void.
155
- *
156
- * @param arr The target array to remove items from.
157
- * @param item The item to remove.
158
- */
159
- export function removeItem(arr, item) {
160
- for (let i = arr.indexOf(item); i >= 0; i = arr.indexOf(item, i))
161
- arr.splice(i, 1);
162
- }
163
- /**
164
- * Remove multiple items from an array (by reference).
165
- * - Deletes all instances of an item from an array by reference, and returns void.
166
- *
167
- * @param arr The target array to remove items from.
168
- * @param items The array of items to remove.
169
- */
170
- export function removeItems(arr, items) {
171
- for (const item of items)
172
- removeItem(arr, item);
101
+ if (arr.indexOf(item) < 0)
102
+ arr.push(item);
103
+ }
104
+ /** Remove multiple items from an array (by reference). */
105
+ export function removeArrayItems(arr, ...items) {
106
+ for (let i = arr.length - 1; i >= 0; i--)
107
+ if (i in arr && items.includes(arr[i]))
108
+ arr.splice(i, 1);
173
109
  }
174
110
  /** Return an array of the unique items in an array. */
175
111
  export function uniqueArray(input) {
@@ -183,14 +119,14 @@ export function uniqueArray(input) {
183
119
  export function limitArray(arr, limit) {
184
120
  return limit > arr.length ? arr : arr.slice(0, limit);
185
121
  }
186
- export function isMinLength(arr, min = 1) {
122
+ export function isArrayMin(arr, min = 1) {
187
123
  return arr.length >= min;
188
124
  }
189
- export function assertMinLength(arr, min = 1) {
190
- if (isArray(arr) && arr.length < min)
125
+ export function assertArrayMin(arr, min = 1) {
126
+ if (!isArray(arr) || !isArrayMin(arr, min))
191
127
  throw new AssertionError(`Must be array with minimum length ${min}`, arr);
192
128
  }
193
- export function getMinLength(arr, min = 1) {
194
- assertMinLength(arr, min);
129
+ export function getArrayMin(arr, min = 1) {
130
+ assertArrayMin(arr, min);
195
131
  return arr;
196
132
  }
package/util/clone.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { Data } from "./data.js";
2
1
  import { ImmutableArray } from "./array.js";
2
+ import { ImmutableObject } from "./object.js";
3
3
  /** Cloneable object implement a `clone()` function that returns a cloned copy. */
4
4
  export interface Cloneable {
5
5
  clone(): this;
@@ -13,4 +13,4 @@ export declare function deepClone<T>(value: T, recursor?: typeof deepClone): T;
13
13
  /** Clone an array. */
14
14
  export declare function cloneArray<T extends ImmutableArray>(input: T, recursor?: <T_1>(value: T_1) => T_1): T;
15
15
  /** Clone an object. */
16
- export declare function cloneObject<T extends Data>(input: T, recursor?: <T_1>(value: T_1) => T_1): T;
16
+ export declare function cloneObject<T extends ImmutableObject>(input: T, recursor?: <T_1>(value: T_1) => T_1): T;
package/util/clone.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { isArray } from "./array.js";
2
- import { isObject } from "./object.js";
3
- import { mapArray, mapData } from "./transform.js";
2
+ import { isData } from "./data.js";
3
+ import { mapArray, mapObject } from "./transform.js";
4
4
  /** Does an object implement `Cloneable` */
5
- export const isCloneable = (v) => isObject(v) && typeof v.cloneable === "function";
5
+ export const isCloneable = (v) => isData(v) && typeof v.cloneable === "function";
6
6
  /** Shallow clone a value. */
7
7
  export const shallowClone = (value) => value;
8
8
  /** Deep clone a value. */
@@ -11,7 +11,7 @@ export function deepClone(value, recursor = deepClone) {
11
11
  return value.clone();
12
12
  if (isArray(value))
13
13
  return cloneArray(value, recursor);
14
- if (isObject(value))
14
+ if (isData(value))
15
15
  return cloneObject(value, recursor);
16
16
  return value;
17
17
  }
@@ -27,7 +27,7 @@ export function cloneArray(input, recursor = shallowClone) {
27
27
  export function cloneObject(input, recursor = shallowClone) {
28
28
  if (isCloneable(input))
29
29
  return input.clone();
30
- const output = mapData(input, recursor);
30
+ const output = mapObject(input, recursor);
31
31
  Object.setPrototypeOf(input, Object.getPrototypeOf(input));
32
32
  return output;
33
33
  }
package/util/data.d.ts CHANGED
@@ -1,70 +1,29 @@
1
+ import type { ImmutableArray } from "./array.js";
1
2
  /** Data object. */
2
3
  export declare type Data = {
3
4
  readonly [K in string]: unknown;
4
5
  };
5
- /** Key from a data object. */
6
- export declare type Key<T extends Data> = keyof T & string;
7
- /** Value from a data object. */
8
- export declare type Value<T extends Data> = T[keyof T & string];
9
- /** Prop from a data object in entry format. */
10
- export declare type Prop<T extends Data> = readonly [Key<T>, Value<T>];
6
+ /** Prop for a data object. */
7
+ export declare type DataProp<T extends Data> = readonly [keyof T & string, T[keyof T & string]];
8
+ /** Key for a data object prop. */
9
+ export declare type DataKey<T extends Data> = keyof T & string;
10
+ /** Value for a data object prop. */
11
+ export declare type DataValue<T extends Data> = T[keyof T & string];
11
12
  /** Data or `null` to indicate the data doesn't exist. */
12
13
  export declare type OptionalData<T extends Data> = T | null;
13
- /** Empty data object. */
14
- export declare type EmptyData = {
15
- readonly [K in never]: never;
16
- };
17
14
  /** Set of named data objects. */
18
15
  export declare type Datas = {
19
16
  readonly [K in string]: Data;
20
17
  };
21
18
  /** Is an unknown value a data object? */
22
19
  export declare const isData: <T extends Data>(value: unknown) => value is T;
20
+ /** Is an unknown value the key for an own prop of a data object. */
21
+ export declare const isDataKey: <T extends Data>(obj: T, key: unknown) => key is DataKey<T>;
23
22
  /** Get the data of a result (returns data or throws `RequiredError` if value is `null` or `undefined`). */
24
23
  export declare function getData<T extends Data>(result: OptionalData<T>): T;
25
- /**
26
- * Mutable type is the opposite of `Readonly<T>` helper type.
27
- * - See https://github.com/microsoft/TypeScript/issues/24509
28
- * - Consistency with `Readonly<T>`
29
- */
30
- export declare type Mutable<T> = {
31
- -readonly [K in keyof T]: T[K];
32
- };
33
- /**
34
- * Deep partial object: deeply convert an object to its partial version.
35
- * - Any value that extends `Data` has its props made partial.
36
- * - Works deeply on nested objects too.
37
- * - Only works for plain objects (i.e. objects that extend `Data`), not arrays and functions.
38
- */
39
- export declare type DeepPartial<T extends Data> = {
40
- [K in keyof T]?: T[K] extends Data ? DeepPartial<T[K]> : T[K];
41
- };
42
- /**
43
- * Deep mutable object: deeply convert an object to its mutable version.
44
- * - Any value that extends `Data` has its props made mutable.
45
- * - Works deeply on nested objects too.
46
- * - Only works for plain objects (i.e. objects that extend `Data`), not arrays and functions.
47
- */
48
- export declare type DeepMutable<T extends Data> = {
49
- -readonly [K in keyof T]: T[K] extends Data ? DeepMutable<T[K]> : T[K];
50
- };
51
- /**
52
- * Deep readonly object: deeply convert an object to its readonly version.
53
- * - Any value that extends `Data` has its props made readonly.
54
- * - Works deeply on nested objects too.
55
- * - Only works for plain objects (i.e. objects that extend `Data`), not arrays and functions.
56
- */
57
- export declare type DeepReadonly<T extends Data> = {
58
- +readonly [K in keyof T]: T[K] extends Data ? DeepReadonly<T[K]> : T[K];
59
- };
60
- /** Pick only the properties of an object that match a type. */
61
- export declare type PickProps<T, TT> = Pick<T, {
62
- [K in keyof T]: T[K] extends TT ? K : never;
63
- }[keyof T]>;
64
- /** Omit the properties of an object that match a type. */
65
- export declare type OmitProps<T, TT> = Omit<T, {
66
- [K in keyof T]: T[K] extends TT ? K : never;
67
- }[keyof T]>;
24
+ /** Get the props of a data object. */
25
+ export declare function getDataProps<T extends Data>(data: T): ImmutableArray<DataProp<T>>;
26
+ export declare function getDataProps<T extends Data>(data: T | Partial<T>): ImmutableArray<DataProp<T>>;
68
27
  /**
69
28
  * Format a data object as a string.
70
29
  * - Use the custom `.toString()` function if it exists (don't use built in `Object.prototype.toString` because it's useless.
@@ -72,3 +31,11 @@ export declare type OmitProps<T, TT> = Omit<T, {
72
31
  * - Use `Object` otherwise.
73
32
  */
74
33
  export declare function formatData(data: Data): string;
34
+ /** Type that represents an empty data object. */
35
+ export declare type EmptyData = {
36
+ readonly [K in never]: never;
37
+ };
38
+ /** An empty object. */
39
+ export declare const EMPTY_DATA: EmptyData;
40
+ /** Function that returns an an empty object. */
41
+ export declare const getEmptyData: () => EmptyData;
package/util/data.js CHANGED
@@ -1,12 +1,18 @@
1
1
  import { RequiredError } from "../error/RequiredError.js";
2
+ import { isPlainObject } from "./object.js";
2
3
  /** Is an unknown value a data object? */
3
- export const isData = (value) => typeof value === "object" && value !== null;
4
+ export const isData = (value) => isPlainObject(value);
5
+ /** Is an unknown value the key for an own prop of a data object. */
6
+ export const isDataKey = (obj, key) => typeof key === "string" && Object.prototype.hasOwnProperty.call(obj, key);
4
7
  /** Get the data of a result (returns data or throws `RequiredError` if value is `null` or `undefined`). */
5
8
  export function getData(result) {
6
9
  if (!result)
7
10
  throw new RequiredError("Data is required");
8
11
  return result;
9
12
  }
13
+ export function getDataProps(data) {
14
+ return Object.entries(data);
15
+ }
10
16
  /**
11
17
  * Format a data object as a string.
12
18
  * - Use the custom `.toString()` function if it exists (don't use built in `Object.prototype.toString` because it's useless.
@@ -25,3 +31,7 @@ export function formatData(data) {
25
31
  return id;
26
32
  return "Object";
27
33
  }
34
+ /** An empty object. */
35
+ export const EMPTY_DATA = {};
36
+ /** Function that returns an an empty object. */
37
+ export const getEmptyData = () => EMPTY_DATA;
@@ -0,0 +1,39 @@
1
+ /** Readonly dictionary object. */
2
+ export declare type ImmutableDictionary<T = unknown> = {
3
+ readonly [K in string]: T;
4
+ };
5
+ /** Writable dictionary object. */
6
+ export declare type MutableDictionary<T = unknown> = {
7
+ [K in string]: T;
8
+ };
9
+ /** Get the type for an item of a dictionary object in entry format. */
10
+ export declare type DictionaryItem<T> = readonly [string, T];
11
+ /** Get the type of the _values_ of the items of a dictionary object. */
12
+ export declare type DictionaryValue<T extends ImmutableDictionary> = T[string];
13
+ /** Something that can be converted to a dictionary object. */
14
+ export declare type PossibleDictionary<T> = ImmutableDictionary<T> | Iterable<DictionaryItem<T>>;
15
+ /** Is an unknown value a dictionary object? */
16
+ export declare const isDictionary: <T extends ImmutableDictionary<T>>(value: unknown) => value is T;
17
+ /** Assert that an unknown value is a dictionary object */
18
+ export declare function assertDictionary<T>(value: ImmutableDictionary<T> | unknown): asserts value is ImmutableDictionary<T>;
19
+ /** Is an unknown value the key for an own prop of an dictionary. */
20
+ export declare const isDictionaryKey: <T>(obj: ImmutableDictionary<T>, key: unknown) => key is string;
21
+ /** turn a possible dictionary into an dictionary. */
22
+ export declare function getDictionary<T>(obj: PossibleDictionary<T>): ImmutableDictionary<T>;
23
+ /** Turn a dictionary object into a set of props. */
24
+ export declare function getDictionaryItems<T>(obj: ImmutableDictionary<T>): readonly DictionaryItem<T>[];
25
+ export declare function getDictionaryItems<T>(obj: PossibleDictionary<T>): Iterable<DictionaryItem<T>>;
26
+ /** Set a prop on a dictionary object (immutably) and return a new object including that prop. */
27
+ export declare const withDictionaryItem: <T>(input: ImmutableDictionary<T>, key: string, value: T) => ImmutableDictionary<T>;
28
+ /** Set several props on a dictionary object (immutably) and return a new object including those props. */
29
+ export declare const withDictionaryItems: <T>(input: ImmutableDictionary<T>, props: PossibleDictionary<T>) => ImmutableDictionary<T>;
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>;
32
+ /** Pick several props from a dictionary object and return a new object with only thos props. */
33
+ export declare const pickDictionaryItems: <T>(input: ImmutableDictionary<T>, ...keys: string[]) => ImmutableDictionary<T>;
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;
36
+ /** Set several named props on a dictionary object (by reference). */
37
+ export declare const setDictionaryEntries: <T>(dict: MutableDictionary<T>, entries: PossibleDictionary<T>) => void;
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;
@@ -0,0 +1,33 @@
1
+ import { AssertionError } from "../error/AssertionError.js";
2
+ import { isIterable } from "./iterate.js";
3
+ import { deleteProps, isPlainObject, omitProps, pickProps, setProp, setProps, withProp, withProps } from "./object.js";
4
+ /** Is an unknown value a dictionary object? */
5
+ export const isDictionary = (value) => isPlainObject(value);
6
+ /** Assert that an unknown value is a dictionary object */
7
+ export function assertDictionary(value) {
8
+ if (!isDictionary(value))
9
+ throw new AssertionError(`Must be dictionary`, value);
10
+ }
11
+ /** Is an unknown value the key for an own prop of an dictionary. */
12
+ export const isDictionaryKey = (obj, key) => typeof key === "string" && Object.prototype.hasOwnProperty.call(obj, key);
13
+ /** turn a possible dictionary into an dictionary. */
14
+ export function getDictionary(obj) {
15
+ return isIterable(obj) ? Object.fromEntries(obj) : obj;
16
+ }
17
+ export function getDictionaryItems(obj) {
18
+ return isIterable(obj) ? obj : Object.entries(obj);
19
+ }
20
+ /** Set a prop on a dictionary object (immutably) and return a new object including that prop. */
21
+ export const withDictionaryItem = withProp;
22
+ /** Set several props on a dictionary object (immutably) and return a new object including those props. */
23
+ export const withDictionaryItems = withProps;
24
+ /** Remove several key/value entries from a dictionary object (immutably) and return a new object without those props. */
25
+ export const withoutDictionaryItems = omitProps;
26
+ /** Pick several props from a dictionary object and return a new object with only thos props. */
27
+ export const pickDictionaryItems = pickProps;
28
+ /** Set a single named prop on a dictionary object (by reference) and return its value. */
29
+ export const setDictionaryEntry = setProp;
30
+ /** Set several named props on a dictionary object (by reference). */
31
+ export const setDictionaryEntries = setProps;
32
+ /** Remove several key/value entries from a dictionary object (by reference). */
33
+ export const deleteDictionaryEntries = deleteProps;