
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
@baethon/pure-mutation
Advanced tools
Use pure functions to mutate an object.
yarn add @baethon/pure-mutation
const { mutate } = require('@baethon/pure-mutation')
const R = require('ramda')
const incrementAge = R.evolve({
age: R.inc
})
const user = {
name: 'Jon',
age: 22
}
mutate(user, incrementAge)
console.log(user)
I feel like making a non-pure function from a pure function is a step backward (which probably is). There's a reason for this.
Some 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.
@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.
Mongoose middleware 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.
schema.pre('init', (doc) => {
delete doc.password
})
If a hook returns some data it won't be considered.
schema.pre('init', R.omit(['password']))
The solution? Make it unpure!
const { unpure } = require('@baethon/pure-mutation')
schema.pre('init', unpure(R.omit(['password'])))
Both 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.
The wrapper makes a diff of the keys. If the returned value has some keys missing they will be removed from the input object.
It is possible to define custom mutators for selected operations:
assign
to assign new values to the input object
exclude
to remove the properties that are no longer present in the result of the applied function
This can be used to set values in a non-plain objects (eg. Mongoose models).
const { mutate } = require('@baethon/pure-mutation')
const User = require('./model/user')
const pluckName = ({ name }) => ({ name })
const user = new User({ name: 'Jon', lastname: 'Snow' })
mutate(user, pluckName, {
assign: (inputRef, values) => inputRef.set(values),
exclude: (inputRef, keys) => keys.forEach(name => {
inputRef.set(name, undefined)
})
})
yarn test
yarn lint
FAQs
Mutate object using pure functions
We found that @baethon/pure-mutation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.