@antfu/utils
Advanced tools
Comparing version 0.2.4 to 0.3.0
@@ -64,2 +64,3 @@ export { debounce, throttle } from 'throttle-debounce'; | ||
declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>; | ||
declare type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any; | ||
/** | ||
@@ -71,3 +72,8 @@ * Divide an array into two parts by a filter function | ||
*/ | ||
declare function partition<T>(array: readonly T[], filter: (i: T, idx: number, arr: readonly T[]) => any): T[][]; | ||
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>): [T[], T[]]; | ||
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>): [T[], T[], T[]]; | ||
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>): [T[], T[], T[], T[]]; | ||
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>): [T[], T[], T[], T[], T[]]; | ||
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>, f5: PartitionFilter<T>): [T[], T[], T[], T[], T[], T[]]; | ||
declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>, f5: PartitionFilter<T>, f6: PartitionFilter<T>): [T[], T[], T[], T[], T[], T[], T[]]; | ||
/** | ||
@@ -123,3 +129,3 @@ * Unique an Array | ||
declare const assert: (condition: boolean, message: string) => asserts condition; | ||
declare const toString: () => string; | ||
declare const toString: (v: any) => string; | ||
declare const noop: () => void; | ||
@@ -180,2 +186,15 @@ | ||
declare function ensurePrefix(prefix: string, str: string): string; | ||
/** | ||
* Dead simple template engine, just like Python's `.format()` | ||
* | ||
* @example | ||
* ``` | ||
* const result = template( | ||
* 'Hello {0}! My name is {1}.', | ||
* 'Inès', | ||
* 'Anthony' | ||
* ) // Hello Inès! My name is Anthony. | ||
* ``` | ||
*/ | ||
declare function template(str: string, ...args: any[]): string; | ||
@@ -306,3 +325,24 @@ declare const timestamp: () => number; | ||
declare function sleep(ms: number, callback?: Fn<any>): Promise<void>; | ||
/** | ||
* Create a promise lock | ||
* | ||
* @example | ||
* ``` | ||
* const lock = createPromiseLock() | ||
* | ||
* lock.run(async () => { | ||
* await doSomething() | ||
* }) | ||
* | ||
* // in anther context: | ||
* await lock.wait() // it will wait all tasking finished | ||
* ``` | ||
*/ | ||
declare function createPromiseLock(): { | ||
run<T = void>(fn: () => Promise<T>): Promise<T>; | ||
wait(): Promise<void>; | ||
isWaiting(): boolean; | ||
clear(): void; | ||
}; | ||
export { ArgumentsType, Arrayable, Awaitable, Constructor, DeepMerge, ElementOf, Fn, MergeInsertions, Nullable, SingletonPromiseReturn, UnionToIntersection, assert, at, batchInvoke, clamp, clampArrayRange, clearUndefined, createSingletonPromise, deepMerge, ensurePrefix, flattenArrayable, hasOwnProperty, invoke, isBoolean, isBrowser, isDef, isFunction, isKeyOf, isNumber, isObject, isString, isTruthy, isWindow, last, mergeArrayable, move, noNull, noop, notNullish, notUndefined, objectEntries, objectKeys, objectMap, objectPick, partition, range, remove, slash, sleep, sum, tap, timestamp, toArray, toString, uniq }; | ||
export { ArgumentsType, Arrayable, Awaitable, Constructor, DeepMerge, ElementOf, Fn, MergeInsertions, Nullable, PartitionFilter, SingletonPromiseReturn, UnionToIntersection, assert, at, batchInvoke, clamp, clampArrayRange, clearUndefined, createPromiseLock, createSingletonPromise, deepMerge, ensurePrefix, flattenArrayable, hasOwnProperty, invoke, isBoolean, isBrowser, isDef, isFunction, isKeyOf, isNumber, isObject, isString, isTruthy, isWindow, last, mergeArrayable, move, noNull, noop, notNullish, notUndefined, objectEntries, objectKeys, objectMap, objectPick, partition, range, remove, slash, sleep, sum, tap, template, timestamp, toArray, toString, uniq }; |
@@ -22,7 +22,16 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/math.ts | ||
} | ||
function partition(array, filter) { | ||
const pass = []; | ||
const fail = []; | ||
array.forEach((e, idx, arr) => (filter(e, idx, arr) ? pass : fail).push(e)); | ||
return [pass, fail]; | ||
function partition(array, ...filters) { | ||
const result = new Array(filters.length + 1).fill(null).map(() => []); | ||
array.forEach((e, idx, arr) => { | ||
let i = 0; | ||
for (const filter of filters) { | ||
if (filter(e, idx, arr)) { | ||
result[i].push(e); | ||
return; | ||
} | ||
i += 1; | ||
} | ||
result[i].push(e); | ||
}); | ||
return result; | ||
} | ||
@@ -83,3 +92,3 @@ function uniq(array) { | ||
}; | ||
var toString2 = Object.prototype.toString; | ||
var toString2 = (v) => Object.prototype.toString.call(v); | ||
var noop = () => { | ||
@@ -121,2 +130,10 @@ }; | ||
} | ||
function template(str, ...args) { | ||
return str.replace(/{(\d+)}/g, (match, key) => { | ||
const index = Number(key); | ||
if (Number.isNaN(index)) | ||
return match; | ||
return args[index]; | ||
}); | ||
} | ||
@@ -214,2 +231,25 @@ // src/time.ts | ||
} | ||
function createPromiseLock() { | ||
const locks = []; | ||
return { | ||
async run(fn) { | ||
const p = fn(); | ||
locks.push(p); | ||
try { | ||
return await p; | ||
} finally { | ||
remove(locks, p); | ||
} | ||
}, | ||
async wait() { | ||
await Promise.allSettled(locks); | ||
}, | ||
isWaiting() { | ||
return Boolean(locks.length); | ||
}, | ||
clear() { | ||
locks.length = 0; | ||
} | ||
}; | ||
} | ||
@@ -314,2 +354,4 @@ // node_modules/.pnpm/throttle-debounce@3.0.1/node_modules/throttle-debounce/esm/index.js | ||
exports.assert = assert; exports.at = at; exports.batchInvoke = batchInvoke; exports.clamp = clamp; exports.clampArrayRange = clampArrayRange; exports.clearUndefined = clearUndefined; exports.createSingletonPromise = createSingletonPromise; exports.debounce = debounce; exports.deepMerge = deepMerge; exports.ensurePrefix = ensurePrefix; exports.flattenArrayable = flattenArrayable; exports.hasOwnProperty = hasOwnProperty; exports.invoke = invoke; exports.isBoolean = isBoolean; exports.isBrowser = isBrowser; exports.isDef = isDef; exports.isFunction = isFunction; exports.isKeyOf = isKeyOf; exports.isNumber = isNumber; exports.isObject = isObject; exports.isString = isString; exports.isTruthy = isTruthy; exports.isWindow = isWindow; exports.last = last; exports.mergeArrayable = mergeArrayable; exports.move = move; exports.noNull = noNull; exports.noop = noop; exports.notNullish = notNullish; exports.notUndefined = notUndefined; exports.objectEntries = objectEntries; exports.objectKeys = objectKeys; exports.objectMap = objectMap; exports.objectPick = objectPick; exports.partition = partition; exports.range = range; exports.remove = remove; exports.slash = slash; exports.sleep = sleep; exports.sum = sum; exports.tap = tap; exports.throttle = throttle; exports.timestamp = timestamp; exports.toArray = toArray; exports.toString = toString2; exports.uniq = uniq; | ||
exports.assert = assert; exports.at = at; exports.batchInvoke = batchInvoke; exports.clamp = clamp; exports.clampArrayRange = clampArrayRange; exports.clearUndefined = clearUndefined; exports.createPromiseLock = createPromiseLock; exports.createSingletonPromise = createSingletonPromise; exports.debounce = debounce; exports.deepMerge = deepMerge; exports.ensurePrefix = ensurePrefix; exports.flattenArrayable = flattenArrayable; exports.hasOwnProperty = hasOwnProperty; exports.invoke = invoke; exports.isBoolean = isBoolean; exports.isBrowser = isBrowser; exports.isDef = isDef; exports.isFunction = isFunction; exports.isKeyOf = isKeyOf; exports.isNumber = isNumber; exports.isObject = isObject; exports.isString = isString; exports.isTruthy = isTruthy; exports.isWindow = isWindow; exports.last = last; exports.mergeArrayable = mergeArrayable; exports.move = move; exports.noNull = noNull; exports.noop = noop; exports.notNullish = notNullish; exports.notUndefined = notUndefined; exports.objectEntries = objectEntries; exports.objectKeys = objectKeys; exports.objectMap = objectMap; exports.objectPick = objectPick; exports.partition = partition; exports.range = range; exports.remove = remove; exports.slash = slash; exports.sleep = sleep; exports.sum = sum; exports.tap = tap; exports.template = template; exports.throttle = throttle; exports.timestamp = timestamp; exports.toArray = toArray; exports.toString = toString2; exports.uniq = uniq; |
{ | ||
"name": "@antfu/utils", | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"description": "Opinionated collection of common JavaScript / TypeScript utils by @antfu", | ||
@@ -40,17 +40,17 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@antfu/eslint-config": "^0.6.6", | ||
"@antfu/eslint-config": "^0.7.0", | ||
"@antfu/ni": "^0.7.0", | ||
"@types/jest": "^26.0.23", | ||
"@types/node": "^15.12.4", | ||
"@types/jest": "^27.0.1", | ||
"@types/node": "^16.7.1", | ||
"bumpp": "^6.0.6", | ||
"eslint": "^7.29.0", | ||
"eslint-plugin-jest": "^24.3.6", | ||
"esno": "^0.7.3", | ||
"eslint": "^7.32.0", | ||
"eslint-plugin-jest": "^24.4.0", | ||
"esno": "^0.9.1", | ||
"git-ensure": "^0.1.0", | ||
"jest": "^27.0.5", | ||
"jest": "^27.0.6", | ||
"throttle-debounce": "^3.0.1", | ||
"ts-jest": "^27.0.3", | ||
"tsup": "^4.11.2", | ||
"typescript": "^4.3.4" | ||
"ts-jest": "^27.0.5", | ||
"tsup": "^4.14.0", | ||
"typescript": "^4.3.5" | ||
} | ||
} |
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
29759
973