@solid-primitives/utils 6.2.2 → 6.3.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/dist/immutable/array.d.ts +112 -0
- package/dist/immutable/array.js +139 -0
- package/dist/immutable/copy.d.ts +27 -0
- package/dist/immutable/copy.js +37 -0
- package/dist/immutable/index.d.ts +6 -257
- package/dist/immutable/index.js +6 -151
- package/dist/immutable/number.d.ts +13 -0
- package/dist/immutable/number.js +37 -0
- package/dist/immutable/object.d.ts +63 -0
- package/dist/immutable/object.js +53 -0
- package/dist/immutable/types.d.ts +7 -0
- package/dist/immutable/types.js +1 -0
- package/dist/immutable/update.d.ts +30 -0
- package/dist/immutable/update.js +29 -0
- package/dist/index.d.ts +126 -0
- package/dist/index.js +213 -0
- package/dist/types.d.ts +89 -0
- package/dist/types.js +1 -0
- package/package.json +7 -14
- package/dist/chunk/R5675YMU.js +0 -139
- package/dist/immutable/index.cjs +0 -161
- package/dist/immutable/index.d.cts +0 -257
- package/dist/index/index.cjs +0 -151
- package/dist/index/index.d.cts +0 -130
- package/dist/index/index.d.ts +0 -130
- package/dist/index/index.js +0 -1
- package/dist/types-d58cbe2f.d.ts +0 -90
package/dist/immutable/index.js
CHANGED
|
@@ -1,151 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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(
|
|
66
|
-
(n, k) => {
|
|
67
|
-
if (k in object)
|
|
68
|
-
n[k] = object[k];
|
|
69
|
-
return n;
|
|
70
|
-
},
|
|
71
|
-
{}
|
|
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 = 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
|
-
|
|
102
|
-
// src/immutable/array.ts
|
|
103
|
-
var push = (list, ...items) => withArrayCopy(list, (list2) => list2.push(...items));
|
|
104
|
-
var drop = (list, n = 1) => list.slice(n);
|
|
105
|
-
var dropRight = (list, n = 1) => list.slice(0, list.length - n);
|
|
106
|
-
var filterOut = (list, item) => filter(list, (i) => i !== item);
|
|
107
|
-
function filter(list, predicate) {
|
|
108
|
-
const newList = list.filter(predicate);
|
|
109
|
-
newList.removed = list.length - newList.length;
|
|
110
|
-
return newList;
|
|
111
|
-
}
|
|
112
|
-
var sort = (list, compareFn) => list.slice().sort(compareFn);
|
|
113
|
-
var map = (list, mapFn) => list.map(mapFn);
|
|
114
|
-
var slice = (list, start, end) => list.slice(start, end);
|
|
115
|
-
var splice = (list, start, deleteCount = 0, ...items) => withArrayCopy(list, (list2) => list2.splice(start, deleteCount, ...items));
|
|
116
|
-
var fill = (list, value, start, end) => list.slice().fill(value, start, end);
|
|
117
|
-
function concat(...a) {
|
|
118
|
-
const result = [];
|
|
119
|
-
for (const i in a) {
|
|
120
|
-
Array.isArray(a[i]) ? result.push(...a[i]) : result.push(a[i]);
|
|
121
|
-
}
|
|
122
|
-
return result;
|
|
123
|
-
}
|
|
124
|
-
var remove = (list, item, ...insertItems) => {
|
|
125
|
-
const index = list.indexOf(item);
|
|
126
|
-
return splice(list, index, 1, ...insertItems);
|
|
127
|
-
};
|
|
128
|
-
var removeItems = (list, ...items) => {
|
|
129
|
-
const res = [];
|
|
130
|
-
for (let i = 0; i < list.length; i++) {
|
|
131
|
-
const item = list[i];
|
|
132
|
-
const ii = items.indexOf(item);
|
|
133
|
-
if (ii !== -1)
|
|
134
|
-
items.splice(ii, 1);
|
|
135
|
-
else
|
|
136
|
-
res.push(item);
|
|
137
|
-
}
|
|
138
|
-
return res;
|
|
139
|
-
};
|
|
140
|
-
var flatten = (arr) => arr.reduce((flat, next) => flat.concat(Array.isArray(next) ? flatten(next) : next), []);
|
|
141
|
-
var sortBy = (arr, ...paths) => flatten(paths).reduce(
|
|
142
|
-
(source, path) => sort(
|
|
143
|
-
source,
|
|
144
|
-
(a, b) => typeof path === "function" ? compare(path(a), path(b)) : compare(get(a, path), get(b, path))
|
|
145
|
-
),
|
|
146
|
-
arr
|
|
147
|
-
);
|
|
148
|
-
var filterInstance = (list, ...classes) => classes.length === 1 ? list.filter((item) => ofClass(item, classes[0])) : list.filter((item) => item && classes.some((c) => ofClass(item, c)));
|
|
149
|
-
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)));
|
|
150
|
-
|
|
151
|
-
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
|
+
export * from "./copy.js";
|
|
2
|
+
export * from "./number.js";
|
|
3
|
+
export * from "./update.js";
|
|
4
|
+
export * from "./object.js";
|
|
5
|
+
export * from "./array.js";
|
|
6
|
+
export * from "./types.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** `a + b + c + ...` */
|
|
2
|
+
export declare function add(...a: number[]): number;
|
|
3
|
+
export declare function add(...a: string[]): string;
|
|
4
|
+
/** `a - b - c - ...` */
|
|
5
|
+
export declare const substract: (a: number, ...b: number[]) => number;
|
|
6
|
+
/** `a * b * c * ...` */
|
|
7
|
+
export declare const multiply: (a: number, ...b: number[]) => number;
|
|
8
|
+
/** `a / b / c / ...` */
|
|
9
|
+
export declare const divide: (a: number, ...b: number[]) => number;
|
|
10
|
+
/** `a ** b ** c ** ...` */
|
|
11
|
+
export declare const power: (a: number, ...b: number[]) => number;
|
|
12
|
+
/** clamp a number value between two other values */
|
|
13
|
+
export declare const clamp: (n: number, min: number, max: number) => number;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export function add(...a) {
|
|
2
|
+
let r = 0;
|
|
3
|
+
for (const n of a) {
|
|
4
|
+
r += n;
|
|
5
|
+
}
|
|
6
|
+
return r;
|
|
7
|
+
}
|
|
8
|
+
/** `a - b - c - ...` */
|
|
9
|
+
export const substract = (a, ...b) => {
|
|
10
|
+
for (const n of b) {
|
|
11
|
+
a -= n;
|
|
12
|
+
}
|
|
13
|
+
return a;
|
|
14
|
+
};
|
|
15
|
+
/** `a * b * c * ...` */
|
|
16
|
+
export const multiply = (a, ...b) => {
|
|
17
|
+
for (const n of b) {
|
|
18
|
+
a *= n;
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
/** `a / b / c / ...` */
|
|
23
|
+
export const divide = (a, ...b) => {
|
|
24
|
+
for (const n of b) {
|
|
25
|
+
a /= n;
|
|
26
|
+
}
|
|
27
|
+
return a;
|
|
28
|
+
};
|
|
29
|
+
/** `a ** b ** c ** ...` */
|
|
30
|
+
export const power = (a, ...b) => {
|
|
31
|
+
for (const n of b) {
|
|
32
|
+
a = a ** n;
|
|
33
|
+
}
|
|
34
|
+
return a;
|
|
35
|
+
};
|
|
36
|
+
/** clamp a number value between two other values */
|
|
37
|
+
export const clamp = (n, min, max) => Math.min(max, Math.max(min, n));
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Modify } from "../index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Create a new subset object without the provided keys
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const newObject = omit({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
|
|
8
|
+
* newObject // => { c: "baz" }
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare const omit: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Omit<O, K>;
|
|
12
|
+
/**
|
|
13
|
+
* Create a new subset object with only the provided keys
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const newObject = pick({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
|
|
18
|
+
* newObject // => { a:"foo", b:"bar" }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare const pick: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Pick<O, K>;
|
|
22
|
+
/**
|
|
23
|
+
* Get a single property value of an object by specifying a path to it.
|
|
24
|
+
*/
|
|
25
|
+
export declare function get<O extends object, K extends keyof O>(obj: O, key: K): O[K];
|
|
26
|
+
export declare function get<O extends object, K1 extends keyof O, K2 extends keyof O[K1]>(obj: O, k1: K1, k2: K2): O[K1][K2];
|
|
27
|
+
export 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];
|
|
28
|
+
export 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];
|
|
29
|
+
export 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];
|
|
30
|
+
export 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];
|
|
31
|
+
/**
|
|
32
|
+
* Split object properties by keys into multiple object copies with a subset of selected properties.
|
|
33
|
+
*
|
|
34
|
+
* @param object original object
|
|
35
|
+
* @param ...keys keys to pick from the source, or multiple arrays of keys *(for splitting into more than 2 objects)*
|
|
36
|
+
* ```ts
|
|
37
|
+
* (keyof object)[][] | (keyof object)[]
|
|
38
|
+
* ```
|
|
39
|
+
* @returns array of subset objects
|
|
40
|
+
*/
|
|
41
|
+
export declare function split<T extends object, K extends keyof T>(object: T, ...keys: K[]): [Pick<T, K>, Omit<T, K>];
|
|
42
|
+
export 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>];
|
|
43
|
+
export 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>];
|
|
44
|
+
export 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>];
|
|
45
|
+
export 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[]]): [
|
|
46
|
+
Pick<T, K1>,
|
|
47
|
+
Pick<T, K2>,
|
|
48
|
+
Pick<T, K3>,
|
|
49
|
+
Pick<T, K4>,
|
|
50
|
+
Pick<T, K5>,
|
|
51
|
+
Omit<T, K1 | K2 | K3 | K4 | K5>
|
|
52
|
+
];
|
|
53
|
+
/**
|
|
54
|
+
* Merges multiple objects into a single one. Only the first level of properties is merged. An alternative to `{ ...a, ...b, ...c }`.
|
|
55
|
+
* @param ...objects objects to merge
|
|
56
|
+
* @example
|
|
57
|
+
* const d = merge(a, b, c)
|
|
58
|
+
*/
|
|
59
|
+
export declare function merge<A extends object, B extends object>(a: A, b: B): Modify<A, B>;
|
|
60
|
+
export declare function merge<A extends object, B extends object, C extends object>(a: A, b: B, c: C): Modify<Modify<A, B>, C>;
|
|
61
|
+
export 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>;
|
|
62
|
+
export 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>;
|
|
63
|
+
export 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>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { withObjectCopy, shallowObjectCopy } from "./copy.js";
|
|
2
|
+
/**
|
|
3
|
+
* Create a new subset object without the provided keys
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const newObject = omit({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
|
|
8
|
+
* newObject // => { c: "baz" }
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export const omit = (object, ...keys) => withObjectCopy(object, object => keys.forEach(key => delete object[key]));
|
|
12
|
+
/**
|
|
13
|
+
* Create a new subset object with only the provided keys
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const newObject = pick({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
|
|
18
|
+
* newObject // => { a:"foo", b:"bar" }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export const pick = (object, ...keys) => keys.reduce((n, k) => {
|
|
22
|
+
if (k in object)
|
|
23
|
+
n[k] = object[k];
|
|
24
|
+
return n;
|
|
25
|
+
}, {});
|
|
26
|
+
export function get(obj, ...keys) {
|
|
27
|
+
let res = obj;
|
|
28
|
+
for (const key of keys) {
|
|
29
|
+
res = res[key];
|
|
30
|
+
}
|
|
31
|
+
return res;
|
|
32
|
+
}
|
|
33
|
+
export function split(object, ...list) {
|
|
34
|
+
const _list = (typeof list[0] === "string" ? [list] : list);
|
|
35
|
+
const copy = shallowObjectCopy(object);
|
|
36
|
+
const result = [];
|
|
37
|
+
for (let i = 0; i < _list.length; i++) {
|
|
38
|
+
const keys = _list[i];
|
|
39
|
+
result.push({});
|
|
40
|
+
for (const key of keys) {
|
|
41
|
+
result[i][key] = copy[key];
|
|
42
|
+
delete copy[key];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return [...result, copy];
|
|
46
|
+
}
|
|
47
|
+
export function merge(...objects) {
|
|
48
|
+
const result = {};
|
|
49
|
+
for (const obj of objects) {
|
|
50
|
+
Object.assign(result, obj);
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ItemsOf } from "../index.js";
|
|
2
|
+
export type Predicate<T> = (item: T, index: number, array: readonly T[]) => boolean;
|
|
3
|
+
export type MappingFn<T, V> = (item: T, index: number, array: readonly T[]) => V;
|
|
4
|
+
export type FlattenArray<T> = T extends any[] ? FlattenArray<ItemsOf<T>> : T;
|
|
5
|
+
export type ModifyValue<O, K extends keyof O, V> = Omit<O, K> & {
|
|
6
|
+
[key in K]: V;
|
|
7
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ModifyValue } from "./types.js";
|
|
2
|
+
export type UpdateSetter<O, K extends keyof O, V> = V | ((prev: O[K]) => V);
|
|
3
|
+
export type Update = {
|
|
4
|
+
<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>>>>>;
|
|
5
|
+
<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>>>>;
|
|
6
|
+
<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>>>;
|
|
7
|
+
<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>>;
|
|
8
|
+
<O extends object, K extends keyof O, V>(object: O, key: K, setter: UpdateSetter<O, K, V>): ModifyValue<O, K, V>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Change single value in an object by key. Allows accessign nested objects by passing multiple keys.
|
|
12
|
+
*
|
|
13
|
+
* Performs a shallow copy of each accessed object.
|
|
14
|
+
*
|
|
15
|
+
* @param object original source
|
|
16
|
+
* @param ...keys keys of sequential accessed objects
|
|
17
|
+
* @param value a value to set in place of a previous one, or a setter function.
|
|
18
|
+
* ```ts
|
|
19
|
+
* V | ((prev: O[K]) => V)
|
|
20
|
+
* ```
|
|
21
|
+
* a new value doesn't have to have the same type as the original
|
|
22
|
+
* @returns changed copy of the original object
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const original = { foo: { bar: { baz: 123 }}};
|
|
26
|
+
* const newObj = update(original, "foo", "bar", "baz", prev => prev + 1)
|
|
27
|
+
* original // { foo: { bar: { baz: 123 }}}
|
|
28
|
+
* newObj // { foo: { bar: { baz: 124 }}}
|
|
29
|
+
*/
|
|
30
|
+
export declare const update: Update;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { withCopy } from "./copy.js";
|
|
2
|
+
/**
|
|
3
|
+
* Change single value in an object by key. Allows accessign nested objects by passing multiple keys.
|
|
4
|
+
*
|
|
5
|
+
* Performs a shallow copy of each accessed object.
|
|
6
|
+
*
|
|
7
|
+
* @param object original source
|
|
8
|
+
* @param ...keys keys of sequential accessed objects
|
|
9
|
+
* @param value a value to set in place of a previous one, or a setter function.
|
|
10
|
+
* ```ts
|
|
11
|
+
* V | ((prev: O[K]) => V)
|
|
12
|
+
* ```
|
|
13
|
+
* a new value doesn't have to have the same type as the original
|
|
14
|
+
* @returns changed copy of the original object
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const original = { foo: { bar: { baz: 123 }}};
|
|
18
|
+
* const newObj = update(original, "foo", "bar", "baz", prev => prev + 1)
|
|
19
|
+
* original // { foo: { bar: { baz: 123 }}}
|
|
20
|
+
* newObj // { foo: { bar: { baz: 124 }}}
|
|
21
|
+
*/
|
|
22
|
+
export const update = (...args) => withCopy(args[0], obj => {
|
|
23
|
+
if (args.length > 3)
|
|
24
|
+
obj[args[1]] = update(obj[args[1]], ...args.slice(2));
|
|
25
|
+
else if (typeof args[2] === "function")
|
|
26
|
+
obj[args[1]] = args[2](obj[args[1]]);
|
|
27
|
+
else
|
|
28
|
+
obj[args[1]] = args[2];
|
|
29
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { onCleanup, createSignal, Accessor, AccessorArray, EffectFunction, NoInfer, SignalOptions } from "solid-js";
|
|
2
|
+
import { isServer } from "solid-js/web";
|
|
3
|
+
import type { AnyClass, MaybeAccessor, MaybeAccessorValue, Noop, AnyObject, AnyFunction } from "./types.js";
|
|
4
|
+
export * from "./types.js";
|
|
5
|
+
export { isServer };
|
|
6
|
+
export declare const isClient: boolean;
|
|
7
|
+
export declare const isDev: boolean;
|
|
8
|
+
export declare const isProd: boolean;
|
|
9
|
+
/** no operation */
|
|
10
|
+
export declare const noop: Noop;
|
|
11
|
+
export declare const trueFn: () => boolean;
|
|
12
|
+
export declare const falseFn: () => boolean;
|
|
13
|
+
/** @deprecated use {@link equalFn} from "solid-js" */
|
|
14
|
+
export declare const defaultEquals: <T>(a: T, b: T) => boolean;
|
|
15
|
+
export declare const EQUALS_FALSE_OPTIONS: {
|
|
16
|
+
readonly equals: false;
|
|
17
|
+
};
|
|
18
|
+
export declare const INTERNAL_OPTIONS: {
|
|
19
|
+
readonly internal: true;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Check if the value is an instance of ___
|
|
23
|
+
*/
|
|
24
|
+
export declare const ofClass: (v: any, c: AnyClass) => boolean;
|
|
25
|
+
/** Check if value is typeof "object" or "function" */
|
|
26
|
+
export declare function isObject(value: any): value is AnyObject;
|
|
27
|
+
export declare const isNonNullable: <T>(i: T) => i is NonNullable<T>;
|
|
28
|
+
export declare const filterNonNullable: <T extends readonly unknown[]>(arr: T) => NonNullable<T[number]>[];
|
|
29
|
+
export declare const compare: (a: any, b: any) => number;
|
|
30
|
+
/**
|
|
31
|
+
* Check shallow array equality
|
|
32
|
+
*/
|
|
33
|
+
export declare const arrayEquals: (a: readonly unknown[], b: readonly unknown[]) => boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Returns a function that will call all functions in the order they were chained with the same arguments.
|
|
36
|
+
*/
|
|
37
|
+
export declare function chain<Args extends [] | any[]>(callbacks: {
|
|
38
|
+
[Symbol.iterator](): IterableIterator<((...args: Args) => any) | undefined>;
|
|
39
|
+
}): (...args: Args) => void;
|
|
40
|
+
/**
|
|
41
|
+
* Returns a function that will call all functions in the reversed order with the same arguments.
|
|
42
|
+
*/
|
|
43
|
+
export declare function reverseChain<Args extends [] | any[]>(callbacks: (((...args: Args) => any) | undefined)[]): (...args: Args) => void;
|
|
44
|
+
export declare const clamp: (n: number, min: number, max: number) => number;
|
|
45
|
+
/**
|
|
46
|
+
* Accesses the value of a MaybeAccessor
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* access("foo") // => "foo"
|
|
50
|
+
* access(() => "foo") // => "foo"
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
|
|
54
|
+
export declare const asArray: <T>(value: T) => (T extends any[] ? T[number] : NonNullable<T>)[];
|
|
55
|
+
/**
|
|
56
|
+
* Access an array of MaybeAccessors
|
|
57
|
+
* @example
|
|
58
|
+
* const list = [1, 2, () => 3)] // T: MaybeAccessor<number>[]
|
|
59
|
+
* const newList = accessArray(list) // T: number[]
|
|
60
|
+
*/
|
|
61
|
+
export declare const accessArray: <A extends unknown>(list: readonly A[]) => MaybeAccessorValue<A>[];
|
|
62
|
+
/**
|
|
63
|
+
* Run the function if the accessed value is not `undefined` nor `null`
|
|
64
|
+
* @param value
|
|
65
|
+
* @param fn
|
|
66
|
+
*/
|
|
67
|
+
export declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void;
|
|
68
|
+
export declare const asAccessor: <A extends MaybeAccessor<unknown>>(v: A) => Accessor<MaybeAccessorValue<A>>;
|
|
69
|
+
/** If value is a function – call it with a given arguments – otherwise get the value as is */
|
|
70
|
+
export declare function accessWith<T>(valueOrFn: T, ...args: T extends AnyFunction ? Parameters<T> : never): T extends AnyFunction ? ReturnType<T> : T;
|
|
71
|
+
/**
|
|
72
|
+
* Solid's `on` helper, but always defers and returns a provided initial value when if does instead of `undefined`.
|
|
73
|
+
*
|
|
74
|
+
* @param deps
|
|
75
|
+
* @param fn
|
|
76
|
+
* @param initialValue
|
|
77
|
+
*/
|
|
78
|
+
export declare function defer<S, Next extends Prev, Prev = Next>(deps: AccessorArray<S> | Accessor<S>, fn: (input: S, prevInput: S, prev: undefined | NoInfer<Prev>) => Next, initialValue: Next): EffectFunction<undefined | NoInfer<Next>, NoInfer<Next>>;
|
|
79
|
+
export declare function defer<S, Next extends Prev, Prev = Next>(deps: AccessorArray<S> | Accessor<S>, fn: (input: S, prevInput: S, prev: undefined | NoInfer<Prev>) => Next, initialValue?: undefined): EffectFunction<undefined | NoInfer<Next>>;
|
|
80
|
+
/**
|
|
81
|
+
* Get entries of an object
|
|
82
|
+
*/
|
|
83
|
+
export declare const entries: <T extends object>(obj: T) => [keyof T, T[keyof T]][];
|
|
84
|
+
/**
|
|
85
|
+
* Get keys of an object
|
|
86
|
+
*/
|
|
87
|
+
export declare const keys: <T extends object>(object: T) => (keyof T)[];
|
|
88
|
+
/**
|
|
89
|
+
* Solid's `onCleanup` that doesn't warn in development if used outside of a component.
|
|
90
|
+
*/
|
|
91
|
+
export declare const tryOnCleanup: typeof onCleanup;
|
|
92
|
+
export declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
|
|
93
|
+
push: (...callbacks: ((arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void)[]) => void;
|
|
94
|
+
execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
|
|
95
|
+
clear: VoidFunction;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Group synchronous function calls.
|
|
99
|
+
* @param fn
|
|
100
|
+
* @returns `fn`
|
|
101
|
+
*/
|
|
102
|
+
export declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (...a: A) => void;
|
|
103
|
+
/**
|
|
104
|
+
* A hydratable version of the {@link createSignal}. It will use the serverValue on the server and the update function on the client. If initialized during hydration it will use serverValue as the initial value and update it once hydration is complete.
|
|
105
|
+
*
|
|
106
|
+
* @param serverValue initial value of the state on the server
|
|
107
|
+
* @param update called once on the client or on hydration to initialize the value
|
|
108
|
+
* @param options {@link SignalOptions}
|
|
109
|
+
* @returns
|
|
110
|
+
* ```ts
|
|
111
|
+
* [state: Accessor<T>, setState: Setter<T>]
|
|
112
|
+
* ```
|
|
113
|
+
* @see {@link createSignal}
|
|
114
|
+
*/
|
|
115
|
+
export declare function createHydratableSignal<T>(serverValue: T, update: () => T, options?: SignalOptions<T>): ReturnType<typeof createSignal<T>>;
|
|
116
|
+
/** @deprecated use {@link createHydratableSignal} instead */
|
|
117
|
+
export declare const createHydrateSignal: typeof createHydratableSignal;
|
|
118
|
+
/**
|
|
119
|
+
* Handle items removed and added to the array by diffing it by refference.
|
|
120
|
+
*
|
|
121
|
+
* @param current new array instance
|
|
122
|
+
* @param prev previous array copy
|
|
123
|
+
* @param handleAdded called once for every added item to array
|
|
124
|
+
* @param handleRemoved called once for every removed from array
|
|
125
|
+
*/
|
|
126
|
+
export declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
|