@solid-primitives/utils 5.5.2 → 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 +82 -0
- package/dist/{index.js → chunk/G4OJDHGR.js} +6 -54
- package/dist/immutable/index.cjs +158 -0
- package/dist/immutable/index.d.ts +257 -0
- package/dist/immutable/index.js +148 -0
- package/dist/{index.cjs → index/index.cjs} +4 -54
- package/dist/index/index.d.ts +122 -0
- package/dist/index/index.js +1 -0
- package/dist/types-38269011.d.ts +90 -0
- package/package.json +24 -9
- package/dist/index.d.ts +0 -225
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)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DEV, untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount
|
|
1
|
+
import { DEV, untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount } from 'solid-js';
|
|
2
2
|
import { isServer } from 'solid-js/web';
|
|
3
3
|
export { isServer } from 'solid-js/web';
|
|
4
4
|
|
|
@@ -13,6 +13,8 @@ var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
|
|
|
13
13
|
function isObject(value) {
|
|
14
14
|
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
15
15
|
}
|
|
16
|
+
var isNonNullable = (i) => i != null;
|
|
17
|
+
var filterNonNullable = (arr) => arr.filter(isNonNullable);
|
|
16
18
|
var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
17
19
|
var arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
|
|
18
20
|
function chain(callbacks) {
|
|
@@ -31,7 +33,7 @@ function reverseChain(callbacks) {
|
|
|
31
33
|
}
|
|
32
34
|
var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
|
|
33
35
|
var access = (v) => typeof v === "function" && !v.length ? v() : v;
|
|
34
|
-
var asArray = (value) => Array.isArray(value) ? value : [value];
|
|
36
|
+
var asArray = (value) => Array.isArray(value) ? value : value ? [value] : [];
|
|
35
37
|
var accessArray = (list) => list.map((v) => access(v));
|
|
36
38
|
var withAccess = (value, fn) => {
|
|
37
39
|
const _value = access(value);
|
|
@@ -65,7 +67,7 @@ function defer(deps, fn, initialValue) {
|
|
|
65
67
|
}
|
|
66
68
|
var entries = Object.entries;
|
|
67
69
|
var keys = Object.keys;
|
|
68
|
-
var tryOnCleanup = (fn) => getOwner() ? onCleanup(fn) : fn;
|
|
70
|
+
var tryOnCleanup = isDev ? (fn) => getOwner() ? onCleanup(fn) : fn : onCleanup;
|
|
69
71
|
var createCallbackStack = () => {
|
|
70
72
|
let stack = [];
|
|
71
73
|
const clear = () => stack = [];
|
|
@@ -98,56 +100,6 @@ function createHydratableSignal(serverValue, update, options) {
|
|
|
98
100
|
return createSignal(update(), options);
|
|
99
101
|
}
|
|
100
102
|
var createHydrateSignal = createHydratableSignal;
|
|
101
|
-
function createStaticStore(init) {
|
|
102
|
-
const copy = { ...init };
|
|
103
|
-
const store = { ...init };
|
|
104
|
-
const cache = /* @__PURE__ */ new Map();
|
|
105
|
-
const getValue = (key) => {
|
|
106
|
-
const saved = cache.get(key);
|
|
107
|
-
if (saved)
|
|
108
|
-
return saved[0]();
|
|
109
|
-
const signal = createSignal(copy[key], {
|
|
110
|
-
internal: true
|
|
111
|
-
});
|
|
112
|
-
cache.set(key, signal);
|
|
113
|
-
delete copy[key];
|
|
114
|
-
return signal[0]();
|
|
115
|
-
};
|
|
116
|
-
const setValue = (key, value) => {
|
|
117
|
-
const saved = cache.get(key);
|
|
118
|
-
if (saved)
|
|
119
|
-
return saved[1](value);
|
|
120
|
-
if (key in copy)
|
|
121
|
-
copy[key] = accessWith(value, [copy[key]]);
|
|
122
|
-
};
|
|
123
|
-
for (const key in init) {
|
|
124
|
-
Object.defineProperty(store, key, { get: getValue.bind(void 0, key) });
|
|
125
|
-
}
|
|
126
|
-
const setter = (a, b) => {
|
|
127
|
-
if (isObject(a)) {
|
|
128
|
-
const entries2 = untrack(
|
|
129
|
-
() => Object.entries(accessWith(a, store))
|
|
130
|
-
);
|
|
131
|
-
batch(() => {
|
|
132
|
-
for (const [key, value] of entries2)
|
|
133
|
-
setValue(key, () => value);
|
|
134
|
-
});
|
|
135
|
-
} else
|
|
136
|
-
setValue(a, b);
|
|
137
|
-
return store;
|
|
138
|
-
};
|
|
139
|
-
return [store, setter];
|
|
140
|
-
}
|
|
141
|
-
function createHydratableStaticStore(serverValue, update) {
|
|
142
|
-
if (isServer)
|
|
143
|
-
return createStaticStore(serverValue);
|
|
144
|
-
if (sharedConfig.context) {
|
|
145
|
-
const [state, setState] = createStaticStore(serverValue);
|
|
146
|
-
onMount(() => setState(update()));
|
|
147
|
-
return [state, setState];
|
|
148
|
-
}
|
|
149
|
-
return createStaticStore(update());
|
|
150
|
-
}
|
|
151
103
|
function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
152
104
|
const currLength = current.length;
|
|
153
105
|
const prevLength = prev.length;
|
|
@@ -180,4 +132,4 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
|
180
132
|
}
|
|
181
133
|
}
|
|
182
134
|
|
|
183
|
-
export { access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal,
|
|
135
|
+
export { 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,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 };
|
|
@@ -14,6 +14,8 @@ exports.ofClass = (v, c) => v instanceof c || v && v.constructor === c;
|
|
|
14
14
|
function isObject(value) {
|
|
15
15
|
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
16
16
|
}
|
|
17
|
+
exports.isNonNullable = (i) => i != null;
|
|
18
|
+
exports.filterNonNullable = (arr) => arr.filter(exports.isNonNullable);
|
|
17
19
|
exports.compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
18
20
|
exports.arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
|
|
19
21
|
function chain(callbacks) {
|
|
@@ -32,7 +34,7 @@ function reverseChain(callbacks) {
|
|
|
32
34
|
}
|
|
33
35
|
exports.clamp = (n, min, max) => Math.min(Math.max(n, min), max);
|
|
34
36
|
exports.access = (v) => typeof v === "function" && !v.length ? v() : v;
|
|
35
|
-
exports.asArray = (value) => Array.isArray(value) ? value : [value];
|
|
37
|
+
exports.asArray = (value) => Array.isArray(value) ? value : value ? [value] : [];
|
|
36
38
|
exports.accessArray = (list) => list.map((v) => exports.access(v));
|
|
37
39
|
exports.withAccess = (value, fn) => {
|
|
38
40
|
const _value = exports.access(value);
|
|
@@ -66,7 +68,7 @@ function defer(deps, fn, initialValue) {
|
|
|
66
68
|
}
|
|
67
69
|
exports.entries = Object.entries;
|
|
68
70
|
exports.keys = Object.keys;
|
|
69
|
-
exports.tryOnCleanup = (fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn;
|
|
71
|
+
exports.tryOnCleanup = exports.isDev ? (fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn : solidJs.onCleanup;
|
|
70
72
|
exports.createCallbackStack = () => {
|
|
71
73
|
let stack = [];
|
|
72
74
|
const clear = () => stack = [];
|
|
@@ -99,56 +101,6 @@ function createHydratableSignal(serverValue, update, options) {
|
|
|
99
101
|
return solidJs.createSignal(update(), options);
|
|
100
102
|
}
|
|
101
103
|
exports.createHydrateSignal = createHydratableSignal;
|
|
102
|
-
function createStaticStore(init) {
|
|
103
|
-
const copy = { ...init };
|
|
104
|
-
const store = { ...init };
|
|
105
|
-
const cache = /* @__PURE__ */ new Map();
|
|
106
|
-
const getValue = (key) => {
|
|
107
|
-
const saved = cache.get(key);
|
|
108
|
-
if (saved)
|
|
109
|
-
return saved[0]();
|
|
110
|
-
const signal = solidJs.createSignal(copy[key], {
|
|
111
|
-
internal: true
|
|
112
|
-
});
|
|
113
|
-
cache.set(key, signal);
|
|
114
|
-
delete copy[key];
|
|
115
|
-
return signal[0]();
|
|
116
|
-
};
|
|
117
|
-
const setValue = (key, value) => {
|
|
118
|
-
const saved = cache.get(key);
|
|
119
|
-
if (saved)
|
|
120
|
-
return saved[1](value);
|
|
121
|
-
if (key in copy)
|
|
122
|
-
copy[key] = accessWith(value, [copy[key]]);
|
|
123
|
-
};
|
|
124
|
-
for (const key in init) {
|
|
125
|
-
Object.defineProperty(store, key, { get: getValue.bind(void 0, key) });
|
|
126
|
-
}
|
|
127
|
-
const setter = (a, b) => {
|
|
128
|
-
if (isObject(a)) {
|
|
129
|
-
const entries2 = solidJs.untrack(
|
|
130
|
-
() => Object.entries(accessWith(a, store))
|
|
131
|
-
);
|
|
132
|
-
solidJs.batch(() => {
|
|
133
|
-
for (const [key, value] of entries2)
|
|
134
|
-
setValue(key, () => value);
|
|
135
|
-
});
|
|
136
|
-
} else
|
|
137
|
-
setValue(a, b);
|
|
138
|
-
return store;
|
|
139
|
-
};
|
|
140
|
-
return [store, setter];
|
|
141
|
-
}
|
|
142
|
-
function createHydratableStaticStore(serverValue, update) {
|
|
143
|
-
if (web.isServer)
|
|
144
|
-
return createStaticStore(serverValue);
|
|
145
|
-
if (solidJs.sharedConfig.context) {
|
|
146
|
-
const [state, setState] = createStaticStore(serverValue);
|
|
147
|
-
solidJs.onMount(() => setState(update()));
|
|
148
|
-
return [state, setState];
|
|
149
|
-
}
|
|
150
|
-
return createStaticStore(update());
|
|
151
|
-
}
|
|
152
104
|
function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
153
105
|
const currLength = current.length;
|
|
154
106
|
const prevLength = prev.length;
|
|
@@ -188,9 +140,7 @@ Object.defineProperty(exports, 'isServer', {
|
|
|
188
140
|
exports.accessWith = accessWith;
|
|
189
141
|
exports.chain = chain;
|
|
190
142
|
exports.createHydratableSignal = createHydratableSignal;
|
|
191
|
-
exports.createHydratableStaticStore = createHydratableStaticStore;
|
|
192
143
|
exports.createMicrotask = createMicrotask;
|
|
193
|
-
exports.createStaticStore = createStaticStore;
|
|
194
144
|
exports.defer = defer;
|
|
195
145
|
exports.handleDiffArray = handleDiffArray;
|
|
196
146
|
exports.isObject = isObject;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Accessor, AccessorArray, NoInfer, EffectFunction, onCleanup, SignalOptions, createSignal } from 'solid-js';
|
|
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';
|
|
5
|
+
export { EffectOptions, OnOptions, ResolvedChildren, ResolvedJSXElement } from 'solid-js/types/reactive/signal';
|
|
6
|
+
|
|
7
|
+
/** no operation */
|
|
8
|
+
declare const noop: Noop;
|
|
9
|
+
declare const trueFn: () => boolean;
|
|
10
|
+
declare const falseFn: () => boolean;
|
|
11
|
+
|
|
12
|
+
declare const isClient: boolean;
|
|
13
|
+
declare const isDev: boolean;
|
|
14
|
+
declare const isProd: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Check if the value is an instance of ___
|
|
17
|
+
*/
|
|
18
|
+
declare const ofClass: (v: any, c: AnyClass) => boolean;
|
|
19
|
+
/** Check if value is typeof "object" or "function" */
|
|
20
|
+
declare function isObject(value: any): value is AnyObject;
|
|
21
|
+
declare const isNonNullable: <T>(i: T) => i is NonNullable<T>;
|
|
22
|
+
declare const filterNonNullable: <T extends readonly unknown[]>(arr: T) => NonNullable<T[number]>[];
|
|
23
|
+
declare const compare: (a: any, b: any) => number;
|
|
24
|
+
/**
|
|
25
|
+
* Check shallow array equality
|
|
26
|
+
*/
|
|
27
|
+
declare const arrayEquals: (a: readonly unknown[], b: readonly unknown[]) => boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Returns a function that will call all functions in the order they were chained with the same arguments.
|
|
30
|
+
*/
|
|
31
|
+
declare function chain<Args extends [] | any[]>(callbacks: {
|
|
32
|
+
[Symbol.iterator](): IterableIterator<((...args: Args) => any) | undefined>;
|
|
33
|
+
}): (...args: Args) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Returns a function that will call all functions in the reversed order with the same arguments.
|
|
36
|
+
*/
|
|
37
|
+
declare function reverseChain<Args extends [] | any[]>(callbacks: (((...args: Args) => any) | undefined)[]): (...args: Args) => void;
|
|
38
|
+
declare const clamp: (n: number, min: number, max: number) => number;
|
|
39
|
+
/**
|
|
40
|
+
* Accesses the value of a MaybeAccessor
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* access("foo") // => "foo"
|
|
44
|
+
* access(() => "foo") // => "foo"
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
|
|
48
|
+
declare const asArray: <T>(value: T) => (T extends any[] ? T[number] : NonNullable<T>)[];
|
|
49
|
+
/**
|
|
50
|
+
* Access an array of MaybeAccessors
|
|
51
|
+
* @example
|
|
52
|
+
* const list = [1, 2, () => 3)] // T: MaybeAccessor<number>[]
|
|
53
|
+
* const newList = accessArray(list) // T: number[]
|
|
54
|
+
*/
|
|
55
|
+
declare const accessArray: <A extends unknown>(list: readonly A[]) => MaybeAccessorValue<A>[];
|
|
56
|
+
/**
|
|
57
|
+
* Run the function if the accessed value is not `undefined` nor `null`
|
|
58
|
+
* @param value
|
|
59
|
+
* @param fn
|
|
60
|
+
*/
|
|
61
|
+
declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void;
|
|
62
|
+
declare const asAccessor: <A extends unknown>(v: A) => Accessor<MaybeAccessorValue<A>>;
|
|
63
|
+
/** If value is a function – call it with a given arguments – otherwise get the value as is */
|
|
64
|
+
declare function accessWith<T>(valueOrFn: T, ...args: T extends AnyFunction ? Parameters<T> : never): T extends AnyFunction ? ReturnType<T> : T;
|
|
65
|
+
/**
|
|
66
|
+
* Solid's `on` helper, but always defers and returns a provided initial value when if does instead of `undefined`.
|
|
67
|
+
*
|
|
68
|
+
* @param deps
|
|
69
|
+
* @param fn
|
|
70
|
+
* @param initialValue
|
|
71
|
+
*/
|
|
72
|
+
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>>;
|
|
73
|
+
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>>;
|
|
74
|
+
/**
|
|
75
|
+
* Get entries of an object
|
|
76
|
+
*/
|
|
77
|
+
declare const entries: <T extends object>(obj: T) => [keyof T, T[keyof T]][];
|
|
78
|
+
/**
|
|
79
|
+
* Get keys of an object
|
|
80
|
+
*/
|
|
81
|
+
declare const keys: <T extends object>(object: T) => (keyof T)[];
|
|
82
|
+
/**
|
|
83
|
+
* Solid's `onCleanup` that doesn't warn in development if used outside of a component.
|
|
84
|
+
*/
|
|
85
|
+
declare const tryOnCleanup: typeof onCleanup;
|
|
86
|
+
declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
|
|
87
|
+
push: (...callbacks: ((arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void)[]) => void;
|
|
88
|
+
execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
|
|
89
|
+
clear: VoidFunction;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Group synchronous function calls.
|
|
93
|
+
* @param fn
|
|
94
|
+
* @returns `fn`
|
|
95
|
+
*/
|
|
96
|
+
declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (...a: A) => void;
|
|
97
|
+
/**
|
|
98
|
+
* 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.
|
|
99
|
+
*
|
|
100
|
+
* @param serverValue initial value of the state on the server
|
|
101
|
+
* @param update called once on the client or on hydration to initialize the value
|
|
102
|
+
* @param options {@link SignalOptions}
|
|
103
|
+
* @returns
|
|
104
|
+
* ```ts
|
|
105
|
+
* [state: Accessor<T>, setState: Setter<T>]
|
|
106
|
+
* ```
|
|
107
|
+
* @see {@link createSignal}
|
|
108
|
+
*/
|
|
109
|
+
declare function createHydratableSignal<T>(serverValue: T, update: () => T, options?: SignalOptions<T>): ReturnType<typeof createSignal<T>>;
|
|
110
|
+
/** @deprecated use {@link createHydratableSignal} instead */
|
|
111
|
+
declare const createHydrateSignal: typeof createHydratableSignal;
|
|
112
|
+
/**
|
|
113
|
+
* Handle items removed and added to the array by diffing it by refference.
|
|
114
|
+
*
|
|
115
|
+
* @param current new array instance
|
|
116
|
+
* @param prev previous array copy
|
|
117
|
+
* @param handleAdded called once for every added item to array
|
|
118
|
+
* @param handleRemoved called once for every removed from array
|
|
119
|
+
*/
|
|
120
|
+
declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
|
|
121
|
+
|
|
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": "
|
|
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,16 +21,32 @@
|
|
|
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
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
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
|
-
"
|
|
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
51
|
"keywords": [
|
|
36
52
|
"utilities",
|
|
@@ -41,7 +57,6 @@
|
|
|
41
57
|
"peerDependencies": {
|
|
42
58
|
"solid-js": "^1.6.12"
|
|
43
59
|
},
|
|
44
|
-
"typesVersions": {},
|
|
45
60
|
"scripts": {
|
|
46
61
|
"build": "jiti ../../scripts/build.ts",
|
|
47
62
|
"test": "vitest -c ../../configs/vitest.config.ts",
|
package/dist/index.d.ts
DELETED
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
import { Accessor, Setter, AccessorArray, NoInfer, EffectFunction, onCleanup, SignalOptions, createSignal } from 'solid-js';
|
|
2
|
-
export { isServer } from 'solid-js/web';
|
|
3
|
-
export { EffectOptions, OnOptions, ResolvedChildren, ResolvedJSXElement } from 'solid-js/types/reactive/signal';
|
|
4
|
-
|
|
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
|
-
/** no operation */
|
|
88
|
-
declare const noop: Noop;
|
|
89
|
-
declare const trueFn: () => boolean;
|
|
90
|
-
declare const falseFn: () => boolean;
|
|
91
|
-
|
|
92
|
-
declare const isClient: boolean;
|
|
93
|
-
declare const isDev: boolean;
|
|
94
|
-
declare const isProd: boolean;
|
|
95
|
-
/**
|
|
96
|
-
* Check if the value is an instance of ___
|
|
97
|
-
*/
|
|
98
|
-
declare const ofClass: (v: any, c: AnyClass) => boolean;
|
|
99
|
-
/** Check if value is typeof "object" or "function" */
|
|
100
|
-
declare function isObject(value: any): value is AnyObject;
|
|
101
|
-
declare const compare: (a: any, b: any) => number;
|
|
102
|
-
/**
|
|
103
|
-
* Check shallow array equality
|
|
104
|
-
*/
|
|
105
|
-
declare const arrayEquals: (a: readonly unknown[], b: readonly unknown[]) => boolean;
|
|
106
|
-
/**
|
|
107
|
-
* Returns a function that will call all functions in the order they were chained with the same arguments.
|
|
108
|
-
*/
|
|
109
|
-
declare function chain<Args extends [] | any[]>(callbacks: {
|
|
110
|
-
[Symbol.iterator](): IterableIterator<((...args: Args) => any) | undefined>;
|
|
111
|
-
}): (...args: Args) => void;
|
|
112
|
-
/**
|
|
113
|
-
* Returns a function that will call all functions in the reversed order with the same arguments.
|
|
114
|
-
*/
|
|
115
|
-
declare function reverseChain<Args extends [] | any[]>(callbacks: (((...args: Args) => any) | undefined)[]): (...args: Args) => void;
|
|
116
|
-
declare const clamp: (n: number, min: number, max: number) => number;
|
|
117
|
-
/**
|
|
118
|
-
* Accesses the value of a MaybeAccessor
|
|
119
|
-
* @example
|
|
120
|
-
* ```ts
|
|
121
|
-
* access("foo") // => "foo"
|
|
122
|
-
* access(() => "foo") // => "foo"
|
|
123
|
-
* ```
|
|
124
|
-
*/
|
|
125
|
-
declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
|
|
126
|
-
declare const asArray: <T>(value: T) => (T extends any[] ? T[number] : T)[];
|
|
127
|
-
/**
|
|
128
|
-
* Access an array of MaybeAccessors
|
|
129
|
-
* @example
|
|
130
|
-
* const list = [1, 2, () => 3)] // T: MaybeAccessor<number>[]
|
|
131
|
-
* const newList = accessArray(list) // T: number[]
|
|
132
|
-
*/
|
|
133
|
-
declare const accessArray: <A extends unknown>(list: readonly A[]) => MaybeAccessorValue<A>[];
|
|
134
|
-
/**
|
|
135
|
-
* Run the function if the accessed value is not `undefined` nor `null`
|
|
136
|
-
* @param value
|
|
137
|
-
* @param fn
|
|
138
|
-
*/
|
|
139
|
-
declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void;
|
|
140
|
-
declare const asAccessor: <A extends unknown>(v: A) => Accessor<MaybeAccessorValue<A>>;
|
|
141
|
-
/** If value is a function – call it with a given arguments – otherwise get the value as is */
|
|
142
|
-
declare function accessWith<T>(valueOrFn: T, ...args: T extends AnyFunction ? Parameters<T> : never): T extends AnyFunction ? ReturnType<T> : T;
|
|
143
|
-
/**
|
|
144
|
-
* Solid's `on` helper, but always defers and returns a provided initial value when if does instead of `undefined`.
|
|
145
|
-
*
|
|
146
|
-
* @param deps
|
|
147
|
-
* @param fn
|
|
148
|
-
* @param initialValue
|
|
149
|
-
*/
|
|
150
|
-
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>>;
|
|
151
|
-
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>>;
|
|
152
|
-
/**
|
|
153
|
-
* Get entries of an object
|
|
154
|
-
*/
|
|
155
|
-
declare const entries: <T extends object>(obj: T) => [keyof T, T[keyof T]][];
|
|
156
|
-
/**
|
|
157
|
-
* Get keys of an object
|
|
158
|
-
*/
|
|
159
|
-
declare const keys: <T extends object>(object: T) => (keyof T)[];
|
|
160
|
-
/**
|
|
161
|
-
* Solid's `onCleanup` that is registered only if there is a root.
|
|
162
|
-
*/
|
|
163
|
-
declare const tryOnCleanup: typeof onCleanup;
|
|
164
|
-
declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
|
|
165
|
-
push: (...callbacks: ((arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void)[]) => void;
|
|
166
|
-
execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
|
|
167
|
-
clear: VoidFunction;
|
|
168
|
-
};
|
|
169
|
-
/**
|
|
170
|
-
* Group synchronous function calls.
|
|
171
|
-
* @param fn
|
|
172
|
-
* @returns `fn`
|
|
173
|
-
*/
|
|
174
|
-
declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (...a: A) => void;
|
|
175
|
-
/**
|
|
176
|
-
* 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.
|
|
177
|
-
*
|
|
178
|
-
* @param serverValue initial value of the state on the server
|
|
179
|
-
* @param update called once on the client or on hydration to initialize the value
|
|
180
|
-
* @param options {@link SignalOptions}
|
|
181
|
-
* @returns
|
|
182
|
-
* ```ts
|
|
183
|
-
* [state: Accessor<T>, setState: Setter<T>]
|
|
184
|
-
* ```
|
|
185
|
-
* @see {@link createSignal}
|
|
186
|
-
*/
|
|
187
|
-
declare function createHydratableSignal<T>(serverValue: T, update: () => T, options?: SignalOptions<T>): ReturnType<typeof createSignal<T>>;
|
|
188
|
-
/** @deprecated use {@link createHydratableSignal} instead */
|
|
189
|
-
declare const createHydrateSignal: typeof createHydratableSignal;
|
|
190
|
-
type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
|
|
191
|
-
(setter: (prev: T) => Partial<T>): T;
|
|
192
|
-
(state: Partial<T>): T;
|
|
193
|
-
<K extends keyof T>(key: K, state: SetterParam<T[K]>): T;
|
|
194
|
-
};
|
|
195
|
-
/**
|
|
196
|
-
* A shallowly wrapped reactive store object. It behaves similarly to the createStore, but with limited features to keep it simple. Designed to be used for reactive objects with static keys, but dynamic values, like reactive Event State, location, etc.
|
|
197
|
-
* @param init initial value of the store
|
|
198
|
-
* @returns
|
|
199
|
-
* ```ts
|
|
200
|
-
* [access: Readonly<T>, write: StaticStoreSetter<T>]
|
|
201
|
-
* ```
|
|
202
|
-
*/
|
|
203
|
-
declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
|
|
204
|
-
/**
|
|
205
|
-
* A hydratable version of the {@link createStaticStore}. 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.
|
|
206
|
-
*
|
|
207
|
-
* @param serverValue initial value of the state on the server
|
|
208
|
-
* @param update called once on the client or on hydration to initialize the value
|
|
209
|
-
* @returns
|
|
210
|
-
* ```ts
|
|
211
|
-
* [access: Readonly<T>, write: StaticStoreSetter<T>]
|
|
212
|
-
* ```
|
|
213
|
-
*/
|
|
214
|
-
declare function createHydratableStaticStore<T extends Readonly<AnyStatic>>(serverValue: T, update: () => T): ReturnType<typeof createStaticStore<T>>;
|
|
215
|
-
/**
|
|
216
|
-
* Handle items removed and added to the array by diffing it by refference.
|
|
217
|
-
*
|
|
218
|
-
* @param current new array instance
|
|
219
|
-
* @param prev previous array copy
|
|
220
|
-
* @param handleAdded called once for every added item to array
|
|
221
|
-
* @param handleRemoved called once for every removed from array
|
|
222
|
-
*/
|
|
223
|
-
declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
|
|
224
|
-
|
|
225
|
-
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, StaticStoreSetter, Tail, Truthy, UnboxLazy, UnionToIntersection, Values, access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal, createHydratableStaticStore, createHydrateSignal, createMicrotask, createStaticStore, defer, entries, falseFn, handleDiffArray, isClient, isDev, isObject, isProd, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess };
|