@solid-primitives/utils 0.1.2 → 0.2.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/README.md CHANGED
@@ -55,4 +55,8 @@ Add `/fp` and `/setter` export entries. Add `destore` and `raceTimeout` util. Mo
55
55
 
56
56
  Updated to Solid 1.3
57
57
 
58
+ 0.1.3
59
+
60
+ Minor upgrades.
61
+
58
62
  </details>
package/dist/index.cjs CHANGED
@@ -25,35 +25,68 @@ var __toCommonJS = /* @__PURE__ */ ((cache) => {
25
25
  var src_exports = {};
26
26
  __export(src_exports, {
27
27
  access: () => access,
28
+ accessArray: () => accessArray,
28
29
  accessAsArray: () => accessAsArray,
29
30
  asAccessor: () => asAccessor,
30
31
  asArray: () => asArray,
32
+ compare: () => compare,
31
33
  createCallbackStack: () => createCallbackStack,
32
- createSubRoot: () => createSubRoot,
33
- createSubRootFunction: () => createSubRootFunction,
34
+ createProxy: () => createProxy,
34
35
  destore: () => destore,
35
36
  entries: () => entries,
36
37
  forEach: () => forEach,
38
+ forEachEntry: () => forEachEntry,
39
+ includes: () => includes,
40
+ isArray: () => isArray,
41
+ isBoolean: () => isBoolean,
37
42
  isClient: () => isClient,
38
43
  isDefined: () => isDefined,
44
+ isDev: () => isDev,
39
45
  isFunction: () => isFunction,
46
+ isNumber: () => isNumber,
47
+ isObject: () => isObject,
48
+ isProd: () => isProd,
40
49
  isServer: () => import_web.isServer,
50
+ isString: () => isString,
51
+ keys: () => keys,
41
52
  noop: () => noop,
53
+ ofClass: () => ofClass,
42
54
  onAccess: () => onAccess,
43
55
  onRootCleanup: () => onRootCleanup,
44
56
  promiseTimeout: () => promiseTimeout,
45
57
  raceTimeout: () => raceTimeout,
58
+ tuple: () => tuple,
59
+ warn: () => warn,
46
60
  withAccess: () => withAccess
47
61
  });
48
62
  var import_solid_js = require("solid-js");
49
63
  var import_web = require("solid-js/web");
50
64
  var noop = () => void 0;
51
65
  var isClient = !import_web.isServer;
66
+ var isDev = import_solid_js.DEV && import_solid_js.DEV.hasOwnProperty("writeSignal");
67
+ var isProd = !isDev;
68
+ var warn = (...a) => isDev && console.warn(...a);
52
69
  var isDefined = (value) => typeof value !== "undefined" && value !== null;
53
70
  var isFunction = (value) => typeof value === "function";
71
+ var isBoolean = (val) => typeof val === "boolean";
72
+ var isNumber = (val) => typeof val === "number";
73
+ var isString = (val) => typeof val === "string";
74
+ var isObject = (val) => toString.call(val) === "[object Object]";
75
+ var isArray = Array.isArray;
76
+ var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
77
+ var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
78
+ var tuple = (input) => input;
79
+ var includes = (arr, ...items) => {
80
+ for (const item of arr) {
81
+ if (items.includes(item))
82
+ return true;
83
+ }
84
+ return false;
85
+ };
54
86
  var access = (v) => isFunction(v) ? v() : v;
55
87
  var accessAsArray = (value) => asArray(access(value));
56
88
  var asArray = (value) => Array.isArray(value) ? value : [value];
89
+ var accessArray = (list) => list.map((v) => access(v));
57
90
  var withAccess = (value, fn) => {
58
91
  const _value = access(value);
59
92
  isDefined(_value) && fn(_value);
@@ -64,7 +97,12 @@ function onAccess(deps, fn, options) {
64
97
  return (0, import_solid_js.on)(source, fn, options);
65
98
  }
66
99
  var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
100
+ var forEachEntry = (object, iterator) => {
101
+ const obj = access(object);
102
+ Object.entries(obj).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, obj));
103
+ };
67
104
  var entries = (object) => Object.entries(access(object));
105
+ var keys = (object) => Object.keys(access(object));
68
106
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
69
107
  function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
70
108
  const promises = asArray(input);
@@ -74,6 +112,7 @@ function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
74
112
  });
75
113
  return race;
76
114
  }
115
+ var onRootCleanup = (fn) => (0, import_solid_js.getOwner)() ? (0, import_solid_js.onCleanup)(fn) : fn;
77
116
  function destore(store) {
78
117
  const _store = store;
79
118
  const result = {};
@@ -82,48 +121,63 @@ function destore(store) {
82
121
  });
83
122
  return result;
84
123
  }
85
- var onRootCleanup = (fn) => (0, import_solid_js.getOwner)() ? (0, import_solid_js.onCleanup)(fn) : fn;
86
- function createSubRoot(fn, owner = (0, import_solid_js.getOwner)()) {
87
- const [dispose, returns] = (0, import_solid_js.createRoot)((dispose2) => [dispose2, fn(dispose2)], owner != null ? owner : void 0);
88
- owner && (0, import_solid_js.runWithOwner)(owner, () => (0, import_solid_js.onCleanup)(dispose));
89
- return returns;
90
- }
91
- function createSubRootFunction(callback, owner) {
92
- return (...args) => createSubRoot(() => callback(...args), owner);
93
- }
94
124
  var createCallbackStack = () => {
95
125
  let stack = [];
96
126
  const clear = () => stack = [];
97
127
  return {
98
128
  push: (...callbacks) => stack.push(...callbacks),
99
- execute: (arg0, arg1, arg2, arg3) => {
129
+ execute(arg0, arg1, arg2, arg3) {
100
130
  stack.forEach((cb) => cb(arg0, arg1, arg2, arg3));
101
131
  clear();
102
132
  },
103
133
  clear
104
134
  };
105
135
  };
136
+ function createProxy(traps) {
137
+ return new Proxy({}, {
138
+ get: (_, k) => traps.get(k),
139
+ set: (_, k, v) => {
140
+ var _a;
141
+ (_a = traps.set) == null ? void 0 : _a.call(traps, k, v);
142
+ return false;
143
+ }
144
+ });
145
+ }
106
146
  module.exports = __toCommonJS(src_exports);
107
147
  // Annotate the CommonJS export names for ESM import in node:
108
148
  0 && (module.exports = {
109
149
  access,
150
+ accessArray,
110
151
  accessAsArray,
111
152
  asAccessor,
112
153
  asArray,
154
+ compare,
113
155
  createCallbackStack,
114
- createSubRoot,
115
- createSubRootFunction,
156
+ createProxy,
116
157
  destore,
117
158
  entries,
118
159
  forEach,
160
+ forEachEntry,
161
+ includes,
162
+ isArray,
163
+ isBoolean,
119
164
  isClient,
120
165
  isDefined,
166
+ isDev,
121
167
  isFunction,
168
+ isNumber,
169
+ isObject,
170
+ isProd,
122
171
  isServer,
172
+ isString,
173
+ keys,
123
174
  noop,
175
+ ofClass,
124
176
  onAccess,
125
177
  onRootCleanup,
126
178
  promiseTimeout,
127
179
  raceTimeout,
180
+ tuple,
181
+ warn,
128
182
  withAccess
129
183
  });
package/dist/index.d.ts CHANGED
@@ -1,19 +1,111 @@
1
1
  import { Accessor, onCleanup } from 'solid-js';
2
- import { OnOptions, EffectFunction, NoInfer, Owner } from 'solid-js/types/reactive/signal';
2
+ import { OnOptions, EffectFunction, NoInfer } from 'solid-js/types/reactive/signal';
3
3
  import { Store } from 'solid-js/store';
4
4
  export { isServer } from 'solid-js/web';
5
- import { N as Noop, M as MaybeAccessorValue, a as MaybeAccessor, O as OnAccessEffectFunction, I as ItemsOf, V as Values, D as Destore, F as Fn, A as AnyFunction } from './types-5d7b3b37';
6
- export { d as AccessReturnTypes, A as AnyFunction, h as AnyObject, g as DeepPartialAny, D as Destore, k as Falsy, j as FalsyValue, F as Fn, I as ItemsOf, K as Keys, b as Many, c as MappingFn, a as MaybeAccessor, M as MaybeAccessorValue, e as Modify, f as ModifyDeep, N as Noop, O as OnAccessEffectFunction, P as Predicate, i as PrimitiveValue, T as Truthy, V as Values } from './types-5d7b3b37';
5
+
6
+ /**
7
+ * A function
8
+ */
9
+ declare type Fn<R = void> = () => R;
10
+ declare type Get<T> = (v: T) => void;
11
+ declare type Clear = () => void;
12
+ /**
13
+ * Can be single or in an array
14
+ */
15
+ declare type Many<T> = T | T[];
16
+ declare type Keys<O extends Object> = keyof O;
17
+ declare type Values<O extends Object> = O[Keys<O>];
18
+ declare type Noop = (...a: any[]) => void;
19
+ declare type Directive<P = true> = (el: Element, props: Accessor<P>) => void;
20
+ /**
21
+ * Infers the type of the array elements
22
+ */
23
+ declare type ItemsOf<T> = T extends (infer E)[] ? E : never;
24
+ declare type ItemsOfMany<T> = T extends any[] ? ItemsOf<T> : T;
25
+ /**
26
+ * T or a reactive/non-reactive function returning T
27
+ */
28
+ declare type MaybeAccessor<T> = T | Accessor<T>;
29
+ /**
30
+ * Accessed value of a MaybeAccessor
31
+ * @example
32
+ * ```ts
33
+ * MaybeAccessorValue<MaybeAccessor<string>>
34
+ * // => string
35
+ * MaybeAccessorValue<MaybeAccessor<() => string>>
36
+ * // => string | (() => string)
37
+ * MaybeAccessorValue<MaybeAccessor<string> | Function>
38
+ * // => string | void
39
+ * ```
40
+ */
41
+ declare type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends Fn ? ReturnType<T> : T;
42
+ declare type OnAccessEffectFunction<S, Prev, Next extends Prev = Prev> = (input: AccessReturnTypes<S>, prevInput: AccessReturnTypes<S>, v: Prev) => Next;
43
+ declare type AccessReturnTypes<S> = S extends MaybeAccessor<any>[] ? {
44
+ [I in keyof S]: AccessReturnTypes<S[I]>;
45
+ } : MaybeAccessorValue<S>;
46
+ /** Allows to make shallow overwrites to an interface */
47
+ declare type Modify<T, R> = Omit<T, keyof R> & R;
48
+ /** Allows to make nested overwrites to an interface */
49
+ declare type ModifyDeep<A extends AnyObject, B extends DeepPartialAny<A>> = {
50
+ [K in keyof A]: B[K] extends never ? A[K] : B[K] extends AnyObject ? ModifyDeep<A[K], B[K]> : B[K];
51
+ } & (A extends AnyObject ? Omit<B, keyof A> : A);
52
+ /** Makes each property optional and turns each leaf property into any, allowing for type overrides by narrowing any. */
53
+ declare type DeepPartialAny<T> = {
54
+ [P in keyof T]?: T[P] extends AnyObject ? DeepPartialAny<T[P]> : any;
55
+ };
56
+ /** `A | B => A & B` */
57
+ declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
58
+ declare type ExtractIfPossible<T, U> = Extract<T, U> extends never ? U : Extract<T, U>;
59
+ declare type AnyObject = Record<string, any>;
60
+ declare type AnyFunction = (...args: any[]) => any;
61
+ 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 FalsyValue = false | 0 | "" | null | undefined;
65
+ declare type Truthy<T> = Exclude<T, FalsyValue>;
66
+ declare type Falsy<T> = Extract<T, FalsyValue>;
67
+ /**
68
+ * Destructible store object, with values changed to accessors
69
+ */
70
+ declare type Destore<T extends Object> = {
71
+ [K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
72
+ };
73
+ declare type Position = {
74
+ x: number;
75
+ y: number;
76
+ };
7
77
 
8
78
  /** no operation */
9
79
  declare const noop: Noop;
10
80
  declare const isClient = true;
11
81
 
82
+ /** development environment */
83
+ declare const isDev: boolean;
84
+ /** production environment */
85
+ declare const isProd: boolean;
86
+ /** `console.warn` only during development */
87
+ declare const warn: typeof console.warn;
12
88
  /**
13
89
  * `if (typeof value !== "undefined" && value !== null)`
14
90
  */
15
91
  declare const isDefined: <T>(value: T | null | undefined) => value is T;
16
92
  declare const isFunction: <T>(value: Function | T) => value is Function;
93
+ declare const isBoolean: (val: any) => val is boolean;
94
+ declare const isNumber: (val: any) => val is number;
95
+ declare const isString: (val: unknown) => val is string;
96
+ declare const isObject: (val: any) => val is object;
97
+ declare const isArray: (val: any) => val is any[];
98
+ declare const ofClass: (v: any, c: AnyClass) => boolean;
99
+ declare const compare: (a: any, b: any) => number;
100
+ /**
101
+ * for creating tuples by inferring type
102
+ * @example
103
+ * const users = tuple(["John", "Jeff", "Joe"]);
104
+ * // users: [string, string, string]
105
+ */
106
+ declare const tuple: <T extends any[] | []>(input: T) => T;
107
+ /** `Array.prototype.includes()` without so strict types. Also allows for checking for multiple items */
108
+ declare const includes: (arr: any[], ...items: any) => boolean;
17
109
  /**
18
110
  * Accesses the value of a MaybeAccessor
19
111
  * @example
@@ -35,6 +127,13 @@ declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
35
127
  */
36
128
  declare const accessAsArray: <T extends unknown, V = MaybeAccessorValue<T>>(value: T) => V extends any[] ? V : V[];
37
129
  declare const asArray: <T>(value: T) => T extends any[] ? T : T[];
130
+ /**
131
+ * Access an array of MaybeAccessors
132
+ * @example
133
+ * const list = [1, 2, () => 3)] // T: MaybeAccessor<number>[]
134
+ * const newList = accessArray(list) // T: number[]
135
+ */
136
+ declare const accessArray: <A extends unknown>(list: readonly A[]) => MaybeAccessorValue<A>[];
38
137
  /**
39
138
  * Run the function if the accessed value is not `undefined` nor `null`
40
139
  * @param value
@@ -55,11 +154,19 @@ declare function onAccess<S extends MaybeAccessor<unknown>[] | [], Next, Init =
55
154
  */
56
155
  declare const forEach: <A extends unknown, V = MaybeAccessorValue<A>>(array: A, iterator: (item: V extends any[] ? ItemsOf<V> : V, index: number, array: V extends any[] ? V : V[]) => void) => void;
57
156
  /**
58
- * Get `Object.entries()` of an MaybeAccessor<Object>
157
+ * Iterate through object entries.
158
+ */
159
+ 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;
160
+ /**
161
+ * Get `Object.entries()` of an MaybeAccessor<object>
59
162
  */
60
- declare const entries: <A extends MaybeAccessor<Object>, V = MaybeAccessorValue<A>>(object: A) => [string, Values<V>][];
163
+ declare const entries: <A extends MaybeAccessor<object>, O = MaybeAccessorValue<A>>(object: A) => [keyof O, Values<O>][];
61
164
  /**
62
- * Creates a promise that resolves *(or rejects)* after gives time.
165
+ * Get `Object.keys()` of an MaybeAccessor<Object>
166
+ */
167
+ declare const keys: <A extends MaybeAccessor<object>>(object: A) => (keyof MaybeAccessorValue<A>)[];
168
+ /**
169
+ * Creates a promise that resolves *(or rejects)* after given time.
63
170
  *
64
171
  * @param ms timeout duration in ms
65
172
  * @param throwOnTimeout promise will be rejected on timeout if set to `true`
@@ -94,6 +201,10 @@ declare const promiseTimeout: (ms: number, throwOnTimeout?: boolean, reason?: st
94
201
  */
95
202
  declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout: true, reason?: string): T extends any[] ? Promise<Awaited<T[number]>> : Promise<Awaited<T>>;
96
203
  declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout?: boolean, reason?: string): T extends any[] ? Promise<Awaited<T[number]> | undefined> : Promise<Awaited<T> | undefined>;
204
+ /**
205
+ * Solid's `onCleanup` that is registered only if there is a root.
206
+ */
207
+ declare const onRootCleanup: typeof onCleanup;
97
208
  /**
98
209
  * Allows the Solid's store to be destructured
99
210
  *
@@ -112,41 +223,19 @@ declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout?: boolea
112
223
  * ```
113
224
  */
114
225
  declare function destore<T extends Object>(store: Store<T>): Destore<T>;
115
- /**
116
- * Solid's `onCleanup` that runs only if there is a root.
117
- */
118
- declare const onRootCleanup: typeof onCleanup;
119
- /**
120
- * Creates a reactive root, which will be disposed when the passed owner does.
121
- *
122
- * @param fn
123
- * @param owner a root that will trigger the cleanup
124
- * @returns whatever the "fn" returns
125
- *
126
- * @example
127
- * const owner = getOwner()
128
- * const handleClick = () => createSubRoot(owner, () => {
129
- * createEffect(() => {})
130
- * });
131
- */
132
- declare function createSubRoot<T>(fn: (dispose: Fn) => T, owner?: Owner | null): T;
133
- /**
134
- * A wrapper for creating functions with the `createSubRoot`
135
- *
136
- * @param callback
137
- * @param owner a root that will trigger the cleanup
138
- * @returns the callback function
139
- *
140
- * @example
141
- * const handleClick = createSubRootFunction(() => {
142
- * createEffect(() => {})
143
- * })
144
- */
145
- declare function createSubRootFunction<T extends AnyFunction>(callback: T, owner?: Owner | null): T;
146
226
  declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
147
- push: (...callbacks: Fn[]) => void;
227
+ push: (...callbacks: ((arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void)[]) => void;
148
228
  execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
149
229
  clear: Fn;
150
230
  };
231
+ /** WIP: an easier to setup and type Proxy */
232
+ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
233
+ get: <K extends keyof T>(key: K) => T[K];
234
+ set: <K extends keyof T>(key: K, value: T[K]) => void;
235
+ }): T;
236
+ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
237
+ get: <K extends keyof T>(key: K) => T[K];
238
+ set?: undefined;
239
+ }): Readonly<T>;
151
240
 
152
- export { access, accessAsArray, asAccessor, asArray, createCallbackStack, createSubRoot, createSubRootFunction, destore, entries, forEach, isClient, isDefined, isFunction, noop, onAccess, onRootCleanup, promiseTimeout, raceTimeout, withAccess };
241
+ export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, Clear, DeepPartialAny, Destore, Directive, ExtractIfPossible, Falsy, FalsyValue, Fn, Get, ItemsOf, ItemsOfMany, Keys, LiteralKey, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Noop, OnAccessEffectFunction, Position, PrimitiveValue, Truthy, UnionToIntersection, Values, access, accessArray, accessAsArray, asAccessor, asArray, compare, createCallbackStack, createProxy, destore, entries, forEach, forEachEntry, includes, isArray, isBoolean, isClient, isDefined, isDev, isFunction, isNumber, isObject, isProd, isString, keys, noop, ofClass, onAccess, onRootCleanup, promiseTimeout, raceTimeout, tuple, warn, withAccess };
package/dist/index.js CHANGED
@@ -1,13 +1,32 @@
1
1
  // src/index.ts
2
- import { createRoot, getOwner, onCleanup, runWithOwner, on } from "solid-js";
2
+ import { getOwner, onCleanup, on, DEV } from "solid-js";
3
3
  import { isServer } from "solid-js/web";
4
4
  var noop = () => void 0;
5
5
  var isClient = !isServer;
6
+ var isDev = DEV && DEV.hasOwnProperty("writeSignal");
7
+ var isProd = !isDev;
8
+ var warn = (...a) => isDev && console.warn(...a);
6
9
  var isDefined = (value) => typeof value !== "undefined" && value !== null;
7
10
  var isFunction = (value) => typeof value === "function";
11
+ var isBoolean = (val) => typeof val === "boolean";
12
+ var isNumber = (val) => typeof val === "number";
13
+ var isString = (val) => typeof val === "string";
14
+ var isObject = (val) => toString.call(val) === "[object Object]";
15
+ var isArray = Array.isArray;
16
+ var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
17
+ var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
18
+ var tuple = (input) => input;
19
+ var includes = (arr, ...items) => {
20
+ for (const item of arr) {
21
+ if (items.includes(item))
22
+ return true;
23
+ }
24
+ return false;
25
+ };
8
26
  var access = (v) => isFunction(v) ? v() : v;
9
27
  var accessAsArray = (value) => asArray(access(value));
10
28
  var asArray = (value) => Array.isArray(value) ? value : [value];
29
+ var accessArray = (list) => list.map((v) => access(v));
11
30
  var withAccess = (value, fn) => {
12
31
  const _value = access(value);
13
32
  isDefined(_value) && fn(_value);
@@ -18,7 +37,12 @@ function onAccess(deps, fn, options) {
18
37
  return on(source, fn, options);
19
38
  }
20
39
  var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
40
+ var forEachEntry = (object, iterator) => {
41
+ const obj = access(object);
42
+ Object.entries(obj).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, obj));
43
+ };
21
44
  var entries = (object) => Object.entries(access(object));
45
+ var keys = (object) => Object.keys(access(object));
22
46
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
23
47
  function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
24
48
  const promises = asArray(input);
@@ -28,6 +52,7 @@ function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
28
52
  });
29
53
  return race;
30
54
  }
55
+ var onRootCleanup = (fn) => getOwner() ? onCleanup(fn) : fn;
31
56
  function destore(store) {
32
57
  const _store = store;
33
58
  const result = {};
@@ -36,46 +61,61 @@ function destore(store) {
36
61
  });
37
62
  return result;
38
63
  }
39
- var onRootCleanup = (fn) => getOwner() ? onCleanup(fn) : fn;
40
- function createSubRoot(fn, owner = getOwner()) {
41
- const [dispose, returns] = createRoot((dispose2) => [dispose2, fn(dispose2)], owner != null ? owner : void 0);
42
- owner && runWithOwner(owner, () => onCleanup(dispose));
43
- return returns;
44
- }
45
- function createSubRootFunction(callback, owner) {
46
- return (...args) => createSubRoot(() => callback(...args), owner);
47
- }
48
64
  var createCallbackStack = () => {
49
65
  let stack = [];
50
66
  const clear = () => stack = [];
51
67
  return {
52
68
  push: (...callbacks) => stack.push(...callbacks),
53
- execute: (arg0, arg1, arg2, arg3) => {
69
+ execute(arg0, arg1, arg2, arg3) {
54
70
  stack.forEach((cb) => cb(arg0, arg1, arg2, arg3));
55
71
  clear();
56
72
  },
57
73
  clear
58
74
  };
59
75
  };
76
+ function createProxy(traps) {
77
+ return new Proxy({}, {
78
+ get: (_, k) => traps.get(k),
79
+ set: (_, k, v) => {
80
+ var _a;
81
+ (_a = traps.set) == null ? void 0 : _a.call(traps, k, v);
82
+ return false;
83
+ }
84
+ });
85
+ }
60
86
  export {
61
87
  access,
88
+ accessArray,
62
89
  accessAsArray,
63
90
  asAccessor,
64
91
  asArray,
92
+ compare,
65
93
  createCallbackStack,
66
- createSubRoot,
67
- createSubRootFunction,
94
+ createProxy,
68
95
  destore,
69
96
  entries,
70
97
  forEach,
98
+ forEachEntry,
99
+ includes,
100
+ isArray,
101
+ isBoolean,
71
102
  isClient,
72
103
  isDefined,
104
+ isDev,
73
105
  isFunction,
106
+ isNumber,
107
+ isObject,
108
+ isProd,
74
109
  isServer,
110
+ isString,
111
+ keys,
75
112
  noop,
113
+ ofClass,
76
114
  onAccess,
77
115
  onRootCleanup,
78
116
  promiseTimeout,
79
117
  raceTimeout,
118
+ tuple,
119
+ warn,
80
120
  withAccess
81
121
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "0.1.2",
3
+ "version": "0.2.2",
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",
@@ -9,6 +9,9 @@
9
9
  "type": "git",
10
10
  "url": "git+https://github.com/davedbase/solid-primitives.git"
11
11
  },
12
+ "bugs": {
13
+ "url": "https://github.com/davedbase/solid-primitives/issues"
14
+ },
12
15
  "private": false,
13
16
  "sideEffects": false,
14
17
  "type": "module",
@@ -18,33 +21,6 @@
18
21
  "files": [
19
22
  "dist"
20
23
  ],
21
- "exports": {
22
- ".": {
23
- "types": "./dist/index.d.ts",
24
- "require": "./dist/index.cjs",
25
- "import": "./dist/index.js"
26
- },
27
- "./fp": {
28
- "types": "./dist/fp.d.ts",
29
- "require": "./dist/fp.cjs",
30
- "import": "./dist/fp.js"
31
- },
32
- "./setter": {
33
- "types": "./dist/setter.d.ts",
34
- "require": "./dist/setter.cjs",
35
- "import": "./dist/setter.js"
36
- }
37
- },
38
- "typesVersions": {
39
- "*": {
40
- "fp": [
41
- "./dist/fp.d.ts"
42
- ],
43
- "setter": [
44
- "./dist/setter.d.ts"
45
- ]
46
- }
47
- },
48
24
  "scripts": {
49
25
  "build": "tsup",
50
26
  "test": "uvu -r solid-register",
@@ -69,6 +45,6 @@
69
45
  "watchlist": "^0.3.1"
70
46
  },
71
47
  "peerDependencies": {
72
- "solid-js": "1.3.0"
48
+ "solid-js": "^1.3.1"
73
49
  }
74
50
  }
@@ -1,44 +0,0 @@
1
- // src/fp.ts
2
- var withArrayCopy = (array, mutator) => {
3
- const copy = array.slice();
4
- mutator(copy);
5
- return copy;
6
- };
7
- var withObjectCopy = (object, mutator) => {
8
- const copy = Object.assign({}, object);
9
- mutator(copy);
10
- return copy;
11
- };
12
- var withCopy = (source, mutator) => Array.isArray(source) ? withArrayCopy(source, mutator) : withObjectCopy(source, mutator);
13
- var push = (list, item) => withArrayCopy(list, (list2) => list2.push(item));
14
- var drop = (list, n = 1) => list.slice(n);
15
- var filterOut = (list, item) => filter(list, (i) => i !== item);
16
- function filter(list, predicate) {
17
- const newList = list.filter(predicate);
18
- newList.removed = list.length - newList.length;
19
- return newList;
20
- }
21
- var map = (list, mapFn) => list.map(mapFn);
22
- var slice = (list, start, end) => list.slice(start, end);
23
- var splice = (list, start, deleteCount, ...items) => withArrayCopy(list, (list2) => list2.splice(start, deleteCount, ...items));
24
- var omit = (object, ...keys) => withObjectCopy(object, (object2) => keys.forEach((key) => delete object2[key]));
25
- var pick = (object, ...keys) => keys.reduce((n, k) => {
26
- if (k in object)
27
- n[k] = object[k];
28
- return n;
29
- }, {});
30
-
31
- export {
32
- withArrayCopy,
33
- withObjectCopy,
34
- withCopy,
35
- push,
36
- drop,
37
- filterOut,
38
- filter,
39
- map,
40
- slice,
41
- splice,
42
- omit,
43
- pick
44
- };
package/dist/fp.cjs DELETED
@@ -1,83 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __reExport = (target, module2, copyDefault, desc) => {
11
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
12
- for (let key of __getOwnPropNames(module2))
13
- if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
14
- __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
15
- }
16
- return target;
17
- };
18
- var __toCommonJS = /* @__PURE__ */ ((cache) => {
19
- return (module2, temp) => {
20
- return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
21
- };
22
- })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
23
-
24
- // src/fp.ts
25
- var fp_exports = {};
26
- __export(fp_exports, {
27
- drop: () => drop,
28
- filter: () => filter,
29
- filterOut: () => filterOut,
30
- map: () => map,
31
- omit: () => omit,
32
- pick: () => pick,
33
- push: () => push,
34
- slice: () => slice,
35
- splice: () => splice,
36
- withArrayCopy: () => withArrayCopy,
37
- withCopy: () => withCopy,
38
- withObjectCopy: () => withObjectCopy
39
- });
40
- var withArrayCopy = (array, mutator) => {
41
- const copy = array.slice();
42
- mutator(copy);
43
- return copy;
44
- };
45
- var withObjectCopy = (object, mutator) => {
46
- const copy = Object.assign({}, object);
47
- mutator(copy);
48
- return copy;
49
- };
50
- var withCopy = (source, mutator) => Array.isArray(source) ? withArrayCopy(source, mutator) : withObjectCopy(source, mutator);
51
- var push = (list, item) => withArrayCopy(list, (list2) => list2.push(item));
52
- var drop = (list, n = 1) => list.slice(n);
53
- var filterOut = (list, item) => filter(list, (i) => i !== item);
54
- function filter(list, predicate) {
55
- const newList = list.filter(predicate);
56
- newList.removed = list.length - newList.length;
57
- return newList;
58
- }
59
- var map = (list, mapFn) => list.map(mapFn);
60
- var slice = (list, start, end) => list.slice(start, end);
61
- var splice = (list, start, deleteCount, ...items) => withArrayCopy(list, (list2) => list2.splice(start, deleteCount, ...items));
62
- var omit = (object, ...keys) => withObjectCopy(object, (object2) => keys.forEach((key) => delete object2[key]));
63
- var pick = (object, ...keys) => keys.reduce((n, k) => {
64
- if (k in object)
65
- n[k] = object[k];
66
- return n;
67
- }, {});
68
- module.exports = __toCommonJS(fp_exports);
69
- // Annotate the CommonJS export names for ESM import in node:
70
- 0 && (module.exports = {
71
- drop,
72
- filter,
73
- filterOut,
74
- map,
75
- omit,
76
- pick,
77
- push,
78
- slice,
79
- splice,
80
- withArrayCopy,
81
- withCopy,
82
- withObjectCopy
83
- });
package/dist/fp.d.ts DELETED
@@ -1,92 +0,0 @@
1
- import { P as Predicate, c as MappingFn } from './types-5d7b3b37';
2
- import 'solid-js';
3
-
4
- /**
5
- * apply mutations to the an array without changing the original
6
- * @param array original array
7
- * @param mutator function applying mutations to the copy of source
8
- * @returns changed array copy
9
- */
10
- declare const withArrayCopy: <T>(array: readonly T[], mutator: (copy: T[]) => void) => T[];
11
- /**
12
- * apply mutations to the an object without changing the original
13
- * @param object original object
14
- * @param mutator function applying mutations to the copy of source
15
- * @returns changed object copy
16
- */
17
- declare const withObjectCopy: <T extends object>(object: T, mutator: (copy: T) => void) => T;
18
- /**
19
- * apply mutations to the an object/array without changing the original
20
- * @param source original object
21
- * @param mutator function applying mutations to the copy of source
22
- * @returns changed object copy
23
- */
24
- declare const withCopy: <T extends object>(source: T, mutator: (copy: T) => void) => T;
25
- /**
26
- * non-mutating `Array.prototype.push()`
27
- * @returns changed array copy
28
- */
29
- declare const push: <T>(list: T[], item: T) => T[];
30
- /**
31
- * non-mutating function that drops n items from the array start.
32
- * @returns changed array copy
33
- *
34
- * @example
35
- * ```ts
36
- * const newList = drop([1,2,3])
37
- * newList // => [2,3]
38
- *
39
- * const newList = drop([1,2,3], 2)
40
- * newList // => [3]
41
- * ```
42
- */
43
- declare const drop: <T>(list: T[], n?: number) => T[];
44
- /**
45
- * non-mutating `Array.prototype.filter()` that filters out passed item
46
- * @returns changed array copy
47
- */
48
- declare const filterOut: <T>(list: readonly T[], item: T) => T[] & {
49
- removed: number;
50
- };
51
- /**
52
- * non-mutating `Array.prototype.filter()` as a standalone function
53
- * @returns changed array copy
54
- */
55
- declare function filter<T>(list: readonly T[], predicate: Predicate<T>): T[] & {
56
- removed: number;
57
- };
58
- /**
59
- * standalone `Array.prototype.map()` function
60
- */
61
- declare const map: <T, V>(list: readonly T[], mapFn: MappingFn<T, V>) => V[];
62
- /**
63
- * standalone `Array.prototype.slice()` function
64
- */
65
- declare const slice: <T>(list: readonly T[], start?: number | undefined, end?: number | undefined) => T[];
66
- /**
67
- * non-mutating `Array.prototype.splice()` as a standalone function
68
- * @returns changed array copy
69
- */
70
- declare const splice: <T>(list: readonly T[], start: number, deleteCount: number, ...items: T[]) => T[];
71
- /**
72
- * Create a new subset object without the provided keys
73
- *
74
- * @example
75
- * ```ts
76
- * const newObject = omit({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
77
- * newObject // => { c: "baz" }
78
- * ```
79
- */
80
- declare const omit: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Omit<O, K>;
81
- /**
82
- * Create a new subset object with only the provided keys
83
- *
84
- * @example
85
- * ```ts
86
- * const newObject = pick({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
87
- * newObject // => { a:"foo", b:"bar" }
88
- * ```
89
- */
90
- declare const pick: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Pick<O, K>;
91
-
92
- export { drop, filter, filterOut, map, omit, pick, push, slice, splice, withArrayCopy, withCopy, withObjectCopy };
package/dist/fp.js DELETED
@@ -1,28 +0,0 @@
1
- import {
2
- drop,
3
- filter,
4
- filterOut,
5
- map,
6
- omit,
7
- pick,
8
- push,
9
- slice,
10
- splice,
11
- withArrayCopy,
12
- withCopy,
13
- withObjectCopy
14
- } from "./chunk-UFTWPD62.js";
15
- export {
16
- drop,
17
- filter,
18
- filterOut,
19
- map,
20
- omit,
21
- pick,
22
- push,
23
- slice,
24
- splice,
25
- withArrayCopy,
26
- withCopy,
27
- withObjectCopy
28
- };
package/dist/setter.cjs DELETED
@@ -1,72 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __reExport = (target, module2, copyDefault, desc) => {
11
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
12
- for (let key of __getOwnPropNames(module2))
13
- if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
14
- __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
15
- }
16
- return target;
17
- };
18
- var __toCommonJS = /* @__PURE__ */ ((cache) => {
19
- return (module2, temp) => {
20
- return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
21
- };
22
- })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
23
-
24
- // src/setter.ts
25
- var setter_exports = {};
26
- __export(setter_exports, {
27
- drop: () => drop2,
28
- filter: () => filter2,
29
- filterOut: () => filterOut2,
30
- map: () => map,
31
- push: () => push2,
32
- slice: () => slice,
33
- splice: () => splice2
34
- });
35
-
36
- // src/fp.ts
37
- var withArrayCopy = (array, mutator) => {
38
- const copy = array.slice();
39
- mutator(copy);
40
- return copy;
41
- };
42
- var push = (list, item) => withArrayCopy(list, (list2) => list2.push(item));
43
- var drop = (list, n = 1) => list.slice(n);
44
- var filterOut = (list, item) => filter(list, (i) => i !== item);
45
- function filter(list, predicate) {
46
- const newList = list.filter(predicate);
47
- newList.removed = list.length - newList.length;
48
- return newList;
49
- }
50
- var splice = (list, start, deleteCount, ...items) => withArrayCopy(list, (list2) => list2.splice(start, deleteCount, ...items));
51
-
52
- // src/setter.ts
53
- var push2 = (item) => (list) => push(list, item);
54
- var drop2 = (n = 1) => (list) => drop(list, n);
55
- var filterOut2 = (item) => (list) => filterOut(list, item);
56
- var filter2 = (predicate) => (list) => filter(list, predicate);
57
- var map = (mapFn) => (list) => list.map(mapFn);
58
- var slice = (start, end) => (list) => list.slice(start, end);
59
- function splice2(start, deleteCount, ...items) {
60
- return (list) => splice(list, start, deleteCount, ...items);
61
- }
62
- module.exports = __toCommonJS(setter_exports);
63
- // Annotate the CommonJS export names for ESM import in node:
64
- 0 && (module.exports = {
65
- drop,
66
- filter,
67
- filterOut,
68
- map,
69
- push,
70
- slice,
71
- splice
72
- });
package/dist/setter.d.ts DELETED
@@ -1,17 +0,0 @@
1
- import { P as Predicate, c as MappingFn } from './types-5d7b3b37';
2
- import 'solid-js';
3
-
4
- declare const push: <T>(item: T) => (list: T[]) => T[];
5
- declare const drop: (n?: number) => <T>(list: T[]) => T[];
6
- declare const filterOut: <T>(item: T) => (list: T[]) => T[] & {
7
- removed: number;
8
- };
9
- declare const filter: <T>(predicate: Predicate<T>) => (list: T[]) => T[] & {
10
- removed: number;
11
- };
12
- declare const map: <T>(mapFn: MappingFn<T, T>) => (list: T[]) => T[];
13
- declare const slice: (start?: number | undefined, end?: number | undefined) => <T>(list: T[]) => T[];
14
- declare function splice(start: number, deleteCount: number): <T>(list: T[]) => T[];
15
- declare function splice<T>(start: number, deleteCount: number, ...items: T[]): (list: T[]) => T[];
16
-
17
- export { drop, filter, filterOut, map, push, slice, splice };
package/dist/setter.js DELETED
@@ -1,27 +0,0 @@
1
- import {
2
- drop,
3
- filter,
4
- filterOut,
5
- push,
6
- splice
7
- } from "./chunk-UFTWPD62.js";
8
-
9
- // src/setter.ts
10
- var push2 = (item) => (list) => push(list, item);
11
- var drop2 = (n = 1) => (list) => drop(list, n);
12
- var filterOut2 = (item) => (list) => filterOut(list, item);
13
- var filter2 = (predicate) => (list) => filter(list, predicate);
14
- var map = (mapFn) => (list) => list.map(mapFn);
15
- var slice = (start, end) => (list) => list.slice(start, end);
16
- function splice2(start, deleteCount, ...items) {
17
- return (list) => splice(list, start, deleteCount, ...items);
18
- }
19
- export {
20
- drop2 as drop,
21
- filter2 as filter,
22
- filterOut2 as filterOut,
23
- map,
24
- push2 as push,
25
- slice,
26
- splice2 as splice
27
- };
@@ -1,64 +0,0 @@
1
- import { Accessor } from 'solid-js';
2
-
3
- /**
4
- * A function
5
- */
6
- declare type Fn<R = void> = () => R;
7
- /**
8
- * Can be single or in an array
9
- */
10
- declare type Many<T> = T | T[];
11
- declare type Keys<O extends Object> = keyof O;
12
- declare type Values<O extends Object> = O[Keys<O>];
13
- declare type Noop = (...a: any[]) => void;
14
- /**
15
- * Infers the type of the array elements
16
- */
17
- declare type ItemsOf<T> = T extends (infer E)[] ? E : never;
18
- declare type Predicate<T> = (item: T, index: number, array: readonly T[]) => boolean;
19
- declare type MappingFn<T, V> = (item: T, index: number, array: readonly T[]) => V;
20
- /**
21
- * T or a reactive/non-reactive function returning T
22
- */
23
- declare type MaybeAccessor<T> = T | Accessor<T>;
24
- /**
25
- * Accessed value of a MaybeAccessor
26
- * @example
27
- * ```ts
28
- * MaybeAccessorValue<MaybeAccessor<string>>
29
- * // => string
30
- * MaybeAccessorValue<MaybeAccessor<() => string>>
31
- * // => string | (() => string)
32
- * MaybeAccessorValue<MaybeAccessor<string> | Function>
33
- * // => string | void
34
- * ```
35
- */
36
- declare type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends Fn ? ReturnType<T> : T;
37
- declare type OnAccessEffectFunction<S, Prev, Next extends Prev = Prev> = (input: AccessReturnTypes<S>, prevInput: AccessReturnTypes<S>, v: Prev) => Next;
38
- declare type AccessReturnTypes<S> = S extends MaybeAccessor<any>[] ? {
39
- [I in keyof S]: AccessReturnTypes<S[I]>;
40
- } : MaybeAccessorValue<S>;
41
- /** Allows to make shallow overwrites to an interface */
42
- declare type Modify<T, R> = Omit<T, keyof R> & R;
43
- /** Allows to make nested overwrites to an interface */
44
- declare type ModifyDeep<A extends AnyObject, B extends DeepPartialAny<A>> = {
45
- [K in keyof A]: B[K] extends never ? A[K] : B[K] extends AnyObject ? ModifyDeep<A[K], B[K]> : B[K];
46
- } & (A extends AnyObject ? Omit<B, keyof A> : A);
47
- /** Makes each property optional and turns each leaf property into any, allowing for type overrides by narrowing any. */
48
- declare type DeepPartialAny<T> = {
49
- [P in keyof T]?: T[P] extends AnyObject ? DeepPartialAny<T[P]> : any;
50
- };
51
- declare type AnyObject = Record<string, any>;
52
- declare type AnyFunction = (...args: any[]) => any;
53
- declare type PrimitiveValue = string | boolean | number | bigint | symbol | null | undefined;
54
- declare type FalsyValue = false | 0 | "" | null | undefined;
55
- declare type Truthy<T> = T extends FalsyValue ? never : T;
56
- declare type Falsy<T> = T extends FalsyValue ? T : never;
57
- /**
58
- * Destructible store object, with values changed to accessors
59
- */
60
- declare type Destore<T extends Object> = {
61
- [K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
62
- };
63
-
64
- export { AnyFunction as A, Destore as D, Fn as F, ItemsOf as I, Keys as K, MaybeAccessorValue as M, Noop as N, OnAccessEffectFunction as O, Predicate as P, Truthy as T, Values as V, MaybeAccessor as a, Many as b, MappingFn as c, AccessReturnTypes as d, Modify as e, ModifyDeep as f, DeepPartialAny as g, AnyObject as h, PrimitiveValue as i, FalsyValue as j, Falsy as k };