@solid-primitives/utils 0.0.260 → 0.1.3

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
@@ -2,6 +2,34 @@
2
2
 
3
3
  Solid Primitives Utilities is a support and helper package for a number of primitives in our library. Please free to augment or centralize useful utilities and methods in this package for sharing.
4
4
 
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @solid-primitives/utils
9
+ # or
10
+ yarn add @solid-primitives/utils
11
+ ```
12
+
13
+ ## Package Entries
14
+
15
+ - [`*` - `@solid-primitives/utils`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/docs/modules/index.md) - General utility functions and types for authoring primitives.
16
+ - [`/fp` - `@solid-primitives/utils/fp`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/docs/modules/fp.md) - Helpers for making changes to immutable data.
17
+ - [`/setter` - `@solid-primitives/utils/setter`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/docs/modules/setter.md) - Modified helpers from the `/fp`, but optimized for usage with signal setters.
18
+
19
+ Import accordingly.
20
+
21
+ ```ts
22
+ import { access, promiseTimeout, MaybeAccessor } from "@solid-primitives/utils";
23
+
24
+ // between these two some function names will overlap.
25
+ import { pick, omit, filter } from "@solid-primitives/utils/fp";
26
+ import { push, filter, drop } from "@solid-primitives/utils/setter";
27
+ ```
28
+
29
+ ## >>> [Modules documentation](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/docs/README.md) <<<
30
+
31
+ Documentation auto-generated from the [Typescript source files](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/src) using [typedoc](https://typedoc.org) + [markdown plugin](https://www.npmjs.com/package/typedoc-plugin-markdown).
32
+
5
33
  ## Changelog
6
34
 
7
35
  <details>
@@ -19,4 +47,16 @@ Republished version with better ESM support and build tooling.
19
47
 
20
48
  Added comments for util methods.
21
49
 
50
+ 0.1.0
51
+
52
+ Add `/fp` and `/setter` export entries. Add `destore` and `raceTimeout` util. More jsdoc.
53
+
54
+ 0.1.2
55
+
56
+ Updated to Solid 1.3
57
+
58
+ 0.1.3
59
+
60
+ Minor upgrades.
61
+
22
62
  </details>
@@ -0,0 +1,49 @@
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 remove = (list, item) => {
25
+ const index = list.indexOf(item);
26
+ return splice(list, index, 1);
27
+ };
28
+ var omit = (object, ...keys) => withObjectCopy(object, (object2) => keys.forEach((key) => delete object2[key]));
29
+ var pick = (object, ...keys) => keys.reduce((n, k) => {
30
+ if (k in object)
31
+ n[k] = object[k];
32
+ return n;
33
+ }, {});
34
+
35
+ export {
36
+ withArrayCopy,
37
+ withObjectCopy,
38
+ withCopy,
39
+ push,
40
+ drop,
41
+ filterOut,
42
+ filter,
43
+ map,
44
+ slice,
45
+ splice,
46
+ remove,
47
+ omit,
48
+ pick
49
+ };
package/dist/fp.cjs ADDED
@@ -0,0 +1,89 @@
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
+ remove: () => remove,
35
+ slice: () => slice,
36
+ splice: () => splice,
37
+ withArrayCopy: () => withArrayCopy,
38
+ withCopy: () => withCopy,
39
+ withObjectCopy: () => withObjectCopy
40
+ });
41
+ var withArrayCopy = (array, mutator) => {
42
+ const copy = array.slice();
43
+ mutator(copy);
44
+ return copy;
45
+ };
46
+ var withObjectCopy = (object, mutator) => {
47
+ const copy = Object.assign({}, object);
48
+ mutator(copy);
49
+ return copy;
50
+ };
51
+ var withCopy = (source, mutator) => Array.isArray(source) ? withArrayCopy(source, mutator) : withObjectCopy(source, mutator);
52
+ var push = (list, item) => withArrayCopy(list, (list2) => list2.push(item));
53
+ var drop = (list, n = 1) => list.slice(n);
54
+ var filterOut = (list, item) => filter(list, (i) => i !== item);
55
+ function filter(list, predicate) {
56
+ const newList = list.filter(predicate);
57
+ newList.removed = list.length - newList.length;
58
+ return newList;
59
+ }
60
+ var map = (list, mapFn) => list.map(mapFn);
61
+ var slice = (list, start, end) => list.slice(start, end);
62
+ var splice = (list, start, deleteCount, ...items) => withArrayCopy(list, (list2) => list2.splice(start, deleteCount, ...items));
63
+ var remove = (list, item) => {
64
+ const index = list.indexOf(item);
65
+ return splice(list, index, 1);
66
+ };
67
+ var omit = (object, ...keys) => withObjectCopy(object, (object2) => keys.forEach((key) => delete object2[key]));
68
+ var pick = (object, ...keys) => keys.reduce((n, k) => {
69
+ if (k in object)
70
+ n[k] = object[k];
71
+ return n;
72
+ }, {});
73
+ module.exports = __toCommonJS(fp_exports);
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ drop,
77
+ filter,
78
+ filterOut,
79
+ map,
80
+ omit,
81
+ pick,
82
+ push,
83
+ remove,
84
+ slice,
85
+ splice,
86
+ withArrayCopy,
87
+ withCopy,
88
+ withObjectCopy
89
+ });
package/dist/fp.d.ts ADDED
@@ -0,0 +1,93 @@
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
+ declare const remove: <T>(list: readonly T[], item: T) => T[];
72
+ /**
73
+ * Create a new subset object without the provided keys
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * const newObject = omit({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
78
+ * newObject // => { c: "baz" }
79
+ * ```
80
+ */
81
+ declare const omit: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Omit<O, K>;
82
+ /**
83
+ * Create a new subset object with only the provided keys
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * const newObject = pick({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
88
+ * newObject // => { a:"foo", b:"bar" }
89
+ * ```
90
+ */
91
+ declare const pick: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Pick<O, K>;
92
+
93
+ export { drop, filter, filterOut, map, omit, pick, push, remove, slice, splice, withArrayCopy, withCopy, withObjectCopy };
package/dist/fp.js ADDED
@@ -0,0 +1,30 @@
1
+ import {
2
+ drop,
3
+ filter,
4
+ filterOut,
5
+ map,
6
+ omit,
7
+ pick,
8
+ push,
9
+ remove,
10
+ slice,
11
+ splice,
12
+ withArrayCopy,
13
+ withCopy,
14
+ withObjectCopy
15
+ } from "./chunk-BVHCDFCW.js";
16
+ export {
17
+ drop,
18
+ filter,
19
+ filterOut,
20
+ map,
21
+ omit,
22
+ pick,
23
+ push,
24
+ remove,
25
+ slice,
26
+ splice,
27
+ withArrayCopy,
28
+ withCopy,
29
+ withObjectCopy
30
+ };
package/dist/index.cjs CHANGED
@@ -1,60 +1,96 @@
1
1
  var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
2
5
  var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
3
6
  var __export = (target, all) => {
4
- __markAsModule(target);
5
7
  for (var name in all)
6
8
  __defProp(target, name, { get: all[name], enumerable: true });
7
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);
8
23
 
9
24
  // src/index.ts
10
- __export(exports, {
25
+ var src_exports = {};
26
+ __export(src_exports, {
11
27
  access: () => access,
12
28
  accessAsArray: () => accessAsArray,
13
- concat: () => concat,
29
+ asAccessor: () => asAccessor,
30
+ asArray: () => asArray,
14
31
  createCallbackStack: () => createCallbackStack,
32
+ createSubRoot: () => createSubRoot,
33
+ createSubRootFunction: () => createSubRootFunction,
15
34
  destore: () => destore,
16
35
  entries: () => entries,
17
36
  forEach: () => forEach,
18
37
  isClient: () => isClient,
19
- isServer: () => isServer,
20
- objectOmit: () => objectOmit,
38
+ isDefined: () => isDefined,
39
+ isFunction: () => isFunction,
40
+ isServer: () => import_web.isServer,
41
+ noop: () => noop,
42
+ onAccess: () => onAccess,
43
+ onRootCleanup: () => onRootCleanup,
21
44
  promiseTimeout: () => promiseTimeout,
22
- stringConcat: () => stringConcat,
23
- toArray: () => toArray,
24
- toFloat: () => toFloat,
25
- toInt: () => toInt,
45
+ raceTimeout: () => raceTimeout,
26
46
  withAccess: () => withAccess
27
47
  });
28
- var isClient = typeof window !== "undefined";
29
- var isServer = !isClient;
30
- var access = (v) => typeof v === "function" ? v() : v;
31
- var accessAsArray = (value) => {
32
- const _value = access(value);
33
- return Array.isArray(_value) ? _value : [_value];
34
- };
48
+ var import_solid_js = require("solid-js");
49
+ var import_web = require("solid-js/web");
50
+ var noop = () => void 0;
51
+ var isClient = !import_web.isServer;
52
+ var isDefined = (value) => typeof value !== "undefined" && value !== null;
53
+ var isFunction = (value) => typeof value === "function";
54
+ var access = (v) => isFunction(v) ? v() : v;
55
+ var accessAsArray = (value) => asArray(access(value));
56
+ var asArray = (value) => Array.isArray(value) ? value : [value];
35
57
  var withAccess = (value, fn) => {
36
58
  const _value = access(value);
37
- if (typeof _value !== "undefined" && _value !== null)
38
- fn(_value);
59
+ isDefined(_value) && fn(_value);
39
60
  };
61
+ var asAccessor = (v) => isFunction(v) ? v : () => v;
62
+ function onAccess(deps, fn, options) {
63
+ const source = deps.map(asAccessor);
64
+ return (0, import_solid_js.on)(source, fn, options);
65
+ }
40
66
  var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
41
67
  var entries = (object) => Object.entries(access(object));
42
68
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
43
- var objectOmit = (object, ...keys) => {
44
- const copy = Object.assign({}, object);
45
- for (const key of keys) {
46
- delete copy[key];
47
- }
48
- return copy;
49
- };
69
+ function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
70
+ const promises = asArray(input);
71
+ const race = Promise.race([...promises, promiseTimeout(ms, throwOnTimeout, reason)]);
72
+ race.finally(() => {
73
+ promises.forEach((p) => p && typeof p === "object" && typeof p.dispose === "function" && p.dispose());
74
+ });
75
+ return race;
76
+ }
50
77
  function destore(store) {
51
78
  const _store = store;
52
79
  const result = {};
53
80
  Object.keys(_store).forEach((key) => {
54
- result[key] = typeof _store[key] === "function" ? _store[key].bind(_store) : () => _store[key];
81
+ result[key] = isFunction(_store[key]) ? _store[key].bind(_store) : () => _store[key];
55
82
  });
56
83
  return result;
57
84
  }
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
+ }
58
94
  var createCallbackStack = () => {
59
95
  let stack = [];
60
96
  const clear = () => stack = [];
@@ -67,30 +103,27 @@ var createCallbackStack = () => {
67
103
  clear
68
104
  };
69
105
  };
70
- var stringConcat = (...a) => a.reduce((t, c) => t + access(c), "");
71
- var concat = (...a) => a.reduce((t, c) => {
72
- const v = access(c);
73
- return Array.isArray(v) ? [...t, ...v] : [...t, v];
74
- }, []);
75
- var toFloat = (string) => Number.parseFloat(access(string));
76
- var toInt = (string, radix) => Number.parseInt(access(string), radix);
77
- var toArray = (...a) => a.map((v) => access(v));
106
+ module.exports = __toCommonJS(src_exports);
78
107
  // Annotate the CommonJS export names for ESM import in node:
79
108
  0 && (module.exports = {
80
109
  access,
81
110
  accessAsArray,
82
- concat,
111
+ asAccessor,
112
+ asArray,
83
113
  createCallbackStack,
114
+ createSubRoot,
115
+ createSubRootFunction,
84
116
  destore,
85
117
  entries,
86
118
  forEach,
87
119
  isClient,
120
+ isDefined,
121
+ isFunction,
88
122
  isServer,
89
- objectOmit,
123
+ noop,
124
+ onAccess,
125
+ onRootCleanup,
90
126
  promiseTimeout,
91
- stringConcat,
92
- toArray,
93
- toFloat,
94
- toInt,
127
+ raceTimeout,
95
128
  withAccess
96
129
  });
package/dist/index.d.ts CHANGED
@@ -1,70 +1,99 @@
1
- import { Accessor } from 'solid-js';
1
+ import { Accessor, onCleanup } from 'solid-js';
2
+ import { OnOptions, EffectFunction, NoInfer, Owner } from 'solid-js/types/reactive/signal';
2
3
  import { Store } from 'solid-js/store';
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';
7
+
8
+ /** no operation */
9
+ declare const noop: Noop;
10
+ declare const isClient = true;
3
11
 
4
12
  /**
5
- * A function
6
- */
7
- declare type Fn<R = void> = () => R;
8
- /**
9
- * Can be single or in an array
10
- */
11
- declare type Many<T> = T | T[];
12
- declare type Keys<O extends Object> = keyof O;
13
- declare type Values<O extends Object> = O[Keys<O>];
14
- /**
15
- * Infers the type of the array elements
16
- */
17
- declare type ItemsOf<T> = T extends (infer E)[] ? E : never;
18
- /**
19
- * T or a reactive/non-reactive function returning T
13
+ * `if (typeof value !== "undefined" && value !== null)`
20
14
  */
21
- declare type MaybeAccessor<T> = T | Accessor<T>;
22
- /**
23
- * Accessed value of a MaybeAccessor
24
- * @example
25
- * MaybeAccessorValue<MaybeAccessor<string>>
26
- * // => string
27
- * MaybeAccessorValue<MaybeAccessor<() => string>>
28
- * // => string | (() => string)
29
- * MaybeAccessorValue<MaybeAccessor<string> | Function>
30
- * // => string | void
31
- */
32
- declare type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends Fn ? ReturnType<T> : T;
33
- declare const isClient: boolean;
34
- declare const isServer: boolean;
15
+ declare const isDefined: <T>(value: T | null | undefined) => value is T;
16
+ declare const isFunction: <T>(value: Function | T) => value is Function;
35
17
  /**
36
18
  * Accesses the value of a MaybeAccessor
37
19
  * @example
38
- * access(x as MaybeAccessor<string>)
39
- * // => string
40
- * access(x as MaybeAccessor<() => string>)
41
- * // => string | (() => string)
42
- * access(x as MaybeAccessor<string> | Function)
43
- * // => string | void
20
+ * ```ts
21
+ * access("foo") // => "foo"
22
+ * access(() => "foo") // => "foo"
23
+ * ```
44
24
  */
45
25
  declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
46
26
  /**
47
27
  * Accesses the value of a MaybeAccessor, but always returns an array
48
28
  * @example
29
+ * ```ts
49
30
  * accessAsArray('abc') // => ['abc']
50
31
  * accessAsArray(() => 'abc') // => ['abc']
51
32
  * accessAsArray([1,2,3]) // => [1,2,3]
52
33
  * accessAsArray(() => [1,2,3]) // => [1,2,3]
34
+ * ```
53
35
  */
54
36
  declare const accessAsArray: <T extends unknown, V = MaybeAccessorValue<T>>(value: T) => V extends any[] ? V : V[];
37
+ declare const asArray: <T>(value: T) => T extends any[] ? T : T[];
55
38
  /**
56
39
  * Run the function if the accessed value is not `undefined` nor `null`
57
40
  * @param value
58
41
  * @param fn
59
42
  */
60
43
  declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void;
44
+ declare const asAccessor: <A extends unknown>(v: A) => Accessor<MaybeAccessorValue<A>>;
45
+ declare function onAccess<S extends MaybeAccessor<unknown>[] | [], Next, Init = unknown>(deps: S, fn: OnAccessEffectFunction<S, Init | Next, Next>, options?: OnOptions): EffectFunction<NoInfer<Init> | NoInfer<Next>, NoInfer<Next>>;
46
+ /**
47
+ * Quickly iterate over an MaybeAccessor<any>
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const myFunc = (source: MaybeAccessor<string[]>) => {
52
+ * forEach(source, item => console.log(item))
53
+ * }
54
+ * ```
55
+ */
61
56
  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
+ /**
58
+ * Get `Object.entries()` of an MaybeAccessor<Object>
59
+ */
62
60
  declare const entries: <A extends MaybeAccessor<Object>, V = MaybeAccessorValue<A>>(object: A) => [string, Values<V>][];
61
+ /**
62
+ * Creates a promise that resolves *(or rejects)* after gives time.
63
+ *
64
+ * @param ms timeout duration in ms
65
+ * @param throwOnTimeout promise will be rejected on timeout if set to `true`
66
+ * @param reason rejection reason
67
+ * @returns Promise<void>
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * await promiseTimeout(1500) // will resolve void after timeout
72
+ * await promiseTimeout(1500, true, 'rejection reason') // will reject 'rejection reason' after timout
73
+ * ```
74
+ */
63
75
  declare const promiseTimeout: (ms: number, throwOnTimeout?: boolean, reason?: string) => Promise<void>;
64
- declare const objectOmit: <T extends Object, K extends (keyof T)[]>(object: T, ...keys: K) => Omit<T, ItemsOf<K>>;
65
- declare type Destore<T extends Object> = {
66
- [K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
67
- };
76
+ /**
77
+ * Combination of `Promise.race()` and `promiseTimeout`.
78
+ *
79
+ * @param promises single promise, or array of promises
80
+ * @param ms timeout duration in ms
81
+ * @param throwOnTimeout promise will be rejected on timeout if set to `true`
82
+ * @param reason rejection reason
83
+ * @returns a promise resulting in value of the first source promises to be resolved
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * // single promise
88
+ * await raceTimeout(new Promise(() => {...}), 3000)
89
+ * // list of promises racing
90
+ * await raceTimeout([new Promise(),new Promise()...], 3000)
91
+ * // reject on timeout
92
+ * await raceTimeout(new Promise(), 3000, true, 'rejection reason')
93
+ * ```
94
+ */
95
+ declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout: true, reason?: string): T extends any[] ? Promise<Awaited<T[number]>> : Promise<Awaited<T>>;
96
+ declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout?: boolean, reason?: string): T extends any[] ? Promise<Awaited<T[number]> | undefined> : Promise<Awaited<T> | undefined>;
68
97
  /**
69
98
  * Allows the Solid's store to be destructured
70
99
  *
@@ -72,6 +101,7 @@ declare type Destore<T extends Object> = {
72
101
  * @returns Destructible object, with values changed to accessors
73
102
  *
74
103
  * @example
104
+ * ```ts
75
105
  * const [state, setState] = createStore({
76
106
  * count: 0,
77
107
  * get double() { return this.count * 2 },
@@ -79,17 +109,44 @@ declare type Destore<T extends Object> = {
79
109
  * const { count, double } = destore(state)
80
110
  * // use it like a signal:
81
111
  * count()
112
+ * ```
82
113
  */
83
114
  declare function destore<T extends Object>(store: Store<T>): Destore<T>;
84
- declare const createCallbackStack: <Arg0 = void, Arg1 = void, Arg2 = void, Arg3 = void>() => {
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
+ declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
85
147
  push: (...callbacks: Fn[]) => void;
86
- execute: (arg0: Arg0, arg1: Arg1, arg2: Arg2, arg3: Arg3) => void;
148
+ execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
87
149
  clear: Fn;
88
150
  };
89
- declare const stringConcat: (...a: MaybeAccessor<any>[]) => string;
90
- declare const concat: <A extends any[], V = MaybeAccessorValue<ItemsOf<A>>>(...a: A) => (V extends any[] ? ItemsOf<V> : V)[];
91
- declare const toFloat: (string: MaybeAccessor<string>) => number;
92
- declare const toInt: (string: MaybeAccessor<string>, radix?: number | undefined) => number;
93
- declare const toArray: <A extends any[]>(...a: A) => MaybeAccessorValue<ItemsOf<A>>[];
94
151
 
95
- export { Destore, Fn, ItemsOf, Keys, Many, MaybeAccessor, MaybeAccessorValue, Values, access, accessAsArray, concat, createCallbackStack, destore, entries, forEach, isClient, isServer, objectOmit, promiseTimeout, stringConcat, toArray, toFloat, toInt, withAccess };
152
+ export { access, accessAsArray, asAccessor, asArray, createCallbackStack, createSubRoot, createSubRootFunction, destore, entries, forEach, isClient, isDefined, isFunction, noop, onAccess, onRootCleanup, promiseTimeout, raceTimeout, withAccess };
package/dist/index.js CHANGED
@@ -1,34 +1,50 @@
1
1
  // src/index.ts
2
- var isClient = typeof window !== "undefined";
3
- var isServer = !isClient;
4
- var access = (v) => typeof v === "function" ? v() : v;
5
- var accessAsArray = (value) => {
6
- const _value = access(value);
7
- return Array.isArray(_value) ? _value : [_value];
8
- };
2
+ import { createRoot, getOwner, onCleanup, runWithOwner, on } from "solid-js";
3
+ import { isServer } from "solid-js/web";
4
+ var noop = () => void 0;
5
+ var isClient = !isServer;
6
+ var isDefined = (value) => typeof value !== "undefined" && value !== null;
7
+ var isFunction = (value) => typeof value === "function";
8
+ var access = (v) => isFunction(v) ? v() : v;
9
+ var accessAsArray = (value) => asArray(access(value));
10
+ var asArray = (value) => Array.isArray(value) ? value : [value];
9
11
  var withAccess = (value, fn) => {
10
12
  const _value = access(value);
11
- if (typeof _value !== "undefined" && _value !== null)
12
- fn(_value);
13
+ isDefined(_value) && fn(_value);
13
14
  };
15
+ var asAccessor = (v) => isFunction(v) ? v : () => v;
16
+ function onAccess(deps, fn, options) {
17
+ const source = deps.map(asAccessor);
18
+ return on(source, fn, options);
19
+ }
14
20
  var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
15
21
  var entries = (object) => Object.entries(access(object));
16
22
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
17
- var objectOmit = (object, ...keys) => {
18
- const copy = Object.assign({}, object);
19
- for (const key of keys) {
20
- delete copy[key];
21
- }
22
- return copy;
23
- };
23
+ function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
24
+ const promises = asArray(input);
25
+ const race = Promise.race([...promises, promiseTimeout(ms, throwOnTimeout, reason)]);
26
+ race.finally(() => {
27
+ promises.forEach((p) => p && typeof p === "object" && typeof p.dispose === "function" && p.dispose());
28
+ });
29
+ return race;
30
+ }
24
31
  function destore(store) {
25
32
  const _store = store;
26
33
  const result = {};
27
34
  Object.keys(_store).forEach((key) => {
28
- result[key] = typeof _store[key] === "function" ? _store[key].bind(_store) : () => _store[key];
35
+ result[key] = isFunction(_store[key]) ? _store[key].bind(_store) : () => _store[key];
29
36
  });
30
37
  return result;
31
38
  }
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
+ }
32
48
  var createCallbackStack = () => {
33
49
  let stack = [];
34
50
  const clear = () => stack = [];
@@ -41,29 +57,25 @@ var createCallbackStack = () => {
41
57
  clear
42
58
  };
43
59
  };
44
- var stringConcat = (...a) => a.reduce((t, c) => t + access(c), "");
45
- var concat = (...a) => a.reduce((t, c) => {
46
- const v = access(c);
47
- return Array.isArray(v) ? [...t, ...v] : [...t, v];
48
- }, []);
49
- var toFloat = (string) => Number.parseFloat(access(string));
50
- var toInt = (string, radix) => Number.parseInt(access(string), radix);
51
- var toArray = (...a) => a.map((v) => access(v));
52
60
  export {
53
61
  access,
54
62
  accessAsArray,
55
- concat,
63
+ asAccessor,
64
+ asArray,
56
65
  createCallbackStack,
66
+ createSubRoot,
67
+ createSubRootFunction,
57
68
  destore,
58
69
  entries,
59
70
  forEach,
60
71
  isClient,
72
+ isDefined,
73
+ isFunction,
61
74
  isServer,
62
- objectOmit,
75
+ noop,
76
+ onAccess,
77
+ onRootCleanup,
63
78
  promiseTimeout,
64
- stringConcat,
65
- toArray,
66
- toFloat,
67
- toInt,
79
+ raceTimeout,
68
80
  withAccess
69
81
  };
@@ -0,0 +1,79 @@
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
+ remove: () => remove2,
33
+ slice: () => slice,
34
+ splice: () => splice2
35
+ });
36
+
37
+ // src/fp.ts
38
+ var withArrayCopy = (array, mutator) => {
39
+ const copy = array.slice();
40
+ mutator(copy);
41
+ return copy;
42
+ };
43
+ var push = (list, item) => withArrayCopy(list, (list2) => list2.push(item));
44
+ var drop = (list, n = 1) => list.slice(n);
45
+ var filterOut = (list, item) => filter(list, (i) => i !== item);
46
+ function filter(list, predicate) {
47
+ const newList = list.filter(predicate);
48
+ newList.removed = list.length - newList.length;
49
+ return newList;
50
+ }
51
+ var splice = (list, start, deleteCount, ...items) => withArrayCopy(list, (list2) => list2.splice(start, deleteCount, ...items));
52
+ var remove = (list, item) => {
53
+ const index = list.indexOf(item);
54
+ return splice(list, index, 1);
55
+ };
56
+
57
+ // src/setter.ts
58
+ var push2 = (item) => (list) => push(list, item);
59
+ var drop2 = (n = 1) => (list) => drop(list, n);
60
+ var filterOut2 = (item) => (list) => filterOut(list, item);
61
+ var filter2 = (predicate) => (list) => filter(list, predicate);
62
+ var map = (mapFn) => (list) => list.map(mapFn);
63
+ var slice = (start, end) => (list) => list.slice(start, end);
64
+ function splice2(start, deleteCount, ...items) {
65
+ return (list) => splice(list, start, deleteCount, ...items);
66
+ }
67
+ var remove2 = (item) => (list) => remove(list, item);
68
+ module.exports = __toCommonJS(setter_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
+ push,
76
+ remove,
77
+ slice,
78
+ splice
79
+ });
@@ -0,0 +1,18 @@
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
+ declare const remove: <T>(item: T) => (list: T[]) => T[];
17
+
18
+ export { drop, filter, filterOut, map, push, remove, slice, splice };
package/dist/setter.js ADDED
@@ -0,0 +1,30 @@
1
+ import {
2
+ drop,
3
+ filter,
4
+ filterOut,
5
+ push,
6
+ remove,
7
+ splice
8
+ } from "./chunk-BVHCDFCW.js";
9
+
10
+ // src/setter.ts
11
+ var push2 = (item) => (list) => push(list, item);
12
+ var drop2 = (n = 1) => (list) => drop(list, n);
13
+ var filterOut2 = (item) => (list) => filterOut(list, item);
14
+ var filter2 = (predicate) => (list) => filter(list, predicate);
15
+ var map = (mapFn) => (list) => list.map(mapFn);
16
+ var slice = (start, end) => (list) => list.slice(start, end);
17
+ function splice2(start, deleteCount, ...items) {
18
+ return (list) => splice(list, start, deleteCount, ...items);
19
+ }
20
+ var remove2 = (item) => (list) => remove(list, item);
21
+ export {
22
+ drop2 as drop,
23
+ filter2 as filter,
24
+ filterOut2 as filterOut,
25
+ map,
26
+ push2 as push,
27
+ remove2 as remove,
28
+ slice,
29
+ splice2 as splice
30
+ };
@@ -0,0 +1,64 @@
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "0.0.260",
3
+ "version": "0.1.3",
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",
@@ -18,10 +18,38 @@
18
18
  "files": [
19
19
  "dist"
20
20
  ],
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
+ },
21
48
  "scripts": {
22
49
  "build": "tsup",
23
50
  "test": "uvu -r solid-register",
24
- "watch-test": "watchlist src test -- npm test"
51
+ "watch-test": "watchlist src test -- npm test",
52
+ "docs": "typedoc"
25
53
  },
26
54
  "keywords": [
27
55
  "utilities",
@@ -30,15 +58,17 @@
30
58
  "primitives"
31
59
  ],
32
60
  "devDependencies": {
33
- "jsdom": "^19.0.0",
61
+ "jsdom": "18.1.1",
34
62
  "prettier": "^2.5.1",
35
- "solid-register": "^0.0.18",
63
+ "solid-register": "^0.1.1",
36
64
  "tslib": "^2.3.1",
37
65
  "tsup": "^5.11.0",
66
+ "typedoc": "^0.22.10",
67
+ "typedoc-plugin-markdown": "^3.11.8",
38
68
  "uvu": "^0.5.2",
39
69
  "watchlist": "^0.3.1"
40
70
  },
41
71
  "peerDependencies": {
42
- "solid-js": "^1.2.6"
72
+ "solid-js": "^1.3.1"
43
73
  }
44
- }
74
+ }