@solid-primitives/utils 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,26 +10,6 @@ npm install @solid-primitives/utils
10
10
  yarn add @solid-primitives/utils
11
11
  ```
12
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
-
33
13
  ## Changelog
34
14
 
35
15
  <details>
package/dist/index.cjs CHANGED
@@ -31,40 +31,69 @@ __export(src_exports, {
31
31
  asArray: () => asArray,
32
32
  compare: () => compare,
33
33
  createCallbackStack: () => createCallbackStack,
34
+ createMicrotask: () => createMicrotask,
35
+ createProxy: () => createProxy,
36
+ definite: () => definite,
34
37
  destore: () => destore,
35
38
  entries: () => entries,
36
39
  forEach: () => forEach,
40
+ forEachEntry: () => forEachEntry,
41
+ includes: () => includes,
37
42
  isArray: () => isArray,
38
43
  isBoolean: () => isBoolean,
39
44
  isClient: () => isClient,
40
45
  isDefined: () => isDefined,
46
+ isDev: () => isDev,
41
47
  isFunction: () => isFunction,
42
48
  isNumber: () => isNumber,
43
49
  isObject: () => isObject,
50
+ isProd: () => isProd,
44
51
  isServer: () => import_web.isServer,
45
52
  isString: () => isString,
46
53
  keys: () => keys,
47
54
  noop: () => noop,
55
+ ofClass: () => ofClass,
48
56
  onAccess: () => onAccess,
49
57
  onRootCleanup: () => onRootCleanup,
50
58
  promiseTimeout: () => promiseTimeout,
51
59
  raceTimeout: () => raceTimeout,
52
60
  tuple: () => tuple,
53
- withAccess: () => withAccess
61
+ warn: () => warn,
62
+ withAccess: () => withAccess,
63
+ withFallback: () => withFallback,
64
+ withFallbackFn: () => withFallbackFn
54
65
  });
55
66
  var import_solid_js = require("solid-js");
56
67
  var import_web = require("solid-js/web");
57
68
  var noop = () => void 0;
58
69
  var isClient = !import_web.isServer;
70
+ var isDev = import_solid_js.DEV && import_solid_js.DEV.hasOwnProperty("writeSignal");
71
+ var isProd = !isDev;
72
+ var warn = (...a) => isDev && console.warn(...a);
59
73
  var isDefined = (value) => typeof value !== "undefined" && value !== null;
60
74
  var isFunction = (value) => typeof value === "function";
61
75
  var isBoolean = (val) => typeof val === "boolean";
62
76
  var isNumber = (val) => typeof val === "number";
63
77
  var isString = (val) => typeof val === "string";
64
78
  var isObject = (val) => toString.call(val) === "[object Object]";
65
- var isArray = (val) => Array.isArray(val);
79
+ var isArray = Array.isArray;
80
+ var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
66
81
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
67
82
  var tuple = (input) => input;
83
+ var definite = (v) => v;
84
+ function withFallback(v, fallback) {
85
+ return isDefined(v) ? v : fallback;
86
+ }
87
+ function withFallbackFn(v, fallback) {
88
+ return isDefined(v) ? v : fallback();
89
+ }
90
+ var includes = (arr, ...items) => {
91
+ for (const item of arr) {
92
+ if (items.includes(item))
93
+ return true;
94
+ }
95
+ return false;
96
+ };
68
97
  var access = (v) => isFunction(v) ? v() : v;
69
98
  var accessAsArray = (value) => asArray(access(value));
70
99
  var asArray = (value) => Array.isArray(value) ? value : [value];
@@ -79,6 +108,10 @@ function onAccess(deps, fn, options) {
79
108
  return (0, import_solid_js.on)(source, fn, options);
80
109
  }
81
110
  var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
111
+ var forEachEntry = (object, iterator) => {
112
+ const obj = access(object);
113
+ Object.entries(obj).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, obj));
114
+ };
82
115
  var entries = (object) => Object.entries(access(object));
83
116
  var keys = (object) => Object.keys(access(object));
84
117
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
@@ -104,13 +137,31 @@ var createCallbackStack = () => {
104
137
  const clear = () => stack = [];
105
138
  return {
106
139
  push: (...callbacks) => stack.push(...callbacks),
107
- execute: (arg0, arg1, arg2, arg3) => {
140
+ execute(arg0, arg1, arg2, arg3) {
108
141
  stack.forEach((cb) => cb(arg0, arg1, arg2, arg3));
109
142
  clear();
110
143
  },
111
144
  clear
112
145
  };
113
146
  };
147
+ function createMicrotask(fn) {
148
+ let calls = 0;
149
+ let args;
150
+ return (...a) => {
151
+ args = a, calls++;
152
+ queueMicrotask(() => --calls === 0 && fn(...args));
153
+ };
154
+ }
155
+ function createProxy(traps) {
156
+ return new Proxy({}, {
157
+ get: (_, k) => traps.get(k),
158
+ set: (_, k, v) => {
159
+ var _a;
160
+ (_a = traps.set) == null ? void 0 : _a.call(traps, k, v);
161
+ return false;
162
+ }
163
+ });
164
+ }
114
165
  module.exports = __toCommonJS(src_exports);
115
166
  // Annotate the CommonJS export names for ESM import in node:
116
167
  0 && (module.exports = {
@@ -121,24 +172,35 @@ module.exports = __toCommonJS(src_exports);
121
172
  asArray,
122
173
  compare,
123
174
  createCallbackStack,
175
+ createMicrotask,
176
+ createProxy,
177
+ definite,
124
178
  destore,
125
179
  entries,
126
180
  forEach,
181
+ forEachEntry,
182
+ includes,
127
183
  isArray,
128
184
  isBoolean,
129
185
  isClient,
130
186
  isDefined,
187
+ isDev,
131
188
  isFunction,
132
189
  isNumber,
133
190
  isObject,
191
+ isProd,
134
192
  isServer,
135
193
  isString,
136
194
  keys,
137
195
  noop,
196
+ ofClass,
138
197
  onAccess,
139
198
  onRootCleanup,
140
199
  promiseTimeout,
141
200
  raceTimeout,
142
201
  tuple,
143
- withAccess
202
+ warn,
203
+ withAccess,
204
+ withFallback,
205
+ withFallbackFn
144
206
  });
package/dist/index.d.ts CHANGED
@@ -7,6 +7,8 @@ export { isServer } from 'solid-js/web';
7
7
  * A function
8
8
  */
9
9
  declare type Fn<R = void> = () => R;
10
+ declare type Get<T> = (v: T) => void;
11
+ declare type Clear = () => void;
10
12
  /**
11
13
  * Can be single or in an array
12
14
  */
@@ -14,10 +16,12 @@ declare type Many<T> = T | T[];
14
16
  declare type Keys<O extends Object> = keyof O;
15
17
  declare type Values<O extends Object> = O[Keys<O>];
16
18
  declare type Noop = (...a: any[]) => void;
19
+ declare type Directive<P = true> = (el: Element, props: Accessor<P>) => void;
17
20
  /**
18
21
  * Infers the type of the array elements
19
22
  */
20
23
  declare type ItemsOf<T> = T extends (infer E)[] ? E : never;
24
+ declare type ItemsOfMany<T> = T extends any[] ? ItemsOf<T> : T;
21
25
  /**
22
26
  * T or a reactive/non-reactive function returning T
23
27
  */
@@ -49,23 +53,39 @@ declare type ModifyDeep<A extends AnyObject, B extends DeepPartialAny<A>> = {
49
53
  declare type DeepPartialAny<T> = {
50
54
  [P in keyof T]?: T[P] extends AnyObject ? DeepPartialAny<T[P]> : any;
51
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>;
52
59
  declare type AnyObject = Record<string, any>;
53
60
  declare type AnyFunction = (...args: any[]) => any;
61
+ declare type AnyClass = abstract new (...args: any) => any;
62
+ declare type LiteralKey = string | number | symbol;
54
63
  declare type PrimitiveValue = string | boolean | number | bigint | symbol | null | undefined;
55
64
  declare type FalsyValue = false | 0 | "" | null | undefined;
56
- declare type Truthy<T> = T extends FalsyValue ? never : T;
57
- declare type Falsy<T> = T extends FalsyValue ? T : never;
65
+ declare type Truthy<T> = Exclude<T, FalsyValue>;
66
+ declare type Falsy<T> = Extract<T, FalsyValue>;
67
+ declare type Fallback<T, F = NonNullable<T>> = NonNullable<T> | F;
58
68
  /**
59
69
  * Destructible store object, with values changed to accessors
60
70
  */
61
71
  declare type Destore<T extends Object> = {
62
72
  [K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
63
73
  };
74
+ declare type Position = {
75
+ x: number;
76
+ y: number;
77
+ };
64
78
 
65
79
  /** no operation */
66
80
  declare const noop: Noop;
67
81
  declare const isClient = true;
68
82
 
83
+ /** development environment */
84
+ declare const isDev: boolean;
85
+ /** production environment */
86
+ declare const isProd: boolean;
87
+ /** `console.warn` only during development */
88
+ declare const warn: typeof console.warn;
69
89
  /**
70
90
  * `if (typeof value !== "undefined" && value !== null)`
71
91
  */
@@ -76,14 +96,39 @@ declare const isNumber: (val: any) => val is number;
76
96
  declare const isString: (val: unknown) => val is string;
77
97
  declare const isObject: (val: any) => val is object;
78
98
  declare const isArray: (val: any) => val is any[];
99
+ /**
100
+ * Check if the value is an instance of ___
101
+ */
102
+ declare const ofClass: (v: any, c: AnyClass) => boolean;
79
103
  declare const compare: (a: any, b: any) => number;
80
104
  /**
81
105
  * for creating tuples by inferring type
82
106
  * @example
83
107
  * const users = tuple(["John", "Jeff", "Joe"]);
84
- * // users: [string, string, string]
108
+ * users // T: [string, string, string]
85
109
  */
86
110
  declare const tuple: <T extends any[] | []>(input: T) => T;
111
+ /**
112
+ * Removes the `null` and `undefined` from the type.
113
+ * @warning **Obviously use with caution**
114
+ */
115
+ declare const definite: <T>(v: T) => NonNullable<T>;
116
+ /**
117
+ * Get the value if it is defined, or get the fallback otherwise
118
+ * @param v the value to check
119
+ * @param fallback the fallback value
120
+ */
121
+ declare function withFallback<T>(v: T, fallback: NonNullable<T>): Fallback<T>;
122
+ declare function withFallback<T, F>(v: T, fallback: F): Fallback<T, F>;
123
+ /**
124
+ * Get the value if it is defined, or get the fallback otherwise
125
+ * @param v the value to check
126
+ * @param fallback function returning a fallback value
127
+ */
128
+ declare function withFallbackFn<T>(v: T, fallback: () => NonNullable<T>): Fallback<T>;
129
+ declare function withFallbackFn<T, F>(v: T, fallback: () => F): Fallback<T, F>;
130
+ /** `Array.prototype.includes()` without so strict types. Also allows for checking for multiple items */
131
+ declare const includes: (arr: any[], ...items: any) => boolean;
87
132
  /**
88
133
  * Accesses the value of a MaybeAccessor
89
134
  * @example
@@ -132,9 +177,13 @@ declare function onAccess<S extends MaybeAccessor<unknown>[] | [], Next, Init =
132
177
  */
133
178
  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;
134
179
  /**
135
- * Get `Object.entries()` of an MaybeAccessor<Object>
180
+ * Iterate through object entries.
181
+ */
182
+ 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;
183
+ /**
184
+ * Get `Object.entries()` of an MaybeAccessor<object>
136
185
  */
137
- declare const entries: <A extends MaybeAccessor<object>, V = MaybeAccessorValue<A>>(object: A) => [string, Values<V>][];
186
+ declare const entries: <A extends MaybeAccessor<object>, O = MaybeAccessorValue<A>>(object: A) => [keyof O, Values<O>][];
138
187
  /**
139
188
  * Get `Object.keys()` of an MaybeAccessor<Object>
140
189
  */
@@ -198,9 +247,24 @@ declare const onRootCleanup: typeof onCleanup;
198
247
  */
199
248
  declare function destore<T extends Object>(store: Store<T>): Destore<T>;
200
249
  declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
201
- push: (...callbacks: Fn[]) => void;
250
+ push: (...callbacks: ((arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void)[]) => void;
202
251
  execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
203
252
  clear: Fn;
204
253
  };
254
+ /**
255
+ * Group synchronous function calls.
256
+ * @param fn
257
+ * @returns `fn`
258
+ */
259
+ declare function createMicrotask<A extends any[] | []>(fn: (...a: A) => void): (...a: A) => void;
260
+ /** WIP: an easier to setup and type Proxy */
261
+ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
262
+ get: <K extends keyof T>(key: K) => T[K];
263
+ set: <K extends keyof T>(key: K, value: T[K]) => void;
264
+ }): T;
265
+ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
266
+ get: <K extends keyof T>(key: K) => T[K];
267
+ set?: undefined;
268
+ }): Readonly<T>;
205
269
 
206
- export { AccessReturnTypes, AnyFunction, AnyObject, DeepPartialAny, Destore, Falsy, FalsyValue, Fn, ItemsOf, Keys, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Noop, OnAccessEffectFunction, PrimitiveValue, Truthy, Values, access, accessArray, accessAsArray, asAccessor, asArray, compare, createCallbackStack, destore, entries, forEach, isArray, isBoolean, isClient, isDefined, isFunction, isNumber, isObject, isString, keys, noop, onAccess, onRootCleanup, promiseTimeout, raceTimeout, tuple, withAccess };
270
+ export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, Clear, DeepPartialAny, Destore, Directive, ExtractIfPossible, Fallback, 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, createMicrotask, createProxy, definite, 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, withFallback, withFallbackFn };
package/dist/index.js CHANGED
@@ -1,17 +1,35 @@
1
1
  // src/index.ts
2
- import { getOwner, onCleanup, 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";
8
11
  var isBoolean = (val) => typeof val === "boolean";
9
12
  var isNumber = (val) => typeof val === "number";
10
13
  var isString = (val) => typeof val === "string";
11
14
  var isObject = (val) => toString.call(val) === "[object Object]";
12
- var isArray = (val) => Array.isArray(val);
15
+ var isArray = Array.isArray;
16
+ var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
13
17
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
14
18
  var tuple = (input) => input;
19
+ var definite = (v) => v;
20
+ function withFallback(v, fallback) {
21
+ return isDefined(v) ? v : fallback;
22
+ }
23
+ function withFallbackFn(v, fallback) {
24
+ return isDefined(v) ? v : fallback();
25
+ }
26
+ var includes = (arr, ...items) => {
27
+ for (const item of arr) {
28
+ if (items.includes(item))
29
+ return true;
30
+ }
31
+ return false;
32
+ };
15
33
  var access = (v) => isFunction(v) ? v() : v;
16
34
  var accessAsArray = (value) => asArray(access(value));
17
35
  var asArray = (value) => Array.isArray(value) ? value : [value];
@@ -26,6 +44,10 @@ function onAccess(deps, fn, options) {
26
44
  return on(source, fn, options);
27
45
  }
28
46
  var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
47
+ var forEachEntry = (object, iterator) => {
48
+ const obj = access(object);
49
+ Object.entries(obj).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, obj));
50
+ };
29
51
  var entries = (object) => Object.entries(access(object));
30
52
  var keys = (object) => Object.keys(access(object));
31
53
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
@@ -51,13 +73,31 @@ var createCallbackStack = () => {
51
73
  const clear = () => stack = [];
52
74
  return {
53
75
  push: (...callbacks) => stack.push(...callbacks),
54
- execute: (arg0, arg1, arg2, arg3) => {
76
+ execute(arg0, arg1, arg2, arg3) {
55
77
  stack.forEach((cb) => cb(arg0, arg1, arg2, arg3));
56
78
  clear();
57
79
  },
58
80
  clear
59
81
  };
60
82
  };
83
+ function createMicrotask(fn) {
84
+ let calls = 0;
85
+ let args;
86
+ return (...a) => {
87
+ args = a, calls++;
88
+ queueMicrotask(() => --calls === 0 && fn(...args));
89
+ };
90
+ }
91
+ function createProxy(traps) {
92
+ return new Proxy({}, {
93
+ get: (_, k) => traps.get(k),
94
+ set: (_, k, v) => {
95
+ var _a;
96
+ (_a = traps.set) == null ? void 0 : _a.call(traps, k, v);
97
+ return false;
98
+ }
99
+ });
100
+ }
61
101
  export {
62
102
  access,
63
103
  accessArray,
@@ -66,24 +106,35 @@ export {
66
106
  asArray,
67
107
  compare,
68
108
  createCallbackStack,
109
+ createMicrotask,
110
+ createProxy,
111
+ definite,
69
112
  destore,
70
113
  entries,
71
114
  forEach,
115
+ forEachEntry,
116
+ includes,
72
117
  isArray,
73
118
  isBoolean,
74
119
  isClient,
75
120
  isDefined,
121
+ isDev,
76
122
  isFunction,
77
123
  isNumber,
78
124
  isObject,
125
+ isProd,
79
126
  isServer,
80
127
  isString,
81
128
  keys,
82
129
  noop,
130
+ ofClass,
83
131
  onAccess,
84
132
  onRootCleanup,
85
133
  promiseTimeout,
86
134
  raceTimeout,
87
135
  tuple,
88
- withAccess
136
+ warn,
137
+ withAccess,
138
+ withFallback,
139
+ withFallbackFn
89
140
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "A bunch of reactive utility types and functions, for building primitives with Solid.js",
5
5
  "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>",
6
6
  "license": "MIT",
@@ -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",
@@ -21,8 +24,7 @@
21
24
  "scripts": {
22
25
  "build": "tsup",
23
26
  "test": "uvu -r solid-register",
24
- "watch-test": "watchlist src test -- npm test",
25
- "docs": "typedoc"
27
+ "watch-test": "watchlist src test -- npm test"
26
28
  },
27
29
  "keywords": [
28
30
  "utilities",
@@ -36,12 +38,10 @@
36
38
  "solid-register": "^0.1.1",
37
39
  "tslib": "^2.3.1",
38
40
  "tsup": "^5.11.0",
39
- "typedoc": "^0.22.10",
40
- "typedoc-plugin-markdown": "^3.11.8",
41
41
  "uvu": "^0.5.2",
42
42
  "watchlist": "^0.3.1"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "solid-js": "^1.3.1"
46
46
  }
47
- }
47
+ }