Array Flatten Utility
The `utils-flatten` package provides a single, straightforward utility function for recursively flattening arrays in JavaScript. It is a minimal, focused library for transforming deeply nested arrays into a single-level array. The package is currently at version 1.0.0 and appears to be an unmaintained or abandoned project, with its last known activity dating back to around 2013, as indicated by the copyright and build status badge. Its primary differentiation, if any, lies in its extreme simplicity and small footprint, predating the widespread adoption of native `Array.prototype.flat()` in modern JavaScript environments (ES2019+). Developers should be aware that contemporary JavaScript offers built-in alternatives, making this package largely redundant for new projects.
Common errors
-
ReferenceError: flatten is not defined
cause Attempting to use `flatten` without properly importing or requiring it, or using an incorrect import style (e.g., in a modern ESM context without transpilation).fixEnsure you are using CommonJS `const flatten = require('utils-flatten');` in a CJS environment. For ESM, use `Array.prototype.flat()` instead. -
TypeError: (0 , _utilsFlatten.default) is not a function
cause This error often occurs when a bundler (like Webpack or Rollup) tries to interpret a CommonJS module's default export when an ESM default import (`import flatten from 'utils-flatten'`) is used, but the CJS module's export structure doesn't align with what the bundler expects for a 'default' export.fixRevert to CommonJS `const flatten = require('utils-flatten');` or, preferably, migrate to `Array.prototype.flat()`.
Warnings
- deprecated The `utils-flatten` package is effectively deprecated by native JavaScript features. Modern environments (ES2019+ / Node.js 11+) include `Array.prototype.flat()` and `Array.prototype.flatMap()` which provide equivalent or superior functionality directly in the language, negating the need for this external dependency.
- gotcha This package is a CommonJS module and does not natively support ES Modules (ESM) syntax. Attempting to use `import flatten from 'utils-flatten'` directly in an ESM environment will result in errors unless a bundler is configured to handle CJS interoperability.
- gotcha The package is unmaintained, with its last known update around 2013 (version 1.0.0). This means it will not receive bug fixes, performance improvements, or security patches. Using unmaintained dependencies can introduce vulnerabilities or compatibility issues with newer Node.js versions or language features.
Install
-
npm install utils-flatten -
yarn add utils-flatten -
pnpm add utils-flatten
Imports
- flatten
import flatten from 'utils-flatten'; import { flatten } from 'utils-flatten';const flatten = require('utils-flatten');
Quickstart
const flatten = require('utils-flatten');
const nestedArray = ['one', ['two', ['three', 'four'], 'five'], 6, [7, [8]]];
const flattenedArray = flatten(nestedArray);
console.log('Original array:', nestedArray);
console.log('Flattened array:', flattenedArray);
// Expected output: ['one', 'two', 'three', 'four', 'five', 6, 7, 8]
const anotherNestedArray = [1, [2, [3, 4], 5], [6, [7, [8, 9]]]];
const fullyFlattened = flatten(anotherNestedArray);
console.log('Another original array:', anotherNestedArray);
console.log('Fully flattened array:', fullyFlattened);