UNPKG

@reflet/express

Version:

Well-defined and well-typed express decorators

43 lines (42 loc) 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.flatMapFast = void 0; function flatMapFast(array, mapper) { // wrapping the original array to avoid mutating it. const flattened = [array]; // usual loop, but, don't put `i++` in third clause // because it won't increment it when the element is an array. for (let i = 0; i < flattened.length;) { const value = flattened[i]; // if the element is an array then we'll put its contents // into `flattened` replacing the current element. if (Array.isArray(value)) { if (value.length > 0) { // to provide the `value` array to splice() // we need to add the splice() args to its front, // these args tell it to splice at `i` and delete what's at `i`. value.unshift(i, 1); // in-place change flattened.splice.apply(flattened, value); // take (i, 1) back off the `value` front so it remains "unchanged". value.splice(0, 2); } else { // remove an empty array from `flattened` flattened.splice(i, 1); } // we don't do `i++` because we want it to re-evaluate the new element // at `i` in case it is an array, or we deleted an empty array at `i`. } else { // it's not an array so map the value and move on to the next element. /* istanbul ignore else - unused */ if (mapper) { flattened[i] = mapper(value); } i++; } } return flattened; } exports.flatMapFast = flatMapFast;