list-flatten
Version:
Flatten an array or array-like list (non-recursive implementation).
38 lines (30 loc) • 914 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = flatten;
require('babel-polyfill');
function flatten(array) {
var currentArray = Array.from(array); // ES2015, in case we pass an array-like thing.
var breadcrumbs = [];
var result = [];
var i = 0;
while (currentArray) {
if (currentArray[i] instanceof Array) {
breadcrumbs.push({ currentArray: currentArray, i: i });
currentArray = currentArray[i];
i = -1;
} else {
result.push(currentArray[i]);
}
var crumb = null;
while (currentArray && i === currentArray.length - 1) {
crumb = breadcrumbs.pop();
currentArray = crumb ? crumb.currentArray : null;
i = crumb ? crumb.i : null;
}
i += 1;
}
return result;
}
//# sourceMappingURL=index.js.map