@solid-primitives/utils 1.1.0 → 1.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/dist/index.cjs CHANGED
@@ -1,7 +1,21 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
4
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
5
19
  var __export = (target, all) => {
6
20
  for (var name in all)
7
21
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -21,18 +35,22 @@ var src_exports = {};
21
35
  __export(src_exports, {
22
36
  access: () => access,
23
37
  accessArray: () => accessArray,
38
+ accessWith: () => accessWith,
24
39
  asAccessor: () => asAccessor,
25
40
  asArray: () => asArray,
41
+ clamp: () => clamp,
26
42
  compare: () => compare,
27
43
  createCallbackStack: () => createCallbackStack,
28
44
  createMicrotask: () => createMicrotask,
29
45
  createProxy: () => createProxy,
46
+ createStaticStore: () => createStaticStore,
30
47
  createTrigger: () => createTrigger,
31
48
  createTriggerCache: () => createTriggerCache,
32
49
  entries: () => entries,
33
50
  forEachEntry: () => forEachEntry,
34
51
  isClient: () => isClient,
35
52
  isDev: () => isDev,
53
+ isObject: () => isObject,
36
54
  isProd: () => isProd,
37
55
  isServer: () => import_web.isServer,
38
56
  keys: () => keys,
@@ -53,7 +71,11 @@ var isDev = import_solid_js.DEV && isClient;
53
71
  var isProd = !isDev;
54
72
  var warn = (...a) => isDev && console.warn(...a);
55
73
  var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
74
+ function isObject(value) {
75
+ return value !== null && (typeof value === "object" || typeof value === "function");
76
+ }
56
77
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
78
+ var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
57
79
  var access = (v) => typeof v === "function" && !v.length ? v() : v;
58
80
  var asArray = (value) => Array.isArray(value) ? value : [value];
59
81
  var accessArray = (list) => list.map((v) => access(v));
@@ -62,11 +84,13 @@ var withAccess = (value, fn) => {
62
84
  typeof _value !== "undefined" && _value !== null && fn(_value);
63
85
  };
64
86
  var asAccessor = (v) => typeof v === "function" ? v : () => v;
65
- var forEachEntry = (object, iterator) => {
66
- const obj = access(object);
67
- Object.entries(obj).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, obj));
68
- };
69
- var entries = (object) => Object.entries(access(object));
87
+ function accessWith(valueOrFn, ...args) {
88
+ return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
89
+ }
90
+ function forEachEntry(object, iterator) {
91
+ Object.entries(object).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, object));
92
+ }
93
+ var entries = Object.entries;
70
94
  var keys = Object.keys;
71
95
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
72
96
  function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
@@ -129,22 +153,68 @@ function createTriggerCache(options) {
129
153
  }
130
154
  };
131
155
  }
156
+ function createStaticStore(init) {
157
+ const copy = __spreadValues({}, init);
158
+ const store = {};
159
+ const cache = /* @__PURE__ */ new Map();
160
+ const getValue = (key) => {
161
+ const saved = cache.get(key);
162
+ if (saved)
163
+ return saved[0]();
164
+ const signal = (0, import_solid_js.createSignal)(copy[key], {
165
+ name: typeof key === "string" ? key : void 0
166
+ });
167
+ cache.set(key, signal);
168
+ delete copy[key];
169
+ return signal[0]();
170
+ };
171
+ const setValue = (key, value) => {
172
+ const saved = cache.get(key);
173
+ if (saved)
174
+ return saved[1](value);
175
+ if (key in copy)
176
+ copy[key] = accessWith(value, [copy[key]]);
177
+ };
178
+ for (const key of keys(init)) {
179
+ store[key] = void 0;
180
+ Object.defineProperty(store, key, {
181
+ get: getValue.bind(void 0, key)
182
+ });
183
+ }
184
+ const setter = (a, b) => {
185
+ if (isObject(a))
186
+ (0, import_solid_js.untrack)(() => {
187
+ (0, import_solid_js.batch)(() => {
188
+ for (const [key, value] of entries(accessWith(a, store)))
189
+ setValue(key, () => value);
190
+ });
191
+ });
192
+ else
193
+ setValue(a, b);
194
+ return store;
195
+ };
196
+ return [store, setter];
197
+ }
132
198
  // Annotate the CommonJS export names for ESM import in node:
133
199
  0 && (module.exports = {
134
200
  access,
135
201
  accessArray,
202
+ accessWith,
136
203
  asAccessor,
137
204
  asArray,
205
+ clamp,
138
206
  compare,
139
207
  createCallbackStack,
140
208
  createMicrotask,
141
209
  createProxy,
210
+ createStaticStore,
142
211
  createTrigger,
143
212
  createTriggerCache,
144
213
  entries,
145
214
  forEachEntry,
146
215
  isClient,
147
216
  isDev,
217
+ isObject,
148
218
  isProd,
149
219
  isServer,
150
220
  keys,
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Accessor, onCleanup } from 'solid-js';
1
+ import { Accessor, Setter, onCleanup } from 'solid-js';
2
2
  import { BaseOptions } from 'solid-js/types/reactive/signal';
3
3
  export { isServer } from 'solid-js/web';
4
4
 
@@ -14,6 +14,7 @@ declare type Directive<P = true> = (el: Element, props: Accessor<P>) => void;
14
14
  */
15
15
  declare type ItemsOf<T> = T extends (infer E)[] ? E : never;
16
16
  declare type ItemsOfMany<T> = T extends any[] ? ItemsOf<T> : T;
17
+ declare type SetterValue<T> = Parameters<Setter<T>>[0];
17
18
  /**
18
19
  * T or a reactive/non-reactive function returning T
19
20
  */
@@ -56,21 +57,14 @@ declare type Definite<T> = {
56
57
  /** `A | B => A & B` */
57
58
  declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
58
59
  declare type ExtractIfPossible<T, U> = Extract<T, U> extends never ? U : Extract<T, U>;
59
- declare type AnyObject = Record<string | symbol | number, any>;
60
+ declare type AnyObject = Record<PropertyKey, any>;
61
+ declare type AnyStatic = [] | any[] | AnyObject;
60
62
  declare type AnyFunction = (...args: any[]) => any;
61
63
  declare type AnyClass = abstract new (...args: any) => any;
62
- declare type LiteralKey = string | number | symbol;
63
- declare type PrimitiveValue = string | boolean | number | bigint | symbol | null | undefined;
64
+ declare type PrimitiveValue = PropertyKey | boolean | bigint | null | undefined;
64
65
  declare type FalsyValue = false | 0 | "" | null | undefined;
65
66
  declare type Truthy<T> = Exclude<T, FalsyValue>;
66
67
  declare type Falsy<T> = Extract<T, FalsyValue>;
67
- declare type Fallback<T, F = NonNullable<T>> = NonNullable<T> | F;
68
- /**
69
- * Destructible store object, with values changed to accessors
70
- */
71
- declare type Destore<T extends Object> = {
72
- [K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
73
- };
74
68
  declare type TriggerCache<T> = {
75
69
  track: (v: T) => void;
76
70
  dirty: (v: T) => void;
@@ -96,7 +90,10 @@ declare const warn: typeof console.warn;
96
90
  * Check if the value is an instance of ___
97
91
  */
98
92
  declare const ofClass: (v: any, c: AnyClass) => boolean;
93
+ /** Check if value is typeof "object" or "function" */
94
+ declare function isObject(value: any): value is AnyObject;
99
95
  declare const compare: (a: any, b: any) => number;
96
+ declare const clamp: (n: number, min: number, max: number) => number;
100
97
  /**
101
98
  * Accesses the value of a MaybeAccessor
102
99
  * @example
@@ -121,14 +118,16 @@ declare const accessArray: <A extends unknown>(list: readonly A[]) => MaybeAcces
121
118
  */
122
119
  declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void;
123
120
  declare const asAccessor: <A extends unknown>(v: A) => Accessor<MaybeAccessorValue<A>>;
121
+ /** If value is a function – call it with a given arguments – otherwise get the value as is */
122
+ declare function accessWith<T>(valueOrFn: T, ...args: T extends AnyFunction ? Parameters<T> : never): T extends AnyFunction ? ReturnType<T> : T;
124
123
  /**
125
124
  * Iterate through object entries.
126
125
  */
127
- declare const forEachEntry: <A extends MaybeAccessor<object>, O = MaybeAccessorValue<A>>(object: A, iterator: (key: keyof O, item: Values<O>, index: number, pairs: [keyof O, Values<O>][], object: O) => void) => void;
126
+ declare function forEachEntry<O extends AnyObject>(object: O, iterator: (key: keyof O, item: Values<O>, index: number, pairs: [keyof O, Values<O>][], object: O) => void): void;
128
127
  /**
129
- * Get `Object.entries()` of an MaybeAccessor<object>
128
+ * Get entries of an object
130
129
  */
131
- declare const entries: <A extends MaybeAccessor<object>, O = MaybeAccessorValue<A>>(object: A) => [keyof O, Values<O>][];
130
+ declare const entries: <T extends object>(obj: T) => [keyof T, T[keyof T]][];
132
131
  /**
133
132
  * Get keys of an object
134
133
  */
@@ -222,5 +221,19 @@ declare function createTrigger(options?: BaseOptions): Trigger;
222
221
  * dirty(2)
223
222
  */
224
223
  declare function createTriggerCache<T>(options?: BaseOptions): TriggerCache<T>;
224
+ declare type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
225
+ (setter: (prev: T) => Partial<T>): T;
226
+ (state: Partial<T>): T;
227
+ <K extends keyof T>(key: K, state: SetterValue<T[K]>): T;
228
+ };
229
+ /**
230
+ * A shallowly wrapped reactive store object. It behaves similarly to the creatStore, 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.
231
+ * @param init initial value of the store
232
+ * @returns
233
+ * ```ts
234
+ * [access: Readonly<T>, write: StaticStoreSetter<T>]
235
+ * ```
236
+ */
237
+ declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
225
238
 
226
- export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, DeepPartialAny, Definite, Destore, Directive, ExtractIfPossible, Fallback, Falsy, FalsyValue, ItemsOf, ItemsOfMany, LiteralKey, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, Trigger, TriggerCache, Truthy, UnionToIntersection, Values, access, accessArray, asAccessor, asArray, compare, createCallbackStack, createMicrotask, createProxy, createTrigger, createTriggerCache, entries, forEachEntry, isClient, isDev, isProd, keys, noop, ofClass, onRootCleanup, promiseTimeout, raceTimeout, warn, withAccess };
239
+ export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Definite, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, SetterValue, StaticStoreSetter, Trigger, TriggerCache, Truthy, UnionToIntersection, Values, access, accessArray, accessWith, asAccessor, asArray, clamp, compare, createCallbackStack, createMicrotask, createProxy, createStaticStore, createTrigger, createTriggerCache, entries, forEachEntry, isClient, isDev, isObject, isProd, keys, noop, ofClass, onRootCleanup, promiseTimeout, raceTimeout, warn, withAccess };
package/dist/index.js CHANGED
@@ -1,5 +1,22 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+
1
18
  // src/index.ts
2
- import { getOwner, onCleanup, createSignal, DEV } from "solid-js";
19
+ import { getOwner, onCleanup, createSignal, DEV, untrack, batch } from "solid-js";
3
20
  import { isServer } from "solid-js/web";
4
21
  var noop = () => void 0;
5
22
  var isClient = !isServer;
@@ -7,7 +24,11 @@ var isDev = DEV && isClient;
7
24
  var isProd = !isDev;
8
25
  var warn = (...a) => isDev && console.warn(...a);
9
26
  var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
27
+ function isObject(value) {
28
+ return value !== null && (typeof value === "object" || typeof value === "function");
29
+ }
10
30
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
31
+ var clamp = (n, min, max) => Math.min(Math.max(n, min), max);
11
32
  var access = (v) => typeof v === "function" && !v.length ? v() : v;
12
33
  var asArray = (value) => Array.isArray(value) ? value : [value];
13
34
  var accessArray = (list) => list.map((v) => access(v));
@@ -16,11 +37,13 @@ var withAccess = (value, fn) => {
16
37
  typeof _value !== "undefined" && _value !== null && fn(_value);
17
38
  };
18
39
  var asAccessor = (v) => typeof v === "function" ? v : () => v;
19
- var forEachEntry = (object, iterator) => {
20
- const obj = access(object);
21
- Object.entries(obj).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, obj));
22
- };
23
- var entries = (object) => Object.entries(access(object));
40
+ function accessWith(valueOrFn, ...args) {
41
+ return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
42
+ }
43
+ function forEachEntry(object, iterator) {
44
+ Object.entries(object).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, object));
45
+ }
46
+ var entries = Object.entries;
24
47
  var keys = Object.keys;
25
48
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
26
49
  function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
@@ -83,21 +106,67 @@ function createTriggerCache(options) {
83
106
  }
84
107
  };
85
108
  }
109
+ function createStaticStore(init) {
110
+ const copy = __spreadValues({}, init);
111
+ const store = {};
112
+ const cache = /* @__PURE__ */ new Map();
113
+ const getValue = (key) => {
114
+ const saved = cache.get(key);
115
+ if (saved)
116
+ return saved[0]();
117
+ const signal = createSignal(copy[key], {
118
+ name: typeof key === "string" ? key : void 0
119
+ });
120
+ cache.set(key, signal);
121
+ delete copy[key];
122
+ return signal[0]();
123
+ };
124
+ const setValue = (key, value) => {
125
+ const saved = cache.get(key);
126
+ if (saved)
127
+ return saved[1](value);
128
+ if (key in copy)
129
+ copy[key] = accessWith(value, [copy[key]]);
130
+ };
131
+ for (const key of keys(init)) {
132
+ store[key] = void 0;
133
+ Object.defineProperty(store, key, {
134
+ get: getValue.bind(void 0, key)
135
+ });
136
+ }
137
+ const setter = (a, b) => {
138
+ if (isObject(a))
139
+ untrack(() => {
140
+ batch(() => {
141
+ for (const [key, value] of entries(accessWith(a, store)))
142
+ setValue(key, () => value);
143
+ });
144
+ });
145
+ else
146
+ setValue(a, b);
147
+ return store;
148
+ };
149
+ return [store, setter];
150
+ }
86
151
  export {
87
152
  access,
88
153
  accessArray,
154
+ accessWith,
89
155
  asAccessor,
90
156
  asArray,
157
+ clamp,
91
158
  compare,
92
159
  createCallbackStack,
93
160
  createMicrotask,
94
161
  createProxy,
162
+ createStaticStore,
95
163
  createTrigger,
96
164
  createTriggerCache,
97
165
  entries,
98
166
  forEachEntry,
99
167
  isClient,
100
168
  isDev,
169
+ isObject,
101
170
  isProd,
102
171
  isServer,
103
172
  keys,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "1.1.0",
3
+ "version": "1.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
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  "scripts": {
25
25
  "build": "tsup",
26
26
  "test": "uvu -r solid-register",
27
- "watch-test": "watchlist src test -- npm test"
27
+ "test:watch": "watchlist src test -- npm test"
28
28
  },
29
29
  "keywords": [
30
30
  "utilities",