{"name":"@fastify/deepmerge","version":"1.1.0","keywords":["merge","deep","recursive","object"],"author":"","license":"MIT","_id":"@fastify/deepmerge@1.1.0","homepage":"https://github.com/fastify/deepmerge#readme","bugs":{"url":"https://github.com/fastify/deepmerge/issues"},"dist":{"shasum":"91f0a5a27034ff76b7bece63a5906894940ace82","tarball":"http://registry-npm.ywwl.com/@fastify/deepmerge/download/@fastify/deepmerge-1.1.0.tgz","fileCount":5,"integrity":"sha512-E8Hfdvs1bG6u0N4vN5Nty6JONUfTdOciyD5rn8KnEsLKIenvOVcr210BQR9t34PRkNyjqnMLGk3e0BsaxRdL+g==","signatures":[{"sig":"MEUCIQDlu79tzRsKj4RqOJqtRsaS8hX+7D7x3O3CmjP86IiHyAIgfaQ8xXt6mTyEbZHnRR+CeRtMT3bF7tj5g6fjb3F/mW4=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":13015,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJixsJIACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqW2g//VGdR8pgAqMXSVX4CtRzRuCe87jl3X9GF7hZ0p1XUVPJAJJlE\r\nKEsr0m4CtbavB7Sb7ZKdoOyPeW4sxoPTzXNcOb5Jogz73C/7WGSD3mjcZCOE\r\n1F6z6L12/XiC2Y4BiVaQCApMQVetWZ+qRVrINXe+zoFbcZQk+NdzSJGs/jnM\r\ntfz+rZhTsXg50eT/lawZ6oT7T5wkOW9ehJgUkQGC82G9/Ku1fz3QSMTBjVjt\r\n9MzruxQzm9RpKxQn+0/U6FvumKeF7zi5KLD9pLviY9vElu8onkVQ+rc4fHrJ\r\n71WeSB0zykUEp40ZRhPwT+EC6lZuYqSD0+Y1KrpwSMWSl3XXNRhZrARjkTkV\r\nKpamfPNuIQJXeKmVUP77yyJQKiPEDjUUMdX+kHwT//aJZfV1oC8MbEozUKAO\r\ncbnRc0KDI/eBJsihJ7XRLWDZ7s3OdaTV39ZI5v4WT1bjnFDJ/LoVR4UCWMAB\r\nyJRY9RUnNKipo9gOlhNMnkYvRZu9eWkZS8JqFVr7ddJIfiSnOzwjF0v5pHto\r\nfWPct2MSW8QEvWPs9zY0Gyx3dLG51TiH25AqJOJf6x+eYWmBJAGLGR3V3WyN\r\nBXexPxPyViicGMp6seaA4oUPnSsJkIJ/QDYe9R7OvbHOhvs8TMuGmp6SxA5c\r\neDkjLMOOKp9PnbsvxoEJ/f+N3GDkVVgxw2c=\r\n=BcYR\r\n-----END PGP SIGNATURE-----\r\n","size":4410,"noattachment":false,"key":"/@fastify/deepmerge/-/@fastify/deepmerge-1.1.0.tgz"},"main":"index.js","types":"types/index.d.ts","gitHead":"2066da1c3b076235a313f2b6c7b30348371d6441","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":"matteo.collina","email":"hello@matteocollina.com"},"repository":{"url":"git+https://github.com/fastify/deepmerge.git","type":"git"},"_npmVersion":"8.11.0","description":"Merges the enumerable properties of two or more objects deeply.","directories":{},"_nodeVersion":"16.15.1","_hasShrinkwrap":false,"devDependencies":{"tap":"^16.3.0","tsd":"^0.22.0","standard":"^17.0.0"},"_npmOperationalInternal":{"tmp":"tmp/deepmerge_1.1.0_1657193031990_0.12621056894400495","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":1657193032115,"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":1657193032115,"dist-tags":{"latest":"3.2.4"}}