Flattie Object Flattening Utility
Flattie is a minimalist and highly performant JavaScript utility for recursively flattening nested objects and arrays into a single-depth object. Currently at version 1.1.1, the library maintains a stable release cadence with incremental patches. Its core differentiators include its extremely small bundle size (203B), demonstrated speed via benchmarks against alternatives like `flat` and `flatten-object`, and customizable behavior for key concatenation via a 'glue' string. By default, it automatically omits nullish values (`null`, `undefined`) from the flattened output, a feature that can be toggled. It also ships with TypeScript type definitions, providing a robust developer experience. Its counterpart, `nestie`, provides the reverse operation (expanding flattened objects).
Common errors
-
TypeError: flattie is not a function
cause Attempting to import `flattie` as a default export or using `require()` without destructuring in a CommonJS environment. The `flattie` function is a named export.fixFor ESM, use `import { flattie } from 'flattie';`. For CommonJS, use `const { flattie } = require('flattie');`. -
Object.prototype.hasOwnProperty.call is not a function
cause This error typically indicates an issue with an object not being a plain object or potential environment incompatibility, though less common with `flattie`'s focused scope. It might occur if `input` is a primitive or a highly custom object lacking standard `Object.prototype` methods.fixEnsure the `input` provided to `flattie` is a valid JavaScript object or array. Verify your Node.js version is `>=8` as specified in the package engines.
Warnings
- gotcha By default, `null` and `undefined` values are automatically excluded from the flattened output. This behavior might be unexpected if you intend to preserve all original data points.
- gotcha Flattie always returns a new object, even if the input is an array. Array elements will be mapped to keys using their numerical indices combined with the specified glue string.
- gotcha While `null` and `undefined` are purged, other 'empty' or special values like empty strings (`''`) or `NaN` are preserved in the output by default.
Install
-
npm install flattie -
yarn add flattie -
pnpm add flattie
Imports
- flattie
import flattie from 'flattie'; const flattie = require('flattie');import { flattie } from 'flattie';
Quickstart
import { flattie } from 'flattie';
const nestedObject = {
user: 'alice',
profile: {
email: 'alice@example.com',
settings: {
theme: 'dark',
notifications: true,
preferences: [
{ id: 1, value: 'optionA' },
{ id: 2, value: null },
{ id: 3, value: 'optionC' }
]
}
},
status: null,
tags: ['admin', 'premium']
};
// Flatten the object with default glue ('.') and nullish purging (true)
const flatDefault = flattie(nestedObject);
console.log('Default Flattened:', flatDefault);
// Flatten the object keeping nullish values
const flatKeepNullish = flattie(nestedObject, '.', true);
console.log('Flattened (keeping nullish):', flatKeepNullish);
// Flatten the object with a custom glue
const flatCustomGlue = flattie(nestedObject, '__');
console.log('Flattened (custom glue):', flatCustomGlue);