@solid-primitives/utils
Advanced tools
Comparing version 0.0.255 to 0.0.260
import { Accessor } from 'solid-js'; | ||
import { Store } from 'solid-js/store'; | ||
/** | ||
* A function | ||
*/ | ||
declare type Fn<R = void> = () => R; | ||
/** | ||
* Can be single or in an array | ||
*/ | ||
declare type Many<T> = T | T[]; | ||
@@ -8,10 +15,47 @@ declare type Keys<O extends Object> = keyof O; | ||
/** | ||
* Infers the element type of an array | ||
* Infers the type of the array elements | ||
*/ | ||
declare type ItemsOf<T> = T extends (infer E)[] ? E : never; | ||
/** | ||
* T or a reactive/non-reactive function returning T | ||
*/ | ||
declare type MaybeAccessor<T> = T | Accessor<T>; | ||
/** | ||
* Accessed value of a MaybeAccessor | ||
* @example | ||
* MaybeAccessorValue<MaybeAccessor<string>> | ||
* // => string | ||
* MaybeAccessorValue<MaybeAccessor<() => string>> | ||
* // => string | (() => string) | ||
* MaybeAccessorValue<MaybeAccessor<string> | Function> | ||
* // => string | void | ||
*/ | ||
declare type MaybeAccessorValue<T extends MaybeAccessor<any>> = T extends Fn ? ReturnType<T> : T; | ||
declare const isClient: boolean; | ||
declare const isServer: boolean; | ||
/** | ||
* Accesses the value of a MaybeAccessor | ||
* @example | ||
* access(x as MaybeAccessor<string>) | ||
* // => string | ||
* access(x as MaybeAccessor<() => string>) | ||
* // => string | (() => string) | ||
* access(x as MaybeAccessor<string> | Function) | ||
* // => string | void | ||
*/ | ||
declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>; | ||
/** | ||
* Accesses the value of a MaybeAccessor, but always returns an array | ||
* @example | ||
* accessAsArray('abc') // => ['abc'] | ||
* accessAsArray(() => 'abc') // => ['abc'] | ||
* accessAsArray([1,2,3]) // => [1,2,3] | ||
* accessAsArray(() => [1,2,3]) // => [1,2,3] | ||
*/ | ||
declare const accessAsArray: <T extends unknown, V = MaybeAccessorValue<T>>(value: T) => V extends any[] ? V : V[]; | ||
/** | ||
* Run the function if the accessed value is not `undefined` nor `null` | ||
* @param value | ||
* @param fn | ||
*/ | ||
declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void; | ||
@@ -22,2 +66,21 @@ 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; | ||
declare const objectOmit: <T extends Object, K extends (keyof T)[]>(object: T, ...keys: K) => Omit<T, ItemsOf<K>>; | ||
declare type Destore<T extends Object> = { | ||
[K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>; | ||
}; | ||
/** | ||
* Allows the Solid's store to be destructured | ||
* | ||
* @param store | ||
* @returns Destructible object, with values changed to accessors | ||
* | ||
* @example | ||
* const [state, setState] = createStore({ | ||
* count: 0, | ||
* get double() { return this.count * 2 }, | ||
* }) | ||
* const { count, double } = destore(state) | ||
* // use it like a signal: | ||
* count() | ||
*/ | ||
declare function destore<T extends Object>(store: Store<T>): Destore<T>; | ||
declare const createCallbackStack: <Arg0 = void, Arg1 = void, Arg2 = void, Arg3 = void>() => { | ||
@@ -34,2 +97,2 @@ push: (...callbacks: Fn[]) => void; | ||
export { Fn, ItemsOf, Keys, Many, MaybeAccessor, MaybeAccessorValue, Values, access, accessAsArray, concat, createCallbackStack, entries, forEach, isClient, objectOmit, promiseTimeout, stringConcat, toArray, toFloat, toInt, withAccess }; | ||
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 }; |
// src/index.ts | ||
var isClient = typeof window !== "undefined"; | ||
var isServer = !isClient; | ||
var access = (v) => typeof v === "function" ? v() : v; | ||
@@ -23,2 +24,10 @@ var accessAsArray = (value) => { | ||
}; | ||
function destore(store) { | ||
const _store = store; | ||
const result = {}; | ||
Object.keys(_store).forEach((key) => { | ||
result[key] = typeof _store[key] === "function" ? _store[key].bind(_store) : () => _store[key]; | ||
}); | ||
return result; | ||
} | ||
var createCallbackStack = () => { | ||
@@ -49,5 +58,7 @@ let stack = []; | ||
createCallbackStack, | ||
destore, | ||
entries, | ||
forEach, | ||
isClient, | ||
isServer, | ||
objectOmit, | ||
@@ -54,0 +65,0 @@ promiseTimeout, |
{ | ||
"name": "@solid-primitives/utils", | ||
"version": "0.0.255", | ||
"version": "0.0.260", | ||
"description": "A bunch of reactive utility types and functions, for building primitives with Solid.js", | ||
@@ -22,3 +22,5 @@ "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>", | ||
"scripts": { | ||
"build": "tsup" | ||
"build": "tsup", | ||
"test": "uvu -r solid-register", | ||
"watch-test": "watchlist src test -- npm test" | ||
}, | ||
@@ -32,12 +34,13 @@ "keywords": [ | ||
"devDependencies": { | ||
"jsdom": "^18.1.1", | ||
"prettier": "^2.4.1", | ||
"jsdom": "^19.0.0", | ||
"prettier": "^2.5.1", | ||
"solid-register": "^0.0.18", | ||
"tslib": "^2.3.1", | ||
"tsup": "^5.10.1", | ||
"uvu": "^0.5.2" | ||
"tsup": "^5.11.0", | ||
"uvu": "^0.5.2", | ||
"watchlist": "^0.3.1" | ||
}, | ||
"peerDependencies": { | ||
"solid-js": "^1.2.5" | ||
"solid-js": "^1.2.6" | ||
} | ||
} | ||
} |
# @solid-primitives/utils | ||
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. | ||
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. | ||
@@ -18,2 +18,6 @@ ## Changelog | ||
0.0.260 | ||
Added comments for util methods. | ||
</details> |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11478
257
23
7