@wixc3/common
Advanced tools
Comparing version 3.0.5 to 3.0.7
@@ -1,2 +0,2 @@ | ||
import { sleep } from "promise-assist"; | ||
import { sleep } from 'promise-assist'; | ||
/** | ||
@@ -3,0 +3,0 @@ * @returns a no operation function |
{ | ||
"name": "@wixc3/common", | ||
"version": "3.0.5", | ||
"version": "3.0.7", | ||
"description": "Common utils, usable in all environments", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
175
README.md
@@ -27,89 +27,90 @@ [![npm version](https://badge.fury.io/js/@wixc3%2Fcommon.svg)](https://badge.fury.io/js/@wixc3%2Fcommon) | ||
| Function | Description | | ||
| ------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| [assertIsString(value, errorMessage)](https://wixplosives.github.io/core3-utils/common.assertisstring) | Throws if value is not a string | | ||
| [at(iterable, index)](https://wixplosives.github.io/core3-utils/common.at) | Finds element by index, including negative index | | ||
| [awaitRecord(obj)](https://wixplosives.github.io/core3-utils/common.awaitrecord) | Awaits a record of promises, and returns a record of their results. | | ||
| [capitalizeFirstLetter(val)](https://wixplosives.github.io/core3-utils/common.capitalizefirstletter) | Capitalize the first letter of a string | | ||
| [chain(value)](https://wixplosives.github.io/core3-utils/common.chain) | Chain iterable operations, each acting on the output of the previous step | | ||
| [chain(value)](https://wixplosives.github.io/core3-utils/common.chain_1) | Chain iterable operations, each acting on the output of the previous step | | ||
| [clamp(value, min, max)](https://wixplosives.github.io/core3-utils/common.clamp) | | | ||
| [concat(iterables)](https://wixplosives.github.io/core3-utils/common.concat) | | | ||
| [createRandomIntWithSeed(seed)](https://wixplosives.github.io/core3-utils/common.createrandomintwithseed) | | | ||
| [defaults(\_source, \_defaultValues, deep, shouldUseDefault)](https://wixplosives.github.io/core3-utils/common.defaults) | Returns an object where missing keys and values/keys that satisfy shouldUseDefault to the value in shouldUseDefault. | | ||
| [enumValues(enumObj)](https://wixplosives.github.io/core3-utils/common.enumvalues) | | | ||
| [enumValues(enumObj)](https://wixplosives.github.io/core3-utils/common.enumvalues_1) | | | ||
| [equalIdents(reference, modified, newline)](https://wixplosives.github.io/core3-utils/common.equalidents) | Matches the indentation of modified to the one of reference | | ||
| [errorToPlainObject(error)](https://wixplosives.github.io/core3-utils/common.errortoplainobject) | | | ||
| [escapeRegExp(str)](https://wixplosives.github.io/core3-utils/common.escaperegexp) | Returns a string safe to be used in RegExp | | ||
| [every(iterable, predicate)](https://wixplosives.github.io/core3-utils/common.every) | | | ||
| [exclude(excluded)](https://wixplosives.github.io/core3-utils/common.exclude) | | | ||
| [filter(iterable, predicate)](https://wixplosives.github.io/core3-utils/common.filter) | | | ||
| [find(iterable, predicate)](https://wixplosives.github.io/core3-utils/common.find) | | | ||
| [first(iterable)](https://wixplosives.github.io/core3-utils/common.first) | Picks the first element of an iterable | | ||
| [flat(iterable, deep)](https://wixplosives.github.io/core3-utils/common.flat) | | | ||
| [flatMap(iterable, mapFn)](https://wixplosives.github.io/core3-utils/common.flatmap) | | | ||
| [forEach(iterable, fn)](https://wixplosives.github.io/core3-utils/common.foreach) | | | ||
| [get(obj, key)](https://wixplosives.github.io/core3-utils/common.get) | Similar to Map.get, but works for plain objects, and returns undefined for null maps and missing keys | | ||
| [get(obj, key)](https://wixplosives.github.io/core3-utils/common.get_1) | | | ||
| [getCartesianProductOfArrays(arrays)](https://wixplosives.github.io/core3-utils/common.getcartesianproductofarrays) | | | ||
| [getErrorCode(error)](https://wixplosives.github.io/core3-utils/common.geterrorcode) | Returns error.code property if the error object has it, otherwise returns undefined. | | ||
| [getIn(obj, path)](https://wixplosives.github.io/core3-utils/common.getin) | | | ||
| [getValue(map, key, errorMessage)](https://wixplosives.github.io/core3-utils/common.getvalue) | Returns a value by key, throws if the value is missing or the map null | | ||
| [getValue(map, key, errorMessage)](https://wixplosives.github.io/core3-utils/common.getvalue_1) | | | ||
| [getValue(map, key, errorMessage)](https://wixplosives.github.io/core3-utils/common.getvalue_2) | | | ||
| [groupBy(elements, property)](https://wixplosives.github.io/core3-utils/common.groupby) | Groups elements by the value of a property | | ||
| [has(obj, key)](https://wixplosives.github.io/core3-utils/common.has) | Similar to Map.has, but works for plain objects, and returns false for null maps | | ||
| [has(obj, key)](https://wixplosives.github.io/core3-utils/common.has_1) | Similar to Map.has, but works for plain objects, and returns false for null maps | | ||
| [has(obj, key)](https://wixplosives.github.io/core3-utils/common.has_2) | | | ||
| [histogram(iterable)](https://wixplosives.github.io/core3-utils/common.histogram) | Calculate a histogram of iterable elements | | ||
| [includes(iterable, item)](https://wixplosives.github.io/core3-utils/common.includes) | | | ||
| [includesCaseInsensitive(str, substr)](https://wixplosives.github.io/core3-utils/common.includescaseinsensitive) | Checks if str contains substr ignoring capitalization | | ||
| [indexToLineAndColumn(content, pos, newline)](https://wixplosives.github.io/core3-utils/common.indextolineandcolumn) | Finds line an column by position index | | ||
| [isDefined(value)](https://wixplosives.github.io/core3-utils/common.isdefined) | Given a value of type Nullable<T>, validates value is T | | ||
| [isEmpty(iterable)](https://wixplosives.github.io/core3-utils/common.isempty) | Checks if an iterable is empty | | ||
| [isErrorLikeObject(error)](https://wixplosives.github.io/core3-utils/common.iserrorlikeobject) | | | ||
| [isIterable(x)](https://wixplosives.github.io/core3-utils/common.isiterable) | | | ||
| [isPlainObject(value)](https://wixplosives.github.io/core3-utils/common.isplainobject) | Checks that value is a POJO | | ||
| [isValidNamingConvention(namingConvention)](https://wixplosives.github.io/core3-utils/common.isvalidnamingconvention) | Checks if namingConvention is supported | | ||
| [join(iterable, separator)](https://wixplosives.github.io/core3-utils/common.join) | | | ||
| [keys(map)](https://wixplosives.github.io/core3-utils/common.keys) | | | ||
| [keys(map)](https://wixplosives.github.io/core3-utils/common.keys_1) | | | ||
| [last(iterable)](https://wixplosives.github.io/core3-utils/common.last) | Picks the last element of an iterable | | ||
| [map(iterable, mapFn)](https://wixplosives.github.io/core3-utils/common.map) | Map iterable elements | | ||
| [mapKeys(obj, mapping)](https://wixplosives.github.io/core3-utils/common.mapkeys) | Maps values of a plain object | | ||
| [mapObject(obj, mapping)](https://wixplosives.github.io/core3-utils/common.mapobject) | Maps key value pairs of a plain object | | ||
| [mapValues(obj, mapping)](https://wixplosives.github.io/core3-utils/common.mapvalues) | Maps values of a plain object | | ||
| [minimalIndent(str)](https://wixplosives.github.io/core3-utils/common.minimalindent) | Shifts all indentation to the left using the line with the least indentation as a baseline | | ||
| [mulberry32(a)](https://wixplosives.github.io/core3-utils/common.mulberry32) | | | ||
| [naiveStripComments(str)](https://wixplosives.github.io/core3-utils/common.naivestripcomments) | Removes comments from string Note that there's lexical no parsing, so stuff like "//'//" will not work | | ||
| [next(iterable, item)](https://wixplosives.github.io/core3-utils/common.next) | Find the element following an item | | ||
| [noIdents(modified, separator)](https://wixplosives.github.io/core3-utils/common.noidents) | Remove line indentation (heading whitespace) | | ||
| [normToRage(normal, min, max)](https://wixplosives.github.io/core3-utils/common.normtorage) | | | ||
| [noWhiteSpace(str)](https://wixplosives.github.io/core3-utils/common.nowhitespace) | Remove white spaces including empty lines | | ||
| [once(fn)](https://wixplosives.github.io/core3-utils/common.once) | Make a function executable only once, following calls are ignored | | ||
| [pick(record, keys)](https://wixplosives.github.io/core3-utils/common.pick) | returns an object composed of the picked object properties | | ||
| [prev(iterable, item)](https://wixplosives.github.io/core3-utils/common.prev) | Find the element before an item | | ||
| [reduce(iterable, reducer, initial)](https://wixplosives.github.io/core3-utils/common.reduce) | | | ||
| [repeat(str, count)](https://wixplosives.github.io/core3-utils/common.repeat) | Generates a string repeating \[str\] \[count\] times | | ||
| [reportError_2(ex)](https://wixplosives.github.io/core3-utils/common.reporterror_2) | Logs an error | | ||
| [reverseObject(obj)](https://wixplosives.github.io/core3-utils/common.reverseobject) | Reverses keys-values of an object, ignoring falsy values. First takes on value collisions. | | ||
| [same(a, b, unordered)](https://wixplosives.github.io/core3-utils/common.same) | Deep comparison of two objects | | ||
| [size(iterable)](https://wixplosives.github.io/core3-utils/common.size) | Evaluate the size of an iterable | | ||
| [skip(iterable, count)](https://wixplosives.github.io/core3-utils/common.skip) | Skips the first elements of an iterable | | ||
| [some(iterable, predicate)](https://wixplosives.github.io/core3-utils/common.some) | | | ||
| [sort(iterable, by)](https://wixplosives.github.io/core3-utils/common.sort) | | | ||
| [stringifyErrorStack(error)](https://wixplosives.github.io/core3-utils/common.stringifyerrorstack) | | | ||
| [templateCompilerProvider(context)](https://wixplosives.github.io/core3-utils/common.templatecompilerprovider) | Similar to templated string, given a fixed context object returns a function that parses strings in it | | ||
| [toCamelCase(str)](https://wixplosives.github.io/core3-utils/common.tocamelcase) | Converts a string to camelCase | | ||
| [toCSSCamelCase(str)](https://wixplosives.github.io/core3-utils/common.tocsscamelcase) | like [toCamelCase()](https://wixplosives.github.io/core3-utils/common.tocamelcase)<!-- -->, but capitalizes first character if input starts with '-' | | ||
| [toCSSKebabCase(str)](https://wixplosives.github.io/core3-utils/common.tocsskebabcase) | like [toKebabCase()](https://wixplosives.github.io/core3-utils/common.tokebabcase)<!-- -->, but prepends '-' if first character of input is UpperCase | | ||
| [toError(value)](https://wixplosives.github.io/core3-utils/common.toerror) | Convert any kind of value to an error instance. Unless the value is already an error instance, it's stringified and used as the error message. | | ||
| [toKebabCase(str)](https://wixplosives.github.io/core3-utils/common.tokebabcase) | Converts a string to kebab-case | | ||
| [toNamingConvention(str, namingConvention)](https://wixplosives.github.io/core3-utils/common.tonamingconvention) | Converts string formatting to a naming convention | | ||
| [toPascalCase(str)](https://wixplosives.github.io/core3-utils/common.topascalcase) | Converts a string to PascalCase | | ||
| [toPascalCaseJsIdentifier(str)](https://wixplosives.github.io/core3-utils/common.topascalcasejsidentifier) | Similar to [toPascalCase()](https://wixplosives.github.io/core3-utils/common.topascalcase)<!-- -->, but drops heading non-letters | | ||
| [unique(iterable, by)](https://wixplosives.github.io/core3-utils/common.unique) | Creates iterable of unique elements | | ||
| [values(map)](https://wixplosives.github.io/core3-utils/common.values) | | | ||
| [values(map)](https://wixplosives.github.io/core3-utils/common.values_1) | | | ||
| Function | Description | | ||
| ------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ||
| [assertIsString(value, errorMessage)](https://wixplosives.github.io/core3-utils/common.assertisstring) | Throws if value is not a string | | ||
| [at(iterable, index)](https://wixplosives.github.io/core3-utils/common.at) | Finds element by index, including negative index | | ||
| [awaitRecord(obj)](https://wixplosives.github.io/core3-utils/common.awaitrecord) | Awaits a record of promises, and returns a record of their results. | | ||
| [capitalizeFirstLetter(val)](https://wixplosives.github.io/core3-utils/common.capitalizefirstletter) | Capitalize the first letter of a string | | ||
| [chain(value)](https://wixplosives.github.io/core3-utils/common.chain) | Chain iterable operations, each acting on the output of the previous step | | ||
| [chain(value)](https://wixplosives.github.io/core3-utils/common.chain_1) | Chain iterable operations, each acting on the output of the previous step | | ||
| [clamp(value, min, max)](https://wixplosives.github.io/core3-utils/common.clamp) | | | ||
| [concat(iterables)](https://wixplosives.github.io/core3-utils/common.concat) | | | ||
| [createRandomIntWithSeed(seed)](https://wixplosives.github.io/core3-utils/common.createrandomintwithseed) | | | ||
| [defaults(\_source, \_defaultValues, deep, shouldUseDefault)](https://wixplosives.github.io/core3-utils/common.defaults) | Returns an object where missing keys and values/keys that satisfy shouldUseDefault to the value in shouldUseDefault. | | ||
| [delayed(fn, wait)](https://wixplosives.github.io/core3-utils/common.delayed) | <p>Ensures <code>func</code> will be called with at least <code>wait</code> ms between runs. For @<!-- -->example, if <code>func</code> is called 3 times in a row: <code>func(1); func(2); func(3);</code> it will wait <code>wait</code> ms between each run: <code>func(1); await sleep(wait); func(2); await sleep(wait); func(3);</code></p><p>This is not throttling (!) since eventually all calls will be ran, while in throttling the calls in the "wait" period are skipped.</p> | | ||
| [enforceSequentialExecution(fn)](https://wixplosives.github.io/core3-utils/common.enforcesequentialexecution) | Ensures that when the async function <code>fn</code> is called twice in a row, the second call only begins after the first one has finished (successfully or not). | | ||
| [enumValues(enumObj)](https://wixplosives.github.io/core3-utils/common.enumvalues) | | | ||
| [enumValues(enumObj)](https://wixplosives.github.io/core3-utils/common.enumvalues_1) | | | ||
| [equalIdents(reference, modified, newline)](https://wixplosives.github.io/core3-utils/common.equalidents) | Matches the indentation of modified to the one of reference | | ||
| [errorToPlainObject(error)](https://wixplosives.github.io/core3-utils/common.errortoplainobject) | | | ||
| [escapeRegExp(str)](https://wixplosives.github.io/core3-utils/common.escaperegexp) | Returns a string safe to be used in RegExp | | ||
| [every(iterable, predicate)](https://wixplosives.github.io/core3-utils/common.every) | | | ||
| [exclude(excluded)](https://wixplosives.github.io/core3-utils/common.exclude) | | | ||
| [filter(iterable, predicate)](https://wixplosives.github.io/core3-utils/common.filter) | | | ||
| [find(iterable, predicate)](https://wixplosives.github.io/core3-utils/common.find) | | | ||
| [first(iterable)](https://wixplosives.github.io/core3-utils/common.first) | Picks the first element of an iterable | | ||
| [flat(iterable, deep)](https://wixplosives.github.io/core3-utils/common.flat) | | | ||
| [flatMap(iterable, mapFn)](https://wixplosives.github.io/core3-utils/common.flatmap) | | | ||
| [forEach(iterable, fn)](https://wixplosives.github.io/core3-utils/common.foreach) | | | ||
| [get(obj, key)](https://wixplosives.github.io/core3-utils/common.get) | Similar to Map.get, but works for plain objects, and returns undefined for null maps and missing keys | | ||
| [get(obj, key)](https://wixplosives.github.io/core3-utils/common.get_1) | | | ||
| [getCartesianProductOfArrays(arrays)](https://wixplosives.github.io/core3-utils/common.getcartesianproductofarrays) | | | ||
| [getErrorCode(error)](https://wixplosives.github.io/core3-utils/common.geterrorcode) | Returns error.code property if the error object has it, otherwise returns undefined. | | ||
| [getIn(obj, path)](https://wixplosives.github.io/core3-utils/common.getin) | | | ||
| [getValue(map, key, errorMessage)](https://wixplosives.github.io/core3-utils/common.getvalue) | Returns a value by key, throws if the value is missing or the map null | | ||
| [getValue(map, key, errorMessage)](https://wixplosives.github.io/core3-utils/common.getvalue_1) | | | ||
| [getValue(map, key, errorMessage)](https://wixplosives.github.io/core3-utils/common.getvalue_2) | | | ||
| [groupBy(elements, property)](https://wixplosives.github.io/core3-utils/common.groupby) | Groups elements by the value of a property | | ||
| [has(obj, key)](https://wixplosives.github.io/core3-utils/common.has) | Similar to Map.has, but works for plain objects, and returns false for null maps | | ||
| [has(obj, key)](https://wixplosives.github.io/core3-utils/common.has_1) | Similar to Map.has, but works for plain objects, and returns false for null maps | | ||
| [has(obj, key)](https://wixplosives.github.io/core3-utils/common.has_2) | | | ||
| [histogram(iterable)](https://wixplosives.github.io/core3-utils/common.histogram) | Calculate a histogram of iterable elements | | ||
| [includes(iterable, item)](https://wixplosives.github.io/core3-utils/common.includes) | | | ||
| [includesCaseInsensitive(str, substr)](https://wixplosives.github.io/core3-utils/common.includescaseinsensitive) | Checks if str contains substr ignoring capitalization | | ||
| [indexToLineAndColumn(content, pos, newline)](https://wixplosives.github.io/core3-utils/common.indextolineandcolumn) | Finds line an column by position index | | ||
| [isDefined(value)](https://wixplosives.github.io/core3-utils/common.isdefined) | Given a value of type Nullable<T>, validates value is T | | ||
| [isEmpty(iterable)](https://wixplosives.github.io/core3-utils/common.isempty) | Checks if an iterable is empty | | ||
| [isErrorLikeObject(error)](https://wixplosives.github.io/core3-utils/common.iserrorlikeobject) | | | ||
| [isIterable(x)](https://wixplosives.github.io/core3-utils/common.isiterable) | | | ||
| [isPlainObject(value)](https://wixplosives.github.io/core3-utils/common.isplainobject) | Checks that value is a POJO | | ||
| [isValidNamingConvention(namingConvention)](https://wixplosives.github.io/core3-utils/common.isvalidnamingconvention) | Checks if namingConvention is supported | | ||
| [join(iterable, separator)](https://wixplosives.github.io/core3-utils/common.join) | | | ||
| [keys(map)](https://wixplosives.github.io/core3-utils/common.keys) | | | ||
| [keys(map)](https://wixplosives.github.io/core3-utils/common.keys_1) | | | ||
| [last(iterable)](https://wixplosives.github.io/core3-utils/common.last) | Picks the last element of an iterable | | ||
| [map(iterable, mapFn)](https://wixplosives.github.io/core3-utils/common.map) | Map iterable elements | | ||
| [mapKeys(obj, mapping)](https://wixplosives.github.io/core3-utils/common.mapkeys) | Maps values of a plain object | | ||
| [mapObject(obj, mapping)](https://wixplosives.github.io/core3-utils/common.mapobject) | Maps key value pairs of a plain object | | ||
| [mapValues(obj, mapping)](https://wixplosives.github.io/core3-utils/common.mapvalues) | Maps values of a plain object | | ||
| [minimalIndent(str)](https://wixplosives.github.io/core3-utils/common.minimalindent) | Shifts all indentation to the left using the line with the least indentation as a baseline | | ||
| [mulberry32(a)](https://wixplosives.github.io/core3-utils/common.mulberry32) | | | ||
| [naiveStripComments(str)](https://wixplosives.github.io/core3-utils/common.naivestripcomments) | Removes comments from string Note that there's lexical no parsing, so stuff like "//'//" will not work | | ||
| [next(iterable, item)](https://wixplosives.github.io/core3-utils/common.next) | Find the element following an item | | ||
| [noIdents(modified, separator)](https://wixplosives.github.io/core3-utils/common.noidents) | Remove line indentation (heading whitespace) | | ||
| [normToRage(normal, min, max)](https://wixplosives.github.io/core3-utils/common.normtorage) | | | ||
| [noWhiteSpace(str)](https://wixplosives.github.io/core3-utils/common.nowhitespace) | Remove white spaces including empty lines | | ||
| [once(fn)](https://wixplosives.github.io/core3-utils/common.once) | Make a function executable only once, following calls are ignored | | ||
| [pick(record, keys)](https://wixplosives.github.io/core3-utils/common.pick) | returns an object composed of the picked object properties | | ||
| [prev(iterable, item)](https://wixplosives.github.io/core3-utils/common.prev) | Find the element before an item | | ||
| [reduce(iterable, reducer, initial)](https://wixplosives.github.io/core3-utils/common.reduce) | | | ||
| [reportError_2(ex)](https://wixplosives.github.io/core3-utils/common.reporterror_2) | Logs an error | | ||
| [reverseObject(obj)](https://wixplosives.github.io/core3-utils/common.reverseobject) | Reverses keys-values of an object, ignoring falsy values. First takes on value collisions. | | ||
| [same(a, b, unordered)](https://wixplosives.github.io/core3-utils/common.same) | Deep comparison of two objects | | ||
| [size(iterable)](https://wixplosives.github.io/core3-utils/common.size) | Evaluate the size of an iterable | | ||
| [skip(iterable, count)](https://wixplosives.github.io/core3-utils/common.skip) | Skips the first elements of an iterable | | ||
| [some(iterable, predicate)](https://wixplosives.github.io/core3-utils/common.some) | | | ||
| [sort(iterable, by)](https://wixplosives.github.io/core3-utils/common.sort) | | | ||
| [stringifyErrorStack(error)](https://wixplosives.github.io/core3-utils/common.stringifyerrorstack) | | | ||
| [templateCompilerProvider(context)](https://wixplosives.github.io/core3-utils/common.templatecompilerprovider) | Similar to templated string, given a fixed context object returns a function that parses strings in it | | ||
| [toCamelCase(str)](https://wixplosives.github.io/core3-utils/common.tocamelcase) | Converts a string to camelCase | | ||
| [toCSSCamelCase(str)](https://wixplosives.github.io/core3-utils/common.tocsscamelcase) | like [toCamelCase()](https://wixplosives.github.io/core3-utils/common.tocamelcase)<!-- -->, but capitalizes first character if input starts with '-' | | ||
| [toCSSKebabCase(str)](https://wixplosives.github.io/core3-utils/common.tocsskebabcase) | like [toKebabCase()](https://wixplosives.github.io/core3-utils/common.tokebabcase)<!-- -->, but prepends '-' if first character of input is UpperCase | | ||
| [toError(value)](https://wixplosives.github.io/core3-utils/common.toerror) | Convert any kind of value to an error instance. Unless the value is already an error instance, it's stringified and used as the error message. | | ||
| [toKebabCase(str)](https://wixplosives.github.io/core3-utils/common.tokebabcase) | Converts a string to kebab-case | | ||
| [toNamingConvention(str, namingConvention)](https://wixplosives.github.io/core3-utils/common.tonamingconvention) | Converts string formatting to a naming convention | | ||
| [toPascalCase(str)](https://wixplosives.github.io/core3-utils/common.topascalcase) | Converts a string to PascalCase | | ||
| [toPascalCaseJsIdentifier(str)](https://wixplosives.github.io/core3-utils/common.topascalcasejsidentifier) | Similar to [toPascalCase()](https://wixplosives.github.io/core3-utils/common.topascalcase)<!-- -->, but drops heading non-letters | | ||
| [unique(iterable, by)](https://wixplosives.github.io/core3-utils/common.unique) | Creates iterable of unique elements | | ||
| [values(map)](https://wixplosives.github.io/core3-utils/common.values) | | | ||
| [values(map)](https://wixplosives.github.io/core3-utils/common.values_1) | | | ||
@@ -116,0 +117,0 @@ ## Variables |
@@ -1,2 +0,2 @@ | ||
import { sleep } from "promise-assist"; | ||
import { sleep } from 'promise-assist'; | ||
@@ -39,7 +39,10 @@ /** | ||
*/ | ||
export function delayed<T extends (...args: any[]) => any>(fn: T, wait: number): (...args: Parameters<T>) => Promise<ReturnType<T>> { | ||
let queue:Promise<ReturnType<T>|void>|null = null; | ||
export function delayed<T extends (...args: any[]) => any>( | ||
fn: T, | ||
wait: number | ||
): (...args: Parameters<T>) => Promise<ReturnType<T>> { | ||
let queue: Promise<ReturnType<T> | void> | null = null; | ||
return ((...args: Parameters<T>) => { | ||
if (!queue) { | ||
queue = Promise.resolve(fn(...args)) | ||
queue = Promise.resolve(fn(...args)); | ||
} else { | ||
@@ -54,3 +57,3 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
queue = tmp.then(()=>sleep(wait)) | ||
queue = tmp.then(() => sleep(wait)); | ||
return tmp as Promise<ReturnType<T>>; | ||
@@ -57,0 +60,0 @@ }) as T; |
@@ -79,9 +79,9 @@ import chai, { expect } from 'chai'; | ||
it('resolves the returned promise with the returned value', async () => { | ||
const fn = delayed((id: number) => id, 100) | ||
const r1 = fn(1) | ||
const r2 = fn(2) | ||
await clock.tickAsync(100) | ||
expect(await r1).to.equal(1) | ||
expect(await r2).to.equal(2) | ||
const fn = delayed((id: number) => id, 100); | ||
const r1 = fn(1); | ||
const r2 = fn(2); | ||
await clock.tickAsync(100); | ||
expect(await r1).to.equal(1); | ||
expect(await r2).to.equal(2); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
354346
5856
157