/
types.ts
119 lines (99 loc) · 2.58 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
export interface Node {
key: string | undefined
val: any
parents: any[]
path: string[]
isLeaf: boolean
/** Useful for postorder traversals to know when you're at the root. */
isRoot: boolean
}
export type NextNode = (
currentNode: Node,
entry: [string, any],
isLeaf: (x: any) => boolean
) => Node
export interface Options {
postOrder?: boolean
jsonCompat?: boolean
traverse?(val: any): any
}
export interface WalkOptions extends Options {
leavesOnly?: boolean
}
export interface MutationOption {
/** Set to true to modify the object instead of returning a new object. */
modifyInPlace?: boolean
}
export type MapOptions = Omit<Options, 'traverse'> & {
shouldSkip?(val: any, node: Node): boolean
}
export type RefOptions = Pick<Options, 'traverse'>
export type WalkFn = (node: Node) => void
export type AsyncWalkFn = (node: Node) => void | Promise<void>
export type FindFn = (node: Node) => boolean
export type Mapper = (node: Node) => any
export type Walker = (obj: object, walkFn: WalkFn, options?: Options) => void
export type Walk = (obj: object, options?: WalkOptions) => Node[]
export type Walkie = (
obj: object,
walkFn: WalkFn,
options?: WalkOptions & MutationOption
) => object
export type WalkieAsync = (
obj: object,
walkFn: AsyncWalkFn,
options?: WalkOptions & MutationOption
) => Promise<object>
export type Map = (
obj: object,
mapper: Mapper,
options?: MapOptions & MutationOption
) => object
export type FindNode = (
obj: object,
findFn: FindFn,
options?: Options
) => Node | undefined
export type MapInternal = (
obj: object,
mapper: Mapper,
options: Required<MapOptions>
) => object
export interface FlattenOptions {
/** Defaults to '.' */
separator?: string
/** Flatten objects and not arrays */
objectsOnly?: boolean
}
export type Flatten = (
obj: object,
options?: WalkOptions & FlattenOptions
) => object
export interface CompactOptions {
removeUndefined?: boolean
removeNull?: boolean
removeEmptyString?: boolean
removeFalse?: boolean
removeNaN?: boolean
removeEmptyObject?: boolean
removeEmptyArray?: boolean
compactArrays?: boolean
removeFn?: (val: any, node: Node) => boolean
}
export type Compact = (
obj: object,
options: CompactOptions & MutationOption
) => object
export interface TruncateOptions {
depth: number
/** Defaults to [Truncated] */
replaceWith?: any
/** Max length of a string. Default to Infinity. */
stringLength?: number
/** Max length of an array */
arrayLength?: number
}
export type Truncate = (
obj: object,
options: TruncateOptions & MutationOption
) => object