Miss any of our Open RFC calls?Watch the recordings here! »

memoize-fn

1.0.2 • Public • Published

memoize-fn

Build status Test coverage NPM version NPM Downloads Prettier Conventional Commits

A memoization library that caches the result of the different arguments

Installation

npm install memoize-fn
# or use yarn 
yarn add memoize-fn

Usage

import memoizeFn from 'memoize-fn'
import { withCtx, robust } from 'memoize-fn'
 
let count = 1
const fn = memoizeFn(() => count++)
 
fn() // => 1
fn() // => 1
fn('new argument') // => 2
fn() // => 1
count // => 3

API

memoize

index.js:15-50

Memoize function that caches the result of the different arguments.

Parameters

  • fn {Function}
  • options Object {MemoizeOptions} (optional, default {})
    • options.once (optional, default false)
    • options.eq (optional, default (prevArgs,newArgs)=>shallowEqual(prevArgs,newArgs))
    • options.cache (optional, default new Map())
    • options.skipEqualThis (optional, default false)

Returns any memoizeFn {Function}

withCtx

index.js:59-78

Memoize function that caches the result of the different arguments and with context

Parameters

  • fn {Function}
  • opts {MemoizeOptions}

Returns CtxFunction

robust

index.js:87-102

Memoize function that caches the result of the different arguments and resets memoize function when catches error asynchronously.

Parameters

  • fn {Function}
  • opts {MemoizeOptions}

Returns CtxFunction

MemoizeOptions

index.js:87-102

Type: {}

Parameters

  • once {boolean} - Only cache once like memoize-one (optional, default false)
  • eq {(prevArgs, newArgs) => boolean} (optional, default shallowEqual)
  • cache {Map} (optional, default newMap())
  • skipEqualThis {boolean} (optional, default false)

CtxFunction

index.js:87-102

Type: Function

Parameters

  • reset {Function} - Resets cache
  • unCache {Function} - Disables cache

Contributing

  • Fork it!
  • Create your new branch:
    git checkout -b feature-new or git checkout -b fix-which-bug
  • Start your magic work now
  • Make sure npm test passes
  • Commit your changes:
    git commit -am 'feat: some description (close #123)' or git commit -am 'fix: some description (fix #123)'
  • Push to the branch: git push
  • Submit a pull request :)

Authors

This library is written and maintained by imcuttle, moyuyc95@gmail.com.

License

MIT - imcuttle 🐟

Install

npm i memoize-fn

DownloadsWeekly Downloads

31

Version

1.0.2

License

MIT

Unpacked Size

24 kB

Total Files

16

Last publish

Collaborators

  • avatar