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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Flattens an array in non-recursive way. Input arrays are not modified.
|
|
|
|
|
*/
|
2019-03-28 14:50:39 +01:00
|
|
|
export function flatten(list: any[], mapFn?: (value: any) => any): any[] {
|
2019-02-20 14:21:20 -08:00
|
|
|
const result: any[] = [];
|
|
|
|
|
let i = 0;
|
|
|
|
|
while (i < list.length) {
|
|
|
|
|
const item = list[i];
|
|
|
|
|
if (Array.isArray(item)) {
|
|
|
|
|
if (item.length > 0) {
|
|
|
|
|
list = item.concat(list.slice(i + 1));
|
|
|
|
|
i = 0;
|
|
|
|
|
} else {
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-03-28 14:50:39 +01:00
|
|
|
result.push(mapFn ? mapFn(item) : item);
|
2019-02-20 14:21:20 -08:00
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|