@solid-primitives/utils 0.1.0 → 0.2.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 +16 -4
- package/dist/index.cjs +144 -0
- package/dist/index.d.ts +206 -0
- package/dist/index.js +89 -0
- package/package.json +8 -32
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,12 @@ 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
|
+
|
|
58
|
+
0.1.3
|
|
59
|
+
|
|
60
|
+
Minor upgrades.
|
|
61
|
+
|
|
50
62
|
</details>
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
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/index.ts
|
|
25
|
+
var src_exports = {};
|
|
26
|
+
__export(src_exports, {
|
|
27
|
+
access: () => access,
|
|
28
|
+
accessArray: () => accessArray,
|
|
29
|
+
accessAsArray: () => accessAsArray,
|
|
30
|
+
asAccessor: () => asAccessor,
|
|
31
|
+
asArray: () => asArray,
|
|
32
|
+
compare: () => compare,
|
|
33
|
+
createCallbackStack: () => createCallbackStack,
|
|
34
|
+
destore: () => destore,
|
|
35
|
+
entries: () => entries,
|
|
36
|
+
forEach: () => forEach,
|
|
37
|
+
isArray: () => isArray,
|
|
38
|
+
isBoolean: () => isBoolean,
|
|
39
|
+
isClient: () => isClient,
|
|
40
|
+
isDefined: () => isDefined,
|
|
41
|
+
isFunction: () => isFunction,
|
|
42
|
+
isNumber: () => isNumber,
|
|
43
|
+
isObject: () => isObject,
|
|
44
|
+
isServer: () => import_web.isServer,
|
|
45
|
+
isString: () => isString,
|
|
46
|
+
keys: () => keys,
|
|
47
|
+
noop: () => noop,
|
|
48
|
+
onAccess: () => onAccess,
|
|
49
|
+
onRootCleanup: () => onRootCleanup,
|
|
50
|
+
promiseTimeout: () => promiseTimeout,
|
|
51
|
+
raceTimeout: () => raceTimeout,
|
|
52
|
+
tuple: () => tuple,
|
|
53
|
+
withAccess: () => withAccess
|
|
54
|
+
});
|
|
55
|
+
var import_solid_js = require("solid-js");
|
|
56
|
+
var import_web = require("solid-js/web");
|
|
57
|
+
var noop = () => void 0;
|
|
58
|
+
var isClient = !import_web.isServer;
|
|
59
|
+
var isDefined = (value) => typeof value !== "undefined" && value !== null;
|
|
60
|
+
var isFunction = (value) => typeof value === "function";
|
|
61
|
+
var isBoolean = (val) => typeof val === "boolean";
|
|
62
|
+
var isNumber = (val) => typeof val === "number";
|
|
63
|
+
var isString = (val) => typeof val === "string";
|
|
64
|
+
var isObject = (val) => toString.call(val) === "[object Object]";
|
|
65
|
+
var isArray = (val) => Array.isArray(val);
|
|
66
|
+
var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
67
|
+
var tuple = (input) => input;
|
|
68
|
+
var access = (v) => isFunction(v) ? v() : v;
|
|
69
|
+
var accessAsArray = (value) => asArray(access(value));
|
|
70
|
+
var asArray = (value) => Array.isArray(value) ? value : [value];
|
|
71
|
+
var accessArray = (list) => list.map((v) => access(v));
|
|
72
|
+
var withAccess = (value, fn) => {
|
|
73
|
+
const _value = access(value);
|
|
74
|
+
isDefined(_value) && fn(_value);
|
|
75
|
+
};
|
|
76
|
+
var asAccessor = (v) => isFunction(v) ? v : () => v;
|
|
77
|
+
function onAccess(deps, fn, options) {
|
|
78
|
+
const source = deps.map(asAccessor);
|
|
79
|
+
return (0, import_solid_js.on)(source, fn, options);
|
|
80
|
+
}
|
|
81
|
+
var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
|
|
82
|
+
var entries = (object) => Object.entries(access(object));
|
|
83
|
+
var keys = (object) => Object.keys(access(object));
|
|
84
|
+
var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
|
|
85
|
+
function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
|
|
86
|
+
const promises = asArray(input);
|
|
87
|
+
const race = Promise.race([...promises, promiseTimeout(ms, throwOnTimeout, reason)]);
|
|
88
|
+
race.finally(() => {
|
|
89
|
+
promises.forEach((p) => p && typeof p === "object" && typeof p.dispose === "function" && p.dispose());
|
|
90
|
+
});
|
|
91
|
+
return race;
|
|
92
|
+
}
|
|
93
|
+
var onRootCleanup = (fn) => (0, import_solid_js.getOwner)() ? (0, import_solid_js.onCleanup)(fn) : fn;
|
|
94
|
+
function destore(store) {
|
|
95
|
+
const _store = store;
|
|
96
|
+
const result = {};
|
|
97
|
+
Object.keys(_store).forEach((key) => {
|
|
98
|
+
result[key] = isFunction(_store[key]) ? _store[key].bind(_store) : () => _store[key];
|
|
99
|
+
});
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
var createCallbackStack = () => {
|
|
103
|
+
let stack = [];
|
|
104
|
+
const clear = () => stack = [];
|
|
105
|
+
return {
|
|
106
|
+
push: (...callbacks) => stack.push(...callbacks),
|
|
107
|
+
execute: (arg0, arg1, arg2, arg3) => {
|
|
108
|
+
stack.forEach((cb) => cb(arg0, arg1, arg2, arg3));
|
|
109
|
+
clear();
|
|
110
|
+
},
|
|
111
|
+
clear
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
module.exports = __toCommonJS(src_exports);
|
|
115
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
116
|
+
0 && (module.exports = {
|
|
117
|
+
access,
|
|
118
|
+
accessArray,
|
|
119
|
+
accessAsArray,
|
|
120
|
+
asAccessor,
|
|
121
|
+
asArray,
|
|
122
|
+
compare,
|
|
123
|
+
createCallbackStack,
|
|
124
|
+
destore,
|
|
125
|
+
entries,
|
|
126
|
+
forEach,
|
|
127
|
+
isArray,
|
|
128
|
+
isBoolean,
|
|
129
|
+
isClient,
|
|
130
|
+
isDefined,
|
|
131
|
+
isFunction,
|
|
132
|
+
isNumber,
|
|
133
|
+
isObject,
|
|
134
|
+
isServer,
|
|
135
|
+
isString,
|
|
136
|
+
keys,
|
|
137
|
+
noop,
|
|
138
|
+
onAccess,
|
|
139
|
+
onRootCleanup,
|
|
140
|
+
promiseTimeout,
|
|
141
|
+
raceTimeout,
|
|
142
|
+
tuple,
|
|
143
|
+
withAccess
|
|
144
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { Accessor, onCleanup } from 'solid-js';
|
|
2
|
+
import { OnOptions, EffectFunction, NoInfer } from 'solid-js/types/reactive/signal';
|
|
3
|
+
import { Store } from 'solid-js/store';
|
|
4
|
+
export { isServer } from 'solid-js/web';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A function
|
|
8
|
+
*/
|
|
9
|
+
declare type Fn<R = void> = () => R;
|
|
10
|
+
/**
|
|
11
|
+
* Can be single or in an array
|
|
12
|
+
*/
|
|
13
|
+
declare type Many<T> = T | T[];
|
|
14
|
+
declare type Keys<O extends Object> = keyof O;
|
|
15
|
+
declare type Values<O extends Object> = O[Keys<O>];
|
|
16
|
+
declare type Noop = (...a: any[]) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Infers the type of the array elements
|
|
19
|
+
*/
|
|
20
|
+
declare type ItemsOf<T> = T extends (infer E)[] ? E : never;
|
|
21
|
+
/**
|
|
22
|
+
* T or a reactive/non-reactive function returning T
|
|
23
|
+
*/
|
|
24
|
+
declare type MaybeAccessor<T> = T | Accessor<T>;
|
|
25
|
+
/**
|
|
26
|
+
* Accessed value of a MaybeAccessor
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* MaybeAccessorValue<MaybeAccessor<string>>
|
|
30
|
+
* // => string
|
|
31
|
+
* MaybeAccessorValue<MaybeAccessor<() => string>>
|
|
32
|
+
* // => string | (() => string)
|
|
33
|
+
* MaybeAccessorValue<MaybeAccessor<string> | Function>
|
|
34
|
+
* // => string | void
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends Fn ? ReturnType<T> : T;
|
|
38
|
+
declare type OnAccessEffectFunction<S, Prev, Next extends Prev = Prev> = (input: AccessReturnTypes<S>, prevInput: AccessReturnTypes<S>, v: Prev) => Next;
|
|
39
|
+
declare type AccessReturnTypes<S> = S extends MaybeAccessor<any>[] ? {
|
|
40
|
+
[I in keyof S]: AccessReturnTypes<S[I]>;
|
|
41
|
+
} : MaybeAccessorValue<S>;
|
|
42
|
+
/** Allows to make shallow overwrites to an interface */
|
|
43
|
+
declare type Modify<T, R> = Omit<T, keyof R> & R;
|
|
44
|
+
/** Allows to make nested overwrites to an interface */
|
|
45
|
+
declare type ModifyDeep<A extends AnyObject, B extends DeepPartialAny<A>> = {
|
|
46
|
+
[K in keyof A]: B[K] extends never ? A[K] : B[K] extends AnyObject ? ModifyDeep<A[K], B[K]> : B[K];
|
|
47
|
+
} & (A extends AnyObject ? Omit<B, keyof A> : A);
|
|
48
|
+
/** Makes each property optional and turns each leaf property into any, allowing for type overrides by narrowing any. */
|
|
49
|
+
declare type DeepPartialAny<T> = {
|
|
50
|
+
[P in keyof T]?: T[P] extends AnyObject ? DeepPartialAny<T[P]> : any;
|
|
51
|
+
};
|
|
52
|
+
declare type AnyObject = Record<string, any>;
|
|
53
|
+
declare type AnyFunction = (...args: any[]) => any;
|
|
54
|
+
declare type PrimitiveValue = string | boolean | number | bigint | symbol | null | undefined;
|
|
55
|
+
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;
|
|
58
|
+
/**
|
|
59
|
+
* Destructible store object, with values changed to accessors
|
|
60
|
+
*/
|
|
61
|
+
declare type Destore<T extends Object> = {
|
|
62
|
+
[K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** no operation */
|
|
66
|
+
declare const noop: Noop;
|
|
67
|
+
declare const isClient = true;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* `if (typeof value !== "undefined" && value !== null)`
|
|
71
|
+
*/
|
|
72
|
+
declare const isDefined: <T>(value: T | null | undefined) => value is T;
|
|
73
|
+
declare const isFunction: <T>(value: Function | T) => value is Function;
|
|
74
|
+
declare const isBoolean: (val: any) => val is boolean;
|
|
75
|
+
declare const isNumber: (val: any) => val is number;
|
|
76
|
+
declare const isString: (val: unknown) => val is string;
|
|
77
|
+
declare const isObject: (val: any) => val is object;
|
|
78
|
+
declare const isArray: (val: any) => val is any[];
|
|
79
|
+
declare const compare: (a: any, b: any) => number;
|
|
80
|
+
/**
|
|
81
|
+
* for creating tuples by inferring type
|
|
82
|
+
* @example
|
|
83
|
+
* const users = tuple(["John", "Jeff", "Joe"]);
|
|
84
|
+
* // users: [string, string, string]
|
|
85
|
+
*/
|
|
86
|
+
declare const tuple: <T extends any[] | []>(input: T) => T;
|
|
87
|
+
/**
|
|
88
|
+
* Accesses the value of a MaybeAccessor
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* access("foo") // => "foo"
|
|
92
|
+
* access(() => "foo") // => "foo"
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>;
|
|
96
|
+
/**
|
|
97
|
+
* Accesses the value of a MaybeAccessor, but always returns an array
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* accessAsArray('abc') // => ['abc']
|
|
101
|
+
* accessAsArray(() => 'abc') // => ['abc']
|
|
102
|
+
* accessAsArray([1,2,3]) // => [1,2,3]
|
|
103
|
+
* accessAsArray(() => [1,2,3]) // => [1,2,3]
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
declare const accessAsArray: <T extends unknown, V = MaybeAccessorValue<T>>(value: T) => V extends any[] ? V : V[];
|
|
107
|
+
declare const asArray: <T>(value: T) => T extends any[] ? T : T[];
|
|
108
|
+
/**
|
|
109
|
+
* Access an array of MaybeAccessors
|
|
110
|
+
* @example
|
|
111
|
+
* const list = [1, 2, () => 3)] // T: MaybeAccessor<number>[]
|
|
112
|
+
* const newList = accessArray(list) // T: number[]
|
|
113
|
+
*/
|
|
114
|
+
declare const accessArray: <A extends unknown>(list: readonly A[]) => MaybeAccessorValue<A>[];
|
|
115
|
+
/**
|
|
116
|
+
* Run the function if the accessed value is not `undefined` nor `null`
|
|
117
|
+
* @param value
|
|
118
|
+
* @param fn
|
|
119
|
+
*/
|
|
120
|
+
declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void;
|
|
121
|
+
declare const asAccessor: <A extends unknown>(v: A) => Accessor<MaybeAccessorValue<A>>;
|
|
122
|
+
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>>;
|
|
123
|
+
/**
|
|
124
|
+
* Quickly iterate over an MaybeAccessor<any>
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* const myFunc = (source: MaybeAccessor<string[]>) => {
|
|
129
|
+
* forEach(source, item => console.log(item))
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
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
|
+
/**
|
|
135
|
+
* Get `Object.entries()` of an MaybeAccessor<Object>
|
|
136
|
+
*/
|
|
137
|
+
declare const entries: <A extends MaybeAccessor<object>, V = MaybeAccessorValue<A>>(object: A) => [string, Values<V>][];
|
|
138
|
+
/**
|
|
139
|
+
* Get `Object.keys()` of an MaybeAccessor<Object>
|
|
140
|
+
*/
|
|
141
|
+
declare const keys: <A extends MaybeAccessor<object>>(object: A) => (keyof MaybeAccessorValue<A>)[];
|
|
142
|
+
/**
|
|
143
|
+
* Creates a promise that resolves *(or rejects)* after given time.
|
|
144
|
+
*
|
|
145
|
+
* @param ms timeout duration in ms
|
|
146
|
+
* @param throwOnTimeout promise will be rejected on timeout if set to `true`
|
|
147
|
+
* @param reason rejection reason
|
|
148
|
+
* @returns Promise<void>
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* await promiseTimeout(1500) // will resolve void after timeout
|
|
153
|
+
* await promiseTimeout(1500, true, 'rejection reason') // will reject 'rejection reason' after timout
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
declare const promiseTimeout: (ms: number, throwOnTimeout?: boolean, reason?: string) => Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Combination of `Promise.race()` and `promiseTimeout`.
|
|
159
|
+
*
|
|
160
|
+
* @param promises single promise, or array of promises
|
|
161
|
+
* @param ms timeout duration in ms
|
|
162
|
+
* @param throwOnTimeout promise will be rejected on timeout if set to `true`
|
|
163
|
+
* @param reason rejection reason
|
|
164
|
+
* @returns a promise resulting in value of the first source promises to be resolved
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* // single promise
|
|
169
|
+
* await raceTimeout(new Promise(() => {...}), 3000)
|
|
170
|
+
* // list of promises racing
|
|
171
|
+
* await raceTimeout([new Promise(),new Promise()...], 3000)
|
|
172
|
+
* // reject on timeout
|
|
173
|
+
* await raceTimeout(new Promise(), 3000, true, 'rejection reason')
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout: true, reason?: string): T extends any[] ? Promise<Awaited<T[number]>> : Promise<Awaited<T>>;
|
|
177
|
+
declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout?: boolean, reason?: string): T extends any[] ? Promise<Awaited<T[number]> | undefined> : Promise<Awaited<T> | undefined>;
|
|
178
|
+
/**
|
|
179
|
+
* Solid's `onCleanup` that is registered only if there is a root.
|
|
180
|
+
*/
|
|
181
|
+
declare const onRootCleanup: typeof onCleanup;
|
|
182
|
+
/**
|
|
183
|
+
* Allows the Solid's store to be destructured
|
|
184
|
+
*
|
|
185
|
+
* @param store
|
|
186
|
+
* @returns Destructible object, with values changed to accessors
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```ts
|
|
190
|
+
* const [state, setState] = createStore({
|
|
191
|
+
* count: 0,
|
|
192
|
+
* get double() { return this.count * 2 },
|
|
193
|
+
* })
|
|
194
|
+
* const { count, double } = destore(state)
|
|
195
|
+
* // use it like a signal:
|
|
196
|
+
* count()
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
declare function destore<T extends Object>(store: Store<T>): Destore<T>;
|
|
200
|
+
declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
|
|
201
|
+
push: (...callbacks: Fn[]) => void;
|
|
202
|
+
execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
|
|
203
|
+
clear: Fn;
|
|
204
|
+
};
|
|
205
|
+
|
|
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 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { getOwner, onCleanup, 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 isBoolean = (val) => typeof val === "boolean";
|
|
9
|
+
var isNumber = (val) => typeof val === "number";
|
|
10
|
+
var isString = (val) => typeof val === "string";
|
|
11
|
+
var isObject = (val) => toString.call(val) === "[object Object]";
|
|
12
|
+
var isArray = (val) => Array.isArray(val);
|
|
13
|
+
var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
14
|
+
var tuple = (input) => input;
|
|
15
|
+
var access = (v) => isFunction(v) ? v() : v;
|
|
16
|
+
var accessAsArray = (value) => asArray(access(value));
|
|
17
|
+
var asArray = (value) => Array.isArray(value) ? value : [value];
|
|
18
|
+
var accessArray = (list) => list.map((v) => access(v));
|
|
19
|
+
var withAccess = (value, fn) => {
|
|
20
|
+
const _value = access(value);
|
|
21
|
+
isDefined(_value) && fn(_value);
|
|
22
|
+
};
|
|
23
|
+
var asAccessor = (v) => isFunction(v) ? v : () => v;
|
|
24
|
+
function onAccess(deps, fn, options) {
|
|
25
|
+
const source = deps.map(asAccessor);
|
|
26
|
+
return on(source, fn, options);
|
|
27
|
+
}
|
|
28
|
+
var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
|
|
29
|
+
var entries = (object) => Object.entries(access(object));
|
|
30
|
+
var keys = (object) => Object.keys(access(object));
|
|
31
|
+
var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
|
|
32
|
+
function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") {
|
|
33
|
+
const promises = asArray(input);
|
|
34
|
+
const race = Promise.race([...promises, promiseTimeout(ms, throwOnTimeout, reason)]);
|
|
35
|
+
race.finally(() => {
|
|
36
|
+
promises.forEach((p) => p && typeof p === "object" && typeof p.dispose === "function" && p.dispose());
|
|
37
|
+
});
|
|
38
|
+
return race;
|
|
39
|
+
}
|
|
40
|
+
var onRootCleanup = (fn) => getOwner() ? onCleanup(fn) : fn;
|
|
41
|
+
function destore(store) {
|
|
42
|
+
const _store = store;
|
|
43
|
+
const result = {};
|
|
44
|
+
Object.keys(_store).forEach((key) => {
|
|
45
|
+
result[key] = isFunction(_store[key]) ? _store[key].bind(_store) : () => _store[key];
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
var createCallbackStack = () => {
|
|
50
|
+
let stack = [];
|
|
51
|
+
const clear = () => stack = [];
|
|
52
|
+
return {
|
|
53
|
+
push: (...callbacks) => stack.push(...callbacks),
|
|
54
|
+
execute: (arg0, arg1, arg2, arg3) => {
|
|
55
|
+
stack.forEach((cb) => cb(arg0, arg1, arg2, arg3));
|
|
56
|
+
clear();
|
|
57
|
+
},
|
|
58
|
+
clear
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
export {
|
|
62
|
+
access,
|
|
63
|
+
accessArray,
|
|
64
|
+
accessAsArray,
|
|
65
|
+
asAccessor,
|
|
66
|
+
asArray,
|
|
67
|
+
compare,
|
|
68
|
+
createCallbackStack,
|
|
69
|
+
destore,
|
|
70
|
+
entries,
|
|
71
|
+
forEach,
|
|
72
|
+
isArray,
|
|
73
|
+
isBoolean,
|
|
74
|
+
isClient,
|
|
75
|
+
isDefined,
|
|
76
|
+
isFunction,
|
|
77
|
+
isNumber,
|
|
78
|
+
isObject,
|
|
79
|
+
isServer,
|
|
80
|
+
isString,
|
|
81
|
+
keys,
|
|
82
|
+
noop,
|
|
83
|
+
onAccess,
|
|
84
|
+
onRootCleanup,
|
|
85
|
+
promiseTimeout,
|
|
86
|
+
raceTimeout,
|
|
87
|
+
tuple,
|
|
88
|
+
withAccess
|
|
89
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solid-primitives/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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",
|
|
@@ -18,37 +18,11 @@
|
|
|
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
|
-
},
|
|
48
21
|
"scripts": {
|
|
49
22
|
"build": "tsup",
|
|
50
23
|
"test": "uvu -r solid-register",
|
|
51
|
-
"watch-test": "watchlist src test -- npm test"
|
|
24
|
+
"watch-test": "watchlist src test -- npm test",
|
|
25
|
+
"docs": "typedoc"
|
|
52
26
|
},
|
|
53
27
|
"keywords": [
|
|
54
28
|
"utilities",
|
|
@@ -57,15 +31,17 @@
|
|
|
57
31
|
"primitives"
|
|
58
32
|
],
|
|
59
33
|
"devDependencies": {
|
|
60
|
-
"jsdom": "
|
|
34
|
+
"jsdom": "18.1.1",
|
|
61
35
|
"prettier": "^2.5.1",
|
|
62
|
-
"solid-register": "^0.
|
|
36
|
+
"solid-register": "^0.1.1",
|
|
63
37
|
"tslib": "^2.3.1",
|
|
64
38
|
"tsup": "^5.11.0",
|
|
39
|
+
"typedoc": "^0.22.10",
|
|
40
|
+
"typedoc-plugin-markdown": "^3.11.8",
|
|
65
41
|
"uvu": "^0.5.2",
|
|
66
42
|
"watchlist": "^0.3.1"
|
|
67
43
|
},
|
|
68
44
|
"peerDependencies": {
|
|
69
|
-
"solid-js": "^1.
|
|
45
|
+
"solid-js": "^1.3.1"
|
|
70
46
|
}
|
|
71
47
|
}
|