@solid-primitives/utils 0.1.1 → 0.1.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 +12 -4
- package/dist/fp.d.ts +7 -1
- package/dist/index.cjs +38 -30
- package/dist/index.d.ts +49 -10
- package/dist/index.js +33 -26
- package/dist/setter.d.ts +1 -1
- package/dist/{types-1e8c1610.d.ts → types-5d7b3b37.d.ts} +11 -1
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -12,20 +12,24 @@ yarn add @solid-primitives/utils
|
|
|
12
12
|
|
|
13
13
|
## Package Entries
|
|
14
14
|
|
|
15
|
-
- [`*` - `@solid-primitives/utils`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/
|
|
16
|
-
- [`/fp` - `@solid-primitives/utils/fp`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/
|
|
17
|
-
- [`/setter` - `@solid-primitives/utils/setter`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/
|
|
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
18
|
|
|
19
19
|
Import accordingly.
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
|
-
import { access, promiseTimeout } from "@solid-primitives/utils";
|
|
22
|
+
import { access, promiseTimeout, MaybeAccessor } from "@solid-primitives/utils";
|
|
23
23
|
|
|
24
24
|
// between these two some function names will overlap.
|
|
25
25
|
import { pick, omit, filter } from "@solid-primitives/utils/fp";
|
|
26
26
|
import { push, filter, drop } from "@solid-primitives/utils/setter";
|
|
27
27
|
```
|
|
28
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
|
+
|
|
29
33
|
## Changelog
|
|
30
34
|
|
|
31
35
|
<details>
|
|
@@ -47,4 +51,8 @@ Added comments for util methods.
|
|
|
47
51
|
|
|
48
52
|
Add `/fp` and `/setter` export entries. Add `destore` and `raceTimeout` util. More jsdoc.
|
|
49
53
|
|
|
54
|
+
0.1.2
|
|
55
|
+
|
|
56
|
+
Updated to Solid 1.3
|
|
57
|
+
|
|
50
58
|
</details>
|
package/dist/fp.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as Predicate, c as MappingFn } from './types-
|
|
1
|
+
import { P as Predicate, c as MappingFn } from './types-5d7b3b37';
|
|
2
2
|
import 'solid-js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -32,11 +32,13 @@ declare const push: <T>(list: T[], item: T) => T[];
|
|
|
32
32
|
* @returns changed array copy
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
35
|
+
* ```ts
|
|
35
36
|
* const newList = drop([1,2,3])
|
|
36
37
|
* newList // => [2,3]
|
|
37
38
|
*
|
|
38
39
|
* const newList = drop([1,2,3], 2)
|
|
39
40
|
* newList // => [3]
|
|
41
|
+
* ```
|
|
40
42
|
*/
|
|
41
43
|
declare const drop: <T>(list: T[], n?: number) => T[];
|
|
42
44
|
/**
|
|
@@ -70,16 +72,20 @@ declare const splice: <T>(list: readonly T[], start: number, deleteCount: number
|
|
|
70
72
|
* Create a new subset object without the provided keys
|
|
71
73
|
*
|
|
72
74
|
* @example
|
|
75
|
+
* ```ts
|
|
73
76
|
* const newObject = omit({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
|
|
74
77
|
* newObject // => { c: "baz" }
|
|
78
|
+
* ```
|
|
75
79
|
*/
|
|
76
80
|
declare const omit: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Omit<O, K>;
|
|
77
81
|
/**
|
|
78
82
|
* Create a new subset object with only the provided keys
|
|
79
83
|
*
|
|
80
84
|
* @example
|
|
85
|
+
* ```ts
|
|
81
86
|
* const newObject = pick({ a:"foo", b:"bar", c: "baz" }, 'a', 'b')
|
|
82
87
|
* newObject // => { a:"foo", b:"bar" }
|
|
88
|
+
* ```
|
|
83
89
|
*/
|
|
84
90
|
declare const pick: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Pick<O, K>;
|
|
85
91
|
|
package/dist/index.cjs
CHANGED
|
@@ -26,56 +26,71 @@ var src_exports = {};
|
|
|
26
26
|
__export(src_exports, {
|
|
27
27
|
access: () => access,
|
|
28
28
|
accessAsArray: () => accessAsArray,
|
|
29
|
-
|
|
29
|
+
asAccessor: () => asAccessor,
|
|
30
|
+
asArray: () => asArray,
|
|
30
31
|
createCallbackStack: () => createCallbackStack,
|
|
32
|
+
createSubRoot: () => createSubRoot,
|
|
33
|
+
createSubRootFunction: () => createSubRootFunction,
|
|
31
34
|
destore: () => destore,
|
|
32
35
|
entries: () => entries,
|
|
33
36
|
forEach: () => forEach,
|
|
34
37
|
isClient: () => isClient,
|
|
35
38
|
isDefined: () => isDefined,
|
|
39
|
+
isFunction: () => isFunction,
|
|
36
40
|
isServer: () => import_web.isServer,
|
|
37
41
|
noop: () => noop,
|
|
42
|
+
onAccess: () => onAccess,
|
|
38
43
|
onRootCleanup: () => onRootCleanup,
|
|
39
44
|
promiseTimeout: () => promiseTimeout,
|
|
40
45
|
raceTimeout: () => raceTimeout,
|
|
41
|
-
stringConcat: () => stringConcat,
|
|
42
|
-
toArray: () => toArray,
|
|
43
|
-
toFloat: () => toFloat,
|
|
44
|
-
toInt: () => toInt,
|
|
45
46
|
withAccess: () => withAccess
|
|
46
47
|
});
|
|
47
48
|
var import_solid_js = require("solid-js");
|
|
48
49
|
var import_web = require("solid-js/web");
|
|
49
|
-
var noop = (
|
|
50
|
-
};
|
|
50
|
+
var noop = () => void 0;
|
|
51
51
|
var isClient = !import_web.isServer;
|
|
52
52
|
var isDefined = (value) => typeof value !== "undefined" && value !== null;
|
|
53
|
-
var
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
};
|
|
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];
|
|
58
57
|
var withAccess = (value, fn) => {
|
|
59
58
|
const _value = access(value);
|
|
60
59
|
isDefined(_value) && fn(_value);
|
|
61
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
|
+
}
|
|
62
66
|
var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
|
|
63
67
|
var entries = (object) => Object.entries(access(object));
|
|
64
68
|
var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
|
|
65
|
-
function raceTimeout(
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
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;
|
|
69
76
|
}
|
|
70
77
|
function destore(store) {
|
|
71
78
|
const _store = store;
|
|
72
79
|
const result = {};
|
|
73
80
|
Object.keys(_store).forEach((key) => {
|
|
74
|
-
result[key] =
|
|
81
|
+
result[key] = isFunction(_store[key]) ? _store[key].bind(_store) : () => _store[key];
|
|
75
82
|
});
|
|
76
83
|
return result;
|
|
77
84
|
}
|
|
78
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
|
+
}
|
|
79
94
|
var createCallbackStack = () => {
|
|
80
95
|
let stack = [];
|
|
81
96
|
const clear = () => stack = [];
|
|
@@ -88,34 +103,27 @@ var createCallbackStack = () => {
|
|
|
88
103
|
clear
|
|
89
104
|
};
|
|
90
105
|
};
|
|
91
|
-
var stringConcat = (...a) => a.reduce((t, c) => t + access(c), "");
|
|
92
|
-
var concat = (...a) => a.reduce((t, c) => {
|
|
93
|
-
const v = access(c);
|
|
94
|
-
return Array.isArray(v) ? [...t, ...v] : [...t, v];
|
|
95
|
-
}, []);
|
|
96
|
-
var toFloat = (string) => Number.parseFloat(access(string));
|
|
97
|
-
var toInt = (string, radix) => Number.parseInt(access(string), radix);
|
|
98
|
-
var toArray = (...a) => a.map((v) => access(v));
|
|
99
106
|
module.exports = __toCommonJS(src_exports);
|
|
100
107
|
// Annotate the CommonJS export names for ESM import in node:
|
|
101
108
|
0 && (module.exports = {
|
|
102
109
|
access,
|
|
103
110
|
accessAsArray,
|
|
104
|
-
|
|
111
|
+
asAccessor,
|
|
112
|
+
asArray,
|
|
105
113
|
createCallbackStack,
|
|
114
|
+
createSubRoot,
|
|
115
|
+
createSubRootFunction,
|
|
106
116
|
destore,
|
|
107
117
|
entries,
|
|
108
118
|
forEach,
|
|
109
119
|
isClient,
|
|
110
120
|
isDefined,
|
|
121
|
+
isFunction,
|
|
111
122
|
isServer,
|
|
112
123
|
noop,
|
|
124
|
+
onAccess,
|
|
113
125
|
onRootCleanup,
|
|
114
126
|
promiseTimeout,
|
|
115
127
|
raceTimeout,
|
|
116
|
-
stringConcat,
|
|
117
|
-
toArray,
|
|
118
|
-
toFloat,
|
|
119
|
-
toInt,
|
|
120
128
|
withAccess
|
|
121
129
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,46 +1,57 @@
|
|
|
1
|
-
import { onCleanup } 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';
|
|
3
4
|
export { isServer } from 'solid-js/web';
|
|
4
|
-
import { M as MaybeAccessorValue, a as MaybeAccessor, I as ItemsOf, V as Values, D as Destore, F as Fn } from './types-
|
|
5
|
-
export {
|
|
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';
|
|
6
7
|
|
|
7
8
|
/** no operation */
|
|
8
|
-
declare const noop:
|
|
9
|
+
declare const noop: Noop;
|
|
9
10
|
declare const isClient = true;
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* `if (typeof value !== "undefined" && value !== null)`
|
|
13
14
|
*/
|
|
14
15
|
declare const isDefined: <T>(value: T | null | undefined) => value is T;
|
|
16
|
+
declare const isFunction: <T>(value: Function | T) => value is Function;
|
|
15
17
|
/**
|
|
16
18
|
* Accesses the value of a MaybeAccessor
|
|
17
19
|
* @example
|
|
20
|
+
* ```ts
|
|
18
21
|
* access("foo") // => "foo"
|
|
19
22
|
* access(() => "foo") // => "foo"
|
|
23
|
+
* ```
|
|
20
24
|
*/
|
|
21
25
|
declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
|
|
22
26
|
/**
|
|
23
27
|
* Accesses the value of a MaybeAccessor, but always returns an array
|
|
24
28
|
* @example
|
|
29
|
+
* ```ts
|
|
25
30
|
* accessAsArray('abc') // => ['abc']
|
|
26
31
|
* accessAsArray(() => 'abc') // => ['abc']
|
|
27
32
|
* accessAsArray([1,2,3]) // => [1,2,3]
|
|
28
33
|
* accessAsArray(() => [1,2,3]) // => [1,2,3]
|
|
34
|
+
* ```
|
|
29
35
|
*/
|
|
30
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[];
|
|
31
38
|
/**
|
|
32
39
|
* Run the function if the accessed value is not `undefined` nor `null`
|
|
33
40
|
* @param value
|
|
34
41
|
* @param fn
|
|
35
42
|
*/
|
|
36
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>>;
|
|
37
46
|
/**
|
|
38
47
|
* Quickly iterate over an MaybeAccessor<any>
|
|
39
48
|
*
|
|
40
49
|
* @example
|
|
50
|
+
* ```ts
|
|
41
51
|
* const myFunc = (source: MaybeAccessor<string[]>) => {
|
|
42
52
|
* forEach(source, item => console.log(item))
|
|
43
53
|
* }
|
|
54
|
+
* ```
|
|
44
55
|
*/
|
|
45
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;
|
|
46
57
|
/**
|
|
@@ -56,8 +67,10 @@ declare const entries: <A extends MaybeAccessor<Object>, V = MaybeAccessorValue<
|
|
|
56
67
|
* @returns Promise<void>
|
|
57
68
|
*
|
|
58
69
|
* @example
|
|
70
|
+
* ```ts
|
|
59
71
|
* await promiseTimeout(1500) // will resolve void after timeout
|
|
60
72
|
* await promiseTimeout(1500, true, 'rejection reason') // will reject 'rejection reason' after timout
|
|
73
|
+
* ```
|
|
61
74
|
*/
|
|
62
75
|
declare const promiseTimeout: (ms: number, throwOnTimeout?: boolean, reason?: string) => Promise<void>;
|
|
63
76
|
/**
|
|
@@ -70,12 +83,14 @@ declare const promiseTimeout: (ms: number, throwOnTimeout?: boolean, reason?: st
|
|
|
70
83
|
* @returns a promise resulting in value of the first source promises to be resolved
|
|
71
84
|
*
|
|
72
85
|
* @example
|
|
86
|
+
* ```ts
|
|
73
87
|
* // single promise
|
|
74
88
|
* await raceTimeout(new Promise(() => {...}), 3000)
|
|
75
89
|
* // list of promises racing
|
|
76
90
|
* await raceTimeout([new Promise(),new Promise()...], 3000)
|
|
77
91
|
* // reject on timeout
|
|
78
92
|
* await raceTimeout(new Promise(), 3000, true, 'rejection reason')
|
|
93
|
+
* ```
|
|
79
94
|
*/
|
|
80
95
|
declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout: true, reason?: string): T extends any[] ? Promise<Awaited<T[number]>> : Promise<Awaited<T>>;
|
|
81
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>;
|
|
@@ -86,6 +101,7 @@ declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout?: boolea
|
|
|
86
101
|
* @returns Destructible object, with values changed to accessors
|
|
87
102
|
*
|
|
88
103
|
* @example
|
|
104
|
+
* ```ts
|
|
89
105
|
* const [state, setState] = createStore({
|
|
90
106
|
* count: 0,
|
|
91
107
|
* get double() { return this.count * 2 },
|
|
@@ -93,21 +109,44 @@ declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout?: boolea
|
|
|
93
109
|
* const { count, double } = destore(state)
|
|
94
110
|
* // use it like a signal:
|
|
95
111
|
* count()
|
|
112
|
+
* ```
|
|
96
113
|
*/
|
|
97
114
|
declare function destore<T extends Object>(store: Store<T>): Destore<T>;
|
|
98
115
|
/**
|
|
99
116
|
* Solid's `onCleanup` that runs only if there is a root.
|
|
100
117
|
*/
|
|
101
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;
|
|
102
146
|
declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
|
|
103
147
|
push: (...callbacks: Fn[]) => void;
|
|
104
148
|
execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
|
|
105
149
|
clear: Fn;
|
|
106
150
|
};
|
|
107
|
-
declare const stringConcat: (...a: MaybeAccessor<any>[]) => string;
|
|
108
|
-
declare const concat: <A extends any[], V = MaybeAccessorValue<ItemsOf<A>>>(...a: A) => (V extends any[] ? ItemsOf<V> : V)[];
|
|
109
|
-
declare const toFloat: (string: MaybeAccessor<string>) => number;
|
|
110
|
-
declare const toInt: (string: MaybeAccessor<string>, radix?: number | undefined) => number;
|
|
111
|
-
declare const toArray: <A extends any[]>(...a: A) => MaybeAccessorValue<ItemsOf<A>>[];
|
|
112
151
|
|
|
113
|
-
export { access, accessAsArray,
|
|
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,36 +1,50 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { getOwner, onCleanup } from "solid-js";
|
|
2
|
+
import { createRoot, getOwner, onCleanup, runWithOwner, on } from "solid-js";
|
|
3
3
|
import { isServer } from "solid-js/web";
|
|
4
|
-
var noop = (
|
|
5
|
-
};
|
|
4
|
+
var noop = () => void 0;
|
|
6
5
|
var isClient = !isServer;
|
|
7
6
|
var isDefined = (value) => typeof value !== "undefined" && value !== null;
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
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];
|
|
13
11
|
var withAccess = (value, fn) => {
|
|
14
12
|
const _value = access(value);
|
|
15
13
|
isDefined(_value) && fn(_value);
|
|
16
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
|
+
}
|
|
17
20
|
var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
|
|
18
21
|
var entries = (object) => Object.entries(access(object));
|
|
19
22
|
var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
|
|
20
|
-
function raceTimeout(
|
|
21
|
-
const
|
|
22
|
-
|
|
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;
|
|
24
30
|
}
|
|
25
31
|
function destore(store) {
|
|
26
32
|
const _store = store;
|
|
27
33
|
const result = {};
|
|
28
34
|
Object.keys(_store).forEach((key) => {
|
|
29
|
-
result[key] =
|
|
35
|
+
result[key] = isFunction(_store[key]) ? _store[key].bind(_store) : () => _store[key];
|
|
30
36
|
});
|
|
31
37
|
return result;
|
|
32
38
|
}
|
|
33
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
|
+
}
|
|
34
48
|
var createCallbackStack = () => {
|
|
35
49
|
let stack = [];
|
|
36
50
|
const clear = () => stack = [];
|
|
@@ -43,32 +57,25 @@ var createCallbackStack = () => {
|
|
|
43
57
|
clear
|
|
44
58
|
};
|
|
45
59
|
};
|
|
46
|
-
var stringConcat = (...a) => a.reduce((t, c) => t + access(c), "");
|
|
47
|
-
var concat = (...a) => a.reduce((t, c) => {
|
|
48
|
-
const v = access(c);
|
|
49
|
-
return Array.isArray(v) ? [...t, ...v] : [...t, v];
|
|
50
|
-
}, []);
|
|
51
|
-
var toFloat = (string) => Number.parseFloat(access(string));
|
|
52
|
-
var toInt = (string, radix) => Number.parseInt(access(string), radix);
|
|
53
|
-
var toArray = (...a) => a.map((v) => access(v));
|
|
54
60
|
export {
|
|
55
61
|
access,
|
|
56
62
|
accessAsArray,
|
|
57
|
-
|
|
63
|
+
asAccessor,
|
|
64
|
+
asArray,
|
|
58
65
|
createCallbackStack,
|
|
66
|
+
createSubRoot,
|
|
67
|
+
createSubRootFunction,
|
|
59
68
|
destore,
|
|
60
69
|
entries,
|
|
61
70
|
forEach,
|
|
62
71
|
isClient,
|
|
63
72
|
isDefined,
|
|
73
|
+
isFunction,
|
|
64
74
|
isServer,
|
|
65
75
|
noop,
|
|
76
|
+
onAccess,
|
|
66
77
|
onRootCleanup,
|
|
67
78
|
promiseTimeout,
|
|
68
79
|
raceTimeout,
|
|
69
|
-
stringConcat,
|
|
70
|
-
toArray,
|
|
71
|
-
toFloat,
|
|
72
|
-
toInt,
|
|
73
80
|
withAccess
|
|
74
81
|
};
|
package/dist/setter.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ declare type Fn<R = void> = () => R;
|
|
|
10
10
|
declare type Many<T> = T | T[];
|
|
11
11
|
declare type Keys<O extends Object> = keyof O;
|
|
12
12
|
declare type Values<O extends Object> = O[Keys<O>];
|
|
13
|
+
declare type Noop = (...a: any[]) => void;
|
|
13
14
|
/**
|
|
14
15
|
* Infers the type of the array elements
|
|
15
16
|
*/
|
|
@@ -23,14 +24,20 @@ declare type MaybeAccessor<T> = T | Accessor<T>;
|
|
|
23
24
|
/**
|
|
24
25
|
* Accessed value of a MaybeAccessor
|
|
25
26
|
* @example
|
|
27
|
+
* ```ts
|
|
26
28
|
* MaybeAccessorValue<MaybeAccessor<string>>
|
|
27
29
|
* // => string
|
|
28
30
|
* MaybeAccessorValue<MaybeAccessor<() => string>>
|
|
29
31
|
* // => string | (() => string)
|
|
30
32
|
* MaybeAccessorValue<MaybeAccessor<string> | Function>
|
|
31
33
|
* // => string | void
|
|
34
|
+
* ```
|
|
32
35
|
*/
|
|
33
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>;
|
|
34
41
|
/** Allows to make shallow overwrites to an interface */
|
|
35
42
|
declare type Modify<T, R> = Omit<T, keyof R> & R;
|
|
36
43
|
/** Allows to make nested overwrites to an interface */
|
|
@@ -44,6 +51,9 @@ declare type DeepPartialAny<T> = {
|
|
|
44
51
|
declare type AnyObject = Record<string, any>;
|
|
45
52
|
declare type AnyFunction = (...args: any[]) => any;
|
|
46
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;
|
|
47
57
|
/**
|
|
48
58
|
* Destructible store object, with values changed to accessors
|
|
49
59
|
*/
|
|
@@ -51,4 +61,4 @@ declare type Destore<T extends Object> = {
|
|
|
51
61
|
[K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
|
|
52
62
|
};
|
|
53
63
|
|
|
54
|
-
export {
|
|
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.1.
|
|
3
|
+
"version": "0.1.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",
|
|
@@ -48,7 +48,8 @@
|
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "tsup",
|
|
50
50
|
"test": "uvu -r solid-register",
|
|
51
|
-
"watch-test": "watchlist src test -- npm test"
|
|
51
|
+
"watch-test": "watchlist src test -- npm test",
|
|
52
|
+
"docs": "typedoc"
|
|
52
53
|
},
|
|
53
54
|
"keywords": [
|
|
54
55
|
"utilities",
|
|
@@ -57,15 +58,17 @@
|
|
|
57
58
|
"primitives"
|
|
58
59
|
],
|
|
59
60
|
"devDependencies": {
|
|
60
|
-
"jsdom": "
|
|
61
|
+
"jsdom": "18.1.1",
|
|
61
62
|
"prettier": "^2.5.1",
|
|
62
|
-
"solid-register": "^0.
|
|
63
|
+
"solid-register": "^0.1.1",
|
|
63
64
|
"tslib": "^2.3.1",
|
|
64
65
|
"tsup": "^5.11.0",
|
|
66
|
+
"typedoc": "^0.22.10",
|
|
67
|
+
"typedoc-plugin-markdown": "^3.11.8",
|
|
65
68
|
"uvu": "^0.5.2",
|
|
66
69
|
"watchlist": "^0.3.1"
|
|
67
70
|
},
|
|
68
71
|
"peerDependencies": {
|
|
69
|
-
"solid-js": "
|
|
72
|
+
"solid-js": "1.3.0"
|
|
70
73
|
}
|
|
71
74
|
}
|