@solid-primitives/utils 0.0.255 → 0.0.260

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
@@ -1,6 +1,6 @@
1
1
  # @solid-primitives/utils
2
2
 
3
- Solid Primitives Utilities is a support and helper package for a number of primitives in our library. Please free to augment or centralise useful utilities and methods in this package for sharing.
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
5
  ## Changelog
6
6
 
@@ -15,4 +15,8 @@ First commit of the timer primitive.
15
15
 
16
16
  Republished version with better ESM support and build tooling.
17
17
 
18
+ 0.0.260
19
+
20
+ Added comments for util methods.
21
+
18
22
  </details>
package/dist/index.cjs CHANGED
@@ -12,9 +12,11 @@ __export(exports, {
12
12
  accessAsArray: () => accessAsArray,
13
13
  concat: () => concat,
14
14
  createCallbackStack: () => createCallbackStack,
15
+ destore: () => destore,
15
16
  entries: () => entries,
16
17
  forEach: () => forEach,
17
18
  isClient: () => isClient,
19
+ isServer: () => isServer,
18
20
  objectOmit: () => objectOmit,
19
21
  promiseTimeout: () => promiseTimeout,
20
22
  stringConcat: () => stringConcat,
@@ -24,6 +26,7 @@ __export(exports, {
24
26
  withAccess: () => withAccess
25
27
  });
26
28
  var isClient = typeof window !== "undefined";
29
+ var isServer = !isClient;
27
30
  var access = (v) => typeof v === "function" ? v() : v;
28
31
  var accessAsArray = (value) => {
29
32
  const _value = access(value);
@@ -44,6 +47,14 @@ var objectOmit = (object, ...keys) => {
44
47
  }
45
48
  return copy;
46
49
  };
50
+ function destore(store) {
51
+ const _store = store;
52
+ const result = {};
53
+ Object.keys(_store).forEach((key) => {
54
+ result[key] = typeof _store[key] === "function" ? _store[key].bind(_store) : () => _store[key];
55
+ });
56
+ return result;
57
+ }
47
58
  var createCallbackStack = () => {
48
59
  let stack = [];
49
60
  const clear = () => stack = [];
@@ -70,9 +81,11 @@ var toArray = (...a) => a.map((v) => access(v));
70
81
  accessAsArray,
71
82
  concat,
72
83
  createCallbackStack,
84
+ destore,
73
85
  entries,
74
86
  forEach,
75
87
  isClient,
88
+ isServer,
76
89
  objectOmit,
77
90
  promiseTimeout,
78
91
  stringConcat,
package/dist/index.d.ts CHANGED
@@ -1,23 +1,86 @@
1
1
  import { Accessor } from 'solid-js';
2
+ import { Store } from 'solid-js/store';
2
3
 
4
+ /**
5
+ * A function
6
+ */
3
7
  declare type Fn<R = void> = () => R;
8
+ /**
9
+ * Can be single or in an array
10
+ */
4
11
  declare type Many<T> = T | T[];
5
12
  declare type Keys<O extends Object> = keyof O;
6
13
  declare type Values<O extends Object> = O[Keys<O>];
7
14
  /**
8
- * Infers the element type of an array
15
+ * Infers the type of the array elements
9
16
  */
10
17
  declare type ItemsOf<T> = T extends (infer E)[] ? E : never;
18
+ /**
19
+ * T or a reactive/non-reactive function returning T
20
+ */
11
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
+ */
12
32
  declare type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends Fn ? ReturnType<T> : T;
13
33
  declare const isClient: boolean;
34
+ declare const isServer: boolean;
35
+ /**
36
+ * Accesses the value of a MaybeAccessor
37
+ * @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
44
+ */
14
45
  declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
46
+ /**
47
+ * Accesses the value of a MaybeAccessor, but always returns an array
48
+ * @example
49
+ * accessAsArray('abc') // => ['abc']
50
+ * accessAsArray(() => 'abc') // => ['abc']
51
+ * accessAsArray([1,2,3]) // => [1,2,3]
52
+ * accessAsArray(() => [1,2,3]) // => [1,2,3]
53
+ */
15
54
  declare const accessAsArray: <T extends unknown, V = MaybeAccessorValue<T>>(value: T) => V extends any[] ? V : V[];
55
+ /**
56
+ * Run the function if the accessed value is not `undefined` nor `null`
57
+ * @param value
58
+ * @param fn
59
+ */
16
60
  declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void;
17
61
  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;
18
62
  declare const entries: <A extends MaybeAccessor<Object>, V = MaybeAccessorValue<A>>(object: A) => [string, Values<V>][];
19
63
  declare const promiseTimeout: (ms: number, throwOnTimeout?: boolean, reason?: string) => Promise<void>;
20
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
+ };
68
+ /**
69
+ * Allows the Solid's store to be destructured
70
+ *
71
+ * @param store
72
+ * @returns Destructible object, with values changed to accessors
73
+ *
74
+ * @example
75
+ * const [state, setState] = createStore({
76
+ * count: 0,
77
+ * get double() { return this.count * 2 },
78
+ * })
79
+ * const { count, double } = destore(state)
80
+ * // use it like a signal:
81
+ * count()
82
+ */
83
+ declare function destore<T extends Object>(store: Store<T>): Destore<T>;
21
84
  declare const createCallbackStack: <Arg0 = void, Arg1 = void, Arg2 = void, Arg3 = void>() => {
22
85
  push: (...callbacks: Fn[]) => void;
23
86
  execute: (arg0: Arg0, arg1: Arg1, arg2: Arg2, arg3: Arg3) => void;
@@ -29,4 +92,4 @@ declare const toFloat: (string: MaybeAccessor<string>) => number;
29
92
  declare const toInt: (string: MaybeAccessor<string>, radix?: number | undefined) => number;
30
93
  declare const toArray: <A extends any[]>(...a: A) => MaybeAccessorValue<ItemsOf<A>>[];
31
94
 
32
- export { Fn, ItemsOf, Keys, Many, MaybeAccessor, MaybeAccessorValue, Values, access, accessAsArray, concat, createCallbackStack, entries, forEach, isClient, objectOmit, promiseTimeout, stringConcat, toArray, toFloat, toInt, withAccess };
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 };
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/index.ts
2
2
  var isClient = typeof window !== "undefined";
3
+ var isServer = !isClient;
3
4
  var access = (v) => typeof v === "function" ? v() : v;
4
5
  var accessAsArray = (value) => {
5
6
  const _value = access(value);
@@ -20,6 +21,14 @@ var objectOmit = (object, ...keys) => {
20
21
  }
21
22
  return copy;
22
23
  };
24
+ function destore(store) {
25
+ const _store = store;
26
+ const result = {};
27
+ Object.keys(_store).forEach((key) => {
28
+ result[key] = typeof _store[key] === "function" ? _store[key].bind(_store) : () => _store[key];
29
+ });
30
+ return result;
31
+ }
23
32
  var createCallbackStack = () => {
24
33
  let stack = [];
25
34
  const clear = () => stack = [];
@@ -45,9 +54,11 @@ export {
45
54
  accessAsArray,
46
55
  concat,
47
56
  createCallbackStack,
57
+ destore,
48
58
  entries,
49
59
  forEach,
50
60
  isClient,
61
+ isServer,
51
62
  objectOmit,
52
63
  promiseTimeout,
53
64
  stringConcat,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "0.0.255",
3
+ "version": "0.0.260",
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",
@@ -19,7 +19,9 @@
19
19
  "dist"
20
20
  ],
21
21
  "scripts": {
22
- "build": "tsup"
22
+ "build": "tsup",
23
+ "test": "uvu -r solid-register",
24
+ "watch-test": "watchlist src test -- npm test"
23
25
  },
24
26
  "keywords": [
25
27
  "utilities",
@@ -28,14 +30,15 @@
28
30
  "primitives"
29
31
  ],
30
32
  "devDependencies": {
31
- "jsdom": "^18.1.1",
32
- "prettier": "^2.4.1",
33
+ "jsdom": "^19.0.0",
34
+ "prettier": "^2.5.1",
33
35
  "solid-register": "^0.0.18",
34
36
  "tslib": "^2.3.1",
35
- "tsup": "^5.10.1",
36
- "uvu": "^0.5.2"
37
+ "tsup": "^5.11.0",
38
+ "uvu": "^0.5.2",
39
+ "watchlist": "^0.3.1"
37
40
  },
38
41
  "peerDependencies": {
39
- "solid-js": "^1.2.5"
42
+ "solid-js": "^1.2.6"
40
43
  }
41
- }
44
+ }