-
Notifications
You must be signed in to change notification settings - Fork 1
/
array.ts
192 lines (175 loc) · 5.08 KB
/
array.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { isNumber, isArray, isEmpty } from './is'
import { defaults } from './object'
/**
* Check if an array contains any elements.
* null/undefined are considered empty.
*
* @export
* @param {(any[] | undefined | null)} arr
* @returns
*/
export function isNullOrEmpty(arr: any[] | undefined | null) {
return arr === null || arr === undefined || arr.length === 0
}
/**
* Like flatMap, this function walks through the nested(multi-dimension) array.
* This function keep the original dimension.
* e.g.: deepMap([1, [2, 3]], x => x + 1) => [2, [3, 4]]
*
* @export
* @param {any[]} deepArray
* @param {(item: any, cord: number[], index: number) => any} iteratee
* @returns
*/
export function deepMap(
deepArray: any[],
iteratee: (item: any, cord: number[], index: number) => any,
pos: number[] = [],
index: any = { i: 0 },
) {
if (!isArray(deepArray)) {
throw new Error(`Argument is not an array: ${deepArray}. position: ${pos}`)
}
return deepArray.map((item, i) => {
if (isArray(item)) {
return deepMap(item, iteratee, [...pos, i], index)
} else {
return iteratee(item, [...pos, i], index.i++)
}
})
}
/**
* Swap value of array's two positions
*
* @export
* @param {any[]} arr
* @param {*} pos1
* @param {*} pos2
* @returns arr
*/
export function swap(arr: any[], pos1: number | number[], pos2: number | number[]): void {
if (isNumber(pos1)) pos1 = [pos1]
if (isNumber(pos2)) pos2 = [pos2]
let parent1: any = []
let parent2: any = []
let val1 = pick(arr, ...pos1, parent1)
let val2 = pick(arr, ...pos2, parent2)
parent1[parent1.length - 1][pos1[pos1.length - 1]] = val2
parent2[parent2.length - 1][pos2[pos2.length - 1]] = val1
}
/**
* Pick a value from a deep array by its indices.
* e.g.: pick(arr, 1, 2, 3) = arr[1][2][3]
* if an array is supplied as the last parameter, it will be filled with arrays in each level
* e.g.: [rootArray, subArray, ..., parentArray]
* @export
* @param {any[]} array
* @param {any} indices target value indices in a deep array. e.g. the indice of value `arr[i1][i2][i3]` is [i1, i2, i3]
* the last one can be an empty array to hold parents in each level
* @returns {any}
*/
export function pick(array: any[], ...indices): any {
let last = indices.pop()
if (!isArray(last)) {
indices.push(last)
last = undefined
}
if (indices.length === 1) {
let [i] = indices
if (typeof i !== 'number') {
throw Error(`Index ${JSON.stringify(i)} is not a number`)
}
if (array !== undefined && array.length > i && i > -1) {
last && last.push(array)
return array[i]
}
throw Error(`Out of bound`)
}
let [first, ...rest] = indices
last && last.push(array) && rest.push(last)
return pick(array[first], ...rest)
}
/**
* Perform binary search on a sorted array. Element will be fed to predict function, if true, the target is
* found and returned, otherwise onward function is called and the returned value determins the next
* move:
* true - left-ward continuance
* false - right-ward continuance
*
* null is returned if none is suitable
*
* @export
* @template T
* @param {T[]} array
* @param {((i: T) => boolean | undefined)} predict
* @param {(i: T) => boolean} onward
* @returns {[T, number]| null} result and its index, null if not found
*/
export function binarySearch<T>(
array: T[],
predict: (i: T) => boolean,
onward: (i: T) => boolean,
start: number = 0,
end?: number,
): [T, number] | null {
if (isEmpty(array)) return null
end = end || array.length
let midIndex = ((start + end) / 2) | 0
let testee = array[midIndex]
if (predict(testee)) {
return [testee, midIndex]
}
if (midIndex === 0 || midIndex === array.length - 1) {
return null
}
let nextSection = onward(testee) ? [start, midIndex] : [midIndex, end]
return binarySearch(array, predict, onward, ...nextSection)
}
/**
* Experiment feature...
*
* @export
* @template T
* @param {T[]} arr
* @param {string} keyProperty
* @param {string} linkProperty
* @param {string} [childrenProp='__children__']
* @returns {T[]}
*/
export function unflatten<T>(
arr: T[],
keyProperty: string,
linkProperty: string,
childrenProp: string = '__children__',
): T[] {
if (arr.length === 0 || arr.length === 1) {
return arr
}
let result = new Array()
let allMap = new Map<any, any>()
let deferred = new Map<any, T[]>()
arr.forEach(element => {
let link = element[linkProperty]
let key = element[keyProperty]
allMap.set(key, element)
if (!link) {
result.push(element)
let deferredChildren = deferred.get(key)
if (deferredChildren) {
defaults(element as any, childrenProp, [])[childrenProp].push(element)
}
} else {
let parent = allMap.get(link)
if (parent) {
defaults(parent as any, childrenProp, [])[childrenProp].push(element)
} else {
let deferredList = deferred.get(link) || new Array()
deferredList.push(element)
deferred.set(link, deferredList)
}
}
}) // cleanup
// tslint:disable-next-line:align
; [deferred, allMap].forEach(m => m.clear())
return result
}