@solid-primitives/utils 6.3.2 → 7.0.0-next.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/LICENSE +71 -0
- package/README.md +295 -0
- package/dist/colors/helpers.d.ts +46 -0
- package/dist/colors/helpers.js +895 -0
- package/dist/colors/index.d.ts +4 -0
- package/dist/colors/index.js +4 -0
- package/dist/colors/intl.d.ts +54 -0
- package/dist/colors/intl.js +68 -0
- package/dist/colors/manipulation.d.ts +144 -0
- package/dist/colors/manipulation.js +278 -0
- package/dist/colors/types.d.ts +77 -0
- package/dist/colors/types.js +14 -0
- package/dist/index.d.ts +164 -21
- package/dist/index.js +171 -18
- package/dist/types.d.ts +4 -2
- package/package.json +84 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,31 +1,54 @@
|
|
|
1
|
-
import { onCleanup, createSignal, type Accessor, type
|
|
2
|
-
|
|
3
|
-
import type { AnyClass, MaybeAccessor, MaybeAccessorValue, Noop, AnyObject, AnyFunction } from "./types.js";
|
|
1
|
+
import { onCleanup, createSignal, type Accessor, type ComputeFunction, type NoInfer, type Setter, type SignalOptions, type Signal, type Store, type StoreSetter } from "solid-js";
|
|
2
|
+
declare const isServer: boolean;
|
|
3
|
+
import type { AccessorArray, AnyClass, MaybeAccessor, MaybeAccessorValue, Noop, AnyObject, AnyFunction } from "./types.js";
|
|
4
4
|
export * from "./types.js";
|
|
5
|
+
/** `true` on the server (SSR), `false` in the browser. */
|
|
5
6
|
export { isServer };
|
|
7
|
+
/** `true` in the browser, `false` on the server. */
|
|
6
8
|
export declare const isClient: boolean;
|
|
9
|
+
/** `true` in the browser with Solid's DEV mode active. */
|
|
7
10
|
export declare const isDev: boolean;
|
|
11
|
+
/** `true` when DEV mode is off (i.e. production browser builds and all SSR). */
|
|
8
12
|
export declare const isProd: boolean;
|
|
9
|
-
/**
|
|
13
|
+
/** No-operation function — accepts any arguments and returns `undefined`. */
|
|
10
14
|
export declare const noop: Noop;
|
|
15
|
+
/** A function that always returns `true`. Useful as a default predicate prop. */
|
|
11
16
|
export declare const trueFn: () => boolean;
|
|
17
|
+
/** A function that always returns `false`. Useful as a default predicate prop. */
|
|
12
18
|
export declare const falseFn: () => boolean;
|
|
13
19
|
/** @deprecated use {@link equalFn} from "solid-js" */
|
|
14
|
-
export declare const defaultEquals:
|
|
20
|
+
export declare const defaultEquals: (value1: any, value2: any) => boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Signal options that disable equality checking, causing the signal to always
|
|
23
|
+
* notify subscribers even when the value hasn't changed. Useful for signals
|
|
24
|
+
* holding mutable objects where structural equality cannot be assumed.
|
|
25
|
+
*/
|
|
15
26
|
export declare const EQUALS_FALSE_OPTIONS: {
|
|
16
27
|
readonly equals: false;
|
|
17
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* Signal options that allow writes from inside an owned reactive scope
|
|
31
|
+
* (component body, memo, or effect) without throwing `SIGNAL_WRITE_IN_OWNED_SCOPE`
|
|
32
|
+
* in Solid 2.0 dev mode.
|
|
33
|
+
*
|
|
34
|
+
* Use only for implementation-detail signals that genuinely need to be written
|
|
35
|
+
* from within a scope — not as a general escape hatch.
|
|
36
|
+
*/
|
|
18
37
|
export declare const INTERNAL_OPTIONS: {
|
|
19
|
-
readonly
|
|
38
|
+
readonly ownedWrite: true;
|
|
20
39
|
};
|
|
21
40
|
/**
|
|
22
|
-
*
|
|
41
|
+
* Returns `true` if `v` is an instance of `c`. Checks both `instanceof` and
|
|
42
|
+
* `constructor` so that values created across different realms (e.g. iframes) match.
|
|
23
43
|
*/
|
|
24
44
|
export declare const ofClass: (v: any, c: AnyClass) => boolean;
|
|
25
45
|
/** Check if value is typeof "object" or "function" */
|
|
26
46
|
export declare function isObject(value: any): value is AnyObject;
|
|
47
|
+
/** Type guard: returns `true` when `i` is neither `null` nor `undefined`. */
|
|
27
48
|
export declare const isNonNullable: <T>(i: T) => i is NonNullable<T>;
|
|
49
|
+
/** Returns a new array with all `null` and `undefined` items removed. */
|
|
28
50
|
export declare const filterNonNullable: <T extends readonly unknown[]>(arr: T) => NonNullable<T[number]>[];
|
|
51
|
+
/** Three-way comparison: returns `-1` if `a < b`, `1` if `a > b`, `0` if equal. */
|
|
29
52
|
export declare const compare: (a: any, b: any) => number;
|
|
30
53
|
/**
|
|
31
54
|
* Check shallow array equality
|
|
@@ -41,6 +64,7 @@ export declare function chain<Args extends [] | any[]>(callbacks: {
|
|
|
41
64
|
* Returns a function that will call all functions in the reversed order with the same arguments.
|
|
42
65
|
*/
|
|
43
66
|
export declare function reverseChain<Args extends [] | any[]>(callbacks: (((...args: Args) => any) | undefined)[]): (...args: Args) => void;
|
|
67
|
+
/** Clamps `n` to the inclusive range `[min, max]`. */
|
|
44
68
|
export declare const clamp: (n: number, min: number, max: number) => number;
|
|
45
69
|
/**
|
|
46
70
|
* Accesses the value of a MaybeAccessor
|
|
@@ -51,6 +75,10 @@ export declare const clamp: (n: number, min: number, max: number) => number;
|
|
|
51
75
|
* ```
|
|
52
76
|
*/
|
|
53
77
|
export declare const access: <T extends MaybeAccessor<any>>(v: T) => MaybeAccessorValue<T>;
|
|
78
|
+
/**
|
|
79
|
+
* Wraps `value` in a single-element array. If `value` is already an array it is
|
|
80
|
+
* returned as-is. Falsy values (except `0` and `false`) produce an empty array.
|
|
81
|
+
*/
|
|
54
82
|
export declare const asArray: <T>(value: T) => (T extends any[] ? T[number] : NonNullable<T>)[];
|
|
55
83
|
/**
|
|
56
84
|
* Access an array of MaybeAccessors
|
|
@@ -65,18 +93,25 @@ export declare const accessArray: <A extends MaybeAccessor<any>>(list: readonly
|
|
|
65
93
|
* @param fn
|
|
66
94
|
*/
|
|
67
95
|
export declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void;
|
|
96
|
+
/**
|
|
97
|
+
* Wraps a plain value in a zero-argument accessor `() => value`.
|
|
98
|
+
* If `v` is already a function it is returned unchanged.
|
|
99
|
+
*/
|
|
68
100
|
export declare const asAccessor: <A extends MaybeAccessor<unknown>>(v: A) => Accessor<MaybeAccessorValue<A>>;
|
|
69
101
|
/** If value is a function – call it with a given arguments – otherwise get the value as is */
|
|
70
102
|
export declare function accessWith<T>(valueOrFn: T, ...args: T extends AnyFunction ? Parameters<T> : never): T extends AnyFunction ? ReturnType<T> : T;
|
|
71
103
|
/**
|
|
72
|
-
*
|
|
104
|
+
* A deferred effect compute function. Skips the first reactive run and returns
|
|
105
|
+
* `initialValue` instead, then calls `fn` on subsequent dependency changes.
|
|
106
|
+
* Useful as the compute phase of `createEffect(defer(...), apply)` when the
|
|
107
|
+
* initial run should be suppressed.
|
|
73
108
|
*
|
|
74
|
-
* @param deps
|
|
75
|
-
* @param fn
|
|
76
|
-
* @param initialValue
|
|
109
|
+
* @param deps - a single accessor or an array of accessors to track
|
|
110
|
+
* @param fn - called with the current input, previous input, and previous return value
|
|
111
|
+
* @param initialValue - returned on the skipped first run (defaults to `undefined`)
|
|
77
112
|
*/
|
|
78
|
-
export declare function defer<S, Next extends Prev, Prev = Next>(deps: AccessorArray<S> | Accessor<S>, fn: (input: S, prevInput: S, prev: undefined | NoInfer<Prev>) => Next, initialValue: Next):
|
|
79
|
-
export declare function defer<S, Next extends Prev, Prev = Next>(deps: AccessorArray<S> | Accessor<S>, fn: (input: S, prevInput: S, prev: undefined | NoInfer<Prev>) => Next, initialValue?: undefined):
|
|
113
|
+
export declare function defer<S, Next extends Prev, Prev = Next>(deps: AccessorArray<S> | Accessor<S>, fn: (input: S, prevInput: S, prev: undefined | NoInfer<Prev>) => Next, initialValue: Next): ComputeFunction<undefined | NoInfer<Next>, NoInfer<Next>>;
|
|
114
|
+
export declare function defer<S, Next extends Prev, Prev = Next>(deps: AccessorArray<S> | Accessor<S>, fn: (input: S, prevInput: S, prev: undefined | NoInfer<Prev>) => Next, initialValue?: undefined): ComputeFunction<undefined | NoInfer<Next>>;
|
|
80
115
|
/**
|
|
81
116
|
* Get entries of an object
|
|
82
117
|
*/
|
|
@@ -89,15 +124,36 @@ export declare const keys: <T extends object>(object: T) => (keyof T)[];
|
|
|
89
124
|
* Solid's `onCleanup` that doesn't warn in development if used outside of a component.
|
|
90
125
|
*/
|
|
91
126
|
export declare const tryOnCleanup: typeof onCleanup;
|
|
127
|
+
/**
|
|
128
|
+
* Creates a stack of callbacks that can be pushed to, executed all at once, or cleared.
|
|
129
|
+
* All callbacks receive the same arguments when `execute` is called; the stack is
|
|
130
|
+
* automatically cleared after execution.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* const stack = createCallbackStack<string>();
|
|
135
|
+
* stack.push(s => console.log("a:", s));
|
|
136
|
+
* stack.push(s => console.log("b:", s));
|
|
137
|
+
* stack.execute("hello"); // logs "a: hello", "b: hello" — stack is now empty
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
92
140
|
export declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
|
|
93
141
|
push: (...callbacks: ((arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void)[]) => void;
|
|
94
142
|
execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
|
|
95
143
|
clear: VoidFunction;
|
|
96
144
|
};
|
|
97
145
|
/**
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
146
|
+
* Returns a debounced wrapper around `fn` that batches rapid synchronous calls
|
|
147
|
+
* into a single invocation. Only the most-recently-passed arguments are forwarded.
|
|
148
|
+
* The counter resets on cleanup so stale microtasks are silently discarded.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* const flush = createMicrotask((value: string) => console.log(value));
|
|
153
|
+
* flush("a");
|
|
154
|
+
* flush("b");
|
|
155
|
+
* flush("c"); // only "c" is logged (in the next microtask)
|
|
156
|
+
* ```
|
|
101
157
|
*/
|
|
102
158
|
export declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (...a: A) => void;
|
|
103
159
|
/**
|
|
@@ -116,11 +172,98 @@ export declare function createHydratableSignal<T>(serverValue: T, update: () =>
|
|
|
116
172
|
/** @deprecated use {@link createHydratableSignal} instead */
|
|
117
173
|
export declare const createHydrateSignal: typeof createHydratableSignal;
|
|
118
174
|
/**
|
|
119
|
-
*
|
|
175
|
+
* Diffs two arrays by reference and calls `handleAdded` / `handleRemoved` for
|
|
176
|
+
* each item that appeared or disappeared. Items are compared with `===`.
|
|
120
177
|
*
|
|
121
|
-
* @param current new array
|
|
122
|
-
* @param prev previous array
|
|
123
|
-
* @param handleAdded called once for
|
|
124
|
-
* @param handleRemoved called once for
|
|
178
|
+
* @param current - the new array
|
|
179
|
+
* @param prev - the previous array snapshot
|
|
180
|
+
* @param handleAdded - called once for each item present in `current` but not `prev`
|
|
181
|
+
* @param handleRemoved - called once for each item present in `prev` but not `current`
|
|
125
182
|
*/
|
|
126
183
|
export declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
|
|
184
|
+
/**
|
|
185
|
+
* Schedules `fn` to run after the browser has painted by nesting two
|
|
186
|
+
* requestAnimationFrame calls. No-op in non-browser environments.
|
|
187
|
+
*/
|
|
188
|
+
export declare const afterPaint: (fn: () => void) => void;
|
|
189
|
+
/**
|
|
190
|
+
* Parse a string as a single JSON value.
|
|
191
|
+
*
|
|
192
|
+
* ```ts
|
|
193
|
+
* const { data } = createSSE<{ status: string }>(url, { transform: json });
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export declare const json: <T>(raw: string) => T;
|
|
197
|
+
/**
|
|
198
|
+
* Parse a string as newline-delimited JSON (NDJSON / JSON Lines).
|
|
199
|
+
*
|
|
200
|
+
* Each non-empty line is parsed as a separate JSON value and returned as an array.
|
|
201
|
+
*
|
|
202
|
+
* ```ts
|
|
203
|
+
* const { data } = createSSE<TickEvent[]>(url, { transform: ndjson });
|
|
204
|
+
* // data() === [{ id: 1, type: "tick" }, { id: 2, type: "tick" }]
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
export declare const ndjson: <T>(raw: string) => T[];
|
|
208
|
+
/**
|
|
209
|
+
* Split a string into individual lines, returning a `string[]`. Empty lines are filtered out.
|
|
210
|
+
*
|
|
211
|
+
* ```ts
|
|
212
|
+
* const { data } = createSSE<string[]>(url, { transform: lines });
|
|
213
|
+
* // data() === ["line one", "line two"]
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
export declare const lines: (raw: string) => string[];
|
|
217
|
+
/**
|
|
218
|
+
* Parse a string as a number using `Number()` semantics.
|
|
219
|
+
*
|
|
220
|
+
* Note: `""` → `0`, non-numeric strings → `NaN`.
|
|
221
|
+
*
|
|
222
|
+
* ```ts
|
|
223
|
+
* const { data } = createSSE<number>(url, { transform: number });
|
|
224
|
+
* // data() === 42
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
export declare const number: (raw: string) => number;
|
|
228
|
+
/**
|
|
229
|
+
* Wrap any `(string) => T` transform in a `try/catch`. Returns `fallback`
|
|
230
|
+
* (default `undefined`) instead of throwing on malformed input.
|
|
231
|
+
*
|
|
232
|
+
* ```ts
|
|
233
|
+
* const { data } = createSSE<MyEvent>(url, { transform: safe(json) });
|
|
234
|
+
* const { data } = createSSE<number>(url, { transform: safe(number, 0) });
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
export declare function safe<T>(transform: (raw: string) => T): (raw: string) => T | undefined;
|
|
238
|
+
export declare function safe<T>(transform: (raw: string) => T, fallback: T): (raw: string) => T;
|
|
239
|
+
/**
|
|
240
|
+
* Compose two transforms into one: the output of `a` is passed as the input of `b`.
|
|
241
|
+
*
|
|
242
|
+
* ```ts
|
|
243
|
+
* const { data } = createSSE<RawEvent[]>(url, {
|
|
244
|
+
* transform: pipe(ndjson<RawEvent>, rows => rows.filter(r => r.type === "tick")),
|
|
245
|
+
* });
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
export declare function pipe<A, B>(a: (raw: string) => A, b: (a: A) => B): (raw: string) => B;
|
|
249
|
+
/**
|
|
250
|
+
* Check if a wrapper element contains a target element.
|
|
251
|
+
* Portal-aware: follows SolidJS `_$host` links so elements rendered inside
|
|
252
|
+
* a `<Portal>` are correctly treated as children of the portal's anchor.
|
|
253
|
+
*/
|
|
254
|
+
export declare const contains: (wrapper: HTMLElement, target: HTMLElement) => boolean;
|
|
255
|
+
/**
|
|
256
|
+
* Wraps a setter function of any signal or store
|
|
257
|
+
*
|
|
258
|
+
* ```ts
|
|
259
|
+
* const [data, setData] = wrapSetter(
|
|
260
|
+
* createSignal(initialData),
|
|
261
|
+
* (setter) => (next) => { console.log(next); return setter(next); },
|
|
262
|
+
* );
|
|
263
|
+
* ```
|
|
264
|
+
* If you destructure signal or store in a longer tuple, you need to use a const assertion for the types to work.
|
|
265
|
+
*/
|
|
266
|
+
export declare function wrapSetter<T>(signal: Signal<T>, wrapper: (setter: Setter<T>) => Setter<T>): Signal<T>;
|
|
267
|
+
export declare function wrapSetter<T>(store: [Store<T>, StoreSetter<T>], wrapper: (setter: StoreSetter<T>) => StoreSetter<T>): [Store<T>, StoreSetter<T>];
|
|
268
|
+
export declare function wrapSetter<T, S extends Signal<T> | [Store<T>, StoreSetter<T>] | [...Signal<T>, ...any[]] | [Store<T>, StoreSetter<T>, ...any[]]>(signalOrStore: S, wrapper: (setter: S[1]) => S[1]): S;
|
|
269
|
+
export declare function wrapSetter<T, S extends Signal<T> | [Store<T>, StoreSetter<T>] | readonly [...Signal<T>, ...any[]] | readonly [Store<T>, StoreSetter<T>, ...any[]]>(signalOrStore: S, wrapper: (setter: S[1]) => S[1]): S;
|
package/dist/index.js
CHANGED
|
@@ -1,31 +1,56 @@
|
|
|
1
|
-
import { getOwner, onCleanup, createSignal, untrack, sharedConfig,
|
|
2
|
-
|
|
1
|
+
import { getOwner, onSettled, onCleanup, createSignal, untrack, sharedConfig, DEV, } from "solid-js";
|
|
2
|
+
// isServer moved from solid-js/web (1.x) to @solidjs/web (2.x).
|
|
3
|
+
// typeof window is a universal fallback compatible with both versions.
|
|
4
|
+
const isServer = typeof window === "undefined";
|
|
3
5
|
export * from "./types.js";
|
|
4
6
|
//
|
|
5
7
|
// GENERAL HELPERS:
|
|
6
8
|
//
|
|
9
|
+
/** `true` on the server (SSR), `false` in the browser. */
|
|
7
10
|
export { isServer };
|
|
11
|
+
/** `true` in the browser, `false` on the server. */
|
|
8
12
|
export const isClient = !isServer;
|
|
13
|
+
/** `true` in the browser with Solid's DEV mode active. */
|
|
9
14
|
export const isDev = isClient && !!DEV;
|
|
15
|
+
/** `true` when DEV mode is off (i.e. production browser builds and all SSR). */
|
|
10
16
|
export const isProd = !isDev;
|
|
11
|
-
/**
|
|
17
|
+
/** No-operation function — accepts any arguments and returns `undefined`. */
|
|
12
18
|
export const noop = (() => void 0);
|
|
19
|
+
/** A function that always returns `true`. Useful as a default predicate prop. */
|
|
13
20
|
export const trueFn = () => true;
|
|
21
|
+
/** A function that always returns `false`. Useful as a default predicate prop. */
|
|
14
22
|
export const falseFn = () => false;
|
|
15
23
|
/** @deprecated use {@link equalFn} from "solid-js" */
|
|
16
|
-
export const defaultEquals =
|
|
24
|
+
export const defaultEquals = Object.is.bind(Object);
|
|
25
|
+
/**
|
|
26
|
+
* Signal options that disable equality checking, causing the signal to always
|
|
27
|
+
* notify subscribers even when the value hasn't changed. Useful for signals
|
|
28
|
+
* holding mutable objects where structural equality cannot be assumed.
|
|
29
|
+
*/
|
|
17
30
|
export const EQUALS_FALSE_OPTIONS = { equals: false };
|
|
18
|
-
export const INTERNAL_OPTIONS = { internal: true };
|
|
19
31
|
/**
|
|
20
|
-
*
|
|
32
|
+
* Signal options that allow writes from inside an owned reactive scope
|
|
33
|
+
* (component body, memo, or effect) without throwing `SIGNAL_WRITE_IN_OWNED_SCOPE`
|
|
34
|
+
* in Solid 2.0 dev mode.
|
|
35
|
+
*
|
|
36
|
+
* Use only for implementation-detail signals that genuinely need to be written
|
|
37
|
+
* from within a scope — not as a general escape hatch.
|
|
38
|
+
*/
|
|
39
|
+
export const INTERNAL_OPTIONS = { ownedWrite: true };
|
|
40
|
+
/**
|
|
41
|
+
* Returns `true` if `v` is an instance of `c`. Checks both `instanceof` and
|
|
42
|
+
* `constructor` so that values created across different realms (e.g. iframes) match.
|
|
21
43
|
*/
|
|
22
44
|
export const ofClass = (v, c) => v instanceof c || (v && v.constructor === c);
|
|
23
45
|
/** Check if value is typeof "object" or "function" */
|
|
24
46
|
export function isObject(value) {
|
|
25
47
|
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
26
48
|
}
|
|
49
|
+
/** Type guard: returns `true` when `i` is neither `null` nor `undefined`. */
|
|
27
50
|
export const isNonNullable = (i) => i != null;
|
|
51
|
+
/** Returns a new array with all `null` and `undefined` items removed. */
|
|
28
52
|
export const filterNonNullable = (arr) => arr.filter(isNonNullable);
|
|
53
|
+
/** Three-way comparison: returns `-1` if `a < b`, `1` if `a > b`, `0` if equal. */
|
|
29
54
|
export const compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
30
55
|
/**
|
|
31
56
|
* Check shallow array equality
|
|
@@ -51,6 +76,7 @@ export function reverseChain(callbacks) {
|
|
|
51
76
|
}
|
|
52
77
|
};
|
|
53
78
|
}
|
|
79
|
+
/** Clamps `n` to the inclusive range `[min, max]`. */
|
|
54
80
|
export const clamp = (n, min, max) => Math.min(Math.max(n, min), max);
|
|
55
81
|
/**
|
|
56
82
|
* Accesses the value of a MaybeAccessor
|
|
@@ -61,6 +87,10 @@ export const clamp = (n, min, max) => Math.min(Math.max(n, min), max);
|
|
|
61
87
|
* ```
|
|
62
88
|
*/
|
|
63
89
|
export const access = (v) => typeof v === "function" && !v.length ? v() : v;
|
|
90
|
+
/**
|
|
91
|
+
* Wraps `value` in a single-element array. If `value` is already an array it is
|
|
92
|
+
* returned as-is. Falsy values (except `0` and `false`) produce an empty array.
|
|
93
|
+
*/
|
|
64
94
|
export const asArray = (value) => Array.isArray(value) ? value : value ? [value] : [];
|
|
65
95
|
/**
|
|
66
96
|
* Access an array of MaybeAccessors
|
|
@@ -79,6 +109,10 @@ export const withAccess = (value, fn) => {
|
|
|
79
109
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
80
110
|
typeof _value != null && fn(_value);
|
|
81
111
|
};
|
|
112
|
+
/**
|
|
113
|
+
* Wraps a plain value in a zero-argument accessor `() => value`.
|
|
114
|
+
* If `v` is already a function it is returned unchanged.
|
|
115
|
+
*/
|
|
82
116
|
export const asAccessor = (v) => (typeof v === "function" ? v : () => v);
|
|
83
117
|
/** If value is a function – call it with a given arguments – otherwise get the value as is */
|
|
84
118
|
export function accessWith(valueOrFn, ...args) {
|
|
@@ -88,7 +122,7 @@ export function defer(deps, fn, initialValue) {
|
|
|
88
122
|
const isArray = Array.isArray(deps);
|
|
89
123
|
let prevInput;
|
|
90
124
|
let shouldDefer = true;
|
|
91
|
-
return prevValue => {
|
|
125
|
+
return ((prevValue) => {
|
|
92
126
|
let input;
|
|
93
127
|
if (isArray) {
|
|
94
128
|
input = Array(deps.length);
|
|
@@ -105,7 +139,7 @@ export function defer(deps, fn, initialValue) {
|
|
|
105
139
|
const result = untrack(() => fn(input, prevInput, prevValue));
|
|
106
140
|
prevInput = input;
|
|
107
141
|
return result;
|
|
108
|
-
};
|
|
142
|
+
});
|
|
109
143
|
}
|
|
110
144
|
/**
|
|
111
145
|
* Get entries of an object
|
|
@@ -121,6 +155,19 @@ export const keys = Object.keys;
|
|
|
121
155
|
export const tryOnCleanup = isDev
|
|
122
156
|
? fn => (getOwner() ? onCleanup(fn) : fn)
|
|
123
157
|
: onCleanup;
|
|
158
|
+
/**
|
|
159
|
+
* Creates a stack of callbacks that can be pushed to, executed all at once, or cleared.
|
|
160
|
+
* All callbacks receive the same arguments when `execute` is called; the stack is
|
|
161
|
+
* automatically cleared after execution.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* const stack = createCallbackStack<string>();
|
|
166
|
+
* stack.push(s => console.log("a:", s));
|
|
167
|
+
* stack.push(s => console.log("b:", s));
|
|
168
|
+
* stack.execute("hello"); // logs "a: hello", "b: hello" — stack is now empty
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
124
171
|
export const createCallbackStack = () => {
|
|
125
172
|
let stack = [];
|
|
126
173
|
const clear = () => (stack = []);
|
|
@@ -134,9 +181,17 @@ export const createCallbackStack = () => {
|
|
|
134
181
|
};
|
|
135
182
|
};
|
|
136
183
|
/**
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
184
|
+
* Returns a debounced wrapper around `fn` that batches rapid synchronous calls
|
|
185
|
+
* into a single invocation. Only the most-recently-passed arguments are forwarded.
|
|
186
|
+
* The counter resets on cleanup so stale microtasks are silently discarded.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```ts
|
|
190
|
+
* const flush = createMicrotask((value: string) => console.log(value));
|
|
191
|
+
* flush("a");
|
|
192
|
+
* flush("b");
|
|
193
|
+
* flush("c"); // only "c" is logged (in the next microtask)
|
|
194
|
+
* ```
|
|
140
195
|
*/
|
|
141
196
|
export function createMicrotask(fn) {
|
|
142
197
|
let calls = 0;
|
|
@@ -163,9 +218,11 @@ export function createHydratableSignal(serverValue, update, options) {
|
|
|
163
218
|
if (isServer) {
|
|
164
219
|
return createSignal(serverValue, options);
|
|
165
220
|
}
|
|
166
|
-
if (sharedConfig.
|
|
221
|
+
if (sharedConfig.hydrating) {
|
|
167
222
|
const [state, setState] = createSignal(serverValue, options);
|
|
168
|
-
|
|
223
|
+
onSettled(() => {
|
|
224
|
+
setState(() => update());
|
|
225
|
+
});
|
|
169
226
|
return [state, setState];
|
|
170
227
|
}
|
|
171
228
|
return createSignal(update(), options);
|
|
@@ -173,12 +230,13 @@ export function createHydratableSignal(serverValue, update, options) {
|
|
|
173
230
|
/** @deprecated use {@link createHydratableSignal} instead */
|
|
174
231
|
export const createHydrateSignal = createHydratableSignal;
|
|
175
232
|
/**
|
|
176
|
-
*
|
|
233
|
+
* Diffs two arrays by reference and calls `handleAdded` / `handleRemoved` for
|
|
234
|
+
* each item that appeared or disappeared. Items are compared with `===`.
|
|
177
235
|
*
|
|
178
|
-
* @param current new array
|
|
179
|
-
* @param prev previous array
|
|
180
|
-
* @param handleAdded called once for
|
|
181
|
-
* @param handleRemoved called once for
|
|
236
|
+
* @param current - the new array
|
|
237
|
+
* @param prev - the previous array snapshot
|
|
238
|
+
* @param handleAdded - called once for each item present in `current` but not `prev`
|
|
239
|
+
* @param handleRemoved - called once for each item present in `prev` but not `current`
|
|
182
240
|
*/
|
|
183
241
|
export function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
184
242
|
const currLength = current.length;
|
|
@@ -211,3 +269,98 @@ export function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
|
211
269
|
handleAdded(currEl);
|
|
212
270
|
}
|
|
213
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* Schedules `fn` to run after the browser has painted by nesting two
|
|
274
|
+
* requestAnimationFrame calls. No-op in non-browser environments.
|
|
275
|
+
*/
|
|
276
|
+
export const afterPaint = (fn) => {
|
|
277
|
+
if (typeof requestAnimationFrame === "function") {
|
|
278
|
+
requestAnimationFrame(() => requestAnimationFrame(fn));
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
// ─── String transforms ────────────────────────────────────────────────────────
|
|
282
|
+
/**
|
|
283
|
+
* Parse a string as a single JSON value.
|
|
284
|
+
*
|
|
285
|
+
* ```ts
|
|
286
|
+
* const { data } = createSSE<{ status: string }>(url, { transform: json });
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
export const json = (raw) => JSON.parse(raw);
|
|
290
|
+
/**
|
|
291
|
+
* Parse a string as newline-delimited JSON (NDJSON / JSON Lines).
|
|
292
|
+
*
|
|
293
|
+
* Each non-empty line is parsed as a separate JSON value and returned as an array.
|
|
294
|
+
*
|
|
295
|
+
* ```ts
|
|
296
|
+
* const { data } = createSSE<TickEvent[]>(url, { transform: ndjson });
|
|
297
|
+
* // data() === [{ id: 1, type: "tick" }, { id: 2, type: "tick" }]
|
|
298
|
+
* ```
|
|
299
|
+
*/
|
|
300
|
+
export const ndjson = (raw) => raw
|
|
301
|
+
.split("\n")
|
|
302
|
+
.filter(line => line !== "")
|
|
303
|
+
.map(line => JSON.parse(line));
|
|
304
|
+
/**
|
|
305
|
+
* Split a string into individual lines, returning a `string[]`. Empty lines are filtered out.
|
|
306
|
+
*
|
|
307
|
+
* ```ts
|
|
308
|
+
* const { data } = createSSE<string[]>(url, { transform: lines });
|
|
309
|
+
* // data() === ["line one", "line two"]
|
|
310
|
+
* ```
|
|
311
|
+
*/
|
|
312
|
+
export const lines = (raw) => raw.split("\n").filter(line => line !== "");
|
|
313
|
+
/**
|
|
314
|
+
* Parse a string as a number using `Number()` semantics.
|
|
315
|
+
*
|
|
316
|
+
* Note: `""` → `0`, non-numeric strings → `NaN`.
|
|
317
|
+
*
|
|
318
|
+
* ```ts
|
|
319
|
+
* const { data } = createSSE<number>(url, { transform: number });
|
|
320
|
+
* // data() === 42
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
export const number = (raw) => Number(raw);
|
|
324
|
+
export function safe(transform, fallback) {
|
|
325
|
+
return (raw) => {
|
|
326
|
+
try {
|
|
327
|
+
return transform(raw);
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
return fallback;
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Compose two transforms into one: the output of `a` is passed as the input of `b`.
|
|
336
|
+
*
|
|
337
|
+
* ```ts
|
|
338
|
+
* const { data } = createSSE<RawEvent[]>(url, {
|
|
339
|
+
* transform: pipe(ndjson<RawEvent>, rows => rows.filter(r => r.type === "tick")),
|
|
340
|
+
* });
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
export function pipe(a, b) {
|
|
344
|
+
return (raw) => b(a(raw));
|
|
345
|
+
}
|
|
346
|
+
// ─── DOM helpers ─────────────────────────────────────────────────────────────
|
|
347
|
+
/**
|
|
348
|
+
* Check if a wrapper element contains a target element.
|
|
349
|
+
* Portal-aware: follows SolidJS `_$host` links so elements rendered inside
|
|
350
|
+
* a `<Portal>` are correctly treated as children of the portal's anchor.
|
|
351
|
+
*/
|
|
352
|
+
export const contains = (wrapper, target) => {
|
|
353
|
+
if (wrapper.contains(target))
|
|
354
|
+
return true;
|
|
355
|
+
let current = target;
|
|
356
|
+
while (current) {
|
|
357
|
+
if (current === wrapper)
|
|
358
|
+
return true;
|
|
359
|
+
// @ts-expect-error: _$host is a SolidJS-internal property set on portal roots
|
|
360
|
+
current = current._$host ?? current.parentElement;
|
|
361
|
+
}
|
|
362
|
+
return false;
|
|
363
|
+
};
|
|
364
|
+
export function wrapSetter(signalOrStore, wrapper) {
|
|
365
|
+
return [signalOrStore[0], wrapper(signalOrStore[1]), ...signalOrStore.slice(2)];
|
|
366
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import type { Accessor, Setter } from "solid-js";
|
|
2
|
-
export type { EffectOptions, OnOptions } from "solid-js";
|
|
3
|
-
export type { ResolvedJSXElement, ResolvedChildren } from "solid-js/types/reactive/signal.js";
|
|
4
2
|
/**
|
|
5
3
|
* Can be single or in an array
|
|
6
4
|
*/
|
|
@@ -31,6 +29,9 @@ export type MaybeAccessor<T> = T | Accessor<T>;
|
|
|
31
29
|
* ```
|
|
32
30
|
*/
|
|
33
31
|
export type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends () => any ? ReturnType<T> : T;
|
|
32
|
+
export type AccessorArray<T> = [...Extract<{
|
|
33
|
+
[K in keyof T]: Accessor<T[K]>;
|
|
34
|
+
}, readonly unknown[]>];
|
|
34
35
|
export type OnAccessEffectFunction<S, Prev, Next extends Prev = Prev> = (input: AccessReturnTypes<S>, prevInput: AccessReturnTypes<S>, v: Prev) => Next;
|
|
35
36
|
export type AccessReturnTypes<S> = S extends MaybeAccessor<any>[] ? {
|
|
36
37
|
[I in keyof S]: AccessReturnTypes<S[I]>;
|
|
@@ -87,3 +88,4 @@ type RawNarrow<T> = (T extends [] ? [] : never) | (T extends string | number | b
|
|
|
87
88
|
};
|
|
88
89
|
export type Narrow<T> = T extends [] ? T : RawNarrow<T>;
|
|
89
90
|
export type NoInfer<T> = [T][T extends any ? 0 : never];
|
|
91
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solid-primitives/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-next.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": [
|
|
@@ -38,12 +38,22 @@
|
|
|
38
38
|
"types": "./dist/immutable/index.d.ts",
|
|
39
39
|
"default": "./dist/immutable/index.js"
|
|
40
40
|
}
|
|
41
|
+
},
|
|
42
|
+
"./colors": {
|
|
43
|
+
"import": {
|
|
44
|
+
"@solid-primitives/source": "./src/colors/index.ts",
|
|
45
|
+
"types": "./dist/colors/index.d.ts",
|
|
46
|
+
"default": "./dist/colors/index.js"
|
|
47
|
+
}
|
|
41
48
|
}
|
|
42
49
|
},
|
|
43
50
|
"typesVersions": {
|
|
44
51
|
"*": {
|
|
45
52
|
"immutable": [
|
|
46
53
|
"./dist/immutable/index.d.ts"
|
|
54
|
+
],
|
|
55
|
+
"colors": [
|
|
56
|
+
"./dist/colors/index.d.ts"
|
|
47
57
|
]
|
|
48
58
|
}
|
|
49
59
|
},
|
|
@@ -53,11 +63,82 @@
|
|
|
53
63
|
"solid",
|
|
54
64
|
"primitives"
|
|
55
65
|
],
|
|
66
|
+
"primitive": {
|
|
67
|
+
"name": "utils",
|
|
68
|
+
"stage": 3,
|
|
69
|
+
"list": [
|
|
70
|
+
"shallowArrayCopy",
|
|
71
|
+
"shallowObjectCopy",
|
|
72
|
+
"shallowCopy",
|
|
73
|
+
"withArrayCopy",
|
|
74
|
+
"withObjectCopy",
|
|
75
|
+
"withCopy",
|
|
76
|
+
"push",
|
|
77
|
+
"drop",
|
|
78
|
+
"dropRight",
|
|
79
|
+
"filterOut",
|
|
80
|
+
"filter",
|
|
81
|
+
"sort",
|
|
82
|
+
"sortBy",
|
|
83
|
+
"map",
|
|
84
|
+
"slice",
|
|
85
|
+
"splice",
|
|
86
|
+
"fill",
|
|
87
|
+
"concat",
|
|
88
|
+
"remove",
|
|
89
|
+
"removeItems",
|
|
90
|
+
"flatten",
|
|
91
|
+
"filterInstance",
|
|
92
|
+
"filterOutInstance",
|
|
93
|
+
"omit",
|
|
94
|
+
"pick",
|
|
95
|
+
"split",
|
|
96
|
+
"merge",
|
|
97
|
+
"get",
|
|
98
|
+
"update",
|
|
99
|
+
"add",
|
|
100
|
+
"substract",
|
|
101
|
+
"multiply",
|
|
102
|
+
"divide",
|
|
103
|
+
"power",
|
|
104
|
+
"clamp",
|
|
105
|
+
"json",
|
|
106
|
+
"ndjson",
|
|
107
|
+
"lines",
|
|
108
|
+
"number",
|
|
109
|
+
"safe",
|
|
110
|
+
"pipe",
|
|
111
|
+
"wrapSetter",
|
|
112
|
+
"parseColor",
|
|
113
|
+
"normalizeColor",
|
|
114
|
+
"tryParseColor",
|
|
115
|
+
"isValidColor",
|
|
116
|
+
"detectColorFormat",
|
|
117
|
+
"getColorChannels",
|
|
118
|
+
"normalizeHue",
|
|
119
|
+
"colorToOKLCH",
|
|
120
|
+
"lighten",
|
|
121
|
+
"darken",
|
|
122
|
+
"saturate",
|
|
123
|
+
"desaturate",
|
|
124
|
+
"complement",
|
|
125
|
+
"mix",
|
|
126
|
+
"contrastRatio",
|
|
127
|
+
"isReadable",
|
|
128
|
+
"colorScale",
|
|
129
|
+
"perceptualColorScale",
|
|
130
|
+
"COLOR_INTL_TRANSLATIONS"
|
|
131
|
+
],
|
|
132
|
+
"category": "Utilities",
|
|
133
|
+
"gzip": 1399
|
|
134
|
+
},
|
|
56
135
|
"peerDependencies": {
|
|
57
|
-
"
|
|
136
|
+
"@solidjs/web": "^2.0.0-beta.15",
|
|
137
|
+
"solid-js": "^2.0.0-beta.15"
|
|
58
138
|
},
|
|
59
139
|
"devDependencies": {
|
|
60
|
-
"
|
|
140
|
+
"@solidjs/web": "2.0.0-beta.15",
|
|
141
|
+
"solid-js": "2.0.0-beta.15"
|
|
61
142
|
},
|
|
62
143
|
"scripts": {
|
|
63
144
|
"dev": "node --import=@nothing-but/node-resolve-ts --experimental-transform-types ../../scripts/dev.ts",
|