@solid-primitives/utils
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,2 +0,2 @@ | ||
import { P as Predicate, c as MappingFn } from './types-1e8c1610'; | ||
import { P as Predicate, c as MappingFn } from './types-5d7b3b37'; | ||
import 'solid-js'; | ||
@@ -35,2 +35,3 @@ | ||
* @example | ||
* ```ts | ||
* const newList = drop([1,2,3]) | ||
@@ -41,2 +42,3 @@ * newList // => [2,3] | ||
* newList // => [3] | ||
* ``` | ||
*/ | ||
@@ -75,4 +77,6 @@ declare const drop: <T>(list: T[], n?: number) => T[]; | ||
* @example | ||
* ```ts | ||
* const newObject = omit({ a:"foo", b:"bar", c: "baz" }, 'a', 'b') | ||
* newObject // => { c: "baz" } | ||
* ``` | ||
*/ | ||
@@ -84,4 +88,6 @@ declare const omit: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Omit<O, K>; | ||
* @example | ||
* ```ts | ||
* const newObject = pick({ a:"foo", b:"bar", c: "baz" }, 'a', 'b') | ||
* newObject // => { a:"foo", b:"bar" } | ||
* ``` | ||
*/ | ||
@@ -88,0 +94,0 @@ declare const pick: <O extends object, K extends keyof O>(object: O, ...keys: K[]) => Pick<O, K>; |
@@ -1,9 +0,10 @@ | ||
import { onCleanup } from 'solid-js'; | ||
import { Accessor, onCleanup } from 'solid-js'; | ||
import { OnOptions, EffectFunction, NoInfer, Owner } from 'solid-js/types/reactive/signal'; | ||
import { Store } from 'solid-js/store'; | ||
export { isServer } from 'solid-js/web'; | ||
import { M as MaybeAccessorValue, a as MaybeAccessor, I as ItemsOf, V as Values, D as Destore, F as Fn } from './types-1e8c1610'; | ||
export { g as AnyFunction, A as AnyObject, f as DeepPartialAny, D as Destore, F as Fn, I as ItemsOf, K as Keys, b as Many, c as MappingFn, a as MaybeAccessor, M as MaybeAccessorValue, d as Modify, e as ModifyDeep, P as Predicate, h as PrimitiveValue, V as Values } from './types-1e8c1610'; | ||
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'; | ||
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'; | ||
/** no operation */ | ||
declare const noop: (...a: any[]) => void; | ||
declare const noop: Noop; | ||
declare const isClient = true; | ||
@@ -15,7 +16,10 @@ | ||
declare const isDefined: <T>(value: T | null | undefined) => value is T; | ||
declare const isFunction: <T>(value: Function | T) => value is Function; | ||
/** | ||
* Accesses the value of a MaybeAccessor | ||
* @example | ||
* ```ts | ||
* access("foo") // => "foo" | ||
* access(() => "foo") // => "foo" | ||
* ``` | ||
*/ | ||
@@ -26,2 +30,3 @@ declare const access: <T extends unknown>(v: T) => MaybeAccessorValue<T>; | ||
* @example | ||
* ```ts | ||
* accessAsArray('abc') // => ['abc'] | ||
@@ -31,4 +36,6 @@ * accessAsArray(() => 'abc') // => ['abc'] | ||
* accessAsArray(() => [1,2,3]) // => [1,2,3] | ||
* ``` | ||
*/ | ||
declare const accessAsArray: <T extends unknown, V = MaybeAccessorValue<T>>(value: T) => V extends any[] ? V : V[]; | ||
declare const asArray: <T>(value: T) => T extends any[] ? T : T[]; | ||
/** | ||
@@ -40,2 +47,4 @@ * Run the function if the accessed value is not `undefined` nor `null` | ||
declare const withAccess: <T, A extends MaybeAccessor<T>, V = MaybeAccessorValue<A>>(value: A, fn: (value: NonNullable<V>) => void) => void; | ||
declare const asAccessor: <A extends unknown>(v: A) => Accessor<MaybeAccessorValue<A>>; | ||
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>>; | ||
/** | ||
@@ -45,5 +54,7 @@ * Quickly iterate over an MaybeAccessor<any> | ||
* @example | ||
* ```ts | ||
* const myFunc = (source: MaybeAccessor<string[]>) => { | ||
* forEach(source, item => console.log(item)) | ||
* } | ||
* ``` | ||
*/ | ||
@@ -64,4 +75,6 @@ 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; | ||
* @example | ||
* ```ts | ||
* await promiseTimeout(1500) // will resolve void after timeout | ||
* await promiseTimeout(1500, true, 'rejection reason') // will reject 'rejection reason' after timout | ||
* ``` | ||
*/ | ||
@@ -79,2 +92,3 @@ declare const promiseTimeout: (ms: number, throwOnTimeout?: boolean, reason?: string) => Promise<void>; | ||
* @example | ||
* ```ts | ||
* // single promise | ||
@@ -86,2 +100,3 @@ * await raceTimeout(new Promise(() => {...}), 3000) | ||
* await raceTimeout(new Promise(), 3000, true, 'rejection reason') | ||
* ``` | ||
*/ | ||
@@ -97,2 +112,3 @@ declare function raceTimeout<T>(promises: T, ms: number, throwOnTimeout: true, reason?: string): T extends any[] ? Promise<Awaited<T[number]>> : Promise<Awaited<T>>; | ||
* @example | ||
* ```ts | ||
* const [state, setState] = createStore({ | ||
@@ -105,2 +121,3 @@ * count: 0, | ||
* count() | ||
* ``` | ||
*/ | ||
@@ -112,2 +129,29 @@ declare function destore<T extends Object>(store: Store<T>): Destore<T>; | ||
declare const onRootCleanup: typeof onCleanup; | ||
/** | ||
* Creates a reactive root, which will be disposed when the passed owner does. | ||
* | ||
* @param fn | ||
* @param owner a root that will trigger the cleanup | ||
* @returns whatever the "fn" returns | ||
* | ||
* @example | ||
* const owner = getOwner() | ||
* const handleClick = () => createSubRoot(owner, () => { | ||
* createEffect(() => {}) | ||
* }); | ||
*/ | ||
declare function createSubRoot<T>(fn: (dispose: Fn) => T, owner?: Owner | null): T; | ||
/** | ||
* A wrapper for creating functions with the `createSubRoot` | ||
* | ||
* @param callback | ||
* @param owner a root that will trigger the cleanup | ||
* @returns the callback function | ||
* | ||
* @example | ||
* const handleClick = createSubRootFunction(() => { | ||
* createEffect(() => {}) | ||
* }) | ||
*/ | ||
declare function createSubRootFunction<T extends AnyFunction>(callback: T, owner?: Owner | null): T; | ||
declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => { | ||
@@ -118,8 +162,3 @@ push: (...callbacks: Fn[]) => void; | ||
}; | ||
declare const stringConcat: (...a: MaybeAccessor<any>[]) => string; | ||
declare const concat: <A extends any[], V = MaybeAccessorValue<ItemsOf<A>>>(...a: A) => (V extends any[] ? ItemsOf<V> : V)[]; | ||
declare const toFloat: (string: MaybeAccessor<string>) => number; | ||
declare const toInt: (string: MaybeAccessor<string>, radix?: number | undefined) => number; | ||
declare const toArray: <A extends any[]>(...a: A) => MaybeAccessorValue<ItemsOf<A>>[]; | ||
export { access, accessAsArray, concat, createCallbackStack, destore, entries, forEach, isClient, isDefined, noop, onRootCleanup, promiseTimeout, raceTimeout, stringConcat, toArray, toFloat, toInt, withAccess }; | ||
export { access, accessAsArray, asAccessor, asArray, createCallbackStack, createSubRoot, createSubRootFunction, destore, entries, forEach, isClient, isDefined, isFunction, noop, onAccess, onRootCleanup, promiseTimeout, raceTimeout, withAccess }; |
// src/index.ts | ||
import { getOwner, onCleanup } from "solid-js"; | ||
import { createRoot, getOwner, onCleanup, runWithOwner, on } from "solid-js"; | ||
import { isServer } from "solid-js/web"; | ||
var noop = (...a) => { | ||
}; | ||
var noop = () => void 0; | ||
var isClient = !isServer; | ||
var isDefined = (value) => typeof value !== "undefined" && value !== null; | ||
var access = (v) => typeof v === "function" ? v() : v; | ||
var accessAsArray = (value) => { | ||
const _value = access(value); | ||
return Array.isArray(_value) ? _value : [_value]; | ||
}; | ||
var isFunction = (value) => typeof value === "function"; | ||
var access = (v) => isFunction(v) ? v() : v; | ||
var accessAsArray = (value) => asArray(access(value)); | ||
var asArray = (value) => Array.isArray(value) ? value : [value]; | ||
var withAccess = (value, fn) => { | ||
@@ -17,9 +15,17 @@ const _value = access(value); | ||
}; | ||
var asAccessor = (v) => isFunction(v) ? v : () => v; | ||
function onAccess(deps, fn, options) { | ||
const source = deps.map(asAccessor); | ||
return on(source, fn, options); | ||
} | ||
var forEach = (array, iterator) => accessAsArray(array).forEach(iterator); | ||
var entries = (object) => Object.entries(access(object)); | ||
var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms)); | ||
function raceTimeout(promises, ms, throwOnTimeout = false, reason = "Timeout") { | ||
const promiseList = Array.isArray(promises) ? promises : [promises]; | ||
promiseList.push(promiseTimeout(ms, throwOnTimeout, reason)); | ||
return Promise.race(promiseList); | ||
function raceTimeout(input, ms, throwOnTimeout = false, reason = "Timeout") { | ||
const promises = asArray(input); | ||
const race = Promise.race([...promises, promiseTimeout(ms, throwOnTimeout, reason)]); | ||
race.finally(() => { | ||
promises.forEach((p) => p && typeof p === "object" && typeof p.dispose === "function" && p.dispose()); | ||
}); | ||
return race; | ||
} | ||
@@ -30,3 +36,3 @@ function destore(store) { | ||
Object.keys(_store).forEach((key) => { | ||
result[key] = typeof _store[key] === "function" ? _store[key].bind(_store) : () => _store[key]; | ||
result[key] = isFunction(_store[key]) ? _store[key].bind(_store) : () => _store[key]; | ||
}); | ||
@@ -36,2 +42,10 @@ return result; | ||
var onRootCleanup = (fn) => getOwner() ? onCleanup(fn) : fn; | ||
function createSubRoot(fn, owner = getOwner()) { | ||
const [dispose, returns] = createRoot((dispose2) => [dispose2, fn(dispose2)], owner != null ? owner : void 0); | ||
owner && runWithOwner(owner, () => onCleanup(dispose)); | ||
return returns; | ||
} | ||
function createSubRootFunction(callback, owner) { | ||
return (...args) => createSubRoot(() => callback(...args), owner); | ||
} | ||
var createCallbackStack = () => { | ||
@@ -49,15 +63,10 @@ let stack = []; | ||
}; | ||
var stringConcat = (...a) => a.reduce((t, c) => t + access(c), ""); | ||
var concat = (...a) => a.reduce((t, c) => { | ||
const v = access(c); | ||
return Array.isArray(v) ? [...t, ...v] : [...t, v]; | ||
}, []); | ||
var toFloat = (string) => Number.parseFloat(access(string)); | ||
var toInt = (string, radix) => Number.parseInt(access(string), radix); | ||
var toArray = (...a) => a.map((v) => access(v)); | ||
export { | ||
access, | ||
accessAsArray, | ||
concat, | ||
asAccessor, | ||
asArray, | ||
createCallbackStack, | ||
createSubRoot, | ||
createSubRootFunction, | ||
destore, | ||
@@ -68,12 +77,10 @@ entries, | ||
isDefined, | ||
isFunction, | ||
isServer, | ||
noop, | ||
onAccess, | ||
onRootCleanup, | ||
promiseTimeout, | ||
raceTimeout, | ||
stringConcat, | ||
toArray, | ||
toFloat, | ||
toInt, | ||
withAccess | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { P as Predicate, c as MappingFn } from './types-1e8c1610'; | ||
import { P as Predicate, c as MappingFn } from './types-5d7b3b37'; | ||
import 'solid-js'; | ||
@@ -3,0 +3,0 @@ |
{ | ||
"name": "@solid-primitives/utils", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A bunch of reactive utility types and functions, for building primitives with Solid.js", | ||
@@ -51,3 +51,4 @@ "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>", | ||
"test": "uvu -r solid-register", | ||
"watch-test": "watchlist src test -- npm test" | ||
"watch-test": "watchlist src test -- npm test", | ||
"docs": "typedoc" | ||
}, | ||
@@ -61,7 +62,9 @@ "keywords": [ | ||
"devDependencies": { | ||
"jsdom": "^19.0.0", | ||
"jsdom": "18.1.1", | ||
"prettier": "^2.5.1", | ||
"solid-register": "^0.0.18", | ||
"solid-register": "^0.1.1", | ||
"tslib": "^2.3.1", | ||
"tsup": "^5.11.0", | ||
"typedoc": "^0.22.10", | ||
"typedoc-plugin-markdown": "^3.11.8", | ||
"uvu": "^0.5.2", | ||
@@ -71,4 +74,4 @@ "watchlist": "^0.3.1" | ||
"peerDependencies": { | ||
"solid-js": "^1.2.6" | ||
"solid-js": "1.3.0" | ||
} | ||
} |
@@ -15,5 +15,5 @@ # @solid-primitives/utils | ||
- [`*` - `@solid-primitives/utils`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/src/index.ts) - General utilities. | ||
- [`/fp` - `@solid-primitives/utils/fp`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/src/fp.ts) - Helpers for making changes to immutable data. | ||
- [`/setter` - `@solid-primitives/utils/setter`](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/src/setter.ts) - Modified helpers from the `/fp`, but optimized for usage with signal setters. | ||
- [`*` - `@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. | ||
- [`/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. | ||
- [`/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. | ||
@@ -23,3 +23,3 @@ Import accordingly. | ||
```ts | ||
import { access, promiseTimeout } from "@solid-primitives/utils"; | ||
import { access, promiseTimeout, MaybeAccessor } from "@solid-primitives/utils"; | ||
@@ -31,2 +31,6 @@ // between these two some function names will overlap. | ||
## >>> [Modules documentation](https://github.com/davedbase/solid-primitives/blob/main/packages/utils/docs/README.md) <<< | ||
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). | ||
## Changelog | ||
@@ -53,2 +57,6 @@ | ||
0.1.2 | ||
Updated to Solid 1.3 | ||
</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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
33139
775
59
0
9