{"id":17448342,"url":"https://github.com/mesqueeb/flatten-anything","last_synced_at":"2025-04-07T18:13:01.254Z","repository":{"id":33986419,"uuid":"165463428","full_name":"mesqueeb/flatten-anything","owner":"mesqueeb","description":"Flatten objects and replace nested props with 'prop.subprop'. A simple and small integration.","archived":false,"fork":false,"pushed_at":"2025-02-19T18:46:40.000Z","size":671,"stargazers_count":32,"open_issues_count":6,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T17:16:37.163Z","etag":null,"topics":["flatten","flatten-deep-object","flatten-nested-props","flatten-object","flatten-paths","get-flat-object","object","pathify","pathify-props","paths"],"latest_commit_sha":null,"homepage":"https://npmjs.com/flatten-anything","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mesqueeb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"mesqueeb","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2019-01-13T03:55:01.000Z","updated_at":"2025-02-19T18:46:44.000Z","dependencies_parsed_at":"2024-06-18T17:07:24.262Z","dependency_job_id":"ff592d2c-5533-4301-aeef-f1c924d2d2c4","html_url":"https://github.com/mesqueeb/flatten-anything","commit_stats":{"total_commits":48,"total_committers":3,"mean_commits":16.0,"dds":"0.45833333333333337","last_synced_commit":"ef67c1ee250027e5d076bce65aadc66744fc771c"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mesqueeb%2Fflatten-anything","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mesqueeb%2Fflatten-anything/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mesqueeb%2Fflatten-anything/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mesqueeb%2Fflatten-anything/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mesqueeb","download_url":"https://codeload.github.com/mesqueeb/flatten-anything/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247704571,"owners_count":20982298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flatten","flatten-deep-object","flatten-nested-props","flatten-object","flatten-paths","get-flat-object","object","pathify","pathify-props","paths"],"created_at":"2024-10-17T20:07:07.264Z","updated_at":"2025-04-07T18:13:01.223Z","avatar_url":"https://github.com/mesqueeb.png","language":"TypeScript","funding_links":["https://github.com/sponsors/mesqueeb"],"categories":[],"sub_categories":[],"readme":"# Flatten anything 🏏\n\n\u003ca href=\"https://www.npmjs.com/package/flatten-anything\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/flatten-anything.svg\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/package/flatten-anything\"\u003e\u003cimg src=\"https://img.shields.io/npm/dw/flatten-anything.svg\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\n```\nnpm i flatten-anything\n```\n\nFlatten objects and replace nested props with 'prop.subprop'. A simple and small integration.\n\nI was looking for:\n\n- A simple solution to flatten objects \u0026 arrays\n- Only flatten plain objects and **not special class instances**!\n\nThis last one is crucial! So many libraries use custom classes that create objects with special prototypes, and such objects all break when trying to flatten them. So we gotta be careful!\n\nflatten-anything will flatten objects and their nested properties, but only as long as they're \"plain objects\". As soon as a sub-prop is not a \"plain object\" and has a special prototype, it will stop flattening there and reference that instance \"as is\". ♻️\n\nVery usable for creating a payload for **Firebase Firestore** `update` function, which only accepts flat objects! With Firestore for example `Firebase.firestore.FieldValue.delete()` does not break.\n\nCan be used in combination with [nestify-anything](https://github.com/mesqueeb/nestify-anything), which does the exact opposite of this one! 😉\n## Meet the family (more tiny utils with TS support)\n\n- [is-what 🙉](https://github.com/mesqueeb/is-what)\n- [is-where 🙈](https://github.com/mesqueeb/is-where)\n- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)\n- [check-anything 👁](https://github.com/mesqueeb/check-anything)\n- [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)\n- [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)\n- [map-anything 🗺](https://github.com/mesqueeb/map-anything)\n- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)\n- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)\n- [case-anything 🐫](https://github.com/mesqueeb/case-anything)\n- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)\n- [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)\n\n## Usage\n\n- If you pass an object, it will flatten all nested properties\n- If you pass an array, it will flatten all nested arrays\n\n### Flatten objects\n\n```js\nimport { flatten } from 'flatten-anything'\n\nconst target = { name: 'Ho-oh', types: { fire: true, flying: true } }\n\nflatten(target)\n// returns {\n//   name: 'Ho-oh',\n//   types.fire: true,\n//   types.flying: true\n// }\n```\n\n\u003e Please note that when you pass an object it will only flatten nested object props, and do nothing to arrays inside the object.\n\n### Flatten arrays\n\n```js\nimport { flatten } from 'flatten-anything'\n\nconst target = [1, 2, ['a', 'b', ['y', 'z']], 3]\n\nflatten(target)\n// returns [1, 2, 'a', 'b', 'y', 'z', 3]\n```\n\n\u003e Please note that when you pass an array it will only flatten direct arrays, and do nothing to objects inside the array.\n\n### Limit the depth to flatten\n\n\u003e currently only works with objects. Please open an issue if you want it to work with arrays as well!\n\n```js\nconst target = {\n  name: 'Lugia',\n  appearence: {\n    // let's only flatten 1 level until here:\n    parts: { wings: true },\n    colors: { white: true, blue: true },\n  },\n}\nconst untilDepth = 1\n\nflatten(target, untilDepth)\n// returns {\n//   name: 'Lugia',\n//   appearence.parts: { wings: true },\n//   appearence.colors: { white: true, blue: true },\n// }\n```\n\n### Flatten only certain props\n\nIt's possible to only flatten eg. one prop.\n\n```js\nimport { flattenObjectProps } from 'flatten-anything'\n\nconst target = {\n  appearence: { hair: 'orange' },\n  traits: { strength: 9000 },\n}\nconst propsToFlatten = ['traits']\n\nflattenObjectProps(target, propsToFlatten)\n// returns {\n//   appearence: { hair: 'orange' },\n//   traits.strength: 9000,\n// }\n```\n\nYou can also point to a nested object property to only flatten specific nested props, but keep the rest as an object!\n\n```js\nconst target = {\n  traits: {\n    strength: 9000,\n    range: { min: 8000, max: 10000 },\n  },\n}\n\nconst propsToFlatten = ['traits.range']\n// only flatten `traits.range` and nothing else.\n\nflattenObjectProps(target, propsToFlatten)\n// returns {\n//   traits: { strength: 9000 },\n//   'traits.range': {min: 8000, max: 10000},\n// }\n```\n\n### Treeshaking\n\nImporting `flatten` allows you to use it for both objects _and_ arrays.\n\nIf you use webpack, rollup, etc. you can _import less code_ by specifying the exact flatten function you need:\n\n```js\nimport { flattenObject } from 'flatten-anything'\n// or\nimport { flattenArray } from 'flatten-anything'\n```\n\n### Example for Firestore\n\n```js\nconst pokemon = {\n  name: 'Charizard',\n  types: { dark: true, fire: true, flying: true },\n}\n// we want to delete the `dark` type from this Pokemon\n\nconst payload = flatten({\n  types: { dark: Firebase.firestore.FieldValue.delete() },\n})\nFirebase.firestore()\n  .doc('pokemon/charizard')\n  .update(payload)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmesqueeb%2Fflatten-anything","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmesqueeb%2Fflatten-anything","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmesqueeb%2Fflatten-anything/lists"}