https://github.com/waynehamadi/nested-functions.git
This project demonstrates two approaches to flatten nested objects with underscore notation: a custom implementation and one using the established flat library.
# Clone the repository
git clone git@github.com:waynehamadi/nested-functions.git
cd nested-functions
# Install dependencies
npm install
The custom implementation (src/flatten.ts) flattens nested objects with specific rules:
import { flattenObject } from "./src/flatten";
const input = {
person: {
name: "John",
age: 30,
},
addresses: [
{
street: "Main St",
},
],
};
const flattened = flattenObject(input);
// Result:
// {
// person_name: "John",
// person_age: 30,
// addresses_0_street: "Main St"
// }
flat libraryInstead of using the custom implementation, it's recommended to use the established flat library:
import flatten from "flat";
const input = {
person: {
name: "John",
age: 30,
},
};
const flattened = flatten(input, { delimiter: "_" });
# Run all tests
npm test
# Run only custom implementation tests
npm test flatten.test.ts
# Run only flat library tests
npm test better-flatten.test.ts
// Custom throws error:
const input = {
user: {
profile: { data: "nested" },
profile_data: "mid-level",
},
user_profile_data: "top-level",
};
// This throws error with custom implementation
flattenObject(input); // Error: "Conflicting field names: user_profile_data appears multiple times"
// flat just overwrites:
flatten(input, { delimiter: "_" }); // Works fine, last value wins
const input = {
emptyArr: [],
emptyObj: {},
};
// Custom removes empty structures
flattenObject(input); // {}
// flat keeps them
flatten(input, { delimiter: "_" }); // { emptyArr: [], emptyObj: {} }
// Custom is strictly typed
type Primitive = string | number | boolean | null | undefined;
type NestedObject = {
[key: string]: Primitive | NestedObject | Array<any>;
};
// flat is more permissive with types
flat?While the custom implementation demonstrates interesting edge cases and strict type safety, for production use you should use the flat library because:
# Run tests in watch mode
npm run test:watch
# Type checking
npm run type-check
ISC