@solid-primitives/utils 4.0.0 → 4.0.1
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/dist/index.cjs +23 -45
- package/dist/index.d.ts +31 -31
- package/dist/index.js +1 -2
- package/package.json +6 -14
package/dist/index.cjs
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var solidJs = require('solid-js');
|
|
6
4
|
var web = require('solid-js/web');
|
|
7
5
|
|
|
8
6
|
// src/index.ts
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
exports.noop = () => void 0;
|
|
8
|
+
exports.isServer = web.isServer;
|
|
9
|
+
exports.isClient = !exports.isServer;
|
|
10
|
+
exports.isDev = solidJs.DEV && exports.isClient;
|
|
11
|
+
exports.isProd = !exports.isDev;
|
|
12
|
+
exports.ofClass = (v, c) => v instanceof c || v && v.constructor === c;
|
|
15
13
|
function isObject(value) {
|
|
16
14
|
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
17
15
|
}
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
exports.compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
17
|
+
exports.arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
|
|
20
18
|
function chain(callbacks) {
|
|
21
19
|
return (...args) => {
|
|
22
20
|
for (const callback of callbacks) {
|
|
@@ -25,22 +23,22 @@ function chain(callbacks) {
|
|
|
25
23
|
}
|
|
26
24
|
};
|
|
27
25
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const _value = access(value);
|
|
26
|
+
exports.clamp = (n, min, max) => Math.min(Math.max(n, min), max);
|
|
27
|
+
exports.access = (v) => typeof v === "function" && !v.length ? v() : v;
|
|
28
|
+
exports.asArray = (value) => Array.isArray(value) ? value : [value];
|
|
29
|
+
exports.accessArray = (list) => list.map((v) => exports.access(v));
|
|
30
|
+
exports.withAccess = (value, fn) => {
|
|
31
|
+
const _value = exports.access(value);
|
|
34
32
|
typeof _value !== "undefined" && _value !== null && fn(_value);
|
|
35
33
|
};
|
|
36
|
-
|
|
34
|
+
exports.asAccessor = (v) => typeof v === "function" ? v : () => v;
|
|
37
35
|
function accessWith(valueOrFn, ...args) {
|
|
38
36
|
return typeof valueOrFn === "function" ? valueOrFn(...args) : valueOrFn;
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
exports.entries = Object.entries;
|
|
39
|
+
exports.keys = Object.keys;
|
|
40
|
+
exports.onRootCleanup = (fn) => solidJs.getOwner() ? solidJs.onCleanup(fn) : fn;
|
|
41
|
+
exports.createCallbackStack = () => {
|
|
44
42
|
let stack = [];
|
|
45
43
|
const clear = () => stack = [];
|
|
46
44
|
return {
|
|
@@ -66,8 +64,7 @@ function createProxy(traps) {
|
|
|
66
64
|
{
|
|
67
65
|
get: (_, k) => traps.get(k),
|
|
68
66
|
set: (_, k, v) => {
|
|
69
|
-
|
|
70
|
-
(_a = traps.set) == null ? void 0 : _a.call(traps, k, v);
|
|
67
|
+
traps.set?.(k, v);
|
|
71
68
|
return false;
|
|
72
69
|
}
|
|
73
70
|
}
|
|
@@ -95,7 +92,7 @@ function createStaticStore(init) {
|
|
|
95
92
|
if (key in copy)
|
|
96
93
|
copy[key] = accessWith(value, [copy[key]]);
|
|
97
94
|
};
|
|
98
|
-
for (const key of keys(init)) {
|
|
95
|
+
for (const key of exports.keys(init)) {
|
|
99
96
|
store[key] = void 0;
|
|
100
97
|
Object.defineProperty(store, key, {
|
|
101
98
|
get: getValue.bind(void 0, key)
|
|
@@ -105,7 +102,7 @@ function createStaticStore(init) {
|
|
|
105
102
|
if (isObject(a))
|
|
106
103
|
solidJs.untrack(() => {
|
|
107
104
|
solidJs.batch(() => {
|
|
108
|
-
for (const [key, value] of entries(accessWith(a, store)))
|
|
105
|
+
for (const [key, value] of exports.entries(accessWith(a, store)))
|
|
109
106
|
setValue(key, () => value);
|
|
110
107
|
});
|
|
111
108
|
});
|
|
@@ -146,31 +143,12 @@ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
|
|
|
146
143
|
handleAdded(currEl);
|
|
147
144
|
}
|
|
148
145
|
}
|
|
149
|
-
|
|
146
|
+
exports.forEachEntry = (obj, fn) => Object.entries(obj).forEach(([key, value]) => fn(key, value));
|
|
150
147
|
|
|
151
|
-
exports.access = access;
|
|
152
|
-
exports.accessArray = accessArray;
|
|
153
148
|
exports.accessWith = accessWith;
|
|
154
|
-
exports.arrayEquals = arrayEquals;
|
|
155
|
-
exports.asAccessor = asAccessor;
|
|
156
|
-
exports.asArray = asArray;
|
|
157
149
|
exports.chain = chain;
|
|
158
|
-
exports.clamp = clamp;
|
|
159
|
-
exports.compare = compare;
|
|
160
|
-
exports.createCallbackStack = createCallbackStack;
|
|
161
150
|
exports.createMicrotask = createMicrotask;
|
|
162
151
|
exports.createProxy = createProxy;
|
|
163
152
|
exports.createStaticStore = createStaticStore;
|
|
164
|
-
exports.entries = entries;
|
|
165
|
-
exports.forEachEntry = forEachEntry;
|
|
166
153
|
exports.handleDiffArray = handleDiffArray;
|
|
167
|
-
exports.isClient = isClient;
|
|
168
|
-
exports.isDev = isDev;
|
|
169
154
|
exports.isObject = isObject;
|
|
170
|
-
exports.isProd = isProd;
|
|
171
|
-
exports.isServer = isServer;
|
|
172
|
-
exports.keys = keys;
|
|
173
|
-
exports.noop = noop;
|
|
174
|
-
exports.ofClass = ofClass;
|
|
175
|
-
exports.onRootCleanup = onRootCleanup;
|
|
176
|
-
exports.withAccess = withAccess;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,20 +3,20 @@ import { Accessor, Setter, onCleanup } from 'solid-js';
|
|
|
3
3
|
/**
|
|
4
4
|
* Can be single or in an array
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
type Many<T> = T | T[];
|
|
7
|
+
type Values<O extends Object> = O[keyof O];
|
|
8
|
+
type Noop = (...a: any[]) => void;
|
|
9
|
+
type Directive<P = true> = (el: Element, props: Accessor<P>) => void;
|
|
10
10
|
/**
|
|
11
11
|
* Infers the type of the array elements
|
|
12
12
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
type ItemsOf<T> = T extends (infer E)[] ? E : never;
|
|
14
|
+
type ItemsOfMany<T> = T extends any[] ? ItemsOf<T> : T;
|
|
15
|
+
type SetterValue<T> = Parameters<Setter<T>>[0];
|
|
16
16
|
/**
|
|
17
17
|
* T or a reactive/non-reactive function returning T
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
type MaybeAccessor<T> = T | Accessor<T>;
|
|
20
20
|
/**
|
|
21
21
|
* Accessed value of a MaybeAccessor
|
|
22
22
|
* @example
|
|
@@ -29,51 +29,51 @@ declare type MaybeAccessor<T> = T | Accessor<T>;
|
|
|
29
29
|
* // => string | void
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends () => any ? ReturnType<T> : T;
|
|
33
|
+
type OnAccessEffectFunction<S, Prev, Next extends Prev = Prev> = (input: AccessReturnTypes<S>, prevInput: AccessReturnTypes<S>, v: Prev) => Next;
|
|
34
|
+
type AccessReturnTypes<S> = S extends MaybeAccessor<any>[] ? {
|
|
35
35
|
[I in keyof S]: AccessReturnTypes<S[I]>;
|
|
36
36
|
} : MaybeAccessorValue<S>;
|
|
37
37
|
/** Allows to make shallow overwrites to an interface */
|
|
38
|
-
|
|
38
|
+
type Modify<T, R> = Omit<T, keyof R> & R;
|
|
39
39
|
/** Allows to make nested overwrites to an interface */
|
|
40
|
-
|
|
40
|
+
type ModifyDeep<A extends AnyObject, B extends DeepPartialAny<A>> = {
|
|
41
41
|
[K in keyof A]: B[K] extends never ? A[K] : B[K] extends AnyObject ? ModifyDeep<A[K], B[K]> : B[K];
|
|
42
42
|
} & (A extends AnyObject ? Omit<B, keyof A> : A);
|
|
43
43
|
/** Makes each property optional and turns each leaf property into any, allowing for type overrides by narrowing any. */
|
|
44
|
-
|
|
44
|
+
type DeepPartialAny<T> = {
|
|
45
45
|
[P in keyof T]?: T[P] extends AnyObject ? DeepPartialAny<T[P]> : any;
|
|
46
46
|
};
|
|
47
47
|
/** Removes the `[...list]` functionality */
|
|
48
|
-
|
|
48
|
+
type NonIterable<T> = T & {
|
|
49
49
|
[Symbol.iterator]: never;
|
|
50
50
|
};
|
|
51
51
|
/** Unwraps the type definition of an object, making it more readable */
|
|
52
|
-
|
|
52
|
+
type Simplify<T> = T extends object ? {
|
|
53
53
|
[K in keyof T]: T[K];
|
|
54
54
|
} : T;
|
|
55
55
|
/** Unboxes type definition, making it more readable */
|
|
56
|
-
|
|
56
|
+
type UnboxLazy<T> = T extends () => infer U ? U : T;
|
|
57
57
|
/** Get the required keys of an object */
|
|
58
|
-
|
|
58
|
+
type RequiredKeys<T> = keyof {
|
|
59
59
|
[K in keyof T as T extends {
|
|
60
60
|
[_ in K]: unknown;
|
|
61
61
|
} ? K : never]: 0;
|
|
62
62
|
};
|
|
63
63
|
/** Remove the first item of a tuple [1, 2, 3, 4] => [2, 3, 4] */
|
|
64
|
-
|
|
64
|
+
type Tail<T extends any[]> = ((...t: T) => void) extends (x: any, ...u: infer U) => void ? U : never;
|
|
65
65
|
/** `A | B => A & B` */
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
66
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
67
|
+
type ExtractIfPossible<T, U> = Extract<T, U> extends never ? U : Extract<T, U>;
|
|
68
|
+
type AnyObject = Record<PropertyKey, any>;
|
|
69
|
+
type AnyStatic = [] | any[] | AnyObject;
|
|
70
|
+
type AnyFunction = (...args: any[]) => any;
|
|
71
|
+
type AnyClass = abstract new (...args: any) => any;
|
|
72
|
+
type PrimitiveValue = PropertyKey | boolean | bigint | null | undefined;
|
|
73
|
+
type FalsyValue = false | 0 | "" | null | undefined;
|
|
74
|
+
type Truthy<T> = Exclude<T, FalsyValue>;
|
|
75
|
+
type Falsy<T> = Extract<T, FalsyValue>;
|
|
76
|
+
type Position = {
|
|
77
77
|
x: number;
|
|
78
78
|
y: number;
|
|
79
79
|
};
|
|
@@ -162,7 +162,7 @@ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
|
|
|
162
162
|
get: <K extends keyof T>(key: K) => T[K];
|
|
163
163
|
set?: undefined;
|
|
164
164
|
}): Readonly<T>;
|
|
165
|
-
|
|
165
|
+
type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
|
|
166
166
|
(setter: (prev: T) => Partial<T>): T;
|
|
167
167
|
(state: Partial<T>): T;
|
|
168
168
|
<K extends keyof T>(key: K, state: SetterValue<T[K]>): T;
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solid-primitives/utils",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1",
|
|
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",
|
|
@@ -21,12 +21,12 @@
|
|
|
21
21
|
"main": "./dist/index.cjs",
|
|
22
22
|
"module": "./dist/index.js",
|
|
23
23
|
"types": "./dist/index.d.ts",
|
|
24
|
+
"browser": {},
|
|
24
25
|
"exports": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
26
|
+
"import": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
28
29
|
},
|
|
29
|
-
"import": "./dist/index.js",
|
|
30
30
|
"require": "./dist/index.cjs"
|
|
31
31
|
},
|
|
32
32
|
"keywords": [
|
|
@@ -35,18 +35,10 @@
|
|
|
35
35
|
"solid",
|
|
36
36
|
"primitives"
|
|
37
37
|
],
|
|
38
|
-
"devDependencies": {
|
|
39
|
-
"jsdom": "^20.0.0",
|
|
40
|
-
"prettier": "^2.7.1",
|
|
41
|
-
"solid-js": "^1.6.0",
|
|
42
|
-
"tslib": "^2.4.0",
|
|
43
|
-
"tsup": "^6.2.3",
|
|
44
|
-
"vitest": "^0.24.1"
|
|
45
|
-
},
|
|
46
|
-
"browser": {},
|
|
47
38
|
"peerDependencies": {
|
|
48
39
|
"solid-js": "^1.6.0"
|
|
49
40
|
},
|
|
41
|
+
"typesVersions": {},
|
|
50
42
|
"scripts": {
|
|
51
43
|
"build": "jiti ../../scripts/build.ts",
|
|
52
44
|
"test": "vitest -c ../../configs/vitest.config.ts",
|