This is a dependency module for various jsonql node / browser modules.
When using purely in browser environment, you should use @jsonql/utils/browser
For ESM you could include the @jsonql/utils/esm
When using it within your JS / TS development, most of the methods can be found in @jsonql/utils
Where the node only methods will be in @jsonql/utils/node
@DREPRECATED
When use with build tool, for better tree shaking. You need to import them directly. Please reference which functions in what file below
accessByPath
(obj: AnyType, path: string)const obj = { a: 1, b: { c: 2} }
const c = accessByPath(obj, 'b.c')
// c === 2
inArray
(arr: AnyTypeArr, value: AnyType): booleantoArray
(arg: AnyType): Arraycompact
(arg: Array): Arrayexample for compact:
const result = compcat([0, 1, 2, null, false, 3])
// [ 1, 2, 3 ]
Basically compact will remove any non-truthy value from the value, even 0!
chainFns
(mainFn: AnyType, ...moreFns: AnyType)It returns a function to accept parameter for the mainFn
, then after execute the function will pass to the next function until we get the final result. This is a sync method.
const fn1 = (a) => a + 1
const fn2 = (b) => b + 2
const fn3 = (c) => c + 4
const fn = chainFns(fn1, fn2, fn3)
const result = fn(1)
// result === 8
You can also do this:
const fn1 = (a) => a + 1
const fn2 = (b) => [b, 2]
const fn3 = (x, y) => x + y
const fn = chainFns(fn1, fn2, fn3)
const result = fn(1)
// result === 4
chainPromises
(promies: Array<Promise>, asObject: boolean | object = false)chainProcessPromises
(initPromise: JsonqlPromiseChainFn, ...promises: Array)queuePromisesProcess
(queue: Array,
...initValue: AnyTypeArr)chainPromises
will put every promises resolve result into an array (default) or if you set the asObject
parameter to true or pass an object, it will merge into one object. In some way this is similar to Promise.all
. The different is when one of the promise failed, it will stop and throw immediately. And it can config to return a merge object as result.
chainProcessPromises
will return a function that accept the argument for the first async function, then each result will pass to the next function as argument. This is the async version of the above chainFns
.
queuePromisesProcess
is a wrapper of the chainProcessPromises
, the call signature makes it easier to call with Reflect.apply
cloneDeepCheap
(obj: AnyType)cloneDeep
(obj: AnyType)cloneDeepCheap
only able to clone object, internally using JSON
cloneDeep
will able to clone all kinds of deep structure object even a class instance (which could be expensive operation).
parseJson
(json: AnyType, t = true): JSON | json | ErrorcreateEvtName
(...args: string[]): stringnil
() => boolean (false)showDeep
(code: unknown): voidformatStr
(str: string, ...args: Array): stringformatStr
example:
const str = 'I want to go from {0} to {1}'
const output = formatStr(str, 'zero', 'one')
// I want to go from zero to one
strToNum
(input: string, t = false): numberstrToBool
(input: string, t = false): booleandasherize
(str: string): stringisEmptyObj
(obj: AnyType): booleanisNotEmpty
(param: unknown): booleannotEmpty
(a: unknown, valueCheck = false): booleanisEmpty
(value: unknown, valueCheck?: boolean): booleangetDirname
(url: string): stringThis is only use in ESM
env in node.js
// there is no __dirname in ESM env
const __dirname = getDirname(import.meta.url)
THIS IS NOT INCLUDED IN THE EXPORT LIST, you need to import it like so:
import { getDirname } from '@jsonql/dist/get-dirname'
isEqualCheap
(obj1: unknown, obj2: unknown): booleanisEqual
(obj1: unknown, obj2: unknown)isEqualCheap
can only compare two object, isEqual
can compare anything
isFunction
(prop: unknown, debug = false): booleanisAsyncFunction
(prop: unknown): booleanparseJWT
(token: string): unknownIt returns typed as unknown
is follow the standard, you have to cast it again when your own structure. In the future release, we might create our own type for a standard JWT decoded object type.
curry
(fn: AnyType, ...args: AnyType[])merge
(target: AnyType, ...sources: AnyType[])flatMap
(arr: AnyType[], callback?: FlatMapCallback)isString
(value: unknown): booleanThese methods used to included from lodash-es
now we implement them ourself, and cut down the library size to just 5kb
logger
(wrapper for console.log to use in browser, set window.DEBUG=true
then it will show)objDefineProps
(obj: AnyType, name: string, setter: AnyType, getter = null)objHasProp
(obj: AnyType, name: string)injectToFn
(resolver: JsonqlResolver | JsonqlAsyncResolver, name: string, data: AnyType, overwrite = false)isObject
(o: AnyType): booleanisPlainObject
(o: AnyType): booleanisClass
(o: AnyType): booleangetConfigValue
(name: string, obj: object)assign
(...args: unknown[]): unknownextend
(alias to assign
)arrToObj
(args: unknown[], processor: MapCallback, initValue = {}): AnyTypeobjectHasKey
(obj: object, key: string): booleanreadOnly
(config: object): AnyTypepromsie
async (cb: AnyType)processAll
async (promises: Array<Promise>)promise is just a new Promise
wrapper save a bit typing
processAll return Array of Array of results, see example below
const tasks = [
Promise.resolve(1),
Promise.reject(2),
Promise.resolve(3)
]
// this will not throw instead put into the result array
processAll(tasks)
.then(result => {
const done = result[0]
const fail = result[1] // if there is no fail then this won't exist
// done --> [1,3]
// fail --> [2]
})
isRegExp
(pat: AnyType): booleangetRegex
(pattern: string | RegExp): RegExp | string | booleantimestamp
(sec = false): numberDefault return mil-seconds
trueTypeOf
(obj: AnyType): stringSpecial Thank you to (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
urlParams
(url: string, params: AnyType): stringcacheBurstUrl
(url: string): stringcacheBurst
(name = '_cb')Please check jsonql for more information.
Joel Chu (c) 2022
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。