memoize-functions

1.0.0 • Public • Published

memoize-functions

npm version dependency status build status

Create a new object replacing functions with memoized versions

Usage

import memoizeFunctions from 'memoize-functions'
 
let obj = {
  info: () =>
    ({ output: 'I beg your pardon' }),
  log: (message = 'Howdy o/') =>
    ({ output: message }),
  warn: ({ message = 'Hey!' }) =>
    ({ output: message }),
  text:
    ({ output: 'String' }),
}
 
let newObj = memoizeFunctions(obj)
 
newObj.log() === newObj.log()
newObj.warn() === newObj.warn()
newObj.info({ message: 'Yo!' }) === newObj.info({ message: 'Yo!' })
newObj.text === newObj.text

Then attributes info, log and warn are replaced by memoized versions (it supports destructured parameters). Attribute text is kept the same, only functions are memoized.

You can also choose which functions should be memoized:

newObj = memoizeFunctions(obj, 'log', 'warn')
 
newObj.log() === newObj.log()
newObj.warn() === newObj.warn()
newObj.info({ message: 'Yo!' }) !== newObj.info({ message: 'Yo!' })
newObj.text === newObj.text

The attribute log and warn are memoized but info is kept the same.

Contributing

First of all, thank you for wanting to help!

  1. Fork it.
  2. Create a feature branch - git checkout -b more_magic
  3. Add tests and make your changes
  4. Check if tests are ok - npm test
  5. Commit changes - git commit -am "Added more magic"
  6. Push to Github - git push origin more_magic
  7. Send a pull request! ❤️ 💖 ❤️

Package Sidebar

Install

npm i memoize-functions

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • aitherios