- v0.9.72Latest
- v0.9.71
- v0.9.70
- v0.9.68
- v0.9.67
- v0.9.66
- v0.9.65
- v0.9.64
- v0.9.63
- v0.9.62
- v0.9.61
- v0.9.60
- v0.9.58
- v0.9.57
- v0.9.56
- v0.9.55
- v0.9.54
- v0.9.53
- v0.9.52
- v0.9.51
- v0.9.50
- v0.9.42
- v0.9.41
- v0.9.40
- v0.9.35
- v0.9.34
- v0.9.33
- v0.9.32
- v0.9.30
- v0.9.20
- v0.9.19
- v0.9.18
- v0.9.17
- v0.9.16
- v0.9.15
- v0.9.14
- v0.9.13
- v0.9.12
- v0.9.11
- v0.9.10
- v0.9.8
- v0.9.7
- v0.9.6
- v0.9.5
- v0.9.4
- v0.9.3
- v0.9.2
- v0.9.1
- v0.9.0
- v0.8.43
- v0.8.41
- v0.8.40
- v0.8.30
- v0.8.25
- v0.8.24
- v0.8.23
- v0.8.22
- v0.8.21
- v0.8.20
- v0.8.11
- v0.8.10
- v0.8.9
- v0.8.8
- v0.8.7
- v0.8.6
- v0.8.5
- v0.8.4
- v0.8.3
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.11
- v0.7.10
- v0.7.8
- v0.7.7
- v0.7.6
- v0.7.5
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.0
- v0.6.31
- v0.6.30
- v0.6.29
- v0.6.27
- v0.6.26
- v0.6.25
- v0.6.24
- v0.6.22
- v0.6.21
- v0.6.20
- v0.6.18
- v0.6.17
- v0.6.16
- v0.6.15
- v0.6.14
- v0.6.13
- v0.6.12
- v0.6.11
- v0.6.10
- v0.6.9
- v0.6.8
- v0.6.7
- v0.6.6
- v0.6.5
- v0.6.4
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.7
- v0.5.5
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
JsExt
Additional functions for JavaScript programming in practice.
Install
npm i @ayonli/jsext
Usages
import jsext from "@ayonli/jsext";
Note: this package works well in both CommonJS and ES Module.
Functions
try<E = Error, T = any, A extends any[] = any[], TReturn = any, TNext = unknown>(fn: (...args: A) => AsyncGenerator<T, TReturn, TNext>, ...args: A): AsyncGenerator<[E, T], [E, TReturn], TNext>
try<E = Error, T = any, A extends any[] = any[], TReturn = any, TNext = unknown>(fn: (...args: A) => Generator<T, TReturn, TNext>, ...args: A): Generator<[E, T], [E, TReturn], TNext>
try<E = Error, R = any, A extends any[] = any[]>(fn: (...args: A) => Promise<R>, ...args: A): Promise<[E, R]>
try<E = Error, R = any, A extends any[] = any[]>(fn: (...args: A) => R, ...args: A): [E, R]
try<E = Error, T = any, TReturn = any, TNext = unknown>(gen: AsyncGenerator<T, TReturn, TNext>): AsyncGenerator<[E, T], [E, TReturn], TNext>
try<E = Error, T = any, TReturn = any, TNext = unknown>(gen: Generator<T, TReturn, TNext>): Generator<[E, T], [E, TReturn], TNext>
try<E = Error, R = any>(job: Promise<R>): Promise<[E, R]>
func<T, R = any, A extends any[] = any[]>(fn: (this: T, defer: (cb: () => void) => void, ...args: A) => R): (this: T, ...args: A) => R
wrap<T, Fn extends (this: T, ...args: any[]) => any>(fn: Fn, wrapper: (this: T, fn: Fn, ...args: Parameters<Fn>) => ReturnType<Fn>): Fn
throttle<T, Fn extends (this: T, ...args: any[]) => any>(handler: Fn, duration: number): Fn
throttle<T, Fn extends (this: T, ...args: any[]) => any>(handler: Fn, options: { duration: number; for?: any }): Fn
mixins<T extends Constructor<any>, M extends any[]>(base: T, ...mixins: { [X in keyof M]: Constructor<M[X]> }): T & Constructor<UnionToIntersection<FlatArray<M, 1>>>
mixins<T extends Constructor<any>, M extends any[]>(base: T, ...mixins: M): T & Constructor<UnionToIntersection<FlatArray<M, 1>>>
isSubclassOf<T, B>(ctor1: Constructor<T>, ctor2: Constructor<B>): boolean
read<I extends AsyncIterable<any>>(iterable: I): I
read(es: EventSource, options?: { event?: string; }): AsyncIterable<string>
read<T extends Uint8Array | string>(ws: WebSocket): AsyncIterable<T>
read<T>(target: EventTarget, eventMap?: { message?: string; error?: string; close?: string; }): AsyncIterable<T>
read<T>(target: NodeJS.EventEmitter, eventMap?: { data?: string; error?: string; close?: string; }): AsyncIterable<T>
run<T, A extends any[] = any[]>(script: string, args?: A, options?: { fn?: string; timeout?: number; adapter?: "worker_threads" | "child_process" }): Promise<{ workerId: number; abort(): Promise<void>; result(): Promise<T>; iterate(): AsyncIterable<T>; }>
See index.ts for details and docs.
Types
AsyncFunction
AsyncGeneratorFunction
AsyncFunctionConstructor
Constructor<T>
TypedArray
Optional<T, K extends keyof T>
Ensured<T, K extends keyof T>
When augmenting, these types will ba attached to the global namespace.
Sub-packages
string
import { capitalize, chunk, /* ... */ } from "@ayonli/jsext/string";
// or
import "@ayonli/jsext/string/augment";
Functions
compare(str1: string, str2: string): -1 | 0 | 1
random(length: number): string
count(str: string, sub: string): number
capitalize(str: string, all?: boolean): string
hyphenate(str: string): string
words(str: string): string[]
chunk(str: string, length: number): string[]
truncate(str: string, length: number): string
byteLength(str: string): number
String
compare(str1: string, str2: string): -1 | 0 | 1
random(length: number): string
prototype
count(sub: string): number
capitalize(all?: boolean): string
hyphenate(): string
words(): string[]
chunk(length: number): string[]
truncate(length: number): string
byteLength(): number
number
import { isFloat, random } from "@ayonli/jsext/number";
// or
import "@ayonli/jsext/number/augment";
Functions
isFloat(value: unknown): boolean
isNumeric(value: unknown): boolean
isBetween(value: number, [min, max]: [number, number]): boolean
random(min: number, max: number): number
sequence(min: number, max: number, step?: number, loop?: boolean): Generator<number, void, unknown>
When augmenting, these functions will be attached to the Number
constructor.
array
import { count, split, /* ... */ } from "@ayonli/jsext/array";
// or
import "@ayonli/jsext/array/augment";
Functions
count<T>(arr: RealArrayLike<T>, ele: T): number
equals<T>(arr1: RealArrayLike<T>, arr2: RealArrayLike<T>): boolean
split<T>(arr: RealArrayLike<T>, delimiter: T): RealArrayLike<T>[]
chunk<T>(arr: RealArrayLike<T>, length: number): RealArrayLike<T>[]
uniq<T>(arr: T[]): T[]
shuffle<T>(arr: T[]): T[]
orderBy<T>(arr: T[], key: keyof T, order: "asc" | "desc" = "asc"): T[]
groupBy<T>(arr: T[], fn: (item: T, i: number) => string | symbol, type?: ObjectConstructor): Record<string | symbol, T[]>
groupBy<T, K extends string>(arr: T[], fn: (item: T, i: number) => K, type: MapConstructor): Map<K, T[]>
Array<T>
prototype
first(): T
last(): T
count(ele: T): number
equals(another: T[]): boolean
split(delimiter: T): T[][]
chunk(length: number): T[][]
uniq(): T[]
shuffle(): T[]
toShuffled(): T[]
toReversed(): T[]
toSorted(fn?: ((a: T, b: T) => number) | undefined): T[]
orderBy(key: keyof T, order?: "asc" | "desc"): T[]
groupBy(fn: (item: T, i: number) => string | symbol, type?: ObjectConstructor): Record<string | symbol, T[]>
groupBy<K>(fn: (item: T, i: number) => K, type: MapConstructor): Map<K, T[]>
uint8array
import { compare, split, /* ... */ } from "@ayonli/jsext/uint8array";
// or
import "@ayonli/jsext/uint8array/augment";
Functions
compare(arr1: Uint8Array, arr2: Uint8Array): -1 | 0 | 1
equals(arr1: Uint8Array, arr2: Uint8Array): boolean
split<T extends Uint8Array>(arr: T, delimiter: number): T[]
chunk<T extends Uint8Array>(arr: T, length: number): T[]
Uint8Array
compare(arr1: Uint8Array, arr2: Uint8Array): -1 | 0 | 1
prototype
equals(another: Uint8Array): boolean
split(delimiter: number): this[]
chunk(length: number): this[]
object
import { hasOwn, path, /* ... */ } from "@ayonli/jsext/object";
// or
import "@ayonli/jsext/object/augment";
Functions
hasOwn(obj: any, key: string | number | symbol): boolean
hasOwnMethod(obj: any, method: string | symbol): boolean
patch<T extends {}, U>(target: T, source: U): T & U
patch<T extends {}, U, V>(target: T, source1: U, source2: V): T & U & V
patch<T extends {}, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W
patch(target: object, ...sources: any[]): any
pick<T extends object, U extends keyof T>(obj: T, keys: U[]): Pick<T, U>
pick<T>(obj: T, keys: (string | symbol)[]): Partial<T>
omit<T extends object, U extends keyof T>(obj: T, keys: U[]): Omit<T, U>
omit<T>(obj: T, keys: (string | symbol)[]): Partial<T>
as(value: unknown, type: StringConstructor): string | null
as(value: unknown, type: NumberConstructor): number | null
as(value: unknown, type: BigIntConstructor): bigint | null
as(value: unknown, type: BooleanConstructor): boolean | null
as(value: unknown, type: SymbolConstructor): symbol | null
as<T>(value: unknown, type: Constructor<T>): T | null
isValid(value: unknown): boolean
When augmenting, these functions will be attached to the Object
constructor.
math
import { sum, avg, product } from "@ayonli/jsext/math";
// or
import "@ayonli/jsext/math/augment";
Functions
sum(...values: number[]): number
avg(...values: number[]): number
product(...values: number[]): number
When augmenting, these functions will be attached to the Math
namespace.
promise
import { timeout, after, sleep, until } from "@ayonli/jsext/promise";
// or
import "@ayonli/jsext/promise/augment";
Functions
timeout<T>(value: T | PromiseLike<T>, ms: number): Promise<T>
after<T>(value: T | PromiseLike<T>, ms: number): Promise<T>
sleep(ms: number): Promise<void>
until(test: () => boolean | Promise<boolean>): Promise<void>
When augmenting, these functions will be attached to the Promise
constructor.
collections
import BiMap from "@ayonli/jsext/collections/BiMap";
import CiMap from "@ayonli/jsext/collections/CiMap";
// or
import { BiMap, CiMap } from "@ayonli/jsext/collections";
// or
import "@ayonli/jsext/collections/augment";
Types
BiMap<K, V>
(extendsMap<K, V>
) Bi-directional map, keys and values are unique and map to each other.prototype
(additional)getKey(value: V): K | undefined
hasValue(value: V): boolean
deleteValue(value: V): boolean
CiMap<K extends string, V>
(implementsMap<K, V>
) Case-insensitive map, keys are case-insensitive.
When augmenting, these types will be exposed to the global namespace.
error
import Exception from "@ayonli/jsext/error/Exception";
// or
import { Exception } from "@ayonli/jsext/error";
// or
import "@ayonli/jsext/error/augment";
Types
Exception
(extendsError
)cause?: unknown
code: number
When augmenting, these types will be exposed to the global namespace.
Functions
toObject<T extends Error>(err: T): { [x: string | symbol]: any; }
fromObject<T extends Error>(obj: { [x: string | symbol]: any; }): T
Error
toObject<T extends Error>(err: T): { [x: string | symbol]: any; }
fromObject<T extends Error>(obj: { [x: string | symbol]: any; }): T
prototype
toJSON(): { [x: string | symbol]: any; }
Import all sub-package augmentation at once
import "@ayonli/jsext/augment";
When to use augmentations
If we’re developing libraries and share them openly on NPM, in order to prevent collision, it’s better not to use augmentations, but use the corresponding functions from the sub-packages instead.
But if we’re developing private projects, using augmentations can save a lot of time, it’s easier to read and write, and make sense.
Web Support
When using this package in the browser, there are three ways to import this package.
- Import From
node_modules
This is the same as above, but requires a module bundler such as webpack.
- Import ES Module
<script type="module">
import jsext from "https://deno.land/x/ayonli_jsext/esm/index.js";
import "https://deno.land/x/ayonli_jsext/esm/augment.js";
// or sub-packages
import { isFloat, isNumeric } from "https://deno.land/x/ayonli_jsext/esm/number/index.js";
import "https://deno.land/x/ayonli_jsext/esm/number/augment.js";
</script>
Note: the ES module can also be used in Deno.
- Include Bundle
<script src="https://deno.land/x/ayonli_jsext/bundle/index.js"></script>
<script>
const jsext = window["@ayonli/jsext"];
// this will also include the augmentations
<script>