shelving 1.78.0 → 1.80.1

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/util/object.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ImmutableArray } from "./array.js";
2
- import type { NotString } from "./string.js";
2
+ import type { Data, Prop } from "./data.js";
3
+ import type { Entry } from "./entry.js";
3
4
  /** Readonly object with string keys. */
4
5
  export declare type ImmutableObject<T = unknown> = {
5
6
  readonly [key: string | number]: T;
@@ -8,8 +9,6 @@ export declare type ImmutableObject<T = unknown> = {
8
9
  export declare type MutableObject<T = unknown> = {
9
10
  [key: string | number]: T;
10
11
  };
11
- /** Extract the type for the values of an object. */
12
- export declare type ObjectType<T extends ImmutableObject> = T[keyof T];
13
12
  /**
14
13
  * Is a value an unknown object?
15
14
  * - This is a TypeScript assertion object that asserts the value extends `ImmutableObject`
@@ -23,75 +22,84 @@ export declare function isPlainObject<T extends ImmutableObject>(value: T | unkn
23
22
  /** Assert that a value is a plain object */
24
23
  export declare function assertPlainObject<T extends ImmutableObject>(value: T | unknown): asserts value is T;
25
24
  /** Is an unknown string an own prop of an object. */
26
- export declare const isEntry: <T extends ImmutableObject<unknown>>(obj: T, key: unknown) => key is keyof T;
25
+ export declare const isProp: <T extends ImmutableObject<unknown>>(obj: T, key: unknown) => key is keyof T;
26
+ /** Turn a data object into an array of props. */
27
+ export declare function getProps<T extends Data>(data: T): ImmutableArray<Prop<T>>;
28
+ export declare function getProps<T extends Data>(data: T | Partial<T>): ImmutableArray<Prop<T>>;
29
+ export declare function getProps<T extends ImmutableObject>(data: T): ImmutableArray<Entry<keyof T, T[keyof T]>>;
30
+ export declare function getProps<T extends ImmutableObject>(data: T | Partial<T>): ImmutableArray<Entry<keyof T, T[keyof T]>>;
27
31
  /**
28
- * Add a key/value entry to a map-like object (immutably).
32
+ * Extract the value of a named prop from a data object.
33
+ * - Extraction is possibly deep if deeper keys are specified.
29
34
  *
30
- * @param input The input object.
35
+ * @param obj The target object to get from.
36
+ * @param k1 The key of the prop in the object to get.
37
+ * @param k2 The sub-key of the prop in the object to get.
38
+ * @param k3 The sub-sub-key of the prop in the object to get.
39
+ * @param k4 The sub-sub-sub-key of the prop in the object to get.
40
+ */
41
+ export declare function getProp<T extends ImmutableObject, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3]>(obj: T, k1: K1, k2: K2, k3: K3, k4: K4): T[K1][K2][K3][K4];
42
+ export declare function getProp<T extends ImmutableObject, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(obj: T, k1: K1, k2: K2, k3: K3): T[K1][K2][K3];
43
+ export declare function getProp<T extends ImmutableObject, K1 extends keyof T, K2 extends keyof T[K1]>(obj: T, k1: K1, k2: K2): T[K1][K2];
44
+ export declare function getProp<T extends ImmutableObject, K1 extends keyof T>(obj: T, k1: K1): T[K1];
45
+ /**
46
+ * Set a prop on an object (immutably).
47
+ *
48
+ * @param input The input data object.
31
49
  * @param key The key of the entry to add.
32
50
  * @param value The value of the entry to add. If set, the entry will only be added if its current value is not `value`
33
51
  *
34
- * @return New object without the specified entry (or same object if entry value didn't change).
52
+ * @return New object without the specified prop (or same object if prop value didn't change).
35
53
  */
36
- export declare const withEntry: <T>(input: ImmutableObject<T>, key: string, value: T) => ImmutableObject<T>;
54
+ export declare function withProp<T extends ImmutableObject, K extends keyof T>(input: T, key: K, value: T[K]): T;
37
55
  /**
38
- * Add several key/value entries on a map-like object (immutably).
56
+ * Set several props on an object (immutably).
39
57
  *
40
- * @param input The input object.
41
- * @return New object with the specified entries added (or same object if no entries changed).
58
+ * @param input The input data object.
59
+ * @param props Set of props to add to the object.
60
+ * @return New object with the specified prop added (or same object if no props changed).
42
61
  */
43
- export declare const withEntries: <T>(input: ImmutableObject<T>, entries: ImmutableObject<T>) => ImmutableObject<T>;
62
+ export declare function withProps<T extends ImmutableObject>(input: T, props: T | Partial<T>): T;
44
63
  /**
45
- * Remove a key/value entry from a map-like object (immutably).
64
+ * Remove several key/value entries from an object (immutably).
46
65
  *
47
66
  * @param input The input object.
48
- * @param key The key of the entry to remove.
49
- * @param value The value of the entry to remove. If not undefined, the entry will only be removed if its current value is exactly `value`
67
+ * @param keys Set of keys for props to remove.
50
68
  *
51
- * @return New object without the specified entries (or same object if the entry didn't exist).
52
- * - If `key` doesn't already exist in `obj` then the exact same input object will be returned.
69
+ * @return New object without the specified entries (or same object if none of the entries existed).
53
70
  */
54
- export declare function withoutEntry<T>(input: ImmutableObject<T>, key: string, value?: T): ImmutableObject<T>;
71
+ export declare function withoutProps<T extends ImmutableObject, K extends keyof T>(input: T, ...keys: K[]): Omit<T, K>;
55
72
  /**
56
- * Remove several key/value entries from a map-like object (immutably).
73
+ * Pick several props from an object (immutably).
57
74
  *
58
75
  * @param input The input object.
59
- * @param keys Set of keys or entries to remove.
76
+ * @param keys Set of keys for props to pick.
60
77
  *
61
- * @return New object without the specified entries (or same object if none of the entries existed).
62
- * - If every entry already exists and has the exact same input object will be returned.
78
+ * @return New object with only the specified props.
63
79
  */
64
- export declare function withoutEntries<T>(input: ImmutableObject<T>, keys: Iterable<string> & NotString): ImmutableObject<T>;
80
+ export declare function pickProps<T extends ImmutableObject, K extends keyof T>(input: T, ...keys: (keyof T)[]): Pick<T, K>;
65
81
  /**
66
- * Set a key/value entry on a map-like object (by reference).
82
+ * Set a single named prop on an object (by reference).
67
83
  *
68
- * @param obj The target object to modify.
84
+ * @param data The target data object to modify.
69
85
  * @param key The key of the prop in the object to set.
70
86
  * @param value The value to set the prop to.
71
87
  */
72
- export declare const setEntry: <T>(obj: MutableObject<T>, key: string, value: T) => T;
73
- /**
74
- * Set several key/value entries on a map-like object (by reference).
75
- *
76
- * @param obj The target object to modify.
77
- * @param entries An object containing new props to set on the object.
78
- */
79
- export declare const setEntries: <T>(obj: MutableObject<T>, entries: ImmutableObject<T>) => void;
88
+ export declare function setProp<T extends MutableObject, K extends keyof T>(data: T, key: K, value: T[K]): T[K];
80
89
  /**
81
- * Remove a key/value entry from a map-like object (by reference).
90
+ * Set several named props on an object (by reference).
82
91
  *
83
92
  * @param obj The target object to modify.
84
- * @param key The key of the entry to remove.
85
- * @param value The value of the entry to remove. If set, the entry will only be removed if its current value is exactly `value`
93
+ * @param props An object containing new props to set on the object.
86
94
  */
87
- export declare function deleteEntry<T>(obj: MutableObject<T>, key: string, value?: T): void;
95
+ export declare function setProps<T extends MutableObject>(obj: T, props: Partial<T>): void;
88
96
  /**
89
- * Remove several key/value entries from a map-like object (by reference).
97
+ * Remove several key/value entries from an object (by reference).
90
98
  *
91
99
  * @param obj The target object to modify.
92
- * @param entries Set of keys or entries to remove.
100
+ * @param keys Set of keys or keys to remove.
93
101
  */
94
- export declare function deleteEntries<T>(obj: MutableObject<T>, keys: ImmutableArray<string>): void;
102
+ export declare function deleteProps<T extends MutableObject>(obj: T, ...keys: (keyof T)[]): void;
95
103
  /** Type that represents an empty object. */
96
104
  export declare type EmptyObject = {
97
105
  readonly [K in never]: never;
@@ -99,4 +107,4 @@ export declare type EmptyObject = {
99
107
  /** An empty object. */
100
108
  export declare const EMPTY_OBJECT: EmptyObject;
101
109
  /** Function that returns an an empty object. */
102
- export declare const GET_EMPTY_OBJECT: () => EmptyObject;
110
+ export declare const getEmptyObject: () => EmptyObject;
package/util/object.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { AssertionError } from "../error/AssertionError.js";
2
- import { setProp, setProps, withProp, withProps } from "./data.js";
3
2
  /**
4
3
  * Is a value an unknown object?
5
4
  * - This is a TypeScript assertion object that asserts the value extends `ImmutableObject`
@@ -25,97 +24,101 @@ export function assertPlainObject(value) {
25
24
  throw new AssertionError(`Must be plain object`, value);
26
25
  }
27
26
  /** Is an unknown string an own prop of an object. */
28
- export const isEntry = (obj, key) => (typeof key === "string" || typeof key === "number" || typeof key === "symbol") && Object.prototype.hasOwnProperty.call(obj, key);
27
+ export const isProp = (obj, key) => (typeof key === "string" || typeof key === "number" || typeof key === "symbol") && Object.prototype.hasOwnProperty.call(obj, key);
28
+ export function getProps(data) {
29
+ return Object.entries(data);
30
+ }
31
+ export function getProp(data, k1, k2, k3, k4) {
32
+ return k2 === undefined ? data[k1] : k3 === undefined ? data[k1][k2] : k4 === undefined ? data[k1][k2][k3] : data[k1][k2][k3][k4];
33
+ }
29
34
  /**
30
- * Add a key/value entry to a map-like object (immutably).
35
+ * Set a prop on an object (immutably).
31
36
  *
32
- * @param input The input object.
37
+ * @param input The input data object.
33
38
  * @param key The key of the entry to add.
34
39
  * @param value The value of the entry to add. If set, the entry will only be added if its current value is not `value`
35
40
  *
36
- * @return New object without the specified entry (or same object if entry value didn't change).
41
+ * @return New object without the specified prop (or same object if prop value didn't change).
37
42
  */
38
- export const withEntry = withProp;
43
+ export function withProp(input, key, value) {
44
+ return input[key] === value ? input : { ...input, [key]: value };
45
+ }
39
46
  /**
40
- * Add several key/value entries on a map-like object (immutably).
47
+ * Set several props on an object (immutably).
41
48
  *
42
- * @param input The input object.
43
- * @return New object with the specified entries added (or same object if no entries changed).
49
+ * @param input The input data object.
50
+ * @param props Set of props to add to the object.
51
+ * @return New object with the specified prop added (or same object if no props changed).
44
52
  */
45
- export const withEntries = withProps;
53
+ export function withProps(input, props) {
54
+ for (const [k, v] of Object.entries(props))
55
+ if (input[k] !== v)
56
+ return { ...input, ...props };
57
+ return input;
58
+ }
46
59
  /**
47
- * Remove a key/value entry from a map-like object (immutably).
60
+ * Remove several key/value entries from an object (immutably).
48
61
  *
49
62
  * @param input The input object.
50
- * @param key The key of the entry to remove.
51
- * @param value The value of the entry to remove. If not undefined, the entry will only be removed if its current value is exactly `value`
63
+ * @param keys Set of keys for props to remove.
52
64
  *
53
- * @return New object without the specified entries (or same object if the entry didn't exist).
54
- * - If `key` doesn't already exist in `obj` then the exact same input object will be returned.
65
+ * @return New object without the specified entries (or same object if none of the entries existed).
55
66
  */
56
- export function withoutEntry(input, key, value) {
57
- if (isEntry(input, key) && (value === undefined || input[key] === value)) {
58
- const { [key]: unused, ...output } = input;
59
- return output;
60
- }
67
+ export function withoutProps(input, ...keys) {
68
+ for (const key of keys)
69
+ if (key in input)
70
+ return Object.fromEntries(Object.entries(input).filter(_doesntHaveKey, keys));
61
71
  return input;
62
72
  }
73
+ function _doesntHaveKey([key]) {
74
+ return !this.includes(key);
75
+ }
63
76
  /**
64
- * Remove several key/value entries from a map-like object (immutably).
77
+ * Pick several props from an object (immutably).
65
78
  *
66
79
  * @param input The input object.
67
- * @param keys Set of keys or entries to remove.
80
+ * @param keys Set of keys for props to pick.
68
81
  *
69
- * @return New object without the specified entries (or same object if none of the entries existed).
70
- * - If every entry already exists and has the exact same input object will be returned.
82
+ * @return New object with only the specified props.
71
83
  */
72
- export function withoutEntries(input, keys) {
73
- let changed = false;
74
- const output = { ...input };
75
- for (const key of keys)
76
- if (isEntry(input, key)) {
77
- delete output[key];
78
- changed = true;
79
- }
80
- return changed ? output : input;
84
+ export function pickProps(input, ...keys) {
85
+ return Object.fromEntries(Object.entries(input).filter(_doesHaveKey, keys));
86
+ }
87
+ function _doesHaveKey([key]) {
88
+ return this.includes(key);
81
89
  }
82
90
  /**
83
- * Set a key/value entry on a map-like object (by reference).
91
+ * Set a single named prop on an object (by reference).
84
92
  *
85
- * @param obj The target object to modify.
93
+ * @param data The target data object to modify.
86
94
  * @param key The key of the prop in the object to set.
87
95
  * @param value The value to set the prop to.
88
96
  */
89
- export const setEntry = setProp;
90
- /**
91
- * Set several key/value entries on a map-like object (by reference).
92
- *
93
- * @param obj The target object to modify.
94
- * @param entries An object containing new props to set on the object.
95
- */
96
- export const setEntries = setProps;
97
+ export function setProp(data, key, value) {
98
+ data[key] = value;
99
+ return value;
100
+ }
97
101
  /**
98
- * Remove a key/value entry from a map-like object (by reference).
102
+ * Set several named props on an object (by reference).
99
103
  *
100
104
  * @param obj The target object to modify.
101
- * @param key The key of the entry to remove.
102
- * @param value The value of the entry to remove. If set, the entry will only be removed if its current value is exactly `value`
105
+ * @param props An object containing new props to set on the object.
103
106
  */
104
- export function deleteEntry(obj, key, value) {
105
- if (value === undefined || obj[key] === value)
106
- delete obj[key];
107
+ export function setProps(obj, props) {
108
+ for (const [k, v] of getProps(props))
109
+ obj[k] = v;
107
110
  }
108
111
  /**
109
- * Remove several key/value entries from a map-like object (by reference).
112
+ * Remove several key/value entries from an object (by reference).
110
113
  *
111
114
  * @param obj The target object to modify.
112
- * @param entries Set of keys or entries to remove.
115
+ * @param keys Set of keys or keys to remove.
113
116
  */
114
- export function deleteEntries(obj, keys) {
117
+ export function deleteProps(obj, ...keys) {
115
118
  for (const key of keys)
116
119
  delete obj[key];
117
120
  }
118
121
  /** An empty object. */
119
122
  export const EMPTY_OBJECT = {};
120
123
  /** Function that returns an an empty object. */
121
- export const GET_EMPTY_OBJECT = () => EMPTY_OBJECT;
124
+ export const getEmptyObject = () => EMPTY_OBJECT;
@@ -1,7 +1,7 @@
1
- import { Data, Value, Prop } from "./data.js";
2
- import { Entry } from "./entry.js";
3
- import type { ImmutableObject } from "./object.js";
4
1
  import type { ArrayType, ImmutableArray } from "./array.js";
2
+ import type { Data, Prop, Value } from "./data.js";
3
+ import { Entry } from "./entry.js";
4
+ import { ImmutableObject } from "./object.js";
5
5
  /** Object that transforms an input value into an output value with its `transform()` method. */
6
6
  export interface Transformable<I, O> {
7
7
  transform(input: I): O;
@@ -20,26 +20,24 @@ export declare type Transformers<T extends Data> = {
20
20
  readonly [K in keyof T]?: Transformer<T[K], T[K]>;
21
21
  };
22
22
  /**
23
- * Transform the props of a data object using a set of transformers for its props.
24
- * @returns New object with changed props (or the same object if no changes were made).
23
+ * Transform the props of a data object using a set of named transformers.
24
+ * @returns New object with changed props.
25
25
  */
26
26
  export declare function transformData<T extends Data>(data: T, transforms: Transformers<T>): T;
27
27
  /**
28
- * Transform the props of a data object using a set of prop transformers.
29
- * @yield Transformed prop entry after calling the corresponding prop transformer.
28
+ * Transform the props of a data object using a set of named transformers.
29
+ * @yield Transformed prop entries after calling the corresponding transformer.
30
30
  */
31
- export declare function transformProps<T extends Data>(data: T, transforms: Transformers<T>): Iterable<Prop<T>>;
31
+ export declare function transformProps<T extends Data>(obj: T, transforms: Transformers<T>): Iterable<Prop<T>>;
32
32
  /**
33
33
  * Transform the _values_ of an iterable set of entries using a transformer.
34
34
  * @yield Transformed entry after calling transforming the new value for each entry.
35
35
  */
36
- export declare function mapEntries<K, I, O>(entries: Iterable<Entry<K, I>>, transformer: (v: I) => O): Iterable<Entry<K, O>>;
37
36
  export declare function mapEntries<K, I, O>(entries: Iterable<Entry<K, I>>, transformer: Transformer<I, O>): Iterable<Entry<K, O>>;
38
37
  /**
39
38
  * Transform the _values_ of a map-like object using a transformer.
40
39
  * @return New object after transforming its entries.
41
40
  */
42
- export declare function mapObject<I, O>(obj: ImmutableObject<I>, transformer: (v: I) => O): ImmutableObject<O>;
43
41
  export declare function mapObject<I, O>(obj: ImmutableObject<I>, transformer: Transformer<I, O>): ImmutableObject<O>;
44
42
  /**
45
43
  * Transform the _values_ of an data object using a transformer.
@@ -53,12 +51,10 @@ export declare function mapData<I extends Data, O extends {
53
51
  * Map an iterable set of items using a transformer.
54
52
  * @yield Transformed items after calling `transformer()` on each.
55
53
  */
56
- export declare function mapItems<I, O>(items: Iterable<I>, transformer: (input: I) => O): Iterable<O>;
57
54
  export declare function mapItems<I, O>(items: Iterable<I>, transformer: Transformer<I, O>): Iterable<O>;
58
55
  /**
59
56
  * Apply a transformer to each item in an array and return the transformed array.
60
57
  * @return The transformed array.
61
58
  */
62
59
  export declare function mapArray<T extends ImmutableArray>(arr: T, transformer: Transformer<ArrayType<T>, ArrayType<T>>): T;
63
- export declare function mapArray<I, O>(arr: Iterable<I>, transformer: (v: I) => O): ImmutableArray<O>;
64
60
  export declare function mapArray<I, O>(arr: Iterable<I>, transformer: Transformer<I, O>): ImmutableArray<O>;
package/util/transform.js CHANGED
@@ -1,36 +1,48 @@
1
1
  import { isFunction } from "./function.js";
2
- import { getProps, isData } from "./data.js";
3
2
  import { getEntries } from "./entry.js";
3
+ import { getProps, isObject } from "./object.js";
4
4
  /** Is an unknown value a transformable. */
5
- export const isTransformable = (v) => isData(v) && typeof v.transform === "function";
5
+ export const isTransformable = (v) => isObject(v) && typeof v.transform === "function";
6
6
  export function transform(input, transformer) {
7
7
  return isFunction(transformer) ? transformer(input) : isTransformable(transformer) ? transformer.transform(input) : transformer;
8
8
  }
9
9
  /**
10
- * Transform the props of a data object using a set of transformers for its props.
11
- * @returns New object with changed props (or the same object if no changes were made).
10
+ * Transform the props of a data object using a set of named transformers.
11
+ * @returns New object with changed props.
12
12
  */
13
13
  export function transformData(data, transforms) {
14
14
  return { ...data, ...Object.fromEntries(transformProps(data, transforms)) };
15
15
  }
16
16
  /**
17
- * Transform the props of a data object using a set of prop transformers.
18
- * @yield Transformed prop entry after calling the corresponding prop transformer.
17
+ * Transform the props of a data object using a set of named transformers.
18
+ * @yield Transformed prop entries after calling the corresponding transformer.
19
19
  */
20
- export function* transformProps(data, transforms) {
20
+ export function* transformProps(obj, transforms) {
21
21
  for (const [k, v] of getProps(transforms))
22
- yield [k, transform(data[k], v)];
22
+ yield [k, transform(obj[k], v)];
23
23
  }
24
+ /**
25
+ * Transform the _values_ of an iterable set of entries using a transformer.
26
+ * @yield Transformed entry after calling transforming the new value for each entry.
27
+ */
24
28
  export function* mapEntries(entries, transformer) {
25
29
  for (const [k, v] of entries)
26
30
  yield [k, transform(v, transformer)];
27
31
  }
32
+ /**
33
+ * Transform the _values_ of a map-like object using a transformer.
34
+ * @return New object after transforming its entries.
35
+ */
28
36
  export function mapObject(obj, transformer) {
29
37
  return Object.fromEntries(mapEntries(getEntries(obj), transformer));
30
38
  }
31
39
  export function mapData(data, transformer) {
32
40
  return mapObject(data, transformer);
33
41
  }
42
+ /**
43
+ * Map an iterable set of items using a transformer.
44
+ * @yield Transformed items after calling `transformer()` on each.
45
+ */
34
46
  export function* mapItems(items, transformer) {
35
47
  for (const item of items)
36
48
  yield transform(item, transformer);
@@ -1,6 +1,6 @@
1
1
  import type { Entry } from "./entry.js";
2
- import type { ImmutableObject } from "./object.js";
3
- import { Data, Prop } from "./data.js";
2
+ import type { Data, Prop } from "./data.js";
3
+ import { ImmutableObject } from "./object.js";
4
4
  import { ImmutableArray } from "./array.js";
5
5
  /** Object that can validate an unknown value with its `validate()` method. */
6
6
  export interface Validatable<T> {
package/util/validate.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { isFeedback } from "../feedback/Feedback.js";
2
2
  import { InvalidFeedback } from "../feedback/InvalidFeedback.js";
3
- import { getProps } from "./data.js";
3
+ import { getProps } from "./object.js";
4
4
  import { getArray } from "./array.js";
5
5
  /** Validate an unknown value with a validator. */
6
6
  export function validate(unsafeValue, validator) {