@rimbu/deep 2.0.4 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,31 +0,0 @@
1
- import type { IsAny, IsPlainObj } from '@rimbu/base';
2
-
3
- /**
4
- * A deep readonly typed version of given type T. Makes all properties or elements read only.
5
- * It maps types using the following rules:
6
- * - arrays and tuples become readonly counterparts, and all element types are wrapped in `Protected` if applicable
7
- * - Maps of key type K and value type V become Maps of key type `Protected<K>` and value type `Protected<V>`
8
- * - Sets of element type E become Sets of element type `Protected<E>`
9
- * - Promises of value type E become Promises of value type `Protected<E>`
10
- * - Objects that have only simple properties (no functions or iterators) will have all the properties as Protected if applicable
11
- * - Any other type will not be mapped
12
- * @typeparam T - the input type
13
- */
14
- export type Protected<T> =
15
- IsAny<T> extends true
16
- ? // to prevent infinite recursion, any will be any
17
- T
18
- : T extends readonly any[] & infer A
19
- ? // convert all keys to readonly and all values to `Protected`
20
- { readonly [K in keyof A]: Protected<A[K]> }
21
- : T extends Map<infer K, infer V>
22
- ? ReadonlyMap<Protected<K>, Protected<V>>
23
- : T extends Set<infer E>
24
- ? ReadonlySet<Protected<E>>
25
- : T extends Promise<infer E>
26
- ? Promise<Protected<E>>
27
- : IsPlainObj<T> extends true
28
- ? // convert all keys to readonly and all values to `Protected`
29
- { readonly [K in keyof T]: Protected<T[K]> }
30
- : // nothing to do, just return `T`
31
- T;
@@ -1,93 +0,0 @@
1
- import type { IsAnyFunc, IsArray } from '@rimbu/base';
2
-
3
- import { Deep, type Path, type Protected } from './internal.mts';
4
-
5
- /**
6
- * Type defining the allowed selectors on an object of type `T`.
7
- * Selectors can be:
8
- * - a path string into type `T`.
9
- * - a function receiving a `Protected` version of type `T`, and returning an arbitrary value.
10
- * - a tuple of `Selectors` for type `T`
11
- * - an object where the property values are `Selectors` for type `T`.
12
- * @typeparam T - the source value type.
13
- */
14
- export type Selector<T> =
15
- | Path.Get<T>
16
- | ((value: Protected<T>) => any)
17
- | readonly Selector<T>[]
18
- | { readonly [key: string | symbol]: Selector<T> };
19
-
20
- export namespace Selector {
21
- /**
22
- * Type defining the shape of allowed selectors, used to improve compiler checking.
23
- * @typeparam SL - the selector type
24
- */
25
- export type Shape<SL> =
26
- IsAnyFunc<SL> extends true
27
- ? // functions are allowed, type provided by `Selector`
28
- SL
29
- : IsArray<SL> extends true
30
- ? // ensure tuple type is preserved
31
- readonly [...(SL extends readonly unknown[] ? SL : never)]
32
- : SL extends { readonly [key: string | number | symbol]: unknown }
33
- ? // ensure all object properties satisfy `Shape`
34
- { readonly [K in keyof SL]: Selector.Shape<SL[K]> }
35
- : // nothing to check
36
- SL;
37
-
38
- /**
39
- * Type defining the result type of applying the SL selector type to the T value type.
40
- * @typeparam T - the source value type
41
- * @typeparam SL - the selector type
42
- */
43
- export type Result<T, SL> =
44
- Selector<T> extends SL
45
- ? never
46
- : SL extends (...args: any[]) => infer R
47
- ? R
48
- : SL extends string
49
- ? Path.Result<T, SL>
50
- : {
51
- readonly [K in keyof SL]: Selector.Result<T, SL[K]>;
52
- };
53
- }
54
-
55
- /**
56
- * Returns the result of applying the given `selector` shape to the given `source` value.
57
- * @typeparam T - the patch value type
58
- * @typeparam SL - the selector shape type
59
- * @param source - the source value to select from
60
- * @param selector - a shape indicating the selection from the source values
61
- * @example
62
- * ```ts
63
- * const item = { a: { b: 1, c: 'a' } };
64
- * Deep.select(item, { q: 'a.c', y: ['a.b', 'a.c'], z: (v) => v.a.b + 1 });
65
- * // => { q: 'a', y: [1, 'a'], z: 2 }
66
- * ```
67
- */
68
- export function select<T, SL extends Selector<T>>(
69
- source: T,
70
- selector: Selector.Shape<SL>
71
- ): Selector.Result<T, SL> {
72
- if (typeof selector === 'function') {
73
- // selector is function, resolve selector function
74
- return (selector as any)(source as Protected<T>);
75
- } else if (typeof selector === 'string') {
76
- // selector is string path, get the value at the given path
77
- return Deep.getAt(source, selector as Path.Get<T>) as any;
78
- } else if (Array.isArray(selector)) {
79
- // selector is tuple, get each tuple item value
80
- return selector.map((s) => select(source, s)) as any;
81
- }
82
-
83
- // selector is object
84
-
85
- const result: any = {};
86
-
87
- for (const key in selector as any) {
88
- // set each selected object key to the selector value
89
- result[key] = select(source, (selector as any)[key]);
90
- }
91
-
92
- return result;
93
- }
@@ -1,201 +0,0 @@
1
- import { Arr } from '@rimbu/base';
2
- import type { Update } from '@rimbu/common';
3
-
4
- /**
5
- * A readonly array of fixed length and types.
6
- */
7
- export type Tuple<T extends Tuple.Source> = Readonly<T>;
8
-
9
- export namespace Tuple {
10
- /**
11
- * A non-empty readonly array that can serve as a source for a Tuple.
12
- */
13
- export type NonEmptySource = readonly [unknown, ...unknown[]];
14
-
15
- /**
16
- * A readonly array that can serve as a source for a Tuple.
17
- */
18
- export type Source = readonly unknown[];
19
-
20
- /**
21
- * Determines whether the given type `T` is a tuple type.
22
- * @typeparam T - the input type
23
- */
24
- export type IsTuple<T> = T extends { length: infer L }
25
- ? 0 extends L
26
- ? false
27
- : true
28
- : false;
29
-
30
- /**
31
- * Returns the indices/keys that are in a tuple.
32
- * @typeparam T - the input tuple type
33
- */
34
- export type KeysOf<T> = { [K in keyof T]: K }[keyof T & number];
35
-
36
- /**
37
- * Convenience method to type Tuple types
38
- * @param values - the values of the tuple
39
- * @example
40
- * ```ts
41
- * const t = Tuple.of(1, 'a', true)
42
- * // type of t => Tuple<[number, string, boolean]>
43
- * ```
44
- */
45
- export function of<T extends Tuple.NonEmptySource>(...values: T): Tuple<T> {
46
- return values as any;
47
- }
48
-
49
- /**
50
- * Returns the item at the given `index` in the given `tuple`.
51
- * @param tuple - the tuple to get the item from
52
- * @param index - the index in of the tuple element
53
- * @example
54
- * ```ts
55
- * const t = Tuple.of(1, 'a', true)
56
- * console.log(Tuple.getIndex(t, 1))
57
- * // => 'a'
58
- * ```
59
- */
60
- export function getIndex<T extends Tuple.Source, K extends keyof T = keyof T>(
61
- tuple: T,
62
- index: K
63
- ): T[K] {
64
- return tuple[index];
65
- }
66
-
67
- /**
68
- * Returns the first element of a Tuple.
69
- * @param tuple - the source tuple
70
- * @example
71
- * ```ts
72
- * const t = Tuple.of(1, 'a', true)
73
- * console.log(Tuple.first(t))
74
- * // => 1
75
- * ```
76
- */
77
- export function first<T extends Tuple.Source>(tuple: T): T[0] {
78
- return tuple[0];
79
- }
80
-
81
- /**
82
- * Returns the second element of a Tuple.
83
- * @param tuple - the source tuple
84
- * @example
85
- * ```ts
86
- * const t = Tuple.of(1, 'a', true)
87
- * console.log(Tuple.second(t))
88
- * // => 'a'
89
- * ```
90
- */
91
- export function second<T extends Tuple.Source>(tuple: T): T[1] {
92
- return tuple[1];
93
- }
94
-
95
- /**
96
- * Returns the last element of a Tuple.
97
- * @param tuple - the source tuple
98
- * @example
99
- * ```ts
100
- * const t = Tuple.of(1, 'a', true)
101
- * console.log(Tuple.last(t))
102
- * // => true
103
- * ```
104
- */
105
- export function last<T extends readonly unknown[], R>(
106
- tuple: readonly [...T, R]
107
- ): R {
108
- return tuple[tuple.length - 1] as any;
109
- }
110
-
111
- /**
112
- * Returns a copy of the given `tuple` where the element at given `index` is updated with the
113
- * given `updater`.
114
- * @param tuple - the source tuple
115
- * @param index - the index in the tuple
116
- * @param updater - the updater for the value
117
- * @example
118
- * ```ts
119
- * const t = Tuple.of(1, 'a', true)
120
- * console.log(Tuple.updateAt(t, 1, 'b'))
121
- * // => [1, 'b', true]
122
- * ```
123
- */
124
- export function updateAt<T extends Tuple.Source, K extends keyof T = keyof T>(
125
- tuple: T,
126
- index: K,
127
- updater: Update<T[K]>
128
- ): T {
129
- return Arr.update(tuple, index as number, updater) as T;
130
- }
131
-
132
- /**
133
- * Returns the given `tuple` with the given `values` appended.
134
- * @param tuple - the source tuple
135
- * @param values - the values to append
136
- * @example
137
- * ```ts
138
- * const t = Tuple.of(1, 'a')
139
- * console.log(Tuple.append(t, true, 5))
140
- * // => [1, 'a', true, 5]
141
- * ```
142
- */
143
- export function append<
144
- T extends Tuple.Source,
145
- V extends readonly [unknown, ...unknown[]],
146
- >(tuple: T, ...values: V): readonly [...T, ...V] {
147
- return [...tuple, ...values];
148
- }
149
-
150
- /**
151
- * Returns a Tuple containing the elements of given `tuple1` followed by the elements
152
- * of given `tuple2`.
153
- * @param tuple1 - the first Tuple
154
- * @param tuple2 - the second Tuple
155
- * @example
156
- * ```ts
157
- * const t1 = Tuple.of(1, 'a')
158
- * const t2 = Tuple.of(true, 5)
159
- * console.log(Tuple.concat(t1, t2))
160
- * // => [1, 'a', true, 5]
161
- * ```
162
- */
163
- export function concat<T1 extends Tuple.Source, T2 extends Tuple.Source>(
164
- tuple1: T1,
165
- tuple2: T2
166
- ): readonly [...T1, ...T2] {
167
- return tuple1.concat(tuple2) as any;
168
- }
169
-
170
- /**
171
- * Returns a Tuple containing all but the last element of the given `tuple`.
172
- * @param tuple - the source tuple
173
- * @example
174
- * ```ts
175
- * const t = Tuple.of(1, 'a', true)
176
- * console.log(Tuple.init(t))
177
- * // => [1, 'a']
178
- * ```
179
- */
180
- export function init<T extends readonly unknown[]>(
181
- tuple: readonly [...T, unknown]
182
- ): Readonly<T> {
183
- return Arr.init(tuple) as any;
184
- }
185
-
186
- /**
187
- * Returns a Tuple containing all but the first element of the given `tuple`.
188
- * @param tuple - the source tuple
189
- * @example
190
- * ```ts
191
- * const t = Tuple.of(1, 'a', true)
192
- * console.log(Tuple.tail(t))
193
- * // => ['a', true]
194
- * ```
195
- */
196
- export function tail<T extends readonly [...unknown[]]>(
197
- tuple: readonly [unknown, ...T]
198
- ): Readonly<T> {
199
- return Arr.tail(tuple) as any;
200
- }
201
- }