0.0.2 • Published 7 years ago

headscratch.js v0.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

Headscratch.js

Headscratch.js on NPM

Memoize a function, making it think about the arguments.

Install

npm i headscratch.js --save

Use

import headscratch from 'headscratch.js'

// define the function to memoize
const add1 = num => num + 1

// memoize it
const memoizedAdd1 = headscratch(add1)

// call the memoized version, passing a single argument or n arguments
memoizedAdd1(1)        // LOG: 2
memoizedAdd1(1, 2)     // LOG: 2

Options

Headscratch support an options object, passed as the second parameter. Defaults shown below:

const memoized = headscratch(func, {
  cache: {},
  serialize: JSON.stringify
})

cache

A cache object, which is used instead of an empty one.

// define and populate cache
const cache = {
  // ...
}

// pass it to headscratch
const memoized = headscratch(func, {
  cache
})

serialize

A function to serialize arguments, which is used to create the cache key.

// define serializer
const serialize = args => args.toString()

// pass it to headscratch
const memoized = headscratch(func, {
  serialize
})

License

MIT. © 2016 Michael Cavalea