@finalapp/utils
v1.2.0
Published
---
Downloads
14
Readme
Utils functions
Object
deepClone
<T>(value: T, cacheMap?: WeakMap) => T
deepMerge
(value: any, source: any, isMergingArrays?: boolean) => any
flattenObject
(value: any, preserveNesting = false) => Record<string, any>
preserveNesting - should return last nesting or all. Default false:
// for object
{ a: { b: [1, 2] } }
// if preserveNesting is true
{
a: { b: [1, 2] },
"a.b": [1, 2],
"a.b.0": 1,
"a.b.1": 1,
}
// if preserveNesting is false
{
"a.b.0": 1,
"a.b.1": 1,
}
getDeep
(object: any, path: string[] | string) => any
getDeepByPath
(path: string[]) => (object: any) => any
getPath
(str: string) => string[]
isDeepEqual
(a: any, b: any) => boolean
isEmpty
(value: any | any[]) => boolean
isObject
(value: any) => boolean
setDeep
(object: any, path: string[], value: any) => any
unflattenObject
<T>(obj: any, parseValue?: (v: any) => any): T
parseValue - allow to parse value before set it, eg:
unflattenObject({"a.b": "1"}, (value) => parseInt(value));
// returns
{ a: { b: 1 } }
unset
(object: any, path: string[]) => any | undefined
It will remove key if value is primitive or when unsetted value will result in emoty obejct or empty array. If result will be empty object then unset
will return undefined
Array
append
<T>(arr: T[], element: T) => T[]
prepend
<T>(arr: T[], element: T) => T[]
remove
<T>(arr: T[], index: number) => T[]
insert
<T>(arr: T[], index: number, newItem: T) => T[]
move
<T>(arr: T[], from: number, to: number) => T[]
swap
<T>(arr: T[], indexA: number, indexB: number) => T[]
Function
identity
<T>(value: T) => T