{"id":16906601,"url":"https://github.com/rsms/js-object-merge","last_synced_at":"2025-03-22T10:31:18.818Z","repository":{"id":66105789,"uuid":"596377","full_name":"rsms/js-object-merge","owner":"rsms","description":"3-way JavaScript Object merging -- Object.merge(v1, v1a, v1b) -\u003e v2","archived":false,"fork":false,"pushed_at":"2010-04-06T01:42:47.000Z","size":92,"stargazers_count":55,"open_issues_count":0,"forks_count":10,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-15T21:17:05.581Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rsms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-04-06T01:41:02.000Z","updated_at":"2025-01-07T14:49:51.000Z","dependencies_parsed_at":"2023-02-19T22:35:15.430Z","dependency_job_id":null,"html_url":"https://github.com/rsms/js-object-merge","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsms%2Fjs-object-merge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsms%2Fjs-object-merge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsms%2Fjs-object-merge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsms%2Fjs-object-merge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rsms","download_url":"https://codeload.github.com/rsms/js-object-merge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244943745,"owners_count":20536290,"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-13T18:43:30.997Z","updated_at":"2025-03-22T10:31:18.558Z","avatar_url":"https://github.com/rsms.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Object.merge(o, a, b) -\u003e c\n\n3-way JavaScript Object merging.\n\nTakes 3 versions of the same object -- where version 2 and 3 are both derived from version 1 -- and generates a 4th version, effectively merging version 2 and 3 together. When a conflict is detected (changes made in both version 2 and 3) changes from version 3 are used and information about the conflict is added to the `conflicts` structure.\n\nI wrote this for using together with [node-couchdb-min](http://github.com/rsms/node-couchdb-min) to provide conflict resolution for CouchDB documents.\n\n## Prototype\n\n    Object.merge(Object origin, Object versionA, Object versionB,\n      Bool shallow | Object base) -\u003e Object\n\n- `origin`: Ancestor version from which both `versionA` and `versionB` is derived.\n\n- `versionA`: Version A of `origin`\n\n- `versionB`: Version B of `origin`\n\n- `shallow`: (*Advanced property*) If true, only resolve values at the first level. By setting this to true, many conflicts which could be merged automatically will not be merged. However, if you only want to test if there's a possibility of conflicts, setting this to true will yield better performance. In most cases this should be false or not set. \n\n- `base`: (*Advanced property*) A base object on which to build the final, merged version. To be entirely sure about how to use this and what it implies, you should probably walk through the source code.\n\n\n**Return value:**\n\n`Object.merge` returns a structure which looks like this:\n\n    { merged: \n       { key1: 123\n       , key2: 'john doe'\n       , key3: [ 'abc', 'ooo', 'xyz' ]\n       }\n    , added: \n       { a: { key1: 789 }\n       , b: { key1: 123, key2: 'john doe' }\n       }\n    , updated: \n       { a: { key3: [ 'abc', 'd,ef', 'xyz' ] }\n       , b: {}\n       }\n    , conflicts: { key1: { a: 789, o: 4, b: 123 } }\n    }\n\n- The `merged` key contains \"version 4\" and is the merged result.\n\n- The `added` key contains information about what parts where added (was not \n  present in the origin/version 1).\n\n- The `updated` key contains information about what parts where updated (was \n  present in origin/version 1).\n\n- If the `conflicts` key is present, there are conflicts and they are described\n  by a structure `key: {a: value, o: value, b: value}` where each `value` is the\n  value in each of the three versions. If the key was not present in the origin,\n  `o` is not present. \n\nUnless `Object.merge` is called with a fourth argument with the constant `true`, conflict resolution is recursive for deep conflicts. In this case complex values (array, object) in the conflict structure will -- instead of the different values, be yet another `conflicts` structure. It might look like this:\n\n    conflicts: {\n      // Simple conflict:\n      age: { \n        a: 12,  b: 13\n      },\n      // Conflict originates deep into a complex value:\n      following: {\n        conflicts: {\n          // Member 'threeLetters' of the \"conflicts\" object is the source:\n          'threeLetters': {\n            a: 'xyz',  o: 'def',  b: 'ooo'\n          }\n        }\n      }\n    }\n\n\n## Example:\n\n    // The original version which both A and B are derived from.\n    origin = {\n      name:'rsms', \n      following:['abc', 'd,ef'],\n      modified:12345678,\n      aliases:{'abc':'Abc'}\n    }\n    // Version A\n    A = {\n      age:12, \n      location:'sto', \n      sex:'m', \n      name:'rsms', \n      modified:12345679,\n      following:['abc', 'cat', 'xyz'],\n      aliases:{'abc':'Abc', 'def':'Def'}\n    }\n    // Version B\n    B = {\n      age:13, \n      name:'rsms', \n      sex:'m', \n      following:['abc', 'ooo'], \n      modified:12345679, \n      aliases:{'abc':'Abc', 'aab':'Aab'}\n    }\n\n    result = Object.merge(origin, A, B);\n    sys.puts('--\u003e\\n'+sys.inspect(result, false, 10));\n\n    --\u003e\n    { merged: \n       { age: 13\n       , name: 'rsms'\n       , sex: 'm'\n       , following: [ 'abc', 'ooo', 'xyz' ]\n       , modified: 12345679\n       , aliases: { abc: 'Abc', aab: 'Aab', def: 'Def' }\n       , location: 'sto'\n       }\n    , added: \n       { a: { age: 12, location: 'sto', sex: 'm' }\n       , b: { age: 13, sex: 'm' }\n       }\n    , updated: \n       { a: \n          { modified: 12345679\n          , following: [ 'abc', 'cat', 'xyz' ]\n          , aliases: { abc: 'Abc', def: 'Def' }\n          }\n       , b: \n          { following: [ 'abc', 'ooo' ]\n          , modified: 12345679\n          , aliases: { abc: 'Abc', aab: 'Aab' }\n          }\n       }\n    , conflicts: \n       { age: { a: 12, b: 13 }\n       , following: { conflicts: { '1': { a: 'cat', o: 'd,ef', b: 'ooo' } } }\n       }\n    }\n\n## Requirements\n\n- `Array.isArray(object) -\u003e Boolean` to be implemented (which it is already in modern JavaScript environments).\n\n## MIT license\n\nCopyright (c) 2010 Rasmus Andersson \u003chttp://hunch.se/\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frsms%2Fjs-object-merge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frsms%2Fjs-object-merge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frsms%2Fjs-object-merge/lists"}