@solid-primitives/utils 7.0.0-next.0 → 7.0.0-next.2
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/colors/helpers.d.ts +46 -41
- package/dist/colors/helpers.js +776 -851
- package/dist/colors/index.d.ts +6 -4
- package/dist/colors/index.js +5 -4
- package/dist/colors/intl.d.ts +56 -52
- package/dist/colors/intl.js +55 -66
- package/dist/colors/manipulation.d.ts +136 -131
- package/dist/colors/manipulation.js +215 -242
- package/dist/colors/types.d.ts +73 -68
- package/dist/colors/types.js +1 -14
- package/dist/immutable/array.d.ts +113 -108
- package/dist/immutable/array.js +122 -128
- package/dist/immutable/copy.d.ts +25 -21
- package/dist/immutable/copy.js +31 -29
- package/dist/immutable/index.d.ts +8 -6
- package/dist/immutable/index.js +7 -6
- package/dist/immutable/number.d.ts +11 -7
- package/dist/immutable/number.js +21 -27
- package/dist/immutable/object.d.ts +55 -58
- package/dist/immutable/object.js +46 -48
- package/dist/immutable/types.d.ts +9 -7
- package/dist/immutable/types.js +1 -1
- package/dist/immutable/update.d.ts +33 -28
- package/dist/immutable/update.js +27 -27
- package/dist/index.d.ts +226 -222
- package/dist/index.js +285 -311
- package/dist/types.d.ts +63 -75
- package/dist/types.js +1 -1
- package/package.json +11 -7
package/dist/index.js
CHANGED
|
@@ -1,366 +1,340 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/* @ts-self-types="./index.d.ts" */
|
|
2
|
+
import { DEV, createSignal, getOwner, onCleanup, onSettled, sharedConfig, untrack } from "solid-js";
|
|
3
|
+
//#region src/index.ts
|
|
4
4
|
const isServer = typeof window === "undefined";
|
|
5
|
-
export * from "./types.js";
|
|
6
|
-
//
|
|
7
|
-
// GENERAL HELPERS:
|
|
8
|
-
//
|
|
9
|
-
/** `true` on the server (SSR), `false` in the browser. */
|
|
10
|
-
export { isServer };
|
|
11
5
|
/** `true` in the browser, `false` on the server. */
|
|
12
|
-
|
|
6
|
+
const isClient = !isServer;
|
|
13
7
|
/** `true` in the browser with Solid's DEV mode active. */
|
|
14
|
-
|
|
8
|
+
const isDev = isClient && !!DEV;
|
|
15
9
|
/** `true` when DEV mode is off (i.e. production browser builds and all SSR). */
|
|
16
|
-
|
|
10
|
+
const isProd = !isDev;
|
|
17
11
|
/** No-operation function — accepts any arguments and returns `undefined`. */
|
|
18
|
-
|
|
12
|
+
const noop = (() => void 0);
|
|
19
13
|
/** A function that always returns `true`. Useful as a default predicate prop. */
|
|
20
|
-
|
|
14
|
+
const trueFn = () => true;
|
|
21
15
|
/** A function that always returns `false`. Useful as a default predicate prop. */
|
|
22
|
-
|
|
16
|
+
const falseFn = () => false;
|
|
23
17
|
/** @deprecated use {@link equalFn} from "solid-js" */
|
|
24
|
-
|
|
18
|
+
const defaultEquals = Object.is.bind(Object);
|
|
25
19
|
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
20
|
+
* Signal options that disable equality checking, causing the signal to always
|
|
21
|
+
* notify subscribers even when the value hasn't changed. Useful for signals
|
|
22
|
+
* holding mutable objects where structural equality cannot be assumed.
|
|
23
|
+
*/
|
|
24
|
+
const EQUALS_FALSE_OPTIONS = { equals: false };
|
|
31
25
|
/**
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
26
|
+
* Signal options that allow writes from inside an owned reactive scope
|
|
27
|
+
* (component body, memo, or effect) without throwing `SIGNAL_WRITE_IN_OWNED_SCOPE`
|
|
28
|
+
* in Solid 2.0 dev mode.
|
|
29
|
+
*
|
|
30
|
+
* Use only for implementation-detail signals that genuinely need to be written
|
|
31
|
+
* from within a scope — not as a general escape hatch.
|
|
32
|
+
*/
|
|
33
|
+
const INTERNAL_OPTIONS = { ownedWrite: true };
|
|
40
34
|
/**
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
35
|
+
* Returns `true` if `v` is an instance of `c`. Checks both `instanceof` and
|
|
36
|
+
* `constructor` so that values created across different realms (e.g. iframes) match.
|
|
37
|
+
*/
|
|
38
|
+
const ofClass = (v, c) => v instanceof c || v && v.constructor === c;
|
|
45
39
|
/** Check if value is typeof "object" or "function" */
|
|
46
|
-
|
|
47
|
-
|
|
40
|
+
function isObject(value) {
|
|
41
|
+
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
48
42
|
}
|
|
49
43
|
/** Type guard: returns `true` when `i` is neither `null` nor `undefined`. */
|
|
50
|
-
|
|
44
|
+
const isNonNullable = (i) => i != null;
|
|
51
45
|
/** Returns a new array with all `null` and `undefined` items removed. */
|
|
52
|
-
|
|
46
|
+
const filterNonNullable = (arr) => arr.filter(isNonNullable);
|
|
53
47
|
/** Three-way comparison: returns `-1` if `a < b`, `1` if `a > b`, `0` if equal. */
|
|
54
|
-
|
|
48
|
+
const compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
55
49
|
/**
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
* Check shallow array equality
|
|
51
|
+
*/
|
|
52
|
+
const arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
|
|
59
53
|
/**
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
};
|
|
54
|
+
* Returns a function that will call all functions in the order they were chained with the same arguments.
|
|
55
|
+
*/
|
|
56
|
+
function chain(callbacks) {
|
|
57
|
+
return (...args) => {
|
|
58
|
+
for (const callback of callbacks) callback && callback(...args);
|
|
59
|
+
};
|
|
67
60
|
}
|
|
68
61
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
62
|
+
* Returns a function that will call all functions in the reversed order with the same arguments.
|
|
63
|
+
*/
|
|
64
|
+
function reverseChain(callbacks) {
|
|
65
|
+
return (...args) => {
|
|
66
|
+
for (let i = callbacks.length - 1; i >= 0; i--) {
|
|
67
|
+
const callback = callbacks[i];
|
|
68
|
+
callback && callback(...args);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
78
71
|
}
|
|
79
72
|
/** Clamps `n` to the inclusive range `[min, max]`. */
|
|
80
|
-
|
|
73
|
+
const clamp = (n, min, max) => Math.min(Math.max(n, min), max);
|
|
74
|
+
/** Creates an ID generator with its own isolated counter and per-generator random segment. Each call to the returned function produces a `timestamp-sequence-random` string that is unique within the generator and with high probability unique across independent generators (e.g. separate processes or SSR requests). */
|
|
75
|
+
const createIdGenerator = () => {
|
|
76
|
+
let seq = 0;
|
|
77
|
+
const rand = Math.random().toString(36).slice(2, 8);
|
|
78
|
+
return () => `${Date.now().toString(36)}-${(++seq).toString(36)}-${rand}`;
|
|
79
|
+
};
|
|
81
80
|
/**
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
81
|
+
* Accesses the value of a MaybeAccessor
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* access("foo") // => "foo"
|
|
85
|
+
* access(() => "foo") // => "foo"
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
const access = (v) => typeof v === "function" && !v.length ? v() : v;
|
|
90
89
|
/**
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
* Wraps `value` in a single-element array. If `value` is already an array it is
|
|
91
|
+
* returned as-is. Falsy values (except `0` and `false`) produce an empty array.
|
|
92
|
+
*/
|
|
93
|
+
const asArray = (value) => Array.isArray(value) ? value : value ? [value] : [];
|
|
95
94
|
/**
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
95
|
+
* Access an array of MaybeAccessors
|
|
96
|
+
* @example
|
|
97
|
+
* const list = [1, 2, () => 3)] // T: MaybeAccessor<number>[]
|
|
98
|
+
* const newList = accessArray(list) // T: number[]
|
|
99
|
+
*/
|
|
100
|
+
const accessArray = (list) => list.map((v) => access(v));
|
|
102
101
|
/**
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
110
|
-
typeof _value != null && fn(_value);
|
|
102
|
+
* Run the function if the accessed value is not `undefined` nor `null`
|
|
103
|
+
* @param value
|
|
104
|
+
* @param fn
|
|
105
|
+
*/
|
|
106
|
+
const withAccess = (value, fn) => {
|
|
107
|
+
fn(access(value));
|
|
111
108
|
};
|
|
112
109
|
/**
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
110
|
+
* Wraps a plain value in a zero-argument accessor `() => value`.
|
|
111
|
+
* If `v` is already a function it is returned unchanged.
|
|
112
|
+
*/
|
|
113
|
+
const asAccessor = (v) => typeof v === "function" ? v : () => v;
|
|
117
114
|
/** If value is a function – call it with a given arguments – otherwise get the value as is */
|
|
118
|
-
|
|
119
|
-
|
|
115
|
+
function accessWith(valueOrFn, ...args) {
|
|
116
|
+
return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
|
|
120
117
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
prevInput = input;
|
|
141
|
-
return result;
|
|
142
|
-
});
|
|
118
|
+
function defer(deps, fn, initialValue) {
|
|
119
|
+
const isArray = Array.isArray(deps);
|
|
120
|
+
let prevInput;
|
|
121
|
+
let shouldDefer = true;
|
|
122
|
+
return ((prevValue) => {
|
|
123
|
+
let input;
|
|
124
|
+
if (isArray) {
|
|
125
|
+
input = Array(deps.length);
|
|
126
|
+
for (let i = 0; i < deps.length; i++) input[i] = deps[i]();
|
|
127
|
+
} else input = deps();
|
|
128
|
+
if (shouldDefer) {
|
|
129
|
+
shouldDefer = false;
|
|
130
|
+
prevInput = input;
|
|
131
|
+
return initialValue;
|
|
132
|
+
}
|
|
133
|
+
const result = untrack(() => fn(input, prevInput, prevValue));
|
|
134
|
+
prevInput = input;
|
|
135
|
+
return result;
|
|
136
|
+
});
|
|
143
137
|
}
|
|
144
138
|
/**
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
139
|
+
* Get entries of an object
|
|
140
|
+
*/
|
|
141
|
+
const entries = Object.entries;
|
|
148
142
|
/**
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
143
|
+
* Get keys of an object
|
|
144
|
+
*/
|
|
145
|
+
const keys = Object.keys;
|
|
152
146
|
/**
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
? fn => (getOwner() ? onCleanup(fn) : fn)
|
|
157
|
-
: onCleanup;
|
|
147
|
+
* Solid's `onCleanup` that doesn't warn in development if used outside of a component.
|
|
148
|
+
*/
|
|
149
|
+
const tryOnCleanup = isDev ? (fn) => getOwner() ? onCleanup(fn) : fn : onCleanup;
|
|
158
150
|
/**
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
151
|
+
* Creates a stack of callbacks that can be pushed to, executed all at once, or cleared.
|
|
152
|
+
* All callbacks receive the same arguments when `execute` is called; the stack is
|
|
153
|
+
* automatically cleared after execution.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* const stack = createCallbackStack<string>();
|
|
158
|
+
* stack.push(s => console.log("a:", s));
|
|
159
|
+
* stack.push(s => console.log("b:", s));
|
|
160
|
+
* stack.execute("hello"); // logs "a: hello", "b: hello" — stack is now empty
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
const createCallbackStack = () => {
|
|
164
|
+
let stack = [];
|
|
165
|
+
const clear = () => stack = [];
|
|
166
|
+
return {
|
|
167
|
+
push: (...callbacks) => stack.push(...callbacks),
|
|
168
|
+
execute(arg0, arg1, arg2, arg3) {
|
|
169
|
+
stack.forEach((cb) => cb(arg0, arg1, arg2, arg3));
|
|
170
|
+
clear();
|
|
171
|
+
},
|
|
172
|
+
clear
|
|
173
|
+
};
|
|
182
174
|
};
|
|
183
175
|
/**
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
176
|
+
* Returns a debounced wrapper around `fn` that batches rapid synchronous calls
|
|
177
|
+
* into a single invocation. Only the most-recently-passed arguments are forwarded.
|
|
178
|
+
* The counter resets on cleanup so stale microtasks are silently discarded.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* const flush = createMicrotask((value: string) => console.log(value));
|
|
183
|
+
* flush("a");
|
|
184
|
+
* flush("b");
|
|
185
|
+
* flush("c"); // only "c" is logged (in the next microtask)
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
function createMicrotask(fn) {
|
|
189
|
+
let calls = 0;
|
|
190
|
+
let args;
|
|
191
|
+
onCleanup(() => calls = 0);
|
|
192
|
+
return (...a) => {
|
|
193
|
+
args = a, calls++;
|
|
194
|
+
queueMicrotask(() => --calls === 0 && fn(...args));
|
|
195
|
+
};
|
|
204
196
|
}
|
|
205
197
|
/**
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
228
|
-
return createSignal(update(), options);
|
|
198
|
+
* 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.
|
|
199
|
+
*
|
|
200
|
+
* @param serverValue initial value of the state on the server
|
|
201
|
+
* @param update called once on the client or on hydration to initialize the value
|
|
202
|
+
* @param options {@link SignalOptions}
|
|
203
|
+
* @returns
|
|
204
|
+
* ```ts
|
|
205
|
+
* [state: Accessor<T>, setState: Setter<T>]
|
|
206
|
+
* ```
|
|
207
|
+
* @see {@link createSignal}
|
|
208
|
+
*/
|
|
209
|
+
function createHydratableSignal(serverValue, update, options) {
|
|
210
|
+
if (isServer) return createSignal(serverValue, options);
|
|
211
|
+
if (sharedConfig.hydrating) {
|
|
212
|
+
const [state, setState] = createSignal(serverValue, options);
|
|
213
|
+
onSettled(() => {
|
|
214
|
+
setState(() => update());
|
|
215
|
+
});
|
|
216
|
+
return [state, setState];
|
|
217
|
+
}
|
|
218
|
+
return createSignal(update(), options);
|
|
229
219
|
}
|
|
230
220
|
/** @deprecated use {@link createHydratableSignal} instead */
|
|
231
|
-
|
|
221
|
+
const createHydrateSignal = createHydratableSignal;
|
|
232
222
|
/**
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
let currEl;
|
|
261
|
-
prev = prev.slice(i);
|
|
262
|
-
current = current.slice(i);
|
|
263
|
-
for (prevEl of prev) {
|
|
264
|
-
if (!current.includes(prevEl))
|
|
265
|
-
handleRemoved(prevEl);
|
|
266
|
-
}
|
|
267
|
-
for (currEl of current) {
|
|
268
|
-
if (!prev.includes(currEl))
|
|
269
|
-
handleAdded(currEl);
|
|
270
|
-
}
|
|
223
|
+
* Diffs two arrays by reference and calls `handleAdded` / `handleRemoved` for
|
|
224
|
+
* each item that appeared or disappeared. Items are compared with `===`.
|
|
225
|
+
*
|
|
226
|
+
* @param current - the new array
|
|
227
|
+
* @param prev - the previous array snapshot
|
|
228
|
+
* @param handleAdded - called once for each item present in `current` but not `prev`
|
|
229
|
+
* @param handleRemoved - called once for each item present in `prev` but not `current`
|
|
230
|
+
*/
|
|
231
|
+
function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
232
|
+
const currLength = current.length;
|
|
233
|
+
const prevLength = prev.length;
|
|
234
|
+
let i = 0;
|
|
235
|
+
if (!prevLength) {
|
|
236
|
+
for (; i < currLength; i++) handleAdded(current[i]);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (!currLength) {
|
|
240
|
+
for (; i < prevLength; i++) handleRemoved(prev[i]);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
for (; i < prevLength; i++) if (prev[i] !== current[i]) break;
|
|
244
|
+
let prevEl;
|
|
245
|
+
let currEl;
|
|
246
|
+
prev = prev.slice(i);
|
|
247
|
+
current = current.slice(i);
|
|
248
|
+
for (prevEl of prev) if (!current.includes(prevEl)) handleRemoved(prevEl);
|
|
249
|
+
for (currEl of current) if (!prev.includes(currEl)) handleAdded(currEl);
|
|
271
250
|
}
|
|
272
251
|
/**
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
requestAnimationFrame(() => requestAnimationFrame(fn));
|
|
279
|
-
}
|
|
252
|
+
* Schedules `fn` to run after the browser has painted by nesting two
|
|
253
|
+
* requestAnimationFrame calls. No-op in non-browser environments.
|
|
254
|
+
*/
|
|
255
|
+
const afterPaint = (fn) => {
|
|
256
|
+
if (typeof requestAnimationFrame === "function") requestAnimationFrame(() => requestAnimationFrame(fn));
|
|
280
257
|
};
|
|
281
|
-
// ─── String transforms ────────────────────────────────────────────────────────
|
|
282
258
|
/**
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
259
|
+
* Parse a string as a single JSON value.
|
|
260
|
+
*
|
|
261
|
+
* ```ts
|
|
262
|
+
* const { data } = createSSE<{ status: string }>(url, { transform: json });
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
const json = (raw) => JSON.parse(raw);
|
|
290
266
|
/**
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
.split("\n")
|
|
302
|
-
.filter(line => line !== "")
|
|
303
|
-
.map(line => JSON.parse(line));
|
|
267
|
+
* Parse a string as newline-delimited JSON (NDJSON / JSON Lines).
|
|
268
|
+
*
|
|
269
|
+
* Each non-empty line is parsed as a separate JSON value and returned as an array.
|
|
270
|
+
*
|
|
271
|
+
* ```ts
|
|
272
|
+
* const { data } = createSSE<TickEvent[]>(url, { transform: ndjson });
|
|
273
|
+
* // data() === [{ id: 1, type: "tick" }, { id: 2, type: "tick" }]
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
const ndjson = (raw) => raw.split("\n").filter((line) => line !== "").map((line) => JSON.parse(line));
|
|
304
277
|
/**
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
278
|
+
* Split a string into individual lines, returning a `string[]`. Empty lines are filtered out.
|
|
279
|
+
*
|
|
280
|
+
* ```ts
|
|
281
|
+
* const { data } = createSSE<string[]>(url, { transform: lines });
|
|
282
|
+
* // data() === ["line one", "line two"]
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
const lines = (raw) => raw.split("\n").filter((line) => line !== "");
|
|
313
286
|
/**
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
};
|
|
287
|
+
* Parse a string as a number using `Number()` semantics.
|
|
288
|
+
*
|
|
289
|
+
* Note: `""` → `0`, non-numeric strings → `NaN`.
|
|
290
|
+
*
|
|
291
|
+
* ```ts
|
|
292
|
+
* const { data } = createSSE<number>(url, { transform: number });
|
|
293
|
+
* // data() === 42
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
const number = (raw) => Number(raw);
|
|
297
|
+
function safe(transform, fallback) {
|
|
298
|
+
return (raw) => {
|
|
299
|
+
try {
|
|
300
|
+
return transform(raw);
|
|
301
|
+
} catch {
|
|
302
|
+
return fallback;
|
|
303
|
+
}
|
|
304
|
+
};
|
|
333
305
|
}
|
|
334
306
|
/**
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
307
|
+
* Compose two transforms into one: the output of `a` is passed as the input of `b`.
|
|
308
|
+
*
|
|
309
|
+
* ```ts
|
|
310
|
+
* const { data } = createSSE<RawEvent[]>(url, {
|
|
311
|
+
* transform: pipe(ndjson<RawEvent>, rows => rows.filter(r => r.type === "tick")),
|
|
312
|
+
* });
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
function pipe(a, b) {
|
|
316
|
+
return (raw) => b(a(raw));
|
|
345
317
|
}
|
|
346
|
-
// ─── DOM helpers ─────────────────────────────────────────────────────────────
|
|
347
318
|
/**
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
current = current._$host ?? current.parentElement;
|
|
361
|
-
}
|
|
362
|
-
return false;
|
|
319
|
+
* Check if a wrapper element contains a target element.
|
|
320
|
+
* Portal-aware: follows SolidJS `_$host` links so elements rendered inside
|
|
321
|
+
* a `<Portal>` are correctly treated as children of the portal's anchor.
|
|
322
|
+
*/
|
|
323
|
+
const contains = (wrapper, target) => {
|
|
324
|
+
if (wrapper.contains(target)) return true;
|
|
325
|
+
let current = target;
|
|
326
|
+
while (current) {
|
|
327
|
+
if (current === wrapper) return true;
|
|
328
|
+
current = current._$host ?? current.parentElement;
|
|
329
|
+
}
|
|
330
|
+
return false;
|
|
363
331
|
};
|
|
364
|
-
|
|
365
|
-
|
|
332
|
+
function wrapSetter(signalOrStore, wrapper) {
|
|
333
|
+
return [
|
|
334
|
+
signalOrStore[0],
|
|
335
|
+
wrapper(signalOrStore[1]),
|
|
336
|
+
...signalOrStore.slice(2)
|
|
337
|
+
];
|
|
366
338
|
}
|
|
339
|
+
//#endregion
|
|
340
|
+
export { EQUALS_FALSE_OPTIONS, INTERNAL_OPTIONS, access, accessArray, accessWith, afterPaint, arrayEquals, asAccessor, asArray, chain, clamp, compare, contains, createCallbackStack, createHydratableSignal, createHydrateSignal, createIdGenerator, createMicrotask, defaultEquals, defer, entries, falseFn, filterNonNullable, handleDiffArray, isClient, isDev, isNonNullable, isObject, isProd, isServer, json, keys, lines, ndjson, noop, number, ofClass, pipe, reverseChain, safe, trueFn, tryOnCleanup, withAccess, wrapSetter };
|