@solid-primitives/utils 6.3.1 → 6.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -96,6 +96,25 @@ setUser(p => update(p, "street", "number", 64));
96
96
  - **`power`** - `a ** b ** c ** ...`
97
97
  - **`clamp`** - clamp a number value between two other values
98
98
 
99
+ ## String transforms
100
+
101
+ `(string) => T` transform functions for converting raw string data into typed values. Useful as the `transform` option for SSE, WebSocket, and similar streaming primitives.
102
+
103
+ ```ts
104
+ import { json, ndjson, safe } from "@solid-primitives/utils";
105
+
106
+ const { data } = createSSE<Event>(url, { transform: json });
107
+ const { data } = createSSE<Event[]>(url, { transform: ndjson });
108
+ const { data } = createSSE<Event>(url, { transform: safe(json) });
109
+ ```
110
+
111
+ - **`json`** - Parse a string as a single JSON value
112
+ - **`ndjson`** - Parse newline-delimited JSON (NDJSON / JSON Lines) into an array
113
+ - **`lines`** - Split a string into a `string[]` by newline, filtering empty lines
114
+ - **`number`** - Parse a string as a number via `Number()`
115
+ - **`safe(transform, fallback?)`** - Wrap any transform in a `try/catch`; returns `fallback` instead of throwing
116
+ - **`pipe(a, b)`** - Compose two transforms into one
117
+
99
118
  ## Changelog
100
119
 
101
120
  See [CHANGELOG.md](./CHANGELOG.md)
@@ -1,5 +1,5 @@
1
- import { AnyClass, ItemsOf, Many } from "../index.js";
2
- import { FlattenArray, MappingFn, Predicate } from "./types.js";
1
+ import { type AnyClass, type ItemsOf, type Many } from "../index.js";
2
+ import type { FlattenArray, MappingFn, Predicate } from "./types.js";
3
3
  /**
4
4
  * non-mutating `Array.prototype.push()`
5
5
  * @returns changed array copy
@@ -1,4 +1,4 @@
1
- import { Modify } from "../index.js";
1
+ import { type Modify } from "../index.js";
2
2
  /**
3
3
  * Create a new subset object without the provided keys
4
4
  *
@@ -1,4 +1,5 @@
1
1
  import { withObjectCopy, shallowObjectCopy } from "./copy.js";
2
+ import {} from "../index.js";
2
3
  /**
3
4
  * Create a new subset object without the provided keys
4
5
  *
@@ -1,4 +1,4 @@
1
- import { ItemsOf } from "../index.js";
1
+ import { type ItemsOf } from "../index.js";
2
2
  export type Predicate<T> = (item: T, index: number, array: readonly T[]) => boolean;
3
3
  export type MappingFn<T, V> = (item: T, index: number, array: readonly T[]) => V;
4
4
  export type FlattenArray<T> = T extends any[] ? FlattenArray<ItemsOf<T>> : T;
@@ -1 +1 @@
1
- export {};
1
+ import {} from "../index.js";
@@ -1,4 +1,4 @@
1
- import { ModifyValue } from "./types.js";
1
+ import { type ModifyValue } from "./types.js";
2
2
  export type UpdateSetter<O, K extends keyof O, V> = V | ((prev: O[K]) => V);
3
3
  export type Update = {
4
4
  <O extends object, K0 extends keyof O, K1 extends keyof O[K0], K2 extends keyof O[K0][K1], K3 extends keyof O[K0][K1][K2], K4 extends keyof O[K0][K1][K2][K3], V>(object: O, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, setter: UpdateSetter<O[K0][K1][K2][K3], K4, V>): ModifyValue<O, K0, ModifyValue<O[K0], K1, ModifyValue<O[K0][K1], K2, ModifyValue<O[K0][K1][K2], K3, ModifyValue<O[K0][K1][K2][K3], K4, V>>>>>;
@@ -1,4 +1,5 @@
1
1
  import { withCopy } from "./copy.js";
2
+ import {} from "./types.js";
2
3
  /**
3
4
  * Change single value in an object by key. Allows accessign nested objects by passing multiple keys.
4
5
  *
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { onCleanup, createSignal, Accessor, AccessorArray, EffectFunction, NoInfer, SignalOptions } from "solid-js";
1
+ import { onCleanup, createSignal, type Accessor, type AccessorArray, type EffectFunction, type NoInfer, type SignalOptions } from "solid-js";
2
2
  import { isServer } from "solid-js/web";
3
3
  import type { AnyClass, MaybeAccessor, MaybeAccessorValue, Noop, AnyObject, AnyFunction } from "./types.js";
4
4
  export * from "./types.js";
@@ -50,7 +50,7 @@ export declare const clamp: (n: number, min: number, max: number) => number;
50
50
  * access(() => "foo") // => "foo"
51
51
  * ```
52
52
  */
53
- export declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
53
+ export declare const access: <T extends MaybeAccessor<any>>(v: T) => MaybeAccessorValue<T>;
54
54
  export declare const asArray: <T>(value: T) => (T extends any[] ? T[number] : NonNullable<T>)[];
55
55
  /**
56
56
  * Access an array of MaybeAccessors
@@ -58,7 +58,7 @@ export declare const asArray: <T>(value: T) => (T extends any[] ? T[number] : No
58
58
  * const list = [1, 2, () => 3)] // T: MaybeAccessor<number>[]
59
59
  * const newList = accessArray(list) // T: number[]
60
60
  */
61
- export declare const accessArray: <A extends unknown>(list: readonly A[]) => MaybeAccessorValue<A>[];
61
+ export declare const accessArray: <A extends MaybeAccessor<any>>(list: readonly A[]) => MaybeAccessorValue<A>[];
62
62
  /**
63
63
  * Run the function if the accessed value is not `undefined` nor `null`
64
64
  * @param value
@@ -124,3 +124,63 @@ export declare const createHydrateSignal: typeof createHydratableSignal;
124
124
  * @param handleRemoved called once for every removed from array
125
125
  */
126
126
  export declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
127
+ /**
128
+ * Parse a string as a single JSON value.
129
+ *
130
+ * ```ts
131
+ * const { data } = createSSE<{ status: string }>(url, { transform: json });
132
+ * ```
133
+ */
134
+ export declare const json: <T>(raw: string) => T;
135
+ /**
136
+ * Parse a string as newline-delimited JSON (NDJSON / JSON Lines).
137
+ *
138
+ * Each non-empty line is parsed as a separate JSON value and returned as an array.
139
+ *
140
+ * ```ts
141
+ * const { data } = createSSE<TickEvent[]>(url, { transform: ndjson });
142
+ * // data() === [{ id: 1, type: "tick" }, { id: 2, type: "tick" }]
143
+ * ```
144
+ */
145
+ export declare const ndjson: <T>(raw: string) => T[];
146
+ /**
147
+ * Split a string into individual lines, returning a `string[]`. Empty lines are filtered out.
148
+ *
149
+ * ```ts
150
+ * const { data } = createSSE<string[]>(url, { transform: lines });
151
+ * // data() === ["line one", "line two"]
152
+ * ```
153
+ */
154
+ export declare const lines: (raw: string) => string[];
155
+ /**
156
+ * Parse a string as a number using `Number()` semantics.
157
+ *
158
+ * Note: `""` → `0`, non-numeric strings → `NaN`.
159
+ *
160
+ * ```ts
161
+ * const { data } = createSSE<number>(url, { transform: number });
162
+ * // data() === 42
163
+ * ```
164
+ */
165
+ export declare const number: (raw: string) => number;
166
+ /**
167
+ * Wrap any `(string) => T` transform in a `try/catch`. Returns `fallback`
168
+ * (default `undefined`) instead of throwing on malformed input.
169
+ *
170
+ * ```ts
171
+ * const { data } = createSSE<MyEvent>(url, { transform: safe(json) });
172
+ * const { data } = createSSE<number>(url, { transform: safe(number, 0) });
173
+ * ```
174
+ */
175
+ export declare function safe<T>(transform: (raw: string) => T): (raw: string) => T | undefined;
176
+ export declare function safe<T>(transform: (raw: string) => T, fallback: T): (raw: string) => T;
177
+ /**
178
+ * Compose two transforms into one: the output of `a` is passed as the input of `b`.
179
+ *
180
+ * ```ts
181
+ * const { data } = createSSE<RawEvent[]>(url, {
182
+ * transform: pipe(ndjson<RawEvent>, rows => rows.filter(r => r.type === "tick")),
183
+ * });
184
+ * ```
185
+ */
186
+ export declare function pipe<A, B>(a: (raw: string) => A, b: (a: A) => B): (raw: string) => B;
package/dist/index.js CHANGED
@@ -211,3 +211,68 @@ export function handleDiffArray(current, prev, handleAdded, handleRemoved) {
211
211
  handleAdded(currEl);
212
212
  }
213
213
  }
214
+ // ─── String transforms ────────────────────────────────────────────────────────
215
+ /**
216
+ * Parse a string as a single JSON value.
217
+ *
218
+ * ```ts
219
+ * const { data } = createSSE<{ status: string }>(url, { transform: json });
220
+ * ```
221
+ */
222
+ export const json = (raw) => JSON.parse(raw);
223
+ /**
224
+ * Parse a string as newline-delimited JSON (NDJSON / JSON Lines).
225
+ *
226
+ * Each non-empty line is parsed as a separate JSON value and returned as an array.
227
+ *
228
+ * ```ts
229
+ * const { data } = createSSE<TickEvent[]>(url, { transform: ndjson });
230
+ * // data() === [{ id: 1, type: "tick" }, { id: 2, type: "tick" }]
231
+ * ```
232
+ */
233
+ export const ndjson = (raw) => raw
234
+ .split("\n")
235
+ .filter(line => line !== "")
236
+ .map(line => JSON.parse(line));
237
+ /**
238
+ * Split a string into individual lines, returning a `string[]`. Empty lines are filtered out.
239
+ *
240
+ * ```ts
241
+ * const { data } = createSSE<string[]>(url, { transform: lines });
242
+ * // data() === ["line one", "line two"]
243
+ * ```
244
+ */
245
+ export const lines = (raw) => raw.split("\n").filter(line => line !== "");
246
+ /**
247
+ * Parse a string as a number using `Number()` semantics.
248
+ *
249
+ * Note: `""` → `0`, non-numeric strings → `NaN`.
250
+ *
251
+ * ```ts
252
+ * const { data } = createSSE<number>(url, { transform: number });
253
+ * // data() === 42
254
+ * ```
255
+ */
256
+ export const number = (raw) => Number(raw);
257
+ export function safe(transform, fallback) {
258
+ return (raw) => {
259
+ try {
260
+ return transform(raw);
261
+ }
262
+ catch {
263
+ return fallback;
264
+ }
265
+ };
266
+ }
267
+ /**
268
+ * Compose two transforms into one: the output of `a` is passed as the input of `b`.
269
+ *
270
+ * ```ts
271
+ * const { data } = createSSE<RawEvent[]>(url, {
272
+ * transform: pipe(ndjson<RawEvent>, rows => rows.filter(r => r.type === "tick")),
273
+ * });
274
+ * ```
275
+ */
276
+ export function pipe(a, b) {
277
+ return (raw) => b(a(raw));
278
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "6.3.1",
3
+ "version": "6.4.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": [
@@ -57,11 +57,11 @@
57
57
  "solid-js": "^1.6.12"
58
58
  },
59
59
  "devDependencies": {
60
- "solid-js": "^1.8.7"
60
+ "solid-js": "^1.9.7"
61
61
  },
62
62
  "scripts": {
63
- "dev": "tsx ../../scripts/dev.ts",
64
- "build": "tsx ../../scripts/build.ts",
63
+ "dev": "node --import=@nothing-but/node-resolve-ts --experimental-transform-types ../../scripts/dev.ts",
64
+ "build": "node --import=@nothing-but/node-resolve-ts --experimental-transform-types ../../scripts/build.ts",
65
65
  "vitest": "vitest -c ../../configs/vitest.config.ts",
66
66
  "test": "pnpm run vitest",
67
67
  "test:ssr": "pnpm run vitest --mode ssr"