@well-balanced/utils
v0.1.5
Published
JavaScript / TypeScript utils
Downloads
479
Maintainers
Readme
utils
JavaScript / TypeScript utils
Table of Contents
Installation
$ npm i @well-balanced/utils
$ pnpm add @well-balanced/utils
$ yarn add @well-balanced/utils
Usage
cleanObject
If the object has fields with an undefined
value, clean it.
example
const user = { id: 'id', name: 'name', age: 20, gender: undefined }
const cleaned = cleanObject(user) // { id: 'id', name: 'name', age: 20}
reduceSeries
Execute a function on each element of an array, sequentially.
example
import { reduceSeries } from '@well-balanced/utils'
const array = [1, 2, 3, 4, 5]
const sum = await reduceSeries(array, async (prev, curr) => prev + curr, 0)
console.log(sum) // 15
mapSeries
Map over an array, applying an asynchronous function sequentially.
example
import { mapSeries } from '@well-balanced/utils'
const array = [1, 2, 3, 4, 5]
const results = await mapSeries(array, async (item) => item * 2)
console.log(results) // [2, 4, 6, 8, 10]
readdirRecursive
Read a directory recursively and return a list of file paths.
example
import { readdirRecursive } from '@well-balanced/utils'
const files = await readdirRecursive('/path/to/directory')
console.log(files)
setPropertyRecursive
Set a nested property on an object, creating intermediate objects as necessary.
example
import { setPropertyRecursive } from '@well-balanced/utils'
const obj = { hello: 'world' }
const updated = setPropertyRecursive(
['key1', 'key2', 'key3'],
'hello world',
obj,
)
console.log(updated) // { key1: { key2: { key3: 'hello world' } }, hello: 'world' }
cursor
Encode and decode cursors for pagination.
example
import { encodeCursor, decodeCursor } from '@well-balanced/utils'
const obj = { page: 1, limit: 10 }
const cursor = encodeCursor(obj)
console.log(cursor) // Encoded cursor string
const decoded = decodeCursor(cursor)
console.log(decoded) // { page: 1, limit: 10 }
source
getFulfilledResult
Extract fulfilled results from an array of settled promises.
example
import { getFulfilledResult } from '@well-balanced/utils'
const promises = [
Promise.resolve(1),
Promise.reject(new Error('Failure')),
Promise.resolve(3),
]
const settled = await Promise.allSettled(promises)
const results = getFulfilledResult(settled)
console.log(results) // [1, 3]
msleep
Sleep for a given number of milliseconds.
example
import { msleep } from '@well-balanced/utils'
await msleep(1000) // Sleep for 1 second
console.log('1 second later')
kst
Get the current date and time in Korea Standard Time.
example
import { kst } from '@well-balanced/utils'
const koreanTime = kst()
console.log(koreanTime)
formatKst
Format a date object to a string in Korea Standard Time.
example
import { formatKst } from '@well-balanced/utils'
const date = new Date()
const formattedDate = formatKst(date, 'yyyy-MM-dd HH:mm:ss')
console.log(formattedDate)
filterOut
Filter out elements from an array based on a comparator function.
example
import { filterOut } from '@well-balanced/utils'
const array = [1, 2, 3, 4, 5]
const valuesToRemove = [2, 4]
const comparator = (a: number, b: number) => a === b
const result = filterOut(array, valuesToRemove, comparator)
console.log(result) // [1, 3, 5]
hashIndex
Generate a hash index from a string.
example
import { hashIndex } from '@well-balanced/utils'
const index = hashIndex('example', 10)
console.log(index)
notEmpty
Check if a value is not null or undefined.
example
import { notEmpty } from '@well-balanced/utils'
const value = 'example'
if (notEmpty(value)) {
console.log('Value is not empty')
}
randomInt
Generate a random integer between a given range.
example
import { randomInt } from '@well-balanced/utils'
const randomValue = randomInt(1, 10)
console.log(randomValue)
safeSample
Get a random element from an array excluding certain values.
example
import { safeSample } from '@well-balanced/utils'
const array = [1, 2, 3, 4, 5]
const valuesToExclude = [2, 4]
const comparator = (a: number, b: number) => a === b
const result = safeSample(array, valuesToExclude, comparator)
console.log(result)
sample
Get a random element from an array.
example
import { sample } from '@well-balanced/utils'
const array = [1, 2, 3, 4, 5]
const result = sample(array)
console.log(result)
License
MIT