{"name":"@fastify/deepmerge","version":"1.2.0","keywords":["merge","deep","recursive","object"],"author":"","license":"MIT","_id":"@fastify/deepmerge@1.2.0","homepage":"https://github.com/fastify/deepmerge#readme","bugs":{"url":"https://github.com/fastify/deepmerge/issues"},"dist":{"shasum":"02e2c28b33799c0dca88cdf7831902f069e54faa","tarball":"http://registry-npm.ywwl.com/@fastify/deepmerge/download/@fastify/deepmerge-1.2.0.tgz","fileCount":5,"integrity":"sha512-pfBLfEykO5uqw5cIzGD24sPfbsLfjESIQSe8oqbcMpodUwO2swPQGvuhdD137+n/1WiSH24vq3z3th2ErFbxVg==","signatures":[{"sig":"MEQCIG4eClQJmxwrXthNMfI+TqJZwafplrVSxi0BEjPf1MglAiAOBWyYEWfln1hPz1af/+4+sO8JadAezz91z965AeYSvA==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":14158,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjg2IUACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmp72Q//XRFxyvaNHDG5r8aH87Ow1/RyuxgZ0wATx50zNvQKjEKKgWHQ\r\nhLfJmQN0NEjcB3cHYBNT8LXtpLUovdEweV5A2z3rkykTBorhZ2yaWE3d/+dV\r\nkM3dG9sesptwqAxSUZW4x17o+hLX6uX696AwLqGBFpl6nQdQHgzJcpfpH5I5\r\nTFiHetFW6dP5WxbddIy4uJ+Gv+BEODRrnKTZHwtbG8mj5kY6od71IevXhe5E\r\n6aI+uIFYLfgV2CYzv6THU0onafIH1Fa1Osv1a4G4wF9P30q/wBIg4HdGeMcX\r\nW27ZF+RH7kaRA0aT4uKoKvGKP2fp6tbUqGedCXqm8gYDa0zP3vUaorFeEUWi\r\nWoShThr8Hk5YuRTzmlKllut8+vBNyqjqXFaCsoVISk67lLyEO6O5nutnbqBt\r\n0LfIqUk1pOl5LUrOqZRKWqE4xXr3metx+8TqLr3AEc9cncI3/BakwcGxW3AV\r\nrA1WxXzU4RkS3UURuu3zpnqjJr3Fdlwm6pnpw+VOtXCO6Du66u5yUXbspADh\r\nWqht7HmYM5uFwTXi6Dzr41oMm23K4js6dPuVzi2IIo0btiHy05lwG8J7t0pQ\r\nEpBbkRg6ahocsoWFkU+ADM2knAfiLF1IkiHibfeNXkRb5Es0WDCwiocF7v+E\r\n2IL/BPTDG/y9XzhQ6EvVQAqLg81Zcj0I+no=\r\n=5HpX\r\n-----END PGP SIGNATURE-----\r\n","size":4713,"noattachment":false,"key":"/@fastify/deepmerge/-/@fastify/deepmerge-1.2.0.tgz"},"main":"index.js","types":"types/index.d.ts","gitHead":"d93badc64487beb6213cc73075b922f79053581b","scripts":{"lint":"standard","test":"npm run test:unit && npm run test:typescript","lint:fix":"standard --fix","test:unit":"tap -J test/*.test.js test/**/*.test.js","test:typescript":"tsd"},"_npmUser":{"name":"eomm","email":"behemoth89@gmail.com"},"repository":{"url":"git+https://github.com/fastify/deepmerge.git","type":"git"},"_npmVersion":"8.10.0","description":"Merges the enumerable properties of two or more objects deeply.","directories":{},"_nodeVersion":"16.13.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.2.0_1669554708568_0.08300424262977701","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":1669554708726,"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":1669554708726,"dist-tags":{"latest":"3.2.4"}}