(
arr: T|RecursiveArray<T>, result: T[] = [], skipTypedArray = false)
| 163 | * @doc {heading: 'Util', namespace: 'util'} |
| 164 | */ |
| 165 | export function |
| 166 | flatten<T extends number|boolean|string|Promise<number>|TypedArray>( |
| 167 | arr: T|RecursiveArray<T>, result: T[] = [], skipTypedArray = false): T[] { |
| 168 | if (result == null) { |
| 169 | result = []; |
| 170 | } |
| 171 | if (typeof arr === 'boolean' || typeof arr === 'number' || |
| 172 | typeof arr === 'string' || base.isPromise(arr) || arr == null || |
| 173 | isTypedArray(arr) && skipTypedArray) { |
| 174 | result.push(arr as T); |
| 175 | } else if (Array.isArray(arr) || isTypedArray(arr)) { |
| 176 | for (let i = 0; i < arr.length; ++i) { |
| 177 | flatten(arr[i], result, skipTypedArray); |
| 178 | } |
| 179 | } else { |
| 180 | let maxIndex = -1; |
| 181 | for (const key of Object.keys(arr)) { |
| 182 | // 0 or positive integer. |
| 183 | if (/^([1-9]+[0-9]*|0)$/.test(key)) { |
| 184 | maxIndex = Math.max(maxIndex, Number(key)); |
| 185 | } |
| 186 | } |
| 187 | for (let i = 0; i <= maxIndex; i++) { |
| 188 | // tslint:disable-next-line: no-unnecessary-type-assertion |
| 189 | flatten((arr as RecursiveArray<T>)[i], result, skipTypedArray); |
| 190 | } |
| 191 | } |
| 192 | return result; |
| 193 | } |
no test coverage detected
searching dependent graphs…