{"name":"@fastify/deepmerge","version":"3.2.0","keywords":["merge","deep","recursive","object","immutable"],"author":{"name":"Aras Abbasi","email":"aras.abbasi@gmail.com"},"license":"MIT","_id":"@fastify/deepmerge@3.2.0","maintainers":[{"name":"simoneb","email":"simone.busoli@gmail.com"},{"name":"delvedor","email":"tommydelved@gmail.com"},{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"jsumners","email":"james.sumners@gmail.com"},{"name":"zekth","email":"vince.legoff@gmail.com"},{"name":"eomm","email":"behemoth89@gmail.com"},{"name":"fox1t","email":"maksim@sinik.it"},{"name":"airhorns","email":"harry@harry.me"},{"name":"kibertoad","email":"iselwin@gmail.com"},{"name":"climba03003","email":"kaka@kakawebsitedemo.com"},{"name":"galvez","email":"jonasgalvez@gmail.com"},{"name":"simenb","email":"sbekkhus91@gmail.com"},{"name":"gurgunday","email":"hey@gurgun.day"},{"name":"metcoder95","email":"me@metcoder.dev"},{"name":"jean-michelet","email":"jean.antoine.michelet@gmail.com"},{"name":"ivan-tymoshenko","email":"ivan@tymoshenko.me"},{"name":"fdawgs","email":"frazer.dev@icloud.com"}],"contributors":[{"name":"Matteo Collina","email":"hello@matteocollina.com"},{"name":"Manuel Spigolon","email":"behemoth89@gmail.com"},{"url":"https://james.sumners.info","name":"James Sumners"},{"url":"https://github.com/fdawgs","name":"Frazer Smith","email":"frazer.dev@icloud.com"}],"homepage":"https://github.com/fastify/deepmerge#readme","bugs":{"url":"https://github.com/fastify/deepmerge/issues"},"dist":{"shasum":"c2f79c1be40720761c263d3026ab98caa916e115","tarball":"http://registry-npm.ywwl.com/@fastify/deepmerge/download/@fastify/deepmerge-3.2.0.tgz","fileCount":5,"integrity":"sha512-aO5giNgFN+rD4fMUAkro9nEL7c9gh5Q3lh0ZGKMDAhQAytf22HLicF/qZ2EYTDmH+XL2WvdazwBfOdmp6NiwBg==","signatures":[{"sig":"MEUCIQDmwVEkbCcvD/MKhtw/n2p9SYJfTa4KJgQx25u7oquNyQIgG67gwolrP43XxOPMZao2edmLDhocwIG1JRadxow77gQ=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":17932,"size":5754,"noattachment":false,"key":"/@fastify/deepmerge/-/@fastify/deepmerge-3.2.0.tgz"},"main":"index.js","type":"commonjs","types":"types/index.d.ts","funding":[{"url":"https://github.com/sponsors/fastify","type":"github"},{"url":"https://opencollective.com/fastify","type":"opencollective"}],"gitHead":"807b021756d8a4a86e8290ba44c4705bf9b8e275","scripts":{"lint":"eslint","test":"npm run test:unit && npm run test:typescript","lint:fix":"eslint --fix","test:unit":"c8 tape test/*.js","test:typescript":"tsd"},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"repository":{"url":"git+https://github.com/fastify/deepmerge.git","type":"git"},"_npmVersion":"11.6.2","description":"Merges the enumerable properties of two or more objects deeply.","directories":{},"_nodeVersion":"24.12.0","_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.3","tsd":"^0.33.0","tape":"^5.7.5","eslint":"^9.17.0","neostandard":"^0.12.0"},"_npmOperationalInternal":{"tmp":"tmp/deepmerge_3.2.0_1768948365652_0.4724781767721027","host":"s3://npm-registry-packages-npm-production"},"readme":"# @fastify/deepmerge\n\n[![CI](https://github.com/fastify/deepmerge/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/deepmerge/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@fastify/deepmerge.svg?style=flat)](https://www.npmjs.com/package/@fastify/deepmerge)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nMerges the enumerable properties of two or more objects deeply. Fastest implementation of deepmerge, see section 'Benchmarks'.\n\n### Install\n```\nnpm i @fastify/deepmerge\n```\n\n### Usage\n\nThe module exports a function, which provides a function to deepmerge Objects.\n\n`@fastify/deepmerge` does not mutate the input objects. It returns a new object, which is the result of the merge.\n\n```\ndeepmerge(options)\n```\n\n`options` is optional and can contain the following values:\n\n- `symbols` (`boolean`, optional) - should also merge object keys that are symbols, default is false\n- `all` (`boolean`, optional) - makes deepmerge accept and merge any number of passed objects, default is false\n- `mergeArray` (`function`, optional) - provide a function, which returns a function to add custom array merging function\n- `cloneProtoObject` (`function`, optional) - provide a function, which must return a clone of the object with the prototype of the object\n- `isMergeableObject` (`function`, optional) - provide a function, which must return true if the object should be merged, default is `isMergeableObject` from this module\n- `onlyDefinedProperties` (`boolean`, optional) - if `true`, properties with `undefined` in the source will not overwrite existing values from the target, default is `false`\n\n```js\nconst deepmerge = require('@fastify/deepmerge')()\nconst result = deepmerge({a: 'value'}, { b: 404 })\nconsole.log(result) // {a: 'value',  b: 404 }\n```\n\n```js\nconst deepmerge = require('@fastify/deepmerge')({ all: true })\nconst result = deepmerge({a: 'value'}, { b: 404 }, { a: 404 })\nconsole.log(result) // {a: 404,  b: 404 }\n```\n\nExample with `onlyDefinedProperties` option:\n\n```js\nconst deepmerge = require('@fastify/deepmerge')({ onlyDefinedProperties: true })\nconst result = deepmerge({ a: 1, b: null }, { a: undefined, b: undefined })\nconsole.log(result) // { a: 1, b: null }\n```\n\n#### mergeArray\n\nThe default mode to merge Arrays is to concat the source-Array to the target-Array.\n\n```js\nconst target = [1, 2, 3]\nconst source = [4, 5, 6]\nconst deepmerge = require('@fastify/deepmerge')()\nconst result = deepmerge(target, source)\nconsole.log(result) // [1, 2, 3, 4, 5, 6]\n```\n\nTo overwrite the default behavior regarding merging Arrays, you can provide a function to the\n`mergeArray` option of the deepmerge-function. The function provided to `mergeArray`\ngets an options-parameter passed, which is an Object containing the following keys and values.\n\n```typescript\nclone: (value: any) => any;\nisMergeableObject: (value: any) => any;\ndeepmerge: DeepMergeFn;\ngetKeys: (value: object) => string[];\n```\n\nThe `mergeArray`-Function needs to return the actual Array merging function, which accepts two parameters of type\nArray, and returns a value.\n\nExample 1: Replace the target-Array with a clone of the source-Array.\n\n```js\nfunction replaceByClonedSource(options) {\n  const clone = options.clone\n  return function (target, source) {\n    return clone(source)\n  }\n}\n\nconst deepmerge = require('@fastify/deepmerge')({ mergeArray: replaceByClonedSource })\nconst result = deepmerge([1, 2, 3], [4, 5, 6])\nconsole.log(result) // [4, 5, 6]\n```\n\nExample 2: Merge each element of the source-Array with the element at the same index-position of the target-Array.\n\n```js\nfunction deepmergeArray(options) {\n  const deepmerge = options.deepmerge\n  const clone = options.clone\n  return function (target, source) {\n    let i = 0\n    const tl = target.length\n    const sl = source.length\n    const il = Math.max(target.length, source.length)\n    const result = new Array(il)\n    for (i = 0; i < il; ++i) {\n      if (i < sl) {\n        result[i] = deepmerge(target[i], source[i])\n      } else {\n        result[i] = clone(target[i])\n      }\n    }\n    return result\n  }\n}\n\n// default behavior\nconst deepmergeConcatArray = require('@fastify/deepmerge')()\nconst resultConcatArray = deepmergeConcatArray([{ a: [1, 2, 3 ]}], [{b: [4, 5, 6]}])\nconsole.log(resultConcatArray) // [ { a: [ 1, 2, 3 ]}, { b: [ 4, 5, 6 ] } ]\n\n// modified behavior\nconst deepmergeDeepmergeArray = require('@fastify/deepmerge')({ mergeArray: deepmergeArray })\nconst resultDeepmergedArray = deepmergeDeepmergeArray([{ a: [1, 2, 3 ]}], [{b: [4, 5, 6]}])\nconsole.log(resultDeepmergedArray) // [ { a: [ 1, 2, 3 ], b: [ 4, 5, 6 ] } ]\n```\n\n#### cloneProtoObject\n\nMerging objects with prototypes, such as Streams or Buffers, are not supported by default.\nYou can provide a custom function to let this module deal with the object that has a `prototype` _(JSON object excluded)_.\n\n```js\nfunction cloneByReference (source) {\n  return source\n}\n\nconst deepmergeByReference = require('@fastify/deepmerge')({\n  cloneProtoObject: cloneByReference\n})\n\nconst result = deepmergeByReference({}, { stream: process.stdout })\nconsole.log(result) // { stream: <ref *1> WriteStream }\n```\n\n#### isMergeableObject\n\nBy default, `@fastify/deepmerge` merges all objects except native `Date` and `RegExp` objects. To exclude certain objects from being merged, you can provide a custom function to the `isMergeableObject` option.\n\nThe default function is exported by this module as `isMergeableObject`.\n\nThe following example shows how to extend the default function to exclude globally defined `FormData` objects from being identified as mergeable objects.\n\n```js\nconst { isMergeableObject: defaultIsMergeableObject } = require('@fastify/deepmerge')\n\n\nfunction customIsMergeableObject (source) {\n  return defaultIsMergeableObject(source) && !(source instanceof FormData)\n}\n\nconst deepmergeWithCustomMergeableObject = require('@fastify/deepmerge')({\n  isMergeableObject: customIsMergeableObject\n})\n```\n\n## Benchmarks\n\nThe benchmarks are available in the benchmark folder.\n\n`npm run bench` - benchmark various use cases of deepmerge:\n```\n@fastify/deepmerge: merge regex with date x 1,256,523,040 ops/sec ±0.16% (92 runs sampled)\n@fastify/deepmerge: merge object with a primitive x 1,256,082,915 ops/sec ±0.25% (97 runs sampled)\n@fastify/deepmerge: merge two arrays containing strings x 25,392,605 ops/sec ±0.22% (97 runs sampled)\n@fastify/deepmerge: two merge arrays containing objects x 1,655,426 ops/sec ±0.65% (96 runs sampled)\n@fastify/deepmerge: merge two flat objects x 15,571,029 ops/sec ±0.45% (96 runs sampled)\n@fastify/deepmerge: merge nested objects x 7,601,328 ops/sec ±0.31% (96 runs sampled)\n```\n\n`npm run bench:compare` - comparison of @fastify/deepmerge with other popular deepmerge implementation:\n```\n@fastify/deepmerge x 605,343 ops/sec ±0.87% (96 runs sampled)\ndeepmerge x 20,312 ops/sec ±1.06% (92 runs sampled)\nmerge-deep x 83,167 ops/sec ±1.30% (94 runs sampled)\nts-deepmerge x 175,977 ops/sec ±0.57% (96 runs sampled)\ndeepmerge-ts x 174,973 ops/sec ±0.44% (93 runs sampled)\nlodash.merge x 89,213 ops/sec ±0.70% (98 runs sampled)\n```\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","publish_time":1768948365791,"_cnpm_publish_time":1768948365791,"dist-tags":{"latest":"3.2.4"}}