memoize-one
Advanced tools
Comparing version 0.0.1-beta to 0.0.2-beta
{ | ||
"name": "memoize-one", | ||
"version": "0.0.1-beta", | ||
"version": "0.0.2-beta", | ||
"description": "A memoization library for memoizing a function with a cache size of one", | ||
@@ -22,2 +22,3 @@ "main": "lib/index.js", | ||
"chai": "3.5.0", | ||
"codecov": "1.0.1", | ||
"eslint": "3.15.0", | ||
@@ -28,2 +29,3 @@ "eslint-plugin-flowtype": "2.30.0", | ||
"mocha": "3.2.0", | ||
"nyc": "10.1.2", | ||
"rimraf": "2.5.4", | ||
@@ -30,0 +32,0 @@ "sinon": "1.17.7" |
@@ -1,3 +0,69 @@ | ||
# memoize-one | ||
# memoizeOne | ||
## TODO: docs | ||
A simple memoization library which only remembers the latest arguments | ||
[![Build Status](https://travis-ci.org/alexreardon/memoize-one.svg?branch=master)](https://travis-ci.org/alexreardon/memoize-one) | ||
[![codecov](https://codecov.io/gh/alexreardon/memoize-one/branch/master/graph/badge.svg)](https://codecov.io/gh/alexreardon/memoize-one) | ||
## DOCS: Work in progress | ||
## Rationale | ||
Cache invalidation is hard: | ||
> There are only two hard things in Computer Science: cache invalidation and naming things. | ||
> | ||
> - Phil Karlton | ||
So keep things simple and just use a cache size of one. | ||
Unlike other memoization libraries, `memoizeOne` only remembers the latest argument - a cache size of one. No need to worry about `maxAge`, `maxSize`, `exlusions` and so on. It simply remembers the last arguments, and if the function next called with the same arguments then it returns the previously computed result. | ||
## Usage | ||
```js | ||
import memoizeOne from 'memoize-one'; | ||
const add = (a, b) => a + b; | ||
const memoizedAdd = memoizeOne(add); | ||
memoizedAdd(1, 2); // 3 | ||
memoizedOne(1, 2); // 3 | ||
// Add function is not executed: previous result is returned | ||
memoizedAdd(2, 3); // 5 | ||
// Add function is called to get new value | ||
memoizedOne(2, 3); // 5 | ||
// Add function is not executed: previous result is returned | ||
memoizedOne(1, 2); // 3 | ||
// Add function is called to get new value. | ||
// While this was previously cached, | ||
// it is not the latest so the cached result is lost | ||
``` | ||
## Installation | ||
```bash | ||
# yarn | ||
yarn add memoize-one | ||
# npm | ||
npm install memoize-one --save | ||
``` | ||
## Other features | ||
### Correctly supports `this` binding | ||
### Custom equality function | ||
### Code health | ||
- Tested with all JavaScript *types* | ||
- 100% code coverage | ||
- flow types for safer internal execution and type checking / auto complete for editors | ||
- Semantically versioned |
// @flow | ||
type EqualityFn = (a: any, b: any) => boolean; | ||
const simpleEquality = (a: any, b: any): boolean => a === b; | ||
const simpleIsEqual = (a: any, b: any): boolean => a === b; | ||
export default function (resultFn: Function, equalityCheck?: EqualityFn = simpleEquality) { | ||
export default function (resultFn: Function, isEqual?: EqualityFn = simpleIsEqual) { | ||
let lastArgs: Array<any> = []; | ||
@@ -13,3 +13,3 @@ let lastResult: any; | ||
if (calledOnce && newArgs.length === lastArgs.length && | ||
lastArgs.every((lastArg, i) => equalityCheck(lastArg, newArgs[i]))) { | ||
lastArgs.every((lastArg, i) => isEqual(lastArg, newArgs[i]))) { | ||
return lastResult; | ||
@@ -16,0 +16,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
124789
12
69
16