npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@baethon/pure-mutation

v1.1.0

Published

Mutate object using pure functions

Downloads

1

Readme

@baethon/pure-mutation

Use pure functions to mutate an object.

Installation

yarn add @baethon/pure-mutation

Example usage

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)

But... why?

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.

Example use case

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'])))

How it works

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.

Custom mutators

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)
    })
})

Testing

yarn test
yarn lint