@solid-primitives/utils 6.0.0 → 6.1.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.
package/README.md CHANGED
@@ -11,9 +11,91 @@ Solid Primitives Utilities is a support and helper package for a number of primi
11
11
  ```bash
12
12
  npm install @solid-primitives/utils
13
13
  # or
14
+ pnpm add @solid-primitives/utils
15
+ # or
14
16
  yarn add @solid-primitives/utils
15
17
  ```
16
18
 
19
+ ## Immutable helpers
20
+
21
+ Functional programming helpers for making non-mutating changes to data. Keeping it immutable. Useful for updating signals.
22
+
23
+ ```ts
24
+ import { pick } from "@solid-primitives/utils/immutable";
25
+
26
+ const original = { foo: 123, bar: "baz" };
27
+ const newObj = pick(original, "foo");
28
+ original; // { foo: 123, bar: "baz" }
29
+ newObj; // { foo: 123 }
30
+ ```
31
+
32
+ Use it for changing signals:
33
+
34
+ ```ts
35
+ import { push, update } from "@solid-primitives/utils/immutable";
36
+
37
+ const [list, setList] = createSignal([1, 2, 3]);
38
+ setList(p => push(p, 4));
39
+
40
+ const [user, setUser] = createSignal({
41
+ name: "John",
42
+ street: { name: "Kingston Cei", number: 24 },
43
+ });
44
+ setUser(p => update(p, "street", "number", 64));
45
+ ```
46
+
47
+ ## List of functions:
48
+
49
+ ### Copying
50
+
51
+ - **`shallowArrayCopy`** - make shallow copy of an array
52
+ - **`shallowObjectCopy`** - make shallow copy of an object
53
+ - **`shallowCopy`** - make shallow copy of an array/object
54
+ - **`withArrayCopy`** - apply mutations to the an array without changing the original
55
+ - **`withObjectCopy`** - apply mutations to the an object without changing the original
56
+ - **`withCopy`** - apply mutations to the an object/array without changing the original
57
+
58
+ ### Array
59
+
60
+ - **`push`** - non-mutating `Array.prototype.push()`
61
+ - **`drop`** - non-mutating function that drops n items from the array start
62
+ - **`dropRight`** - non-mutating function that drops n items from the array end
63
+ - **`filterOut`** - standalone `Array.prototype.filter()` that filters out passed item
64
+ - **`filter`** - standalone `Array.prototype.filter()`
65
+ - **`sort`** - non-mutating `Array.prototype.sort()` as a standalone function
66
+ - **`sortBy`** - Sort an array by object key, or multiple keys
67
+ - **`map`** - standalone `Array.prototype.map()` function
68
+ - **`slice`** - standalone `Array.prototype.slice()` function
69
+ - **`splice`** - non-mutating `Array.prototype.splice()` as a standalone function
70
+ - **`fill`** - non-mutating `Array.prototype.fill()` as a standalone function
71
+ - **`concat`** - Creates a new array concatenating array with any additional arrays and/or values.
72
+ - **`remove`** - Remove item from array
73
+ - **`removeItems`** - Remove multiple items from an array
74
+ - **`flatten`** - Flattens a nested array into a one-level array
75
+ - **`filterInstance`** - Flattens a nested array into a one-level array
76
+ - **`filterOutInstance`** - Flattens a nested array into a one-level array
77
+
78
+ ### Object
79
+
80
+ - **`omit`** - Create a new subset object without the provided keys
81
+ - **`pick`** - Create a new subset object with only the provided keys
82
+ - **`split`** - Split object into multiple subset objects.
83
+ - **`merge`** - Merges multiple objects into a single one.
84
+
85
+ ### Object/Array
86
+
87
+ - **`get`** - Get a single property value of an object by specifying a path to it.
88
+ - **`update`** - Change single value in an object by key, or series of recursing keys.
89
+
90
+ ### Number
91
+
92
+ - **`add`** - `a + b + c + ...` _(works for numbers or strings)_
93
+ - **`substract`** - `a - b - c - ...`
94
+ - **`multiply`** - `a * b * c * ...`
95
+ - **`divide`** - `a / b / c / ...`
96
+ - **`power`** - `a ** b ** c ** ...`
97
+ - **`clamp`** - clamp a number value between two other values
98
+
17
99
  ## Changelog
18
100
 
19
101
  See [CHANGELOG.md](./CHANGELOG.md)
@@ -0,0 +1,158 @@
1
+ 'use strict';
2
+
3
+ require('solid-js');
4
+ require('solid-js/web');
5
+
6
+ // src/immutable/copy.ts
7
+ exports.shallowArrayCopy = (array) => array.slice();
8
+ exports.shallowObjectCopy = (object) => Object.assign({}, object);
9
+ exports.shallowCopy = (source) => Array.isArray(source) ? exports.shallowArrayCopy(source) : exports.shallowObjectCopy(source);
10
+ exports.withArrayCopy = (array, mutator) => {
11
+ const copy = exports.shallowArrayCopy(array);
12
+ mutator(copy);
13
+ return copy;
14
+ };
15
+ exports.withObjectCopy = (object, mutator) => {
16
+ const copy = exports.shallowObjectCopy(object);
17
+ mutator(copy);
18
+ return copy;
19
+ };
20
+ exports.withCopy = (source, mutator) => Array.isArray(source) ? exports.withArrayCopy(source, mutator) : exports.withObjectCopy(source, mutator);
21
+
22
+ // src/immutable/number.ts
23
+ function add(...a) {
24
+ let r = 0;
25
+ for (const n of a) {
26
+ r += n;
27
+ }
28
+ return r;
29
+ }
30
+ exports.substract = (a, ...b) => {
31
+ for (const n of b) {
32
+ a -= n;
33
+ }
34
+ return a;
35
+ };
36
+ exports.multiply = (a, ...b) => {
37
+ for (const n of b) {
38
+ a *= n;
39
+ }
40
+ return a;
41
+ };
42
+ exports.divide = (a, ...b) => {
43
+ for (const n of b) {
44
+ a /= n;
45
+ }
46
+ return a;
47
+ };
48
+ exports.power = (a, ...b) => {
49
+ for (const n of b) {
50
+ a = a ** n;
51
+ }
52
+ return a;
53
+ };
54
+ exports.clamp = (n, min, max) => Math.min(max, Math.max(min, n));
55
+
56
+ // src/immutable/update.ts
57
+ exports.update = (...args) => exports.withCopy(args[0], (obj) => {
58
+ if (args.length > 3)
59
+ obj[args[1]] = exports.update(obj[args[1]], ...args.slice(2));
60
+ else if (typeof args[2] === "function")
61
+ obj[args[1]] = args[2](obj[args[1]]);
62
+ else
63
+ obj[args[1]] = args[2];
64
+ });
65
+
66
+ // src/immutable/object.ts
67
+ exports.omit = (object, ...keys) => exports.withObjectCopy(object, (object2) => keys.forEach((key) => delete object2[key]));
68
+ exports.pick = (object, ...keys) => keys.reduce((n, k) => {
69
+ if (k in object)
70
+ n[k] = object[k];
71
+ return n;
72
+ }, {});
73
+ function get(obj, ...keys) {
74
+ let res = obj;
75
+ for (const key of keys) {
76
+ res = res[key];
77
+ }
78
+ return res;
79
+ }
80
+ function split(object, ...list) {
81
+ const _list = typeof list[0] === "string" ? [list] : list;
82
+ const copy = exports.shallowObjectCopy(object);
83
+ const result = [];
84
+ for (let i = 0; i < _list.length; i++) {
85
+ const keys = _list[i];
86
+ result.push({});
87
+ for (const key of keys) {
88
+ result[i][key] = copy[key];
89
+ delete copy[key];
90
+ }
91
+ }
92
+ return [...result, copy];
93
+ }
94
+ function merge(...objects) {
95
+ const result = {};
96
+ for (const obj of objects) {
97
+ Object.assign(result, obj);
98
+ }
99
+ return result;
100
+ }
101
+ var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
102
+ var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
103
+
104
+ // src/immutable/array.ts
105
+ exports.push = (list, ...items) => exports.withArrayCopy(list, (list2) => list2.push(...items));
106
+ exports.drop = (list, n = 1) => list.slice(n);
107
+ exports.dropRight = (list, n = 1) => list.slice(0, list.length - n);
108
+ exports.filterOut = (list, item) => filter(list, (i) => i !== item);
109
+ function filter(list, predicate) {
110
+ const newList = list.filter(predicate);
111
+ newList.removed = list.length - newList.length;
112
+ return newList;
113
+ }
114
+ exports.sort = (list, compareFn) => list.slice().sort(compareFn);
115
+ exports.map = (list, mapFn) => list.map(mapFn);
116
+ exports.slice = (list, start, end) => list.slice(start, end);
117
+ exports.splice = (list, start, deleteCount = 0, ...items) => exports.withArrayCopy(list, (list2) => list2.splice(start, deleteCount, ...items));
118
+ exports.fill = (list, value, start, end) => list.slice().fill(value, start, end);
119
+ function concat(...a) {
120
+ const result = [];
121
+ for (const i in a) {
122
+ Array.isArray(a[i]) ? result.push(...a[i]) : result.push(a[i]);
123
+ }
124
+ return result;
125
+ }
126
+ exports.remove = (list, item, ...insertItems) => {
127
+ const index = list.indexOf(item);
128
+ return exports.splice(list, index, 1, ...insertItems);
129
+ };
130
+ exports.removeItems = (list, ...items) => {
131
+ const res = [];
132
+ for (let i = 0; i < list.length; i++) {
133
+ const item = list[i];
134
+ const ii = items.indexOf(item);
135
+ if (ii !== -1)
136
+ items.splice(ii, 1);
137
+ else
138
+ res.push(item);
139
+ }
140
+ return res;
141
+ };
142
+ exports.flatten = (arr) => arr.reduce((flat, next) => flat.concat(Array.isArray(next) ? exports.flatten(next) : next), []);
143
+ exports.sortBy = (arr, ...paths) => exports.flatten(paths).reduce(
144
+ (source, path) => exports.sort(
145
+ source,
146
+ (a, b) => typeof path === "function" ? compare(path(a), path(b)) : compare(get(a, path), get(b, path))
147
+ ),
148
+ arr
149
+ );
150
+ exports.filterInstance = (list, ...classes) => classes.length === 1 ? list.filter((item) => ofClass(item, classes[0])) : list.filter((item) => item && classes.some((c) => ofClass(item, c)));
151
+ exports.filterOutInstance = (list, ...classes) => classes.length === 1 ? list.filter((item) => item && !ofClass(item, classes[0])) : list.filter((item) => item && !classes.some((c) => ofClass(item, c)));
152
+
153
+ exports.add = add;
154
+ exports.concat = concat;
155
+ exports.filter = filter;
156
+ exports.get = get;
157
+ exports.merge = merge;
158
+ exports.split = split;
@@ -0,0 +1,257 @@
1
+ import { I as ItemsOf, g as Modify, d as Many, A as AnyClass } from '../types-38269011.js';
2
+ import 'solid-js';
3
+
4
+ /** make shallow copy of an array */
5
+ declare const shallowArrayCopy: <T>(array: readonly T[]) => T[];
6
+ /** make shallow copy of an object */
7
+ declare const shallowObjectCopy: <T extends object>(object: T) => T;
8
+ /** make shallow copy of an array/object */
9
+ declare const shallowCopy: <T extends object>(source: T) => T;
10
+ /**
11
+ * apply mutations to the an array without changing the original
12
+ * @param array original array
13
+ * @param mutator function applying mutations to the copy of source
14
+ * @returns changed array copy
15
+ */
16
+ declare const withArrayCopy: <T>(array: readonly T[], mutator: (copy: T[]) => void) => T[];
17
+ /**
18
+ * apply mutations to the an object without changing the original
19
+ * @param object original object
20
+ * @param mutator function applying mutations to the copy of source
21
+ * @returns changed object copy
22
+ */
23
+ declare const withObjectCopy: <T extends object>(object: T, mutator: (copy: T) => void) => T;
24
+ /**
25
+ * apply mutations to the an object/array without changing the original
26
+ * @param source original object
27
+ * @param mutator function applying mutations to the copy of source
28
+ * @returns changed object copy
29
+ */
30
+ declare const withCopy: <T extends object>(source: T, mutator: (copy: T) => void) => T;
31
+
32
+ /** `a + b + c + ...` */
33
+ declare function add(...a: number[]): number;
34
+ declare function add(...a: string[]): string;
35
+ /** `a - b - c - ...` */
36
+ declare const substract: (a: number, ...b: number[]) => number;
37
+ /** `a * b * c * ...` */
38
+ declare const multiply: (a: number, ...b: number[]) => number;
39
+ /** `a / b / c / ...` */
40
+ declare const divide: (a: number, ...b: number[]) => number;
41
+ /** `a ** b ** c ** ...` */
42
+ declare const power: (a: number, ...b: number[]) => number;
43
+ /** clamp a number value between two other values */
44
+ declare const clamp: (n: number, min: number, max: number) => number;
45
+
46
+ type Predicate<T> = (item: T, index: number, array: readonly T[]) => boolean;
47
+ type MappingFn<T, V> = (item: T, index: number, array: readonly T[]) => V;
48
+ type FlattenArray<T> = T extends any[] ? FlattenArray<ItemsOf<T>> : T;
49
+ type ModifyValue<O, K extends keyof O, V> = Omit<O, K> & {
50
+ [key in K]: V;
51
+ };
52
+
53
+ type UpdateSetter<O, K extends keyof O, V> = V | ((prev: O[K]) => V);
54
+ type Update = {
55
+ <O extends object, K0 extends keyof O, K1 extends keyof O[K0], K2 extends keyof O[K0][K1], K3 extends keyof O[K0][K1][K2], K4 extends keyof O[K0][K1][K2][K3], V>(object: O, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, setter: UpdateSetter<O[K0][K1][K2][K3], K4, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, ModifyValue<O[K0][K1], K2, ModifyValue<O[K0][K1][K2], K3, ModifyValue<O[K0][K1][K2][K3], K4, V>>>>>;
56
+ <O extends object, K0 extends keyof O, K1 extends keyof O[K0], K2 extends keyof O[K0][K1], K3 extends keyof O[K0][K1][K2], V>(object: O, k0: K0, k1: K1, k2: K2, k3: K3, setter: UpdateSetter<O[K0][K1][K2], K3, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, ModifyValue<O[K0][K1], K2, ModifyValue<O[K0][K1][K2], K3, V>>>>;
57
+ <O extends object, K0 extends keyof O, K1 extends keyof O[K0], K2 extends keyof O[K0][K1], V>(object: O, k0: K0, k1: K1, k2: K2, setter: UpdateSetter<O[K0][K1], K2, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, ModifyValue<O[K0][K1], K2, V>>>;
58
+ <O extends object, K0 extends keyof O, K1 extends keyof O[K0], V>(object: O, k0: K0, k1: K1, setter: UpdateSetter<O[K0], K1, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, V>>;
59
+ <O extends object, K extends keyof O, V>(object: O, key: K, setter: UpdateSetter<O, K, V>): ModifyValue<O, K, V>;
60
+ };
61
+ /**
62
+ * Change single value in an object by key. Allows accessign nested objects by passing multiple keys.
63
+ *
64
+ * Performs a shallow copy of each accessed object.
65
+ *
66
+ * @param object original source
67
+ * @param ...keys keys of sequential accessed objects
68
+ * @param value a value to set in place of a previous one, or a setter function.
69
+ * ```ts
70
+ * V | ((prev: O[K]) => V)
71
+ * ```
72
+ * a new value doesn't have to have the same type as the original
73
+ * @returns changed copy of the original object
74
+ *
75
+ * @example
76
+ * const original = { foo: { bar: { baz: 123 }}};
77
+ * const newObj = update(original, "foo", "bar", "baz", prev => prev + 1)
78
+ * original // { foo: { bar: { baz: 123 }}}
79
+ * newObj // { foo: { bar: { baz: 124 }}}
80
+ */
81
+ declare const update: Update;
82
+
83
+ /**
84
+ * Create a new subset object without the provided keys
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * const newObject = omit({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
89
+ * newObject // => { c: "baz" }
90
+ * ```
91
+ */
92
+ declare const omit: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Omit<O, K>;
93
+ /**
94
+ * Create a new subset object with only the provided keys
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * const newObject = pick({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
99
+ * newObject // => { a:"foo", b:"bar" }
100
+ * ```
101
+ */
102
+ declare const pick: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Pick<O, K>;
103
+ /**
104
+ * Get a single property value of an object by specifying a path to it.
105
+ */
106
+ declare function get<O extends object, K extends keyof O>(obj: O, key: K): O[K];
107
+ declare function get<O extends object, K1 extends keyof O, K2 extends keyof O[K1]>(obj: O, k1: K1, k2: K2): O[K1][K2];
108
+ declare function get<O extends object, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2]>(obj: O, k1: K1, k2: K2, k3: K3): O[K1][K2][K3];
109
+ declare function get<O extends object, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], K4 extends keyof O[K1][K2][K3]>(obj: O, k1: K1, k2: K2, k3: K3, k4: K4): O[K1][K2][K3][K4];
110
+ declare function get<O extends object, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], K4 extends keyof O[K1][K2][K3], K5 extends keyof O[K1][K2][K3][K4]>(obj: O, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): O[K1][K2][K3][K4][K5];
111
+ declare function get<O extends object, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], K4 extends keyof O[K1][K2][K3], K5 extends keyof O[K1][K2][K3][K4], K6 extends keyof O[K1][K2][K3][K4][K5]>(obj: O, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): O[K1][K2][K3][K4][K5][K6];
112
+ /**
113
+ * Split object properties by keys into multiple object copies with a subset of selected properties.
114
+ *
115
+ * @param object original object
116
+ * @param ...keys keys to pick from the source, or multiple arrays of keys *(for splitting into more than 2 objects)*
117
+ * ```ts
118
+ * (keyof object)[][] | (keyof object)[]
119
+ * ```
120
+ * @returns array of subset objects
121
+ */
122
+ declare function split<T extends object, K extends keyof T>(object: T, ...keys: K[]): [Pick<T, K>, Omit<T, K>];
123
+ declare function split<T extends object, K1 extends keyof T, K2 extends keyof T>(object: T, ...keys: [K1[], K2[]]): [Pick<T, K1>, Pick<T, K2>, Omit<T, K1 | K2>];
124
+ declare function split<T extends object, K1 extends keyof T, K2 extends keyof T, K3 extends keyof T>(object: T, ...keys: [K1[], K2[], K3[]]): [Pick<T, K1>, Pick<T, K2>, Pick<T, K3>, Omit<T, K1 | K2 | K3>];
125
+ declare function split<T extends object, K1 extends keyof T, K2 extends keyof T, K3 extends keyof T, K4 extends keyof T>(object: T, ...keys: [K1[], K2[], K3[], K4[]]): [Pick<T, K1>, Pick<T, K2>, Pick<T, K3>, Pick<T, K4>, Omit<T, K1 | K2 | K3 | K4>];
126
+ declare function split<T extends object, K1 extends keyof T, K2 extends keyof T, K3 extends keyof T, K4 extends keyof T, K5 extends keyof T>(object: T, ...keys: [K1[], K2[], K3[], K4[], K5[]]): [
127
+ Pick<T, K1>,
128
+ Pick<T, K2>,
129
+ Pick<T, K3>,
130
+ Pick<T, K4>,
131
+ Pick<T, K5>,
132
+ Omit<T, K1 | K2 | K3 | K4 | K5>
133
+ ];
134
+ /**
135
+ * Merges multiple objects into a single one. Only the first level of properties is merged. An alternative to `{ ...a, ...b, ...c }`.
136
+ * @param ...objects objects to merge
137
+ * @example
138
+ * const d = merge(a, b, c)
139
+ */
140
+ declare function merge<A extends object, B extends object>(a: A, b: B): Modify<A, B>;
141
+ declare function merge<A extends object, B extends object, C extends object>(a: A, b: B, c: C): Modify<Modify<A, B>, C>;
142
+ declare function merge<A extends object, B extends object, C extends object, D extends object>(a: A, b: B, c: C, d: D): Modify<Modify<Modify<A, B>, C>, D>;
143
+ declare function merge<A extends object, B extends object, C extends object, D extends object, E extends object>(a: A, b: B, c: C, d: D, e: E): Modify<Modify<Modify<Modify<A, B>, C>, D>, E>;
144
+ declare function merge<A extends object, B extends object, C extends object, D extends object, E extends object, F extends object>(a: A, b: B, c: C, d: D, e: E, f: F): Modify<Modify<Modify<Modify<Modify<A, B>, C>, D>, E>, F>;
145
+
146
+ /**
147
+ * non-mutating `Array.prototype.push()`
148
+ * @returns changed array copy
149
+ */
150
+ declare const push: <T>(list: readonly T[], ...items: T[]) => T[];
151
+ /**
152
+ * non-mutating function that drops n items from the array start.
153
+ * @returns changed array copy
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * const newList = drop([1,2,3])
158
+ * newList // => [2,3]
159
+ *
160
+ * const newList = drop([1,2,3], 2)
161
+ * newList // => [3]
162
+ * ```
163
+ */
164
+ declare const drop: <T>(list: T[], n?: number) => T[];
165
+ /**
166
+ * non-mutating function that drops n items from the array end.
167
+ * @returns changed array copy
168
+ *
169
+ * @example
170
+ * ```ts
171
+ * const newList = dropRight([1,2,3])
172
+ * newList // => [1,2]
173
+ *
174
+ * const newList = dropRight([1,2,3], 2)
175
+ * newList // => [1]
176
+ * ```
177
+ */
178
+ declare const dropRight: <T>(list: T[], n?: number) => T[];
179
+ /**
180
+ * standalone `Array.prototype.filter()` that filters out passed item
181
+ * @returns changed array copy
182
+ */
183
+ declare const filterOut: <T>(list: readonly T[], item: T) => T[] & {
184
+ removed: number;
185
+ };
186
+ /**
187
+ * standalone `Array.prototype.filter()`
188
+ * @returns changed array copy
189
+ */
190
+ declare function filter<T>(list: readonly T[], predicate: Predicate<T>): T[] & {
191
+ removed: number;
192
+ };
193
+ /**
194
+ * non-mutating `Array.prototype.sort()` as a standalone function
195
+ * @returns changed array copy
196
+ */
197
+ declare const sort: <T>(list: T[], compareFn?: ((a: T, b: T) => number) | undefined) => T[];
198
+ /**
199
+ * standalone `Array.prototype.map()` function
200
+ */
201
+ declare const map: <T, V>(list: readonly T[], mapFn: MappingFn<T, V>) => V[];
202
+ /**
203
+ * standalone `Array.prototype.slice()` function
204
+ */
205
+ declare const slice: <T>(list: readonly T[], start?: number, end?: number) => T[];
206
+ /**
207
+ * non-mutating `Array.prototype.splice()` as a standalone function
208
+ * @returns changed array copy
209
+ */
210
+ declare const splice: <T>(list: readonly T[], start: number, deleteCount?: number, ...items: T[]) => T[];
211
+ /**
212
+ * non-mutating `Array.prototype.fill()` as a standalone function
213
+ * @returns changed array copy
214
+ */
215
+ declare const fill: <T>(list: readonly T[], value: T, start?: number, end?: number) => T[];
216
+ /**
217
+ * Creates a new array concatenating array with any additional arrays and/or values.
218
+ * @param ...a values or arrays
219
+ * @returns new concatenated array
220
+ */
221
+ declare function concat<A extends any[], V extends ItemsOf<A>>(...a: A): Array<V extends any[] ? ItemsOf<V> : V>;
222
+ /**
223
+ * Remove item from array
224
+ * @returns changed array copy
225
+ */
226
+ declare const remove: <T>(list: readonly T[], item: T, ...insertItems: T[]) => T[];
227
+ /**
228
+ * Remove multiple items from an array
229
+ * @returns changed array copy
230
+ */
231
+ declare const removeItems: <T>(list: readonly T[], ...items: T[]) => T[];
232
+ /**
233
+ * Flattens a nested array into a one-level array
234
+ * @returns changed array copy
235
+ */
236
+ declare const flatten: <T extends any[]>(arr: T) => FlattenArray<T>[];
237
+ /**
238
+ * Sort an array by object key, or multiple keys
239
+ * @returns changed array copy
240
+ */
241
+ declare const sortBy: <T>(arr: T[], ...paths: T extends object ? (Many<keyof T> | Many<(item: T) => any>)[] : Many<(item: T) => any>[]) => T[];
242
+ /**
243
+ * Returns a subset of items that are instances of provided Classes
244
+ * @param list list of original items
245
+ * @param ...classes list or classes
246
+ * @returns changed array copy
247
+ */
248
+ declare const filterInstance: <T, I extends AnyClass[]>(list: readonly T[], ...classes: I) => Extract<T, InstanceType<ItemsOf<I>>>[];
249
+ /**
250
+ * Returns a subset of items that aren't instances of provided Classes
251
+ * @param list list of original items
252
+ * @param ...classes list or classes
253
+ * @returns changed array copy
254
+ */
255
+ declare const filterOutInstance: <T, I extends AnyClass[]>(list: readonly T[], ...classes: I) => Exclude<T, InstanceType<ItemsOf<I>>>[];
256
+
257
+ export { FlattenArray, MappingFn, ModifyValue, Predicate, Update, UpdateSetter, add, clamp, concat, divide, drop, dropRight, fill, filter, filterInstance, filterOut, filterOutInstance, flatten, get, map, merge, multiply, omit, pick, power, push, remove, removeItems, shallowArrayCopy, shallowCopy, shallowObjectCopy, slice, sort, sortBy, splice, split, substract, update, withArrayCopy, withCopy, withObjectCopy };
@@ -0,0 +1,148 @@
1
+ import { compare, ofClass } from '../chunk/G4OJDHGR.js';
2
+
3
+ // src/immutable/copy.ts
4
+ var shallowArrayCopy = (array) => array.slice();
5
+ var shallowObjectCopy = (object) => Object.assign({}, object);
6
+ var shallowCopy = (source) => Array.isArray(source) ? shallowArrayCopy(source) : shallowObjectCopy(source);
7
+ var withArrayCopy = (array, mutator) => {
8
+ const copy = shallowArrayCopy(array);
9
+ mutator(copy);
10
+ return copy;
11
+ };
12
+ var withObjectCopy = (object, mutator) => {
13
+ const copy = shallowObjectCopy(object);
14
+ mutator(copy);
15
+ return copy;
16
+ };
17
+ var withCopy = (source, mutator) => Array.isArray(source) ? withArrayCopy(source, mutator) : withObjectCopy(source, mutator);
18
+
19
+ // src/immutable/number.ts
20
+ function add(...a) {
21
+ let r = 0;
22
+ for (const n of a) {
23
+ r += n;
24
+ }
25
+ return r;
26
+ }
27
+ var substract = (a, ...b) => {
28
+ for (const n of b) {
29
+ a -= n;
30
+ }
31
+ return a;
32
+ };
33
+ var multiply = (a, ...b) => {
34
+ for (const n of b) {
35
+ a *= n;
36
+ }
37
+ return a;
38
+ };
39
+ var divide = (a, ...b) => {
40
+ for (const n of b) {
41
+ a /= n;
42
+ }
43
+ return a;
44
+ };
45
+ var power = (a, ...b) => {
46
+ for (const n of b) {
47
+ a = a ** n;
48
+ }
49
+ return a;
50
+ };
51
+ var clamp = (n, min, max) => Math.min(max, Math.max(min, n));
52
+
53
+ // src/immutable/update.ts
54
+ var update = (...args) => withCopy(args[0], (obj) => {
55
+ if (args.length > 3)
56
+ obj[args[1]] = update(obj[args[1]], ...args.slice(2));
57
+ else if (typeof args[2] === "function")
58
+ obj[args[1]] = args[2](obj[args[1]]);
59
+ else
60
+ obj[args[1]] = args[2];
61
+ });
62
+
63
+ // src/immutable/object.ts
64
+ var omit = (object, ...keys) => withObjectCopy(object, (object2) => keys.forEach((key) => delete object2[key]));
65
+ var pick = (object, ...keys) => keys.reduce((n, k) => {
66
+ if (k in object)
67
+ n[k] = object[k];
68
+ return n;
69
+ }, {});
70
+ function get(obj, ...keys) {
71
+ let res = obj;
72
+ for (const key of keys) {
73
+ res = res[key];
74
+ }
75
+ return res;
76
+ }
77
+ function split(object, ...list) {
78
+ const _list = typeof list[0] === "string" ? [list] : list;
79
+ const copy = shallowObjectCopy(object);
80
+ const result = [];
81
+ for (let i = 0; i < _list.length; i++) {
82
+ const keys = _list[i];
83
+ result.push({});
84
+ for (const key of keys) {
85
+ result[i][key] = copy[key];
86
+ delete copy[key];
87
+ }
88
+ }
89
+ return [...result, copy];
90
+ }
91
+ function merge(...objects) {
92
+ const result = {};
93
+ for (const obj of objects) {
94
+ Object.assign(result, obj);
95
+ }
96
+ return result;
97
+ }
98
+
99
+ // src/immutable/array.ts
100
+ var push = (list, ...items) => withArrayCopy(list, (list2) => list2.push(...items));
101
+ var drop = (list, n = 1) => list.slice(n);
102
+ var dropRight = (list, n = 1) => list.slice(0, list.length - n);
103
+ var filterOut = (list, item) => filter(list, (i) => i !== item);
104
+ function filter(list, predicate) {
105
+ const newList = list.filter(predicate);
106
+ newList.removed = list.length - newList.length;
107
+ return newList;
108
+ }
109
+ var sort = (list, compareFn) => list.slice().sort(compareFn);
110
+ var map = (list, mapFn) => list.map(mapFn);
111
+ var slice = (list, start, end) => list.slice(start, end);
112
+ var splice = (list, start, deleteCount = 0, ...items) => withArrayCopy(list, (list2) => list2.splice(start, deleteCount, ...items));
113
+ var fill = (list, value, start, end) => list.slice().fill(value, start, end);
114
+ function concat(...a) {
115
+ const result = [];
116
+ for (const i in a) {
117
+ Array.isArray(a[i]) ? result.push(...a[i]) : result.push(a[i]);
118
+ }
119
+ return result;
120
+ }
121
+ var remove = (list, item, ...insertItems) => {
122
+ const index = list.indexOf(item);
123
+ return splice(list, index, 1, ...insertItems);
124
+ };
125
+ var removeItems = (list, ...items) => {
126
+ const res = [];
127
+ for (let i = 0; i < list.length; i++) {
128
+ const item = list[i];
129
+ const ii = items.indexOf(item);
130
+ if (ii !== -1)
131
+ items.splice(ii, 1);
132
+ else
133
+ res.push(item);
134
+ }
135
+ return res;
136
+ };
137
+ var flatten = (arr) => arr.reduce((flat, next) => flat.concat(Array.isArray(next) ? flatten(next) : next), []);
138
+ var sortBy = (arr, ...paths) => flatten(paths).reduce(
139
+ (source, path) => sort(
140
+ source,
141
+ (a, b) => typeof path === "function" ? compare(path(a), path(b)) : compare(get(a, path), get(b, path))
142
+ ),
143
+ arr
144
+ );
145
+ var filterInstance = (list, ...classes) => classes.length === 1 ? list.filter((item) => ofClass(item, classes[0])) : list.filter((item) => item && classes.some((c) => ofClass(item, c)));
146
+ var filterOutInstance = (list, ...classes) => classes.length === 1 ? list.filter((item) => item && !ofClass(item, classes[0])) : list.filter((item) => item && !classes.some((c) => ofClass(item, c)));
147
+
148
+ export { add, clamp, concat, divide, drop, dropRight, fill, filter, filterInstance, filterOut, filterOutInstance, flatten, get, map, merge, multiply, omit, pick, power, push, remove, removeItems, shallowArrayCopy, shallowCopy, shallowObjectCopy, slice, sort, sortBy, splice, split, substract, update, withArrayCopy, withCopy, withObjectCopy };
@@ -1,89 +1,9 @@
1
- import { Accessor, Setter, AccessorArray, NoInfer, EffectFunction, onCleanup, SignalOptions, createSignal } from 'solid-js';
1
+ import { Accessor, AccessorArray, NoInfer, EffectFunction, onCleanup, SignalOptions, createSignal } from 'solid-js';
2
2
  export { isServer } from 'solid-js/web';
3
+ import { N as Noop, A as AnyClass, a as AnyObject, M as MaybeAccessorValue, b as MaybeAccessor, c as AnyFunction } from '../types-38269011.js';
4
+ export { f as AccessReturnTypes, k as AnyStatic, i as DeepPartialAny, D as Directive, E as ExtractIfPossible, m as Falsy, F as FalsyValue, I as ItemsOf, e as ItemsOfMany, d as Many, g as Modify, h as ModifyDeep, r as Narrow, s as NoInfer, j as NonIterable, O as OnAccessEffectFunction, n as Position, P as PrimitiveValue, R as RequiredKeys, S as SetterParam, p as Simplify, o as Size, T as Tail, l as Truthy, q as UnboxLazy, U as UnionToIntersection, V as Values } from '../types-38269011.js';
3
5
  export { EffectOptions, OnOptions, ResolvedChildren, ResolvedJSXElement } from 'solid-js/types/reactive/signal';
4
6
 
5
- /**
6
- * Can be single or in an array
7
- */
8
- type Many<T> = T | T[];
9
- type Values<O extends Object> = O[keyof O];
10
- type Noop = (...a: any[]) => void;
11
- type Directive<P = true> = (el: Element, props: Accessor<P>) => void;
12
- /**
13
- * Infers the type of the array elements
14
- */
15
- type ItemsOf<T> = T extends (infer E)[] ? E : never;
16
- type ItemsOfMany<T> = T extends any[] ? ItemsOf<T> : T;
17
- type SetterParam<T> = Parameters<Setter<T>>[0];
18
- /**
19
- * T or a reactive/non-reactive function returning T
20
- */
21
- type MaybeAccessor<T> = T | Accessor<T>;
22
- /**
23
- * Accessed value of a MaybeAccessor
24
- * @example
25
- * ```ts
26
- * MaybeAccessorValue<MaybeAccessor<string>>
27
- * // => string
28
- * MaybeAccessorValue<MaybeAccessor<() => string>>
29
- * // => string | (() => string)
30
- * MaybeAccessorValue<MaybeAccessor<string> | Function>
31
- * // => string | void
32
- * ```
33
- */
34
- type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends () => any ? ReturnType<T> : T;
35
- type OnAccessEffectFunction<S, Prev, Next extends Prev = Prev> = (input: AccessReturnTypes<S>, prevInput: AccessReturnTypes<S>, v: Prev) => Next;
36
- type AccessReturnTypes<S> = S extends MaybeAccessor<any>[] ? {
37
- [I in keyof S]: AccessReturnTypes<S[I]>;
38
- } : MaybeAccessorValue<S>;
39
- /** Allows to make shallow overwrites to an interface */
40
- type Modify<T, R> = Omit<T, keyof R> & R;
41
- /** Allows to make nested overwrites to an interface */
42
- type ModifyDeep<A extends AnyObject, B extends DeepPartialAny<A>> = {
43
- [K in keyof A]: B[K] extends never ? A[K] : B[K] extends AnyObject ? ModifyDeep<A[K], B[K]> : B[K];
44
- } & (A extends AnyObject ? Omit<B, keyof A> : A);
45
- /** Makes each property optional and turns each leaf property into any, allowing for type overrides by narrowing any. */
46
- type DeepPartialAny<T> = {
47
- [P in keyof T]?: T[P] extends AnyObject ? DeepPartialAny<T[P]> : any;
48
- };
49
- /** Removes the `[...list]` functionality */
50
- type NonIterable<T> = T & {
51
- [Symbol.iterator]: never;
52
- };
53
- /** Unwraps the type definition of an object, making it more readable */
54
- type Simplify<T> = T extends object ? {
55
- [K in keyof T]: T[K];
56
- } : T;
57
- /** Unboxes type definition, making it more readable */
58
- type UnboxLazy<T> = T extends () => infer U ? U : T;
59
- /** Get the required keys of an object */
60
- type RequiredKeys<T> = keyof {
61
- [K in keyof T as T extends {
62
- [_ in K]: unknown;
63
- } ? K : never]: 0;
64
- };
65
- /** Remove the first item of a tuple [1, 2, 3, 4] => [2, 3, 4] */
66
- type Tail<T extends any[]> = ((...t: T) => void) extends (x: any, ...u: infer U) => void ? U : never;
67
- /** `A | B => A & B` */
68
- type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
69
- type ExtractIfPossible<T, U> = Extract<T, U> extends never ? U : Extract<T, U>;
70
- type AnyObject = Record<PropertyKey, any>;
71
- type AnyStatic = [] | any[] | AnyObject;
72
- type AnyFunction = (...args: any[]) => any;
73
- type AnyClass = abstract new (...args: any) => any;
74
- type PrimitiveValue = PropertyKey | boolean | bigint | null | undefined;
75
- type FalsyValue = false | 0 | "" | null | undefined;
76
- type Truthy<T> = Exclude<T, FalsyValue>;
77
- type Falsy<T> = Extract<T, FalsyValue>;
78
- type Position = {
79
- x: number;
80
- y: number;
81
- };
82
- type RawNarrow<T> = (T extends [] ? [] : never) | (T extends string | number | bigint | boolean ? T : never) | {
83
- [K in keyof T]: T[K] extends Function ? T[K] : RawNarrow<T[K]>;
84
- };
85
- type Narrow<T extends any> = T extends [] ? T : RawNarrow<T>;
86
-
87
7
  /** no operation */
88
8
  declare const noop: Noop;
89
9
  declare const trueFn: () => boolean;
@@ -199,4 +119,4 @@ declare const createHydrateSignal: typeof createHydratableSignal;
199
119
  */
200
120
  declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
201
121
 
202
- export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Narrow, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, RequiredKeys, SetterParam, Simplify, Tail, Truthy, UnboxLazy, UnionToIntersection, Values, access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal, createHydrateSignal, createMicrotask, defer, entries, falseFn, filterNonNullable, handleDiffArray, isClient, isDev, isNonNullable, isObject, isProd, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess };
122
+ export { AnyClass, AnyFunction, AnyObject, MaybeAccessor, MaybeAccessorValue, Noop, access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal, createHydrateSignal, createMicrotask, defer, entries, falseFn, filterNonNullable, handleDiffArray, isClient, isDev, isNonNullable, isObject, isProd, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess };
@@ -0,0 +1 @@
1
+ export { access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal, createHydrateSignal, createMicrotask, defer, entries, falseFn, filterNonNullable, handleDiffArray, isClient, isDev, isNonNullable, isObject, isProd, isServer, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess } from '../chunk/G4OJDHGR.js';
@@ -0,0 +1,90 @@
1
+ import { Accessor, Setter } from 'solid-js';
2
+
3
+ /**
4
+ * Can be single or in an array
5
+ */
6
+ type Many<T> = T | T[];
7
+ type Values<O extends Object> = O[keyof O];
8
+ type Noop = (...a: any[]) => void;
9
+ type Directive<P = true> = (el: Element, props: Accessor<P>) => void;
10
+ /**
11
+ * Infers the type of the array elements
12
+ */
13
+ type ItemsOf<T> = T extends (infer E)[] ? E : never;
14
+ type ItemsOfMany<T> = T extends any[] ? ItemsOf<T> : T;
15
+ type SetterParam<T> = Parameters<Setter<T>>[0];
16
+ /**
17
+ * T or a reactive/non-reactive function returning T
18
+ */
19
+ type MaybeAccessor<T> = T | Accessor<T>;
20
+ /**
21
+ * Accessed value of a MaybeAccessor
22
+ * @example
23
+ * ```ts
24
+ * MaybeAccessorValue<MaybeAccessor<string>>
25
+ * // => string
26
+ * MaybeAccessorValue<MaybeAccessor<() => string>>
27
+ * // => string | (() => string)
28
+ * MaybeAccessorValue<MaybeAccessor<string> | Function>
29
+ * // => string | void
30
+ * ```
31
+ */
32
+ type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends () => any ? ReturnType<T> : T;
33
+ type OnAccessEffectFunction<S, Prev, Next extends Prev = Prev> = (input: AccessReturnTypes<S>, prevInput: AccessReturnTypes<S>, v: Prev) => Next;
34
+ type AccessReturnTypes<S> = S extends MaybeAccessor<any>[] ? {
35
+ [I in keyof S]: AccessReturnTypes<S[I]>;
36
+ } : MaybeAccessorValue<S>;
37
+ /** Allows to make shallow overwrites to an interface */
38
+ type Modify<T, R> = Omit<T, keyof R> & R;
39
+ /** Allows to make nested overwrites to an interface */
40
+ type ModifyDeep<A extends AnyObject, B extends DeepPartialAny<A>> = {
41
+ [K in keyof A]: B[K] extends never ? A[K] : B[K] extends AnyObject ? ModifyDeep<A[K], B[K]> : B[K];
42
+ } & (A extends AnyObject ? Omit<B, keyof A> : A);
43
+ /** Makes each property optional and turns each leaf property into any, allowing for type overrides by narrowing any. */
44
+ type DeepPartialAny<T> = {
45
+ [P in keyof T]?: T[P] extends AnyObject ? DeepPartialAny<T[P]> : any;
46
+ };
47
+ /** Removes the `[...list]` functionality */
48
+ type NonIterable<T> = T & {
49
+ [Symbol.iterator]: never;
50
+ };
51
+ /** Get the required keys of an object */
52
+ type RequiredKeys<T> = keyof {
53
+ [K in keyof T as T extends {
54
+ [_ in K]: unknown;
55
+ } ? K : never]: 0;
56
+ };
57
+ /** Remove the first item of a tuple [1, 2, 3, 4] => [2, 3, 4] */
58
+ type Tail<T extends any[]> = ((...t: T) => void) extends (x: any, ...u: infer U) => void ? U : never;
59
+ /** `A | B => A & B` */
60
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
61
+ type ExtractIfPossible<T, U> = Extract<T, U> extends never ? U : Extract<T, U>;
62
+ type AnyObject = Record<PropertyKey, any>;
63
+ type AnyStatic = [] | any[] | AnyObject;
64
+ type AnyFunction = (...args: any[]) => any;
65
+ type AnyClass = abstract new (...args: any) => any;
66
+ type PrimitiveValue = PropertyKey | boolean | bigint | null | undefined;
67
+ type FalsyValue = false | 0 | "" | null | undefined;
68
+ type Truthy<T> = Exclude<T, FalsyValue>;
69
+ type Falsy<T> = Extract<T, FalsyValue>;
70
+ type Position = {
71
+ x: number;
72
+ y: number;
73
+ };
74
+ type Size = {
75
+ width: number;
76
+ height: number;
77
+ };
78
+ /** Unwraps the type definition of an object, making it more readable */
79
+ type Simplify<T> = T extends object ? {
80
+ [K in keyof T]: T[K];
81
+ } : T;
82
+ /** Unboxes type definition, making it more readable */
83
+ type UnboxLazy<T> = T extends () => infer U ? U : T;
84
+ type RawNarrow<T> = (T extends [] ? [] : never) | (T extends string | number | bigint | boolean ? T : never) | {
85
+ [K in keyof T]: T[K] extends Function ? T[K] : RawNarrow<T[K]>;
86
+ };
87
+ type Narrow<T extends any> = T extends [] ? T : RawNarrow<T>;
88
+ type NoInfer<T> = [T][T extends any ? 0 : never];
89
+
90
+ export { AnyClass as A, Directive as D, ExtractIfPossible as E, FalsyValue as F, ItemsOf as I, MaybeAccessorValue as M, Noop as N, OnAccessEffectFunction as O, PrimitiveValue as P, RequiredKeys as R, SetterParam as S, Tail as T, UnionToIntersection as U, Values as V, AnyObject as a, MaybeAccessor as b, AnyFunction as c, Many as d, ItemsOfMany as e, AccessReturnTypes as f, Modify as g, ModifyDeep as h, DeepPartialAny as i, NonIterable as j, AnyStatic as k, Truthy as l, Falsy as m, Position as n, Size as o, Simplify as p, UnboxLazy as q, Narrow as r, NoInfer as s };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "6.0.0",
3
+ "version": "6.1.0",
4
4
  "description": "A bunch of reactive utility types and functions, for building primitives with Solid.js",
5
5
  "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>",
6
6
  "contributors": [
@@ -21,18 +21,33 @@
21
21
  "dist"
22
22
  ],
23
23
  "type": "module",
24
- "main": "./dist/index.cjs",
25
- "module": "./dist/index.js",
26
- "types": "./dist/index.d.ts",
24
+ "main": "./dist/index/index.cjs",
25
+ "module": "./dist/index/index.js",
26
+ "types": "./dist/index/index.d.ts",
27
27
  "browser": {},
28
28
  "exports": {
29
- "import": {
30
- "types": "./dist/index.d.ts",
31
- "default": "./dist/index.js"
29
+ ".": {
30
+ "import": {
31
+ "types": "./dist/index/index.d.ts",
32
+ "default": "./dist/index/index.js"
33
+ },
34
+ "require": "./dist/index/index.cjs"
32
35
  },
33
- "require": "./dist/index.cjs"
36
+ "./immutable": {
37
+ "import": {
38
+ "types": "./dist/immutable/index.d.ts",
39
+ "default": "./dist/immutable/index.js"
40
+ },
41
+ "require": "./dist/immutable/index.cjs"
42
+ }
43
+ },
44
+ "typesVersions": {
45
+ "*": {
46
+ "immutable": [
47
+ "./dist/immutable/index.d.ts"
48
+ ]
49
+ }
34
50
  },
35
- "typesVersions": {},
36
51
  "keywords": [
37
52
  "utilities",
38
53
  "reactivity",
File without changes
File without changes