flatten pipe

by Huy Le

JavaScript

// Is flatten useful?
// #NotUsefulPipe

import { Injectable, Pipe, PipeTransform } from "@angular/core";

@Pipe({ name: `flatten` })
@Injectable()
export class FlattenPipe implements PipeTransform {
    transform<T>(value:Array<T> = []) {
        if (Array.isArray(value)) { 
        		return value.reduce((a,c) =>
            		a.concat(Array.isArray(c) ? flatten(c) : c), []);
        }
        
        throw new Error(`What's there to flatten?`);
    }
}