{"_id":"@baethon/pure-mutation","_rev":"2-adf00693c7a6ea33c81ecb01504912bb","name":"@baethon/pure-mutation","dist-tags":{"latest":"1.1.0"},"versions":{"1.0.0":{"name":"@baethon/pure-mutation","version":"1.0.0","description":"Mutate object using pure functions","main":"index.js","repository":{"type":"git","url":"git+https://github.com/baethon/pure-mutation.js.git"},"author":{"name":"Radoslaw Mejer","email":"radmen@baethon.com"},"license":"MIT","scripts":{"test":"mocha *.test.js && tsd","lint":"standard","lint:fix":"standard --fix"},"devDependencies":{"chai":"^4.2.0","husky":"^3.1.0","mocha":"^6.2.2","sinon":"^7.5.0","sinon-chai":"^3.3.0","standard":"^14.3.1","tsd":"^0.11.0"},"dependencies":{"lodash.difference":"^4.5.0"},"husky":{"hooks":{"pre-push":"npm run lint"}},"tsd":{"compilerOptions":{"esModuleInterop":true}},"gitHead":"db23ed33f5b7ee0ff736d9e298be0db35517d59f","bugs":{"url":"https://github.com/baethon/pure-mutation.js/issues"},"homepage":"https://github.com/baethon/pure-mutation.js#readme","_id":"@baethon/pure-mutation@1.0.0","_nodeVersion":"13.1.0","_npmVersion":"6.12.1","dist":{"integrity":"sha512-8vqx4TquGRBjAcMpuR5P1jMtLl+xg6L6Liiz7ndfU7IL/ZrnxHRCY7JActxogrHYWCzcvpNfMtro1u2pAwTzeQ==","shasum":"7c510e35d81f677752a8caa68a2b725abe119d8f","tarball":"https://registry.npmjs.org/@baethon/pure-mutation/-/pure-mutation-1.0.0.tgz","fileCount":5,"unpackedSize":5389,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJd+U90CRA9TVsSAnZWagAAsxgQAJkN6fDYlvyPGMqA7y05\nVo0pDgksjcvcu8dCw75W8L/PYmm5O1oX0Evwr9UMriyK3/kaVotudvN5Y3Fl\nU51reXqZNVqZ0CSzAsO17DE50BJ8jBU3FwQidMJPEOMTuXAL0Gq4qRK3FioW\norWrFKvHoc40TcxRgOdX0la5R16sTIxp6R49b7AsWkEERbEW3O0Er/RSMOUC\n549O8lzHOm9aF4AM2SSgGVDbDuVO0anOqbLqDs/zqYTx6V8j0GpxTlBAsxgw\nfggnyE3OnT+rjymqvcBFnKpV2kgbPrvHZ9U+kaReImq6Ft9NdiUOvH9+601Q\n464ZmUA3bM+yHc7AJuHYGDGo8tEq7KTnGMawlU5abTVLF0pcWzZQoPsJ45VL\nWhW4RgfZUO2jVKtFoc/CJwOhvTbkSO3Dnw8qMijJdT4DwVIW7zcznnYRn1zn\nYG7isarRz/qKCSoMZpK8hG6jOD3nNUnUqNsDEniT0/cs9gPyjt9LrI6t0Yk+\nZfNhKtbXUY0lpnG7QpfOOU3tHgL6TX4rN9ghu2JfibVmzJwDUQ8WHo02k9Xx\nFcNAAEO3XJjj7WP6SrrirCVLclDsCFsnpHwIL85qgaR3P0jjoVw1GN1OB9Pu\nqk6uT+5uF83o3i6V4nsR7GgBru1NKrd9S4jSBu+l5NyAS7SUzhjUIoj1Ctze\nJJSU\r\n=tCw0\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBbkAU5xgLytPsGfbhs0tFNduGm6REbs8XOaaD6JcJipAiEA3/ri2M/Ek5IpMX0ah7CxcO0cDVamPhioZlqera4QRIA="}]},"maintainers":[{"name":"baethon_owner","email":"radmen+npmjs.baethon@gmail.com"},{"name":"radmen","email":"radmen+npmjs@gmail.com"}],"_npmUser":{"name":"radmen","email":"radmen+npmjs@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/pure-mutation_1.0.0_1576619892390_0.21354498872745142"},"_hasShrinkwrap":false},"1.1.0":{"name":"@baethon/pure-mutation","version":"1.1.0","description":"Mutate object using pure functions","main":"index.js","repository":{"type":"git","url":"https://github.com/baethon/pure-mutation.js"},"author":{"name":"Radoslaw Mejer","email":"radmen@baethon.com"},"license":"MIT","scripts":{"test":"mocha *.test.js && tsd","lint":"standard","lint:fix":"standard --fix"},"devDependencies":{"chai":"^4.2.0","husky":"^3.1.0","lint-staged":"^9.5.0","mocha":"^6.2.2","sinon":"^7.5.0","sinon-chai":"^3.3.0","standard":"^14.3.1","tsd":"^0.11.0"},"dependencies":{"lodash.difference":"^4.5.0"},"husky":{"hooks":{"pre-commit":"lint-staged"}},"tsd":{"compilerOptions":{"esModuleInterop":true}},"lint-staged":{"*.js":["npm run lint:fix"]},"licenseText":"MIT License\n\nCopyright (c) 2019 Baethon\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 all\ncopies 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 THE\nSOFTWARE.\n","_id":"@baethon/pure-mutation@1.1.0","dist":{"shasum":"ec8b6a09cb96b5a2ed8aa5fc20644dd9554c8ab8","integrity":"sha512-/jP0HLT9JHChkDoSSSHojCl2K1EHeBiGW1j6ZtMXm9gmwr5tGXc+dAguuXZMXRDwLS/JTddGap7iX5lDhYwdOA==","tarball":"https://registry.npmjs.org/@baethon/pure-mutation/-/pure-mutation-1.1.0.tgz","fileCount":6,"unpackedSize":7089,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJeI0R5CRA9TVsSAnZWagAA8d8P/2QjQRcNrCcGG4C9jlQa\nHJU5FGO0EYh1DPn0uGhxiL4zMS5+VK5mjYvErVDv0cdla2r+Pm7HswdMji0z\nscydA77fJkQ0x3YXpxXshCQ2lHGByBklGoLiVwk+S5z+N6Ju2gbWah7zawCZ\nXTnVCOCKavPiaXATa7b2ud9ui+0EnIXy15+JE9PFK2EzqXys4Rdr3Fms15Cp\nL367ew8C7X9Twj5Jnb2ZKPa9/qzo6Vykn5KxPBGUx4QMHqrjh04D0QROuthh\n2QaReZ4FSEWuxjShFi5joy7Eg3JwdQjFR9F7Pf0CjF0IldJ7NjJZ9R+dw7MD\nHkKQp8UD6PfczIkJXAWCqxjIFrdw4oJ1h7Vwhps6V5eJcCFagvTc5p+ENHu3\n9s6Ulz/Se910o9yDAxRuUgBcAU7cZhuHHK/u64ExevjyDJWI+ZYWHE2jMzN1\nIIRWsjRhIOUvRqMHJgGs7CmJvTHbk/HbEV501rRYVQAhYn7hxNYjcU+FAZib\nSsmt2X30W63WRXorR/OXMR0G5cHJMyALtvsZ0nP+xU/HyWy2Wqm1AsLiSFg0\n0v655KGhn10HRw5zi9eWM0KomhT4G7uDSl6RNEVYeGjaD2R6u0mp73VXnNmi\nCX8s3xSeKOQWbcb/c2UF2URReCSf5z3Na5yiuSzA52lXOjFFhV14KHg+eHfS\nxN1d\r\n=DLoQ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDWdbxy+9pEvYmZqiAmlfa4M/9p7KBfjB0oYQvrnqg4bAIgGYWRVVdt+pDgRoQ0NlT35+WvRrarM5NSWcrJ4oGPL18="}]},"maintainers":[{"name":"baethon_owner","email":"radmen+npmjs.baethon@gmail.com"},{"name":"radmen","email":"radmen+npmjs@gmail.com"}],"_npmUser":{"name":"radmen","email":"radmen+npmjs@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/pure-mutation_1.1.0_1579369592827_0.5751845544911076"},"_hasShrinkwrap":false}},"time":{"created":"2019-12-17T21:58:12.252Z","1.0.0":"2019-12-17T21:58:12.530Z","modified":"2022-04-04T17:30:11.666Z","1.1.0":"2020-01-18T17:46:32.960Z"},"maintainers":[{"name":"baethon_owner","email":"radmen+npmjs.baethon@gmail.com"},{"name":"radmen","email":"radmen+npmjs@gmail.com"}],"description":"Mutate object using pure functions","repository":{"type":"git","url":"https://github.com/baethon/pure-mutation.js"},"author":{"name":"Radoslaw Mejer","email":"radmen@baethon.com"},"license":"MIT","readme":"## @baethon/pure-mutation\n\nUse pure functions to mutate an object.\n\n## Installation\n\n```\nyarn add @baethon/pure-mutation\n```\n\n## Example usage\n\n```js\nconst { mutate } = require('@baethon/pure-mutation')\nconst R = require('ramda')\n\nconst incrementAge = R.evolve({    \n  age: R.inc\n})\n\nconst user = {    \n  name: 'Jon',    \n  age: 22\n}\n\nmutate(user, incrementAge)\nconsole.log(user)\n```\n\n## But... why?\n\nI feel like making a non-pure function from a pure function is a step backward (which probably is). There's a reason for this.\n\nSome libraries require that a given callback mutates the input data. Now, imagine that you'd like to use one of your sweet pure functions. It's not possible! You need to either make them dirty or write a second implementation which will be non-pure. Absolutely barbaric.\n\n`@baethon/pure-mutation` is an attempt to solve this type of problem. You can create your pure function and, when required, use it as a non-pure mutating function.\n\n### Example use case\n\n[Mongoose middleware](https://mongoosejs.com/docs/middleware.html) can be used to ie. modify the data which will be returned from the database. To do this, one needs to add a _pre init_ hook. Unfortunately, this hook has to mutate the input object.\n\n```js\nschema.pre('init', (doc) => {    \n  delete doc.password\n})\n```\n\nIf a hook returns some data it won't be considered.\n\n```js\nschema.pre('init', R.omit(['password']))\n```\n\nThe solution? Make it unpure!\n\n```js\nconst { unpure } = require('@baethon/pure-mutation')\nschema.pre('init', unpure(R.omit(['password'])))\n```\n\n## How it works\n\nBoth `mutate` and `unpure` methods will create a wrapper around the pure function. When called, it will pass a shallow copy to the wrapped function. The returned value will be mixed-in back to the input object.\n\nThe wrapper makes a diff of the keys. If the returned value has some keys missing they will be removed from the input object.\n\n## Custom mutators\n\nIt is possible to define custom mutators for selected operations:\n\n- `assign` to assign new values to the input object\n\n- `exclude` to remove the properties that are no longer present in the result of the applied function\n\nThis can be used to set values in a non-plain objects (eg. Mongoose models).\n\n```js\nconst { mutate } = require('@baethon/pure-mutation')\nconst User = require('./model/user')\n\nconst pluckName = ({ name }) => ({ name })\nconst user = new User({ name: 'Jon', lastname: 'Snow' })\n\nmutate(user, pluckName, {\n    assign: (inputRef, values) => inputRef.set(values),\n    exclude: (inputRef, keys) => keys.forEach(name => {\n        inputRef.set(name, undefined)\n    })\n})\n```\n\n## Testing\n\n```\nyarn test\nyarn lint\n```\n","readmeFilename":"README.md"}