UNPKG

@gvray/eskit

Version:

A rich and colorful toolkit about typescript and javascript.

77 lines 2.55 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Flattens a nested array into a single-level array. * 将嵌套数组展平为单级数组。 * * This function recursively flattens all nested arrays regardless of depth. * It modifies the input array during processing, so pass a copy if you need * to preserve the original array. * 此函数递归展平所有嵌套数组,无论深度如何。 * 它在处理过程中会修改输入数组,如果需要保留原始数组,请传递副本。 * * @template T - The type of the array elements / 数组元素的类型 * @param array - The array to flatten / 要展平的数组 * @returns The flattened array / 展平后的数组 * * @example * ```typescript * // Basic flattening / 基本展平 * const nested = [1, [2, 3], [4, [5, 6]]] * flatten([...nested]) // [1, 2, 3, 4, 5, 6] * * // Mixed types / 混合类型 * const mixed = ['a', ['b', 'c'], ['d', ['e', 'f']]] * flatten([...mixed]) // ['a', 'b', 'c', 'd', 'e', 'f'] * * // Deep nesting / 深度嵌套 * const deep = [1, [2, [3, [4, [5]]]]] * flatten([...deep]) // [1, 2, 3, 4, 5] * * // Empty arrays / 空数组 * const withEmpty = [1, [], [2, []], 3] * flatten([...withEmpty]) // [1, 2, 3] * ``` * * @since 1.0.0 */ var flatten = function (array) { var result = []; while (array.length) { var item = array.shift(); if (Array.isArray(item)) { array.unshift.apply(array, __spreadArray([], __read(item), false)); } else { result.push(item); } } return result; }; exports.default = flatten; //# sourceMappingURL=flatten.js.map