Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

memoize-one

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memoize-one - npm Package Compare versions

Comparing version 2.0.1 to 3.0.0

5

lib/index.js

@@ -19,4 +19,3 @@ "use strict";

// breaking cache when context (this) or arguments change
return function () {
var result = function result() {
for (var _len = arguments.length, newArgs = Array(_len), _key = 0; _key < _len; _key++) {

@@ -36,2 +35,4 @@ newArgs[_key] = arguments[_key];

};
return result;
};

@@ -38,0 +39,0 @@

4

package.json
{
"name": "memoize-one",
"version": "2.0.1",
"version": "3.0.0",
"description": "A memoization library which only remembers the latest invocation",

@@ -24,3 +24,3 @@ "main": "lib/index.js",

"eslint-plugin-flowtype": "2.32.1",
"flow-bin": "0.45.0",
"flow-bin": "0.46.0",
"flow-copy-source": "1.1.0",

@@ -27,0 +27,0 @@ "mocha": "3.3.0",

// @flow
type EqualityFn = (a: any, b: any) => boolean;
type EqualityFn = (a: mixed, b: mixed) => boolean;
const simpleIsEqual = (a: any, b: any): boolean => a === b;
const simpleIsEqual: EqualityFn = (a: mixed, b: mixed): boolean => a === b;
export default function (resultFn: Function, isEqual?: EqualityFn = simpleIsEqual) {
let lastThis: any;
let lastArgs: Array<any> = [];
let lastResult: any;
// <ResultFn: (...Array<any>) => mixed>
// The purpose of this typing is to ensure that the returned memoized
// function has the same type as the provided function (`resultFn`).
// ResultFn: Generic type (which is the same as the resultFn).
// (...Array<any>): Accepts any length of arguments - and they are not checked
// mixed: The result can be anything but needs to be checked before usage
export default function <ResultFn: (...Array<any>) => mixed>(resultFn: ResultFn, isEqual?: EqualityFn = simpleIsEqual): ResultFn {
let lastThis: mixed;
let lastArgs: Array<mixed> = [];
let lastResult: mixed;
let calledOnce: boolean = false;
const isNewArgEqualToLast = (newArg, index) => isEqual(newArg, lastArgs[index]);
const isNewArgEqualToLast = (newArg: mixed, index: number): boolean => isEqual(newArg, lastArgs[index]);
// breaking cache when context (this) or arguments change
return function (...newArgs: Array<any>) {
// breaking cache when context (this) or arguments change
const result = function (...newArgs: Array<mixed>) {
if (calledOnce &&
lastThis === this &&
newArgs.length === lastArgs.length &&
newArgs.every(isNewArgEqualToLast)) {
lastThis === this &&
newArgs.length === lastArgs.length &&
newArgs.every(isNewArgEqualToLast)) {
return lastResult;

@@ -29,2 +35,5 @@ }

};
}
// telling flow to ignore the type of `result` as we know it is `ResultFn`
return (result: any);
}

@@ -485,3 +485,21 @@ // @flow

});
describe('flow typing', () => {
it('should maintain the type of the original function', () => {
// this test will create a flow error if the typing is incorrect
type SubtractFn = (a: number, b: number) => number;
const subtract: SubtractFn = (a: number, b: number): number => a - b;
const requiresASubtractFn = (fn: SubtractFn): number => fn(2, 1);
const memoizedSubtract: SubtractFn = memoizeOne(subtract);
// will cause a flow error if `fn` is not of type `SubtractFn`
const result1 = requiresASubtractFn(memoizedSubtract);
const result2 = requiresASubtractFn(memoizeOne(subtract));
expect(result1).to.equal(1);
expect(result2).to.equal(1);
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc