https://www.npmjs.com/package/@alwaystudios/as-utils
yarn add @alwaystudios/as-utils
Retry a promise for customizable delay time and number of attempts
Defaults: attempts = 10, timeout = 200
const result = await promiseRetry()(someAsyncFunction)
const result = await promiseRetry({ attempts: 2, timeout: 200 })(someAsyncFunction)
setTimeout as a promise
await promiseTimeout(2000)
time the duration of a process
const start = new Date().getTime()
const stopTimer = processTimer()
await promiseTimeout(200)
const duration = stopTimer()
concatenates a variable array of strings ignoring falsy values
concatenate('a', 'b', '', null, false, undefined, 'c') // = 'a b c'
encrypts a string using aes-256-cbc
const hash = encrypt(dataToEncrypt, key)
decrypts a string using aes-256-cbc
const hash = encrypt(dataToEncrypt, key)
const decryptedData = decrypt(hash, key)
creates a subscriber process that will run a poller process on a given interval and then process that data
createSubscriberProcess(pollerProcessAsyncFunction, processDataFunction, processErrorFunction, 200)
Processes data in batches (default batch size = 100)
await batchProcessor(data, func, batchSize)
Reads a csv and runs a processor function (default batch size = 100)
await csvBatchRunner({ filename, f, onError: onErrorFunction, batchSize })
Reads data from a csv file
const data = await readCsv(filename, onErrorFunction)
if(isPlainObject({ test: 'test' })) {
// do something
}
normal text kebabified-text
Capitalise the first character of the first word in a sentence
[1, 2, undefined, null, 0].filter(truthy) // returns [1, 2]
await waitUntil(() => expect(mock).toHaveBeenCalledTimes(1))
Removes undefined keys from an object
Debounce a function with a given timeout (default = 500)
const timeout = 1000
const debouncedFunc = debounce(myFunc, timeout)
debouncedFunc(1)
debouncedFunc(2)
debouncedFunc(3) // myFunc is called with 3
const data = {
topKey1: {
midKey1: 'value midKey 1',
midKey2: 3,
},
topKey2: {
midKey1: 33,
midKey2: 'value midKey 2',
midKey3: {
lowKey: 444,
},
},
}
deepPartialDiff({ topKey1: {} }, data) // = data
deepPartialDiff(
{
topKey1: {
midKey1: 'value midKey 1',
midKey2: 3,
},
topKey2: {
midKey1: 33,
midKey2: 'value midKey 2',
midKey3: {},
},
},
data,
) // = {
topKey2: {
midKey3: {
lowKey: 444,
},
},
}
A limited parallel execution, asynchronous task queue
const queue = new TaskQueue(concurrencyLimit)
queue.addTask(fn)