{"name":"@fastify/deepmerge","version":"1.3.0","keywords":["merge","deep","recursive","object"],"author":"","license":"MIT","_id":"@fastify/deepmerge@1.3.0","homepage":"https://github.com/fastify/deepmerge#readme","bugs":{"url":"https://github.com/fastify/deepmerge/issues"},"dist":{"shasum":"8116858108f0c7d9fd460d05a7d637a13fe3239a","tarball":"http://registry-npm.ywwl.com/@fastify/deepmerge/download/@fastify/deepmerge-1.3.0.tgz","fileCount":5,"integrity":"sha512-J8TOSBq3SoZbDhM9+R/u77hP93gz/rajSA+K2kGyijPpORPWUXHUpTaleoj+92As0S9uPRP7Oi8IqMf0u+ro6A==","signatures":[{"sig":"MEYCIQCJP0nQPF5GMZV0dHkW9NJsEnxinHVGZFovU/kWgeY0xAIhAOwLo+dcCLi7h75H86Qm5WsKSZXCwhl8O2r9nTg6Ah4N","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":14264,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjjTIQACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoCbRAAgR9VzCNoEh7SLGdZ2Y8cQqwG0t8Pv1q8RKswe2Vu7R2IHdaA\r\n1CsBhtiOCJ0gFzvg53184XR0d9CdeVwjyKAkd3zGCaYNxM4I6+mOIDbFuP7Y\r\n/PStx4MEX7Vh9dF5zL4gjeg+6OV7zuqs6MP9dP8DVNjwDZB46tBdB4Vbl/On\r\nBFolFk07S733TXKUpryA9N8hhuvJ99Ysr5BjUEGpCcgGR2nL3t7DE467casT\r\niYMgOGcXGLBcLjXWKRzV6ODckvqRSNoC3NMOJCzWw255J4jtjAHEESkIb9q+\r\nOuRabqRCMK1UxbL4M+ckfFPGrItrMNSdY/ewCgkoDlJ977i73Vpb7ds1vQG+\r\naMOFgA8lTdNDS5nJwocMAFdUTAfesAKiDuzSl+BnPG+EteBowhk3FH+icuzZ\r\n+QkzFmlOgDf+PdSIMrqhQZ9wmw8cyheqlaKSzLhq+AW2JrZ8eTJKLa4AvID9\r\nWVk58trmG69zDELpTVcjZYJmNerIRjZsjvOnEAtbf7dn+ZA3PBPni/Dx8dJq\r\n6MTyVfASOokwyKn3XmWPKBPDFGkooNEAn1lZcSAjHz/SKVRGeEFpP5Ymj/sx\r\nyyZnI2V9v1aLvWkzfHLpPWjgFBttK62eeqZpYdmi/LymSVul+3dBRS0r1ZTF\r\n1KXxIicJ1jQ0Ny1EaqEcZ43PzNx6LRRNjys=\r\n=oZ4G\r\n-----END PGP SIGNATURE-----\r\n","size":4733,"noattachment":false,"key":"/@fastify/deepmerge/-/@fastify/deepmerge-1.3.0.tgz"},"main":"index.js","types":"types/index.d.ts","gitHead":"2983866428f0654bc29f232d0a034a6f80d4c0cb","scripts":{"lint":"standard","test":"npm run test:unit && npm run test:typescript","lint:fix":"standard --fix","test:unit":"tap","test:typescript":"tsd"},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"repository":{"url":"git+https://github.com/fastify/deepmerge.git","type":"git"},"_npmVersion":"9.1.2","description":"Merges the enumerable properties of two or more objects deeply.","directories":{},"_nodeVersion":"16.17.0","_hasShrinkwrap":false,"devDependencies":{"tap":"^16.3.0","tsd":"^0.24.1","tape":"^5.6.1","standard":"^17.0.0"},"_npmOperationalInternal":{"tmp":"tmp/deepmerge_1.3.0_1670197776595_0.04233355448632903","host":"s3://npm-registry-packages"},"readme":"# @fastify/deepmerge\n\n![CI](https://github.com/fastify/deepmerge/workflows/CI/badge.svg)\n[![NPM version](https://img.shields.io/npm/v/@fastify/deepmerge.svg?style=flat)](https://www.npmjs.com/package/@fastify/deepmerge)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)\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```\ndeepmerge(options)\n```\n\n`options` is optional and can contain following values\n\n- `symbols` (`boolean`, optional) - should also merge object-keys which are symbols, default is false\n- `all` (`boolean`, optional) - merges all parameters, 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\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\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 behaviour 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 behaviour\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 behaviour\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## 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":1670197776764,"maintainers":[{"name":"gurgunday","email":"hey@gurgun.day"},{"name":"metcoder95","email":"me@metcoder.dev"},{"name":"galvez","email":"jonasgalvez@gmail.com"},{"name":"simenb","email":"sbekkhus91@gmail.com"},{"name":"coopflow","email":"community@coopflow.com"},{"name":"simoneb","email":"simone.busoli@gmail.com"},{"name":"rafaelgss","email":"rafael.nunu@hotmail.com"},{"name":"starptech","email":"deusdustin@gmail.com"},{"name":"delvedor","email":"tommydelved@gmail.com"},{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"allevo","email":"tomallevi@gmail.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"}],"_cnpm_publish_time":1670197776764,"dist-tags":{"latest":"3.2.4"}}