@solid-primitives/utils 1.2.0 → 1.5.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/index.cjs +80 -1
- package/dist/index.d.ts +31 -14
- package/dist/index.js +79 -2
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
4
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
5
19
|
var __export = (target, all) => {
|
|
6
20
|
for (var name in all)
|
|
7
21
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -21,18 +35,23 @@ var src_exports = {};
|
|
|
21
35
|
__export(src_exports, {
|
|
22
36
|
access: () => access,
|
|
23
37
|
accessArray: () => accessArray,
|
|
38
|
+
accessWith: () => accessWith,
|
|
24
39
|
asAccessor: () => asAccessor,
|
|
25
40
|
asArray: () => asArray,
|
|
41
|
+
chain: () => chain,
|
|
42
|
+
clamp: () => clamp,
|
|
26
43
|
compare: () => compare,
|
|
27
44
|
createCallbackStack: () => createCallbackStack,
|
|
28
45
|
createMicrotask: () => createMicrotask,
|
|
29
46
|
createProxy: () => createProxy,
|
|
47
|
+
createStaticStore: () => createStaticStore,
|
|
30
48
|
createTrigger: () => createTrigger,
|
|
31
49
|
createTriggerCache: () => createTriggerCache,
|
|
32
50
|
entries: () => entries,
|
|
33
51
|
forEachEntry: () => forEachEntry,
|
|
34
52
|
isClient: () => isClient,
|
|
35
53
|
isDev: () => isDev,
|
|
54
|
+
isObject: () => isObject,
|
|
36
55
|
isProd: () => isProd,
|
|
37
56
|
isServer: () => import_web.isServer,
|
|
38
57
|
keys: () => keys,
|
|
@@ -53,7 +72,17 @@ var isDev = import_solid_js.DEV && isClient;
|
|
|
53
72
|
var isProd = !isDev;
|
|
54
73
|
var warn = (...a) => isDev && console.warn(...a);
|
|
55
74
|
var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
|
|
75
|
+
function isObject(value) {
|
|
76
|
+
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
77
|
+
}
|
|
56
78
|
var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
79
|
+
function chain(...callbacks) {
|
|
80
|
+
return (...args) => {
|
|
81
|
+
for (const callback of callbacks)
|
|
82
|
+
callback(...args);
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
|
|
57
86
|
var access = (v) => typeof v === "function" && !v.length ? v() : v;
|
|
58
87
|
var asArray = (value) => Array.isArray(value) ? value : [value];
|
|
59
88
|
var accessArray = (list) => list.map((v) => access(v));
|
|
@@ -62,10 +91,13 @@ var withAccess = (value, fn) => {
|
|
|
62
91
|
typeof _value !== "undefined" && _value !== null && fn(_value);
|
|
63
92
|
};
|
|
64
93
|
var asAccessor = (v) => typeof v === "function" ? v : () => v;
|
|
94
|
+
function accessWith(valueOrFn, ...args) {
|
|
95
|
+
return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
|
|
96
|
+
}
|
|
65
97
|
function forEachEntry(object, iterator) {
|
|
66
98
|
Object.entries(object).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, object));
|
|
67
99
|
}
|
|
68
|
-
var entries =
|
|
100
|
+
var entries = Object.entries;
|
|
69
101
|
var keys = Object.keys;
|
|
70
102
|
var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
|
|
71
103
|
function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
|
|
@@ -128,22 +160,69 @@ function createTriggerCache(options) {
|
|
|
128
160
|
}
|
|
129
161
|
};
|
|
130
162
|
}
|
|
163
|
+
function createStaticStore(init) {
|
|
164
|
+
const copy = __spreadValues({}, init);
|
|
165
|
+
const store = {};
|
|
166
|
+
const cache = /* @__PURE__ */ new Map();
|
|
167
|
+
const getValue = (key) => {
|
|
168
|
+
const saved = cache.get(key);
|
|
169
|
+
if (saved)
|
|
170
|
+
return saved[0]();
|
|
171
|
+
const signal = (0, import_solid_js.createSignal)(copy[key], {
|
|
172
|
+
name: typeof key === "string" ? key : void 0
|
|
173
|
+
});
|
|
174
|
+
cache.set(key, signal);
|
|
175
|
+
delete copy[key];
|
|
176
|
+
return signal[0]();
|
|
177
|
+
};
|
|
178
|
+
const setValue = (key, value) => {
|
|
179
|
+
const saved = cache.get(key);
|
|
180
|
+
if (saved)
|
|
181
|
+
return saved[1](value);
|
|
182
|
+
if (key in copy)
|
|
183
|
+
copy[key] = accessWith(value, [copy[key]]);
|
|
184
|
+
};
|
|
185
|
+
for (const key of keys(init)) {
|
|
186
|
+
store[key] = void 0;
|
|
187
|
+
Object.defineProperty(store, key, {
|
|
188
|
+
get: getValue.bind(void 0, key)
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
const setter = (a, b) => {
|
|
192
|
+
if (isObject(a))
|
|
193
|
+
(0, import_solid_js.untrack)(() => {
|
|
194
|
+
(0, import_solid_js.batch)(() => {
|
|
195
|
+
for (const [key, value] of entries(accessWith(a, store)))
|
|
196
|
+
setValue(key, () => value);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
else
|
|
200
|
+
setValue(a, b);
|
|
201
|
+
return store;
|
|
202
|
+
};
|
|
203
|
+
return [store, setter];
|
|
204
|
+
}
|
|
131
205
|
// Annotate the CommonJS export names for ESM import in node:
|
|
132
206
|
0 && (module.exports = {
|
|
133
207
|
access,
|
|
134
208
|
accessArray,
|
|
209
|
+
accessWith,
|
|
135
210
|
asAccessor,
|
|
136
211
|
asArray,
|
|
212
|
+
chain,
|
|
213
|
+
clamp,
|
|
137
214
|
compare,
|
|
138
215
|
createCallbackStack,
|
|
139
216
|
createMicrotask,
|
|
140
217
|
createProxy,
|
|
218
|
+
createStaticStore,
|
|
141
219
|
createTrigger,
|
|
142
220
|
createTriggerCache,
|
|
143
221
|
entries,
|
|
144
222
|
forEachEntry,
|
|
145
223
|
isClient,
|
|
146
224
|
isDev,
|
|
225
|
+
isObject,
|
|
147
226
|
isProd,
|
|
148
227
|
isServer,
|
|
149
228
|
keys,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Accessor, onCleanup } from 'solid-js';
|
|
1
|
+
import { Accessor, Setter, onCleanup } from 'solid-js';
|
|
2
2
|
import { BaseOptions } from 'solid-js/types/reactive/signal';
|
|
3
3
|
export { isServer } from 'solid-js/web';
|
|
4
4
|
|
|
@@ -14,6 +14,7 @@ declare type Directive<P = true> = (el: Element, props: Accessor<P>) => void;
|
|
|
14
14
|
*/
|
|
15
15
|
declare type ItemsOf<T> = T extends (infer E)[] ? E : never;
|
|
16
16
|
declare type ItemsOfMany<T> = T extends any[] ? ItemsOf<T> : T;
|
|
17
|
+
declare type SetterValue<T> = Parameters<Setter<T>>[0];
|
|
17
18
|
/**
|
|
18
19
|
* T or a reactive/non-reactive function returning T
|
|
19
20
|
*/
|
|
@@ -56,21 +57,14 @@ declare type Definite<T> = {
|
|
|
56
57
|
/** `A | B => A & B` */
|
|
57
58
|
declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
58
59
|
declare type ExtractIfPossible<T, U> = Extract<T, U> extends never ? U : Extract<T, U>;
|
|
59
|
-
declare type AnyObject = Record<
|
|
60
|
+
declare type AnyObject = Record<PropertyKey, any>;
|
|
61
|
+
declare type AnyStatic = [] | any[] | AnyObject;
|
|
60
62
|
declare type AnyFunction = (...args: any[]) => any;
|
|
61
63
|
declare type AnyClass = abstract new (...args: any) => any;
|
|
62
|
-
declare type
|
|
63
|
-
declare type PrimitiveValue = string | boolean | number | bigint | symbol | null | undefined;
|
|
64
|
+
declare type PrimitiveValue = PropertyKey | boolean | bigint | null | undefined;
|
|
64
65
|
declare type FalsyValue = false | 0 | "" | null | undefined;
|
|
65
66
|
declare type Truthy<T> = Exclude<T, FalsyValue>;
|
|
66
67
|
declare type Falsy<T> = Extract<T, FalsyValue>;
|
|
67
|
-
declare type Fallback<T, F = NonNullable<T>> = NonNullable<T> | F;
|
|
68
|
-
/**
|
|
69
|
-
* Destructible store object, with values changed to accessors
|
|
70
|
-
*/
|
|
71
|
-
declare type Destore<T extends Object> = {
|
|
72
|
-
[K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
|
|
73
|
-
};
|
|
74
68
|
declare type TriggerCache<T> = {
|
|
75
69
|
track: (v: T) => void;
|
|
76
70
|
dirty: (v: T) => void;
|
|
@@ -96,7 +90,14 @@ declare const warn: typeof console.warn;
|
|
|
96
90
|
* Check if the value is an instance of ___
|
|
97
91
|
*/
|
|
98
92
|
declare const ofClass: (v: any, c: AnyClass) => boolean;
|
|
93
|
+
/** Check if value is typeof "object" or "function" */
|
|
94
|
+
declare function isObject(value: any): value is AnyObject;
|
|
99
95
|
declare const compare: (a: any, b: any) => number;
|
|
96
|
+
/**
|
|
97
|
+
* Returns a function that will call all functions in the order they were chained with the same arguments.
|
|
98
|
+
*/
|
|
99
|
+
declare function chain<Args extends [] | any[]>(...callbacks: ((...args: Args) => any)[]): (...args: Args) => void;
|
|
100
|
+
declare const clamp: (n: number, min: number, max: number) => number;
|
|
100
101
|
/**
|
|
101
102
|
* Accesses the value of a MaybeAccessor
|
|
102
103
|
* @example
|
|
@@ -121,14 +122,16 @@ declare const accessArray: <A extends unknown>(list: readonly A[]) => MaybeAcces
|
|
|
121
122
|
*/
|
|
122
123
|
declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void;
|
|
123
124
|
declare const asAccessor: <A extends unknown>(v: A) => Accessor<MaybeAccessorValue<A>>;
|
|
125
|
+
/** If value is a function – call it with a given arguments – otherwise get the value as is */
|
|
126
|
+
declare function accessWith<T>(valueOrFn: T, ...args: T extends AnyFunction ? Parameters<T> : never): T extends AnyFunction ? ReturnType<T> : T;
|
|
124
127
|
/**
|
|
125
128
|
* Iterate through object entries.
|
|
126
129
|
*/
|
|
127
130
|
declare function forEachEntry<O extends AnyObject>(object: O, iterator: (key: keyof O, item: Values<O>, index: number, pairs: [keyof O, Values<O>][], object: O) => void): void;
|
|
128
131
|
/**
|
|
129
|
-
* Get
|
|
132
|
+
* Get entries of an object
|
|
130
133
|
*/
|
|
131
|
-
declare const entries: <
|
|
134
|
+
declare const entries: <T extends object>(obj: T) => [keyof T, T[keyof T]][];
|
|
132
135
|
/**
|
|
133
136
|
* Get keys of an object
|
|
134
137
|
*/
|
|
@@ -222,5 +225,19 @@ declare function createTrigger(options?: BaseOptions): Trigger;
|
|
|
222
225
|
* dirty(2)
|
|
223
226
|
*/
|
|
224
227
|
declare function createTriggerCache<T>(options?: BaseOptions): TriggerCache<T>;
|
|
228
|
+
declare type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
|
|
229
|
+
(setter: (prev: T) => Partial<T>): T;
|
|
230
|
+
(state: Partial<T>): T;
|
|
231
|
+
<K extends keyof T>(key: K, state: SetterValue<T[K]>): T;
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* A shallowly wrapped reactive store object. It behaves similarly to the creatStore, 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.
|
|
235
|
+
* @param init initial value of the store
|
|
236
|
+
* @returns
|
|
237
|
+
* ```ts
|
|
238
|
+
* [access: Readonly<T>, write: StaticStoreSetter<T>]
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
|
|
225
242
|
|
|
226
|
-
export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, DeepPartialAny, Definite,
|
|
243
|
+
export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Definite, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, SetterValue, StaticStoreSetter, Trigger, TriggerCache, Truthy, UnionToIntersection, Values, access, accessArray, accessWith, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createMicrotask, createProxy, createStaticStore, createTrigger, createTriggerCache, entries, forEachEntry, isClient, isDev, isObject, isProd, keys, noop, ofClass, onRootCleanup, promiseTimeout, raceTimeout, warn, withAccess };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
|
|
1
18
|
// src/index.ts
|
|
2
|
-
import { getOwner, onCleanup, createSignal, DEV } from "solid-js";
|
|
19
|
+
import { getOwner, onCleanup, createSignal, DEV, untrack, batch } from "solid-js";
|
|
3
20
|
import { isServer } from "solid-js/web";
|
|
4
21
|
var noop = () => void 0;
|
|
5
22
|
var isClient = !isServer;
|
|
@@ -7,7 +24,17 @@ var isDev = DEV && isClient;
|
|
|
7
24
|
var isProd = !isDev;
|
|
8
25
|
var warn = (...a) => isDev && console.warn(...a);
|
|
9
26
|
var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
|
|
27
|
+
function isObject(value) {
|
|
28
|
+
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
29
|
+
}
|
|
10
30
|
var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
31
|
+
function chain(...callbacks) {
|
|
32
|
+
return (...args) => {
|
|
33
|
+
for (const callback of callbacks)
|
|
34
|
+
callback(...args);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
|
|
11
38
|
var access = (v) => typeof v === "function" && !v.length ? v() : v;
|
|
12
39
|
var asArray = (value) => Array.isArray(value) ? value : [value];
|
|
13
40
|
var accessArray = (list) => list.map((v) => access(v));
|
|
@@ -16,10 +43,13 @@ var withAccess = (value, fn) => {
|
|
|
16
43
|
typeof _value !== "undefined" && _value !== null && fn(_value);
|
|
17
44
|
};
|
|
18
45
|
var asAccessor = (v) => typeof v === "function" ? v : () => v;
|
|
46
|
+
function accessWith(valueOrFn, ...args) {
|
|
47
|
+
return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
|
|
48
|
+
}
|
|
19
49
|
function forEachEntry(object, iterator) {
|
|
20
50
|
Object.entries(object).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, object));
|
|
21
51
|
}
|
|
22
|
-
var entries =
|
|
52
|
+
var entries = Object.entries;
|
|
23
53
|
var keys = Object.keys;
|
|
24
54
|
var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
|
|
25
55
|
function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
|
|
@@ -82,21 +112,68 @@ function createTriggerCache(options) {
|
|
|
82
112
|
}
|
|
83
113
|
};
|
|
84
114
|
}
|
|
115
|
+
function createStaticStore(init) {
|
|
116
|
+
const copy = __spreadValues({}, init);
|
|
117
|
+
const store = {};
|
|
118
|
+
const cache = /* @__PURE__ */ new Map();
|
|
119
|
+
const getValue = (key) => {
|
|
120
|
+
const saved = cache.get(key);
|
|
121
|
+
if (saved)
|
|
122
|
+
return saved[0]();
|
|
123
|
+
const signal = createSignal(copy[key], {
|
|
124
|
+
name: typeof key === "string" ? key : void 0
|
|
125
|
+
});
|
|
126
|
+
cache.set(key, signal);
|
|
127
|
+
delete copy[key];
|
|
128
|
+
return signal[0]();
|
|
129
|
+
};
|
|
130
|
+
const setValue = (key, value) => {
|
|
131
|
+
const saved = cache.get(key);
|
|
132
|
+
if (saved)
|
|
133
|
+
return saved[1](value);
|
|
134
|
+
if (key in copy)
|
|
135
|
+
copy[key] = accessWith(value, [copy[key]]);
|
|
136
|
+
};
|
|
137
|
+
for (const key of keys(init)) {
|
|
138
|
+
store[key] = void 0;
|
|
139
|
+
Object.defineProperty(store, key, {
|
|
140
|
+
get: getValue.bind(void 0, key)
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
const setter = (a, b) => {
|
|
144
|
+
if (isObject(a))
|
|
145
|
+
untrack(() => {
|
|
146
|
+
batch(() => {
|
|
147
|
+
for (const [key, value] of entries(accessWith(a, store)))
|
|
148
|
+
setValue(key, () => value);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
else
|
|
152
|
+
setValue(a, b);
|
|
153
|
+
return store;
|
|
154
|
+
};
|
|
155
|
+
return [store, setter];
|
|
156
|
+
}
|
|
85
157
|
export {
|
|
86
158
|
access,
|
|
87
159
|
accessArray,
|
|
160
|
+
accessWith,
|
|
88
161
|
asAccessor,
|
|
89
162
|
asArray,
|
|
163
|
+
chain,
|
|
164
|
+
clamp,
|
|
90
165
|
compare,
|
|
91
166
|
createCallbackStack,
|
|
92
167
|
createMicrotask,
|
|
93
168
|
createProxy,
|
|
169
|
+
createStaticStore,
|
|
94
170
|
createTrigger,
|
|
95
171
|
createTriggerCache,
|
|
96
172
|
entries,
|
|
97
173
|
forEachEntry,
|
|
98
174
|
isClient,
|
|
99
175
|
isDev,
|
|
176
|
+
isObject,
|
|
100
177
|
isProd,
|
|
101
178
|
isServer,
|
|
102
179
|
keys,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solid-primitives/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.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
|
"license": "MIT",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"scripts": {
|
|
25
25
|
"build": "tsup",
|
|
26
26
|
"test": "uvu -r solid-register",
|
|
27
|
-
"watch
|
|
27
|
+
"test:watch": "watchlist src test -- npm test"
|
|
28
28
|
},
|
|
29
29
|
"keywords": [
|
|
30
30
|
"utilities",
|