@solid-primitives/utils 5.5.1 → 6.0.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 +6 -59
- package/dist/index.d.ts +7 -29
- package/dist/index.js +9 -56
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -8,11 +8,14 @@ exports.noop = () => void 0;
|
|
|
8
8
|
exports.trueFn = () => true;
|
|
9
9
|
exports.falseFn = () => false;
|
|
10
10
|
exports.isClient = !web.isServer;
|
|
11
|
-
exports.
|
|
11
|
+
exports.isDev = exports.isClient && !!solidJs.DEV;
|
|
12
|
+
exports.isProd = !exports.isDev;
|
|
12
13
|
exports.ofClass = (v, c) => v instanceof c || v && v.constructor === c;
|
|
13
14
|
function isObject(value) {
|
|
14
15
|
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
15
16
|
}
|
|
17
|
+
exports.isNonNullable = (i) => i != null;
|
|
18
|
+
exports.filterNonNullable = (arr) => arr.filter(exports.isNonNullable);
|
|
16
19
|
exports.compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
17
20
|
exports.arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
|
|
18
21
|
function chain(callbacks) {
|
|
@@ -31,7 +34,7 @@ function reverseChain(callbacks) {
|
|
|
31
34
|
}
|
|
32
35
|
exports.clamp = (n, min, max) => Math.min(Math.max(n, min), max);
|
|
33
36
|
exports.access = (v) => typeof v === "function" && !v.length ? v() : v;
|
|
34
|
-
exports.asArray = (value) => Array.isArray(value) ? value : [value];
|
|
37
|
+
exports.asArray = (value) => Array.isArray(value) ? value : value ? [value] : [];
|
|
35
38
|
exports.accessArray = (list) => list.map((v) => exports.access(v));
|
|
36
39
|
exports.withAccess = (value, fn) => {
|
|
37
40
|
const _value = exports.access(value);
|
|
@@ -65,7 +68,7 @@ function defer(deps, fn, initialValue) {
|
|
|
65
68
|
}
|
|
66
69
|
exports.entries = Object.entries;
|
|
67
70
|
exports.keys = Object.keys;
|
|
68
|
-
exports.tryOnCleanup = (fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn;
|
|
71
|
+
exports.tryOnCleanup = exports.isDev ? (fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn : solidJs.onCleanup;
|
|
69
72
|
exports.createCallbackStack = () => {
|
|
70
73
|
let stack = [];
|
|
71
74
|
const clear = () => stack = [];
|
|
@@ -98,56 +101,6 @@ function createHydratableSignal(serverValue, update, options) {
|
|
|
98
101
|
return solidJs.createSignal(update(), options);
|
|
99
102
|
}
|
|
100
103
|
exports.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 = solidJs.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 = solidJs.untrack(
|
|
129
|
-
() => Object.entries(accessWith(a, store))
|
|
130
|
-
);
|
|
131
|
-
solidJs.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 (web.isServer)
|
|
143
|
-
return createStaticStore(serverValue);
|
|
144
|
-
if (solidJs.sharedConfig.context) {
|
|
145
|
-
const [state, setState] = createStaticStore(serverValue);
|
|
146
|
-
solidJs.onMount(() => setState(update()));
|
|
147
|
-
return [state, setState];
|
|
148
|
-
}
|
|
149
|
-
return createStaticStore(update());
|
|
150
|
-
}
|
|
151
104
|
function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
152
105
|
const currLength = current.length;
|
|
153
106
|
const prevLength = prev.length;
|
|
@@ -180,10 +133,6 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
|
180
133
|
}
|
|
181
134
|
}
|
|
182
135
|
|
|
183
|
-
Object.defineProperty(exports, 'isDev', {
|
|
184
|
-
enumerable: true,
|
|
185
|
-
get: function () { return web.isDev; }
|
|
186
|
-
});
|
|
187
136
|
Object.defineProperty(exports, 'isServer', {
|
|
188
137
|
enumerable: true,
|
|
189
138
|
get: function () { return web.isServer; }
|
|
@@ -191,9 +140,7 @@ Object.defineProperty(exports, 'isServer', {
|
|
|
191
140
|
exports.accessWith = accessWith;
|
|
192
141
|
exports.chain = chain;
|
|
193
142
|
exports.createHydratableSignal = createHydratableSignal;
|
|
194
|
-
exports.createHydratableStaticStore = createHydratableStaticStore;
|
|
195
143
|
exports.createMicrotask = createMicrotask;
|
|
196
|
-
exports.createStaticStore = createStaticStore;
|
|
197
144
|
exports.defer = defer;
|
|
198
145
|
exports.handleDiffArray = handleDiffArray;
|
|
199
146
|
exports.isObject = isObject;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Accessor, Setter, AccessorArray, NoInfer, EffectFunction, onCleanup, SignalOptions, createSignal } from 'solid-js';
|
|
2
|
-
export {
|
|
2
|
+
export { isServer } from 'solid-js/web';
|
|
3
3
|
export { EffectOptions, OnOptions, ResolvedChildren, ResolvedJSXElement } from 'solid-js/types/reactive/signal';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -90,6 +90,7 @@ declare const trueFn: () => boolean;
|
|
|
90
90
|
declare const falseFn: () => boolean;
|
|
91
91
|
|
|
92
92
|
declare const isClient: boolean;
|
|
93
|
+
declare const isDev: boolean;
|
|
93
94
|
declare const isProd: boolean;
|
|
94
95
|
/**
|
|
95
96
|
* Check if the value is an instance of ___
|
|
@@ -97,6 +98,8 @@ declare const isProd: boolean;
|
|
|
97
98
|
declare const ofClass: (v: any, c: AnyClass) => boolean;
|
|
98
99
|
/** Check if value is typeof "object" or "function" */
|
|
99
100
|
declare function isObject(value: any): value is AnyObject;
|
|
101
|
+
declare const isNonNullable: <T>(i: T) => i is NonNullable<T>;
|
|
102
|
+
declare const filterNonNullable: <T extends readonly unknown[]>(arr: T) => NonNullable<T[number]>[];
|
|
100
103
|
declare const compare: (a: any, b: any) => number;
|
|
101
104
|
/**
|
|
102
105
|
* Check shallow array equality
|
|
@@ -122,7 +125,7 @@ declare const clamp: (n: number, min: number, max: number) => number;
|
|
|
122
125
|
* ```
|
|
123
126
|
*/
|
|
124
127
|
declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
|
|
125
|
-
declare const asArray: <T>(value: T) => (T extends any[] ? T[number] : T)[];
|
|
128
|
+
declare const asArray: <T>(value: T) => (T extends any[] ? T[number] : NonNullable<T>)[];
|
|
126
129
|
/**
|
|
127
130
|
* Access an array of MaybeAccessors
|
|
128
131
|
* @example
|
|
@@ -157,7 +160,7 @@ declare const entries: <T extends object>(obj: T) => [keyof T, T[keyof T]][];
|
|
|
157
160
|
*/
|
|
158
161
|
declare const keys: <T extends object>(object: T) => (keyof T)[];
|
|
159
162
|
/**
|
|
160
|
-
* Solid's `onCleanup` that
|
|
163
|
+
* Solid's `onCleanup` that doesn't warn in development if used outside of a component.
|
|
161
164
|
*/
|
|
162
165
|
declare const tryOnCleanup: typeof onCleanup;
|
|
163
166
|
declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
|
|
@@ -186,31 +189,6 @@ declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (
|
|
|
186
189
|
declare function createHydratableSignal<T>(serverValue: T, update: () => T, options?: SignalOptions<T>): ReturnType<typeof createSignal<T>>;
|
|
187
190
|
/** @deprecated use {@link createHydratableSignal} instead */
|
|
188
191
|
declare const createHydrateSignal: typeof createHydratableSignal;
|
|
189
|
-
type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
|
|
190
|
-
(setter: (prev: T) => Partial<T>): T;
|
|
191
|
-
(state: Partial<T>): T;
|
|
192
|
-
<K extends keyof T>(key: K, state: SetterParam<T[K]>): T;
|
|
193
|
-
};
|
|
194
|
-
/**
|
|
195
|
-
* 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.
|
|
196
|
-
* @param init initial value of the store
|
|
197
|
-
* @returns
|
|
198
|
-
* ```ts
|
|
199
|
-
* [access: Readonly<T>, write: StaticStoreSetter<T>]
|
|
200
|
-
* ```
|
|
201
|
-
*/
|
|
202
|
-
declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
|
|
203
|
-
/**
|
|
204
|
-
* 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.
|
|
205
|
-
*
|
|
206
|
-
* @param serverValue initial value of the state on the server
|
|
207
|
-
* @param update called once on the client or on hydration to initialize the value
|
|
208
|
-
* @returns
|
|
209
|
-
* ```ts
|
|
210
|
-
* [access: Readonly<T>, write: StaticStoreSetter<T>]
|
|
211
|
-
* ```
|
|
212
|
-
*/
|
|
213
|
-
declare function createHydratableStaticStore<T extends Readonly<AnyStatic>>(serverValue: T, update: () => T): ReturnType<typeof createStaticStore<T>>;
|
|
214
192
|
/**
|
|
215
193
|
* Handle items removed and added to the array by diffing it by refference.
|
|
216
194
|
*
|
|
@@ -221,4 +199,4 @@ declare function createHydratableStaticStore<T extends Readonly<AnyStatic>>(serv
|
|
|
221
199
|
*/
|
|
222
200
|
declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
|
|
223
201
|
|
|
224
|
-
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,
|
|
202
|
+
export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Narrow, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, RequiredKeys, SetterParam, Simplify, Tail, Truthy, UnboxLazy, UnionToIntersection, Values, access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal, createHydrateSignal, createMicrotask, defer, entries, falseFn, filterNonNullable, handleDiffArray, isClient, isDev, isNonNullable, isObject, isProd, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess };
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
import { untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount
|
|
2
|
-
import { isServer
|
|
3
|
-
export {
|
|
1
|
+
import { DEV, untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount } from 'solid-js';
|
|
2
|
+
import { isServer } from 'solid-js/web';
|
|
3
|
+
export { isServer } from 'solid-js/web';
|
|
4
4
|
|
|
5
5
|
// src/index.ts
|
|
6
6
|
var noop = () => void 0;
|
|
7
7
|
var trueFn = () => true;
|
|
8
8
|
var falseFn = () => false;
|
|
9
9
|
var isClient = !isServer;
|
|
10
|
+
var isDev = isClient && !!DEV;
|
|
10
11
|
var isProd = !isDev;
|
|
11
12
|
var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
|
|
12
13
|
function isObject(value) {
|
|
13
14
|
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
14
15
|
}
|
|
16
|
+
var isNonNullable = (i) => i != null;
|
|
17
|
+
var filterNonNullable = (arr) => arr.filter(isNonNullable);
|
|
15
18
|
var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
16
19
|
var arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
|
|
17
20
|
function chain(callbacks) {
|
|
@@ -30,7 +33,7 @@ function reverseChain(callbacks) {
|
|
|
30
33
|
}
|
|
31
34
|
var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
|
|
32
35
|
var access = (v) => typeof v === "function" && !v.length ? v() : v;
|
|
33
|
-
var asArray = (value) => Array.isArray(value) ? value : [value];
|
|
36
|
+
var asArray = (value) => Array.isArray(value) ? value : value ? [value] : [];
|
|
34
37
|
var accessArray = (list) => list.map((v) => access(v));
|
|
35
38
|
var withAccess = (value, fn) => {
|
|
36
39
|
const _value = access(value);
|
|
@@ -64,7 +67,7 @@ function defer(deps, fn, initialValue) {
|
|
|
64
67
|
}
|
|
65
68
|
var entries = Object.entries;
|
|
66
69
|
var keys = Object.keys;
|
|
67
|
-
var tryOnCleanup = (fn) => getOwner() ? onCleanup(fn) : fn;
|
|
70
|
+
var tryOnCleanup = isDev ? (fn) => getOwner() ? onCleanup(fn) : fn : onCleanup;
|
|
68
71
|
var createCallbackStack = () => {
|
|
69
72
|
let stack = [];
|
|
70
73
|
const clear = () => stack = [];
|
|
@@ -97,56 +100,6 @@ function createHydratableSignal(serverValue, update, options) {
|
|
|
97
100
|
return createSignal(update(), options);
|
|
98
101
|
}
|
|
99
102
|
var createHydrateSignal = createHydratableSignal;
|
|
100
|
-
function createStaticStore(init) {
|
|
101
|
-
const copy = { ...init };
|
|
102
|
-
const store = { ...init };
|
|
103
|
-
const cache = /* @__PURE__ */ new Map();
|
|
104
|
-
const getValue = (key) => {
|
|
105
|
-
const saved = cache.get(key);
|
|
106
|
-
if (saved)
|
|
107
|
-
return saved[0]();
|
|
108
|
-
const signal = createSignal(copy[key], {
|
|
109
|
-
internal: true
|
|
110
|
-
});
|
|
111
|
-
cache.set(key, signal);
|
|
112
|
-
delete copy[key];
|
|
113
|
-
return signal[0]();
|
|
114
|
-
};
|
|
115
|
-
const setValue = (key, value) => {
|
|
116
|
-
const saved = cache.get(key);
|
|
117
|
-
if (saved)
|
|
118
|
-
return saved[1](value);
|
|
119
|
-
if (key in copy)
|
|
120
|
-
copy[key] = accessWith(value, [copy[key]]);
|
|
121
|
-
};
|
|
122
|
-
for (const key in init) {
|
|
123
|
-
Object.defineProperty(store, key, { get: getValue.bind(void 0, key) });
|
|
124
|
-
}
|
|
125
|
-
const setter = (a, b) => {
|
|
126
|
-
if (isObject(a)) {
|
|
127
|
-
const entries2 = untrack(
|
|
128
|
-
() => Object.entries(accessWith(a, store))
|
|
129
|
-
);
|
|
130
|
-
batch(() => {
|
|
131
|
-
for (const [key, value] of entries2)
|
|
132
|
-
setValue(key, () => value);
|
|
133
|
-
});
|
|
134
|
-
} else
|
|
135
|
-
setValue(a, b);
|
|
136
|
-
return store;
|
|
137
|
-
};
|
|
138
|
-
return [store, setter];
|
|
139
|
-
}
|
|
140
|
-
function createHydratableStaticStore(serverValue, update) {
|
|
141
|
-
if (isServer)
|
|
142
|
-
return createStaticStore(serverValue);
|
|
143
|
-
if (sharedConfig.context) {
|
|
144
|
-
const [state, setState] = createStaticStore(serverValue);
|
|
145
|
-
onMount(() => setState(update()));
|
|
146
|
-
return [state, setState];
|
|
147
|
-
}
|
|
148
|
-
return createStaticStore(update());
|
|
149
|
-
}
|
|
150
103
|
function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
151
104
|
const currLength = current.length;
|
|
152
105
|
const prevLength = prev.length;
|
|
@@ -179,4 +132,4 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
|
179
132
|
}
|
|
180
133
|
}
|
|
181
134
|
|
|
182
|
-
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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solid-primitives/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.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": [
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"require": "./dist/index.cjs"
|
|
34
34
|
},
|
|
35
|
+
"typesVersions": {},
|
|
35
36
|
"keywords": [
|
|
36
37
|
"utilities",
|
|
37
38
|
"reactivity",
|
|
@@ -41,7 +42,6 @@
|
|
|
41
42
|
"peerDependencies": {
|
|
42
43
|
"solid-js": "^1.6.12"
|
|
43
44
|
},
|
|
44
|
-
"typesVersions": {},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "jiti ../../scripts/build.ts",
|
|
47
47
|
"test": "vitest -c ../../configs/vitest.config.ts",
|