utils
Saulx utils package
deepEqual
Compare objects
import { stringHash } from '@saulx/utils'
console.log(deepEqual({ a: { b: true } }, { a: { b: true } }))
deepCopy
Create a deepcopy of objects
import { deepCopy } from '@saulx/utils'
console.log(deepCopy({ x: true }))
deepMerge
Merge an object into another object, arrays are treated as new values
import { deepMerge } from '@saulx/utils'
const a = { x: { a: { c: true, x: [1, 2] } } }
const b = { y: true }
const c = { x: { a: { b: true, x: ['bla'] } } }
console.log(deepMerge(a, b))
console.log(deepMerge(a, b, c))
deepMergeArrays
Merge an object into another object, arrays are treated as objects
import { deepMergeArrays } from '@saulx/utils'
const a = { x: { a: { c: true, x: [1, 2, 3] } } }
const b = { y: true }
const c = { x: { a: { b: true, x: ['bla'] } } }
console.log(deepMergeArrays(a, b))
console.log(deepMergeArrays(a, b, c))
isObject
Checks if a variable is an object and not an array
import { isObject } from '@saulx/utils'
console.log(isObject({ x: true }))
console.log(isObject([1, 2, 3]))
wait
Timeout in a promise, default is 100ms
import { wait } from '@saulx/utils'
const somethingAsync = async () => {
await wait()
console.log('after 100ms')
await wait(1000)
console.log('after 1100ms')
}
somethingAsync()
readStream
Sink a read stream into a promise
import { readStream } from '@saulx/utils'
import fs from 'fs'
const aReadStream = fs.createReadStream('somefile')
const myResult = await readStream(aReadStream)
toEnvVar
Convert a string to a env variable safe name
import { toEnvVar } from '@saulx/utils'
const x = toEnvVar('@based/bla-bla-bla$_!')
console.log(x)
queued
Pass any async function and queue it based on the arguments, also shares the function execution for the same args
Accepts 5 arguments maximum!
import { queued, wait } from '@saulx/utils'
const myFn = queued(async (a: string) => {
await wait(1000)
return a + '!'
})
await Promise.all([
myFn('bla'),
myFn('x')
myFn('bla')
])
import { queued, wait } from '@saulx/utils'
const myFn = queued(async (a: string) => {
await wait(1000)
return a + '!'
}, {
dedup: (a) => {
return a
},
concurrency: 10
})
await Promise.all([
myFn('bla'),
myFn('x')
myFn('bla')
])
getType
Returns a string with the operand/type of the javascrit primitive. Adds 'null' and 'array'.
getType('')
getType('this is a string')
getType(123)
getType(12.3)
getType(-12.3)
getType(-123)
getType(BigInt('1'))
getType(true)
getType(false)
getType(undefined)
getType({})
getType({ a: 'wawa' })
getType(() => {})
getType([])
getType([1, 2, 3])
getType(null)
walker
Generic structure walker. By default walks objects.
const result = []
await walk(objectToWalk, async (item, info) => {
result.push({
value: item,
name: info.name,
path: info.path,
type: info.type
})
})
By configuring the options you can walk any kind of structure
await walk(
objectToWalk,
itemFn,
options: {
listFn,
itemMatchFn,
recureseFn,
targetValidationFn,
previousPath,
}
})