import { padLeft } from '@based/utils'console.log(padLeft('a', 4, 'b')) // "bbba"const y = padRight('a', 4, 'b')t.is(y, 'abbb')
padRight
Add padding to a string
import { padLeft } from '@based/utils'console.log(padRight('a', 4, 'b')) // "abbb"
queued
Pass any async function and queue it based on the arguments, also shares the function execution for the same args
Accepts 10 arguments maximum
import { queued, wait } from '@based/utils'const myFn = queued(async (a: string) => { await wait(1000) return a + '!'})// will execute bla first then xawait Promise.all([ myFn('bla'), myFn('x') myFn('bla') // bla will be shared])
import { queued, wait } from '@based/utils'const myFn = queued(async (a: string) => { await wait(1000) return a + '!'}, { dedup: (a) => { // choose the value to use for dedup (to share results) return a }, concurrency: 10 // max concurrency of 10})// will execute all at the same time (scince concurrency is 10)// will only execute 'bla' once since it has the same arguments used in idawait Promise.all([ myFn('bla'), myFn('x') myFn('bla') // bla will be shared])
getType
Returns a string with the operand/type of the javascrit primitive. Adds 'null' and 'array'.
Generic structure walker. By default walks objects.
const result = []await walk(objectToWalk, async (item, info) => { result.push({ value: item, name: info.name, // property name path: info.path, // slash separated path in object type: info.type, // operand type })}) // returns void
By configuring the options you can walk any kind of structure
await walk( objectToWalk, // starting target itemFn, // function to run for each matched item options: { // check types for details listFn, // function to list each path. Should return a list of items. itemMatchFn, // function to choose which items to run itemFn on recureseFn, // function to choose wchich items to recurse on targetValidationFn, // function to validate starting path previousPath, // prefix to add to paths } )
nonRecursiveWalker
Generic object walker that does not use recursion.
const obj = { a1: { a1b1: { a1b1c1: 'a1b1c1', a1b1c2: { a1b1c2d1: 'a1b1c2d1', }, }, }, a2: 'a2', a3: { a3b1: 'a3b1', },}nonRecursiveWalker( obj, // Object to walk ( target, // reference to matched property path, // path as a string[] type, // 0 for property, 1 for object ) => { if (path.join('.') === 'a1.a1b1') { console.log('Object found type is 1') } if (path.join('.') === 'a2') { console.log('Property found type is 0') } }, true, // also match objects)