@solid-primitives/utils 5.5.2 → 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 CHANGED
@@ -14,6 +14,8 @@ exports.ofClass = (v, c) => v instanceof c || v && v.constructor === c;
14
14
  function isObject(value) {
15
15
  return value !== null && (typeof value === "object" || typeof value === "function");
16
16
  }
17
+ exports.isNonNullable = (i) => i != null;
18
+ exports.filterNonNullable = (arr) => arr.filter(exports.isNonNullable);
17
19
  exports.compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
18
20
  exports.arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
19
21
  function chain(callbacks) {
@@ -32,7 +34,7 @@ function reverseChain(callbacks) {
32
34
  }
33
35
  exports.clamp = (n, min, max) => Math.min(Math.max(n, min), max);
34
36
  exports.access = (v) => typeof v === "function" && !v.length ? v() : v;
35
- exports.asArray = (value) => Array.isArray(value) ? value : [value];
37
+ exports.asArray = (value) => Array.isArray(value) ? value : value ? [value] : [];
36
38
  exports.accessArray = (list) => list.map((v) => exports.access(v));
37
39
  exports.withAccess = (value, fn) => {
38
40
  const _value = exports.access(value);
@@ -66,7 +68,7 @@ function defer(deps, fn, initialValue) {
66
68
  }
67
69
  exports.entries = Object.entries;
68
70
  exports.keys = Object.keys;
69
- exports.tryOnCleanup = (fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn;
71
+ exports.tryOnCleanup = exports.isDev ? (fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn : solidJs.onCleanup;
70
72
  exports.createCallbackStack = () => {
71
73
  let stack = [];
72
74
  const clear = () => stack = [];
@@ -99,56 +101,6 @@ function createHydratableSignal(serverValue, update, options) {
99
101
  return solidJs.createSignal(update(), options);
100
102
  }
101
103
  exports.createHydrateSignal = createHydratableSignal;
102
- function createStaticStore(init) {
103
- const copy = { ...init };
104
- const store = { ...init };
105
- const cache = /* @__PURE__ */ new Map();
106
- const getValue = (key) => {
107
- const saved = cache.get(key);
108
- if (saved)
109
- return saved[0]();
110
- const signal = solidJs.createSignal(copy[key], {
111
- internal: true
112
- });
113
- cache.set(key, signal);
114
- delete copy[key];
115
- return signal[0]();
116
- };
117
- const setValue = (key, value) => {
118
- const saved = cache.get(key);
119
- if (saved)
120
- return saved[1](value);
121
- if (key in copy)
122
- copy[key] = accessWith(value, [copy[key]]);
123
- };
124
- for (const key in init) {
125
- Object.defineProperty(store, key, { get: getValue.bind(void 0, key) });
126
- }
127
- const setter = (a, b) => {
128
- if (isObject(a)) {
129
- const entries2 = solidJs.untrack(
130
- () => Object.entries(accessWith(a, store))
131
- );
132
- solidJs.batch(() => {
133
- for (const [key, value] of entries2)
134
- setValue(key, () => value);
135
- });
136
- } else
137
- setValue(a, b);
138
- return store;
139
- };
140
- return [store, setter];
141
- }
142
- function createHydratableStaticStore(serverValue, update) {
143
- if (web.isServer)
144
- return createStaticStore(serverValue);
145
- if (solidJs.sharedConfig.context) {
146
- const [state, setState] = createStaticStore(serverValue);
147
- solidJs.onMount(() => setState(update()));
148
- return [state, setState];
149
- }
150
- return createStaticStore(update());
151
- }
152
104
  function handleDiffArray(current, prev, handleAdded, handleRemoved) {
153
105
  const currLength = current.length;
154
106
  const prevLength = prev.length;
@@ -188,9 +140,7 @@ Object.defineProperty(exports, 'isServer', {
188
140
  exports.accessWith = accessWith;
189
141
  exports.chain = chain;
190
142
  exports.createHydratableSignal = createHydratableSignal;
191
- exports.createHydratableStaticStore = createHydratableStaticStore;
192
143
  exports.createMicrotask = createMicrotask;
193
- exports.createStaticStore = createStaticStore;
194
144
  exports.defer = defer;
195
145
  exports.handleDiffArray = handleDiffArray;
196
146
  exports.isObject = isObject;
package/dist/index.d.ts CHANGED
@@ -98,6 +98,8 @@ declare const isProd: boolean;
98
98
  declare const ofClass: (v: any, c: AnyClass) => boolean;
99
99
  /** Check if value is typeof "object" or "function" */
100
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]>[];
101
103
  declare const compare: (a: any, b: any) => number;
102
104
  /**
103
105
  * Check shallow array equality
@@ -123,7 +125,7 @@ declare const clamp: (n: number, min: number, max: number) => number;
123
125
  * ```
124
126
  */
125
127
  declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
126
- 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>)[];
127
129
  /**
128
130
  * Access an array of MaybeAccessors
129
131
  * @example
@@ -158,7 +160,7 @@ declare const entries: <T extends object>(obj: T) => [keyof T, T[keyof T]][];
158
160
  */
159
161
  declare const keys: <T extends object>(object: T) => (keyof T)[];
160
162
  /**
161
- * Solid's `onCleanup` that is registered only if there is a root.
163
+ * Solid's `onCleanup` that doesn't warn in development if used outside of a component.
162
164
  */
163
165
  declare const tryOnCleanup: typeof onCleanup;
164
166
  declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
@@ -187,31 +189,6 @@ declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (
187
189
  declare function createHydratableSignal<T>(serverValue: T, update: () => T, options?: SignalOptions<T>): ReturnType<typeof createSignal<T>>;
188
190
  /** @deprecated use {@link createHydratableSignal} instead */
189
191
  declare const createHydrateSignal: typeof createHydratableSignal;
190
- type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
191
- (setter: (prev: T) => Partial<T>): T;
192
- (state: Partial<T>): T;
193
- <K extends keyof T>(key: K, state: SetterParam<T[K]>): T;
194
- };
195
- /**
196
- * 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.
197
- * @param init initial value of the store
198
- * @returns
199
- * ```ts
200
- * [access: Readonly<T>, write: StaticStoreSetter<T>]
201
- * ```
202
- */
203
- declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
204
- /**
205
- * 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.
206
- *
207
- * @param serverValue initial value of the state on the server
208
- * @param update called once on the client or on hydration to initialize the value
209
- * @returns
210
- * ```ts
211
- * [access: Readonly<T>, write: StaticStoreSetter<T>]
212
- * ```
213
- */
214
- declare function createHydratableStaticStore<T extends Readonly<AnyStatic>>(serverValue: T, update: () => T): ReturnType<typeof createStaticStore<T>>;
215
192
  /**
216
193
  * Handle items removed and added to the array by diffing it by refference.
217
194
  *
@@ -222,4 +199,4 @@ declare function createHydratableStaticStore<T extends Readonly<AnyStatic>>(serv
222
199
  */
223
200
  declare function handleDiffArray<T>(current: readonly T[], prev: readonly T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
224
201
 
225
- 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, StaticStoreSetter, Tail, Truthy, UnboxLazy, UnionToIntersection, Values, access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal, createHydratableStaticStore, createHydrateSignal, createMicrotask, createStaticStore, defer, entries, falseFn, handleDiffArray, isClient, isDev, isObject, isProd, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess };
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,4 +1,4 @@
1
- import { DEV, untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount, batch } from 'solid-js';
1
+ import { DEV, untrack, getOwner, onCleanup, createSignal, sharedConfig, onMount } from 'solid-js';
2
2
  import { isServer } from 'solid-js/web';
3
3
  export { isServer } from 'solid-js/web';
4
4
 
@@ -13,6 +13,8 @@ var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
13
13
  function isObject(value) {
14
14
  return value !== null && (typeof value === "object" || typeof value === "function");
15
15
  }
16
+ var isNonNullable = (i) => i != null;
17
+ var filterNonNullable = (arr) => arr.filter(isNonNullable);
16
18
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
17
19
  var arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
18
20
  function chain(callbacks) {
@@ -31,7 +33,7 @@ function reverseChain(callbacks) {
31
33
  }
32
34
  var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
33
35
  var access = (v) => typeof v === "function" && !v.length ? v() : v;
34
- var asArray = (value) => Array.isArray(value) ? value : [value];
36
+ var asArray = (value) => Array.isArray(value) ? value : value ? [value] : [];
35
37
  var accessArray = (list) => list.map((v) => access(v));
36
38
  var withAccess = (value, fn) => {
37
39
  const _value = access(value);
@@ -65,7 +67,7 @@ function defer(deps, fn, initialValue) {
65
67
  }
66
68
  var entries = Object.entries;
67
69
  var keys = Object.keys;
68
- var tryOnCleanup = (fn) => getOwner() ? onCleanup(fn) : fn;
70
+ var tryOnCleanup = isDev ? (fn) => getOwner() ? onCleanup(fn) : fn : onCleanup;
69
71
  var createCallbackStack = () => {
70
72
  let stack = [];
71
73
  const clear = () => stack = [];
@@ -98,56 +100,6 @@ function createHydratableSignal(serverValue, update, options) {
98
100
  return createSignal(update(), options);
99
101
  }
100
102
  var 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 = 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 = untrack(
129
- () => Object.entries(accessWith(a, store))
130
- );
131
- 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 (isServer)
143
- return createStaticStore(serverValue);
144
- if (sharedConfig.context) {
145
- const [state, setState] = createStaticStore(serverValue);
146
- onMount(() => setState(update()));
147
- return [state, setState];
148
- }
149
- return createStaticStore(update());
150
- }
151
103
  function handleDiffArray(current, prev, handleAdded, handleRemoved) {
152
104
  const currLength = current.length;
153
105
  const prevLength = prev.length;
@@ -180,4 +132,4 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
180
132
  }
181
133
  }
182
134
 
183
- export { access, accessArray, accessWith, arrayEquals, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createHydratableSignal, createHydratableStaticStore, createHydrateSignal, createMicrotask, createStaticStore, defer, entries, falseFn, handleDiffArray, isClient, isDev, isObject, isProd, keys, noop, ofClass, reverseChain, trueFn, tryOnCleanup, withAccess };
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": "5.5.2",
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",