From 9f86b198f993c0e2469fb8cf151ce5baf09ddadd Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Thu, 21 Apr 2016 14:50:01 +0200 Subject: [PATCH 1/6] Fix resolvers bug. Fixes #12 --- lib/configure-map.js | 2 +- test/lib/configure-map.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/configure-map.js b/lib/configure-map.js index 522910a..0b2853d 100644 --- a/lib/configure-map.js +++ b/lib/configure-map.js @@ -39,7 +39,7 @@ module.exports = function (original, length, options) { return cache[id]; } } - if (args.length === 1) result = call.call(original, this, arg); + if (args.length === 1) result = call.call(original, this, args[0]); else result = apply.call(original, this, args); if (id === null) { id = get(args); diff --git a/test/lib/configure-map.js b/test/lib/configure-map.js index 2d0fa08..e13e61c 100644 --- a/test/lib/configure-map.js +++ b/test/lib/configure-map.js @@ -54,6 +54,10 @@ module.exports = function () { a(fn(), r, "Third"); a(i, 1, "Called once"); }, + "One arg": function (a) { + var fn = memoize(function (elo) { ++i; return arguments; }, { resolvers: [Boolean] }); + a.deep(aFrom(r = fn('elo')), [true], "First"); + }, "Some Args": function (a) { var x = {}; i = 0; From c007aa3fa59b4bfea3a4a5bd7f0f2a882345558b Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Thu, 21 Apr 2016 15:31:08 +0200 Subject: [PATCH 2/6] v0.3.10 --- CHANGES | 5 +++++ package.json | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index cb05bb3..d932bb0 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +v0.3.10 -- 2016.04.21 +* Fix resolvers bug (#12) +* Fix arguments support for weak mode +* Update repository address after rename + v0.3.9 -- 2015.08.04 * Update dependencies * Improve documentation diff --git a/package.json b/package.json index 4980891..14cacdb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "memoizee", - "version": "0.3.9", + "version": "0.3.10", "description": "Memoize/cache function results", "author": "Mariusz Nowak (http://www.medikoo.com/)", "keywords": [ @@ -26,15 +26,15 @@ }, "dependencies": { "d": "~0.1.1", - "es5-ext": "~0.10.7", + "es5-ext": "~0.10.11", "es6-weak-map": "~0.1.4", - "event-emitter": "~0.3.3", + "event-emitter": "~0.3.4", "lru-queue": "0.1", "next-tick": "~0.2.2", "timers-ext": "0.1" }, "devDependencies": { - "tad": "~0.2.3", + "tad": "~0.2.4", "xlint": "~0.2.2", "xlint-jslint-medikoo": "~0.1.4" }, From a95a551ec6ddb3fdd22b0efa56764cbcfff2f005 Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Thu, 21 Apr 2016 15:31:50 +0200 Subject: [PATCH 3/6] Update Travis CI configuration --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9ec96e5..b3efd71 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ sudo: false # http://docs.travis-ci.com/user/workers/container-based-infrastructure/ language: node_js node_js: - - 0.10 - 0.12 - - iojs + - 4 + - 5 before_install: - mkdir node_modules; ln -s ../ node_modules/memoizee From 7810c1bfd3ad17650c04f942408badb6fd79f820 Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Thu, 21 Apr 2016 15:35:12 +0200 Subject: [PATCH 4/6] Fix Travis CI badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c5f9806..b3437c8 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,7 @@ Memoize statistics: * _%Cache_ – What's the percentage of cache hits (of all function calls) * _Source location_ – Where in the source code given memoization was initialized -### Tests [![Build Status](https://travis-ci.org/medikoo/memoize.svg)](https://travis-ci.org/medikoo/memoize) +### Tests [![Build Status](https://travis-ci.org/medikoo/memoizee.svg)](https://travis-ci.org/medikoo/memoizee) $ npm test From a835d4db785d1a766c7353d15edd5b0f4d598011 Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Thu, 21 Apr 2016 17:48:00 +0200 Subject: [PATCH 5/6] Try new Travis CI conf --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index b3efd71..9a5fce7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,6 @@ node_js: - 4 - 5 -before_install: - - mkdir node_modules; ln -s ../ node_modules/memoizee - notifications: email: - medikoo+memoize@medikoo.com From 75422fd9dc44144d046b53ea8b395d6c5f325c9d Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Mon, 4 Jul 2016 16:22:53 +0200 Subject: [PATCH 6/6] Document `normalzizer` option --- README.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b3437c8..aacd5bb 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,32 @@ memoized('/path/one'); memoized('/path/one'); // Cache hit ``` -#### Resolvers +#### Cache id resolution (normalization) + +By default cache id for given call is resolved either by: +- Direct Comparison of values passed in arguments as they are. In such case two different objects, even if their characteristics is exactly same (e.g. `var a = { foo: 'bar' }, b = { foo: 'bar' }`) will be treated as two different values. +- Comparison of stringified values of given arguments (`primitive` mode), which serves well, when arguments are expected to be primitive values, or objects that stringify naturally do unique values (e.g. arrays) + +Still above two methods do not serve all cases, e.g. if we want to memoize function where arguments are hash objects which we do not want to compare by instance but by its content. + +##### Writing custom cache id normalizers + +There's a `normalizer` option through which we can pass custom cache id normalization function +e.g. if we want to memoize a function where argument is a hash object which we do not want to compare by instance but by its content, then we can achieve it as following: + +```javascript +var mFn = memoize(function (hash) { + // body of memoized function +}, { normalizer: function (args) { + // args is arguments object as accessible in memoized function + return JSON.stringify(args[0]); +} }); + +mfn({ foo: 'bar' }); +mfn({ foo: 'bar' }); // Cache hit +``` + +#### Argument resolvers When we're expecting arguments of certain type it's good to coerce them before doing memoization. We can do that by passing additional resolvers array: