UNPKG

@andranik-arakelyan/js-utilities

Version:
1 lines 536 B
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.flatten=flatten;function flatten(array,depth=1){if(!Array.isArray(array)){throw new Error("First argument must be an array")}if(typeof depth!=="number"||depth<0){throw new Error("Depth must be a non-negative number")}if(array.length===0){return[]}if(depth===0){return array.slice()}const result=[];for(const item of array){if(Array.isArray(item)&&depth>0){const flattened=flatten(item,depth-1);result.push(...flattened)}else{result.push(item)}}return result}