{"id":13447118,"url":"https://github.com/TehShrike/deepmerge","last_synced_at":"2025-03-21T17:30:46.088Z","repository":{"id":2316211,"uuid":"3276413","full_name":"TehShrike/deepmerge","owner":"TehShrike","description":"A library for deep (recursive) merging of Javascript objects","archived":false,"fork":false,"pushed_at":"2023-04-21T05:16:12.000Z","size":476,"stargazers_count":2785,"open_issues_count":54,"forks_count":214,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-18T19:26:24.138Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/TehShrike.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2012-01-26T19:00:20.000Z","updated_at":"2025-03-16T13:01:55.000Z","dependencies_parsed_at":"2023-07-05T21:01:40.171Z","dependency_job_id":null,"html_url":"https://github.com/TehShrike/deepmerge","commit_stats":{"total_commits":273,"total_committers":38,"mean_commits":7.184210526315789,"dds":0.4798534798534798,"last_synced_commit":"5b87756a5671635679001cbac72aa42f23472c81"},"previous_names":["nrf110/deepmerge","kyleamathews/deepmerge"],"tags_count":46,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TehShrike%2Fdeepmerge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TehShrike%2Fdeepmerge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TehShrike%2Fdeepmerge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TehShrike%2Fdeepmerge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TehShrike","download_url":"https://codeload.github.com/TehShrike/deepmerge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244838041,"owners_count":20518771,"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-07-31T05:01:08.721Z","updated_at":"2025-03-21T17:30:45.819Z","avatar_url":"https://github.com/TehShrike.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","工具类库","开发辅助","UI Components / UI Tools"],"sub_categories":[],"readme":"# deepmerge\n\nMerges the enumerable properties of two or more objects deeply.\n\n\u003e UMD bundle is 723B minified+gzipped\n\n## Getting Started\n\n### Example Usage\n\u003c!--js\nconst merge = require('./')\n--\u003e\n\n```js\nconst x = {\n\tfoo: { bar: 3 },\n\tarray: [{\n\t\tdoes: 'work',\n\t\ttoo: [ 1, 2, 3 ]\n\t}]\n}\n\nconst y = {\n\tfoo: { baz: 4 },\n\tquux: 5,\n\tarray: [{\n\t\tdoes: 'work',\n\t\ttoo: [ 4, 5, 6 ]\n\t}, {\n\t\treally: 'yes'\n\t}]\n}\n\nconst output = {\n\tfoo: {\n\t\tbar: 3,\n\t\tbaz: 4\n\t},\n\tarray: [{\n\t\tdoes: 'work',\n\t\ttoo: [ 1, 2, 3 ]\n\t}, {\n\t\tdoes: 'work',\n\t\ttoo: [ 4, 5, 6 ]\n\t}, {\n\t\treally: 'yes'\n\t}],\n\tquux: 5\n}\n\nmerge(x, y) // =\u003e output\n```\n\n\n### Installation\n\nWith [npm](http://npmjs.org) do:\n\n```sh\nnpm install deepmerge\n```\n\ndeepmerge can be used directly in the browser without the use of package managers/bundlers as well:  [UMD version from unpkg.com](https://unpkg.com/deepmerge/dist/umd.js).\n\n\n### Include\n\ndeepmerge exposes a CommonJS entry point:\n\n```\nconst merge = require('deepmerge')\n```\n\nThe ESM entry point was dropped due to a [Webpack bug](https://github.com/webpack/webpack/issues/6584).\n\n# API\n\n\n## `merge(x, y, [options])`\n\nMerge two objects `x` and `y` deeply, returning a new merged object with the\nelements from both `x` and `y`.\n\nIf an element at the same key is present for both `x` and `y`, the value from\n`y` will appear in the result.\n\nMerging creates a new object, so that neither `x` or `y` is modified.\n\n**Note:** By default, arrays are merged by concatenating them.\n\n## `merge.all(arrayOfObjects, [options])`\n\nMerges any number of objects into a single result object.\n\n```js\nconst foobar = { foo: { bar: 3 } }\nconst foobaz = { foo: { baz: 4 } }\nconst bar = { bar: 'yay!' }\n\nmerge.all([ foobar, foobaz, bar ]) // =\u003e { foo: { bar: 3, baz: 4 }, bar: 'yay!' }\n```\n\n\n## Options\n\n### `arrayMerge`\n\nThere are multiple ways to merge two arrays, below are a few examples but you can also create your own custom function.\n\nYour `arrayMerge` function will be called with three arguments: a `target` array, the `source` array, and an `options` object with these properties:\n\n- `isMergeableObject(value)`\n- `cloneUnlessOtherwiseSpecified(value, options)`\n\n#### `arrayMerge` example: overwrite target array\n\nOverwrites the existing array values completely rather than concatenating them:\n\n```js\nconst overwriteMerge = (destinationArray, sourceArray, options) =\u003e sourceArray\n\nmerge(\n\t[1, 2, 3],\n\t[3, 2, 1],\n\t{ arrayMerge: overwriteMerge }\n) // =\u003e [3, 2, 1]\n```\n\n#### `arrayMerge` example: combine arrays\n\nCombines objects at the same index in the two arrays.\n\nThis was the default array merging algorithm pre-version-2.0.0.\n\n```js\nconst combineMerge = (target, source, options) =\u003e {\n\tconst destination = target.slice()\n\n\tsource.forEach((item, index) =\u003e {\n\t\tif (typeof destination[index] === 'undefined') {\n\t\t\tdestination[index] = options.cloneUnlessOtherwiseSpecified(item, options)\n\t\t} else if (options.isMergeableObject(item)) {\n\t\t\tdestination[index] = merge(target[index], item, options)\n\t\t} else if (target.indexOf(item) === -1) {\n\t\t\tdestination.push(item)\n\t\t}\n\t})\n\treturn destination\n}\n\nmerge(\n\t[{ a: true }],\n\t[{ b: true }, 'ah yup'],\n\t{ arrayMerge: combineMerge }\n) // =\u003e [{ a: true, b: true }, 'ah yup']\n```\n\n### `isMergeableObject`\n\nBy default, deepmerge clones every property from almost every kind of object.\n\nYou may not want this, if your objects are of special types, and you want to copy the whole object instead of just copying its properties.\n\nYou can accomplish this by passing in a function for the `isMergeableObject` option.\n\nIf you only want to clone properties of plain objects, and ignore all \"special\" kinds of instantiated objects, you probably want to drop in [`is-plain-object`](https://github.com/jonschlinkert/is-plain-object).\n\n```js\nconst { isPlainObject } = require('is-plain-object')\n\nfunction SuperSpecial() {\n\tthis.special = 'oh yeah man totally'\n}\n\nconst instantiatedSpecialObject = new SuperSpecial()\n\nconst target = {\n\tsomeProperty: {\n\t\tcool: 'oh for sure'\n\t}\n}\n\nconst source = {\n\tsomeProperty: instantiatedSpecialObject\n}\n\nconst defaultOutput = merge(target, source)\n\ndefaultOutput.someProperty.cool // =\u003e 'oh for sure'\ndefaultOutput.someProperty.special // =\u003e 'oh yeah man totally'\ndefaultOutput.someProperty instanceof SuperSpecial // =\u003e false\n\nconst customMergeOutput = merge(target, source, {\n\tisMergeableObject: isPlainObject\n})\n\ncustomMergeOutput.someProperty.cool // =\u003e undefined\ncustomMergeOutput.someProperty.special // =\u003e 'oh yeah man totally'\ncustomMergeOutput.someProperty instanceof SuperSpecial // =\u003e true\n```\n\n### `customMerge`\n\nSpecifies a function which can be used to override the default merge behavior for a property, based on the property name.\n\nThe `customMerge` function will be passed the key for each property, and should return the function which should be used to merge the values for that property.\n\nIt may also return undefined, in which case the default merge behaviour will be used.\n\n```js\nconst alex = {\n\tname: {\n\t\tfirst: 'Alex',\n\t\tlast: 'Alexson'\n\t},\n\tpets: ['Cat', 'Parrot']\n}\n\nconst tony = {\n\tname: {\n\t\tfirst: 'Tony',\n\t\tlast: 'Tonison'\n\t},\n\tpets: ['Dog']\n}\n\nconst mergeNames = (nameA, nameB) =\u003e `${nameA.first} and ${nameB.first}`\n\nconst options = {\n\tcustomMerge: (key) =\u003e {\n\t\tif (key === 'name') {\n\t\t\treturn mergeNames\n\t\t}\n\t}\n}\n\nconst result = merge(alex, tony, options)\n\nresult.name // =\u003e 'Alex and Tony'\nresult.pets // =\u003e ['Cat', 'Parrot', 'Dog']\n```\n\n\n### `clone`\n\n*Deprecated.*\n\nDefaults to `true`.\n\nIf `clone` is `false` then child objects will be copied directly instead of being cloned.  This was the default behavior before version 2.x.\n\n\n# Testing\n\nWith [npm](http://npmjs.org) do:\n\n```sh\nnpm test\n```\n\n\n# License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTehShrike%2Fdeepmerge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTehShrike%2Fdeepmerge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTehShrike%2Fdeepmerge/lists"}