2019-02-20 14:21:20 -08:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Equivalent to ES6 spread, add each item to an array.
|
|
|
|
|
*
|
|
|
|
|
* @param items The items to add
|
|
|
|
|
* @param arr The array to which you want to add the items
|
|
|
|
|
*/
|
|
|
|
|
export function addAllToArray(items: any[], arr: any[]) {
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
|
arr.push(items[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-05-14 15:55:46 -07:00
|
|
|
* Flattens an array.
|
2019-02-20 14:21:20 -08:00
|
|
|
*/
|
2019-05-14 15:55:46 -07:00
|
|
|
export function flatten(list: any[], dst?: any[]): any[] {
|
|
|
|
|
if (dst === undefined) dst = list;
|
|
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
|
let item = list[i];
|
2019-02-20 14:21:20 -08:00
|
|
|
if (Array.isArray(item)) {
|
2019-05-14 15:55:46 -07:00
|
|
|
// we need to inline it.
|
|
|
|
|
if (dst === list) {
|
|
|
|
|
// Our assumption that the list was already flat was wrong and
|
|
|
|
|
// we need to clone flat since we need to write to it.
|
|
|
|
|
dst = list.slice(0, i);
|
2019-02-20 14:21:20 -08:00
|
|
|
}
|
2019-05-14 15:55:46 -07:00
|
|
|
flatten(item, dst);
|
|
|
|
|
} else if (dst !== list) {
|
|
|
|
|
dst.push(item);
|
2019-02-20 14:21:20 -08:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-14 15:55:46 -07:00
|
|
|
return dst;
|
2019-06-11 22:53:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function deepForEach<T>(input: (T | any[])[], fn: (value: T) => void): void {
|
|
|
|
|
input.forEach(value => Array.isArray(value) ? deepForEach(value, fn) : fn(value));
|
|
|
|
|
}
|