{"id":16688853,"url":"https://github.com/danieldietrich/merge-objects","last_synced_at":"2025-03-21T18:33:36.561Z","repository":{"id":35113477,"uuid":"208347940","full_name":"danieldietrich/merge-objects","owner":"danieldietrich","description":"Merges multiple objects into a new object.","archived":false,"fork":false,"pushed_at":"2023-01-05T00:38:22.000Z","size":305,"stargazers_count":3,"open_issues_count":16,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-17T17:26:01.178Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/danieldietrich.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":".github/CODEOWNERS","security":null,"support":null},"funding":{"github":"danieldietrich","custom":"https://www.paypal.me/danieldietrich13"}},"created_at":"2019-09-13T21:16:04.000Z","updated_at":"2023-06-07T06:58:08.000Z","dependencies_parsed_at":"2023-01-15T14:06:16.498Z","dependency_job_id":null,"html_url":"https://github.com/danieldietrich/merge-objects","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieldietrich%2Fmerge-objects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieldietrich%2Fmerge-objects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieldietrich%2Fmerge-objects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieldietrich%2Fmerge-objects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danieldietrich","download_url":"https://codeload.github.com/danieldietrich/merge-objects/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244787014,"owners_count":20510040,"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":[],"created_at":"2024-10-12T15:45:27.176Z","updated_at":"2025-03-21T18:33:36.238Z","avatar_url":"https://github.com/danieldietrich.png","language":"TypeScript","readme":"[![npm version](https://img.shields.io/npm/v/@danieldietrich/merge-objects?logo=npm\u0026style=flat-square)](https://www.npmjs.com/package/@danieldietrich/merge-objects/)[![vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/@danieldietrich/merge-objects?style=flat-square)](https://snyk.io/test/npm/@danieldietrich/merge-objects)[![minzipped size](https://img.shields.io/bundlephobia/minzip/@danieldietrich/merge-objects?style=flat-square)](https://bundlephobia.com/result?p=@danieldietrich/merge-objects@latest)\n\u0026nbsp;\n[![build](https://img.shields.io/travis/danieldietrich/merge-objects?logo=github\u0026style=flat-square)](https://travis-ci.org/danieldietrich/merge-objects/)[![coverage](https://img.shields.io/codecov/c/github/danieldietrich/merge-objects?style=flat-square)](https://codecov.io/gh/danieldietrich/merge-objects/)\n\u0026nbsp;\n![Platform](https://img.shields.io/badge/platform-Node%20v10%20+%20Browser%20%28ES6%2fES2015%29-decc47?logo=TypeScript\u0026style=flat-square)\n\u0026nbsp;\n[![Sponsor](https://img.shields.io/badge/GitHub-💖Sponsors-b5b7b9?logo=github\u0026style=flat-square)](https://github.com/sponsors/danieldietrich)[![donate](https://img.shields.io/badge/Donate-PayPal-blue.svg?style=flat-square)](https://paypal.me/danieldietrich13)[![license](https://img.shields.io/github/license/danieldietrich/merge-objects?style=flat-square)](https://opensource.org/licenses/MIT/)\n\u0026nbsp;\n[![Follow](https://img.shields.io/twitter/follow/danieldietrich?label=Follow\u0026style=social)](https://twitter.com/danieldietrich/)\n\n# merge-objects\n\nMerges multiple objects into a new object.\n\nCompared to _Object.assign()_ or object spreads _{...obj1, ...obj2}_, values are not assigned as a whole. Instead, properties that don't hold the value _undefined_ are recursively merged. The difference to Lodash's __.merge_ or jQuery's _deep extend_ is that arrays are concatenated.\n\nFeatures:\n\n* Type-safe: computes intersection of input arguments\n* Fail-safe: allows any input type but only merges objects\n* Immutable: creates a new object instead of assigning to the first argument\n* Concatenates arrays []\n* Recursively merges objects {}\n* Skips undefined properties\n* Overwrites properties if value is not undefined\n* Merges up to 4 objects, composes for more.\n\n## Installation\n\n```bash\nnpm i @danieldietrich/merge-objects\n```\n\n## Usage\n\nThe module supports ES6 _import_ and CommonJS _require_ style.\n\n```ts\nimport mergeObjects from '@danieldietrich/merge-objects';\n\n// -- merge n objects\n// = {a: {b: 2, c: [1, 2], d: 3, e: 4}}\nmergeObjects(\n    {a: {b: 1, c: [1], d: 3}},\n    {a: {b: 2, c: [2]}},\n    {} || undefined,\n    {a: {b: undefined, e: 4}}\n);\n\n// -- remove undefined properties\n// = {b: [1, undefined, 2], c: {}}\nmergeObjects(\n    {a: undefined, b: [1, undefined, 2], c: {d: undefined}}\n);\n```\n\n## Type safety\n\nWhile merge-objects works well with JavaScript, it comes with TypeScript types. The merged object type is inferred by the TypeScript compiler:\n\n```ts\n/*\nconst o: {\n    0: number;\n    a: number;\n    b: number[];\n    c: number[];\n    f: () =\u003e void;\n} \u0026 {\n    0: number;\n    a: string;\n    b: number[];\n    c: string[];\n    f: (arg: number) =\u003e true;\n}\n*/\nconst o = mergeObjects(\n    {0: 1, a: 1,   b: [1], c: [1],   f: () =\u003e {}},\n    {0: 2, a: \"2\", b: [2], c: [\"2\"], f: (arg: number) =\u003e true},\n    {} || undefined,\n);\n```\n\n✅ When objects have similar field types we walk on the sunny path. The compiler infers the union type of the input arguments. The merge result is the intersection of non-nullable input types. This is exactly the way _Object.assign()_ works, see type definitions in [lib.es2015.core.d.ts](https://github.com/microsoft/TypeScript/blob/b963e1a2a7e3b1056ee2552927fa08fcc51e4c7d/lib/lib.es2015.core.d.ts#L305).\n\n```ts\n// const first: number\nconst first = o[0];\n\n// const b: number[]\nconst b = o.b;\n\n// compiler error \"Property 'x' does not exist on type '...'.\"\nconst x = o.x;\n```\n\n✴️ If object fields have orthogonal types, the intersection type results in the type _never_. Functions are an exception, here we get different overloads. However, these might not reflect the actual field type at runtime.\n\n```ts\n// computed type: string \u0026 number, eff. never;\n// actual type: string\nconst a = o.a;\n\n// computed type: number[] \u0026 string[], eff. never[]\n// actual type: Array\u003cnumber | string\u003e\nconst c = o.c;\n\n// computed type: (() =\u003e void) \u0026 ((arg: number) =\u003e true)\n// actual type: ((arg: number) =\u003e true)\nconst f = o.f;\n```\n\nBottom line: thou shalt not merge incompatible objects.\n\n---\n\nCopyright \u0026copy; 2020 by [Daniel Dietrich](cafebab3@gmail.com). Released under the [MIT](https://opensource.org/licenses/MIT/) license.\n","funding_links":["https://github.com/sponsors/danieldietrich","https://www.paypal.me/danieldietrich13","https://paypal.me/danieldietrich13"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanieldietrich%2Fmerge-objects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanieldietrich%2Fmerge-objects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanieldietrich%2Fmerge-objects/lists"}