mirror of
https://github.com/BreizhHardware/memoizee.git
synced 2026-01-18 16:37:21 +01:00
Provide no-args handling out of a box
This commit is contained in:
22
index.js
22
index.js
@@ -9,18 +9,18 @@ module.exports = function (fn/*, options*/) {
|
||||
|
||||
if (!options.normalizer) {
|
||||
length = options.length = resolveLength(options.length, fn.length, options.async);
|
||||
if (length === 0) {
|
||||
options.normalizer = require('./normalizers/0');
|
||||
} else if (options.primitive) {
|
||||
if (length === false) {
|
||||
options.normalizer = require('./normalizers/primitive');
|
||||
} else if (length > 1) {
|
||||
options.normalizer = require('./normalizers/get-primitive-fixed')(length);
|
||||
if (length !== 0) {
|
||||
if (options.primitive) {
|
||||
if (length === false) {
|
||||
options.normalizer = require('./normalizers/primitive');
|
||||
} else if (length > 1) {
|
||||
options.normalizer = require('./normalizers/get-primitive-fixed')(length);
|
||||
}
|
||||
} else {
|
||||
if (length === false) options.normalizer = require('./normalizers/get')();
|
||||
else if (length === 1) options.normalizer = require('./normalizers/get-1')();
|
||||
else options.normalizer = require('./normalizers/get-fixed')(length);
|
||||
}
|
||||
} else {
|
||||
if (length === false) options.normalizer = require('./normalizers/get')();
|
||||
else if (length === 1) options.normalizer = require('./normalizers/get-1')();
|
||||
else options.normalizer = require('./normalizers/get-fixed')(length);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ resolveArgs = function (args) {
|
||||
};
|
||||
|
||||
module.exports = function (original, length, options) {
|
||||
var cache = create(null), conf, memLength, get, set, del, clear
|
||||
var cache = create(null), conf, memLength, get, set, del, clear, extDel
|
||||
, getListeners, setListeners, deleteListeners, memoized, resolve, resolvers;
|
||||
if (length !== false) memLength = length;
|
||||
else if (isNaN(original.length)) memLength = 1;
|
||||
@@ -73,6 +73,22 @@ module.exports = function (original, length, options) {
|
||||
if (setListeners) conf.emit('set', id);
|
||||
return result;
|
||||
}, memLength);
|
||||
} else if (length === 0) {
|
||||
memoized = function () {
|
||||
var result;
|
||||
if (hasOwnProperty.call(cache, 'data')) {
|
||||
if (getListeners) conf.emit('get', 'data', arguments, this);
|
||||
return cache.data;
|
||||
}
|
||||
if (!arguments.length) result = call.call(original, this);
|
||||
else result = apply.call(original, this, arguments);
|
||||
if (hasOwnProperty.call(cache, 'data')) {
|
||||
throw customError("Circular invocation", 'CIRCULAR_INVOCATION');
|
||||
}
|
||||
cache.data = result;
|
||||
if (setListeners) conf.emit('set', 'data');
|
||||
return result;
|
||||
};
|
||||
} else {
|
||||
memoized = function (arg) {
|
||||
var result, args = arguments;
|
||||
@@ -126,18 +142,25 @@ module.exports = function (original, length, options) {
|
||||
emit: emit,
|
||||
updateEnv: function () { original = conf.original; }
|
||||
};
|
||||
defineProperties(memoized, {
|
||||
__memoized__: d(true),
|
||||
delete: d(get ? defineLength(function (arg) {
|
||||
if (get) {
|
||||
extDel = defineLength(function (arg) {
|
||||
var id, args = arguments;
|
||||
if (resolve) args = resolve(args);
|
||||
id = get(args);
|
||||
if (id === null) return;
|
||||
conf.delete(id);
|
||||
}, memLength) : function (arg) {
|
||||
}, memLength);
|
||||
} else if (length === 0) {
|
||||
extDel = function () { return conf.delete('data'); };
|
||||
} else {
|
||||
extDel = function (arg) {
|
||||
if (resolve) arg = resolve(arguments)[0];
|
||||
return conf.delete(arg);
|
||||
}),
|
||||
};
|
||||
}
|
||||
defineProperties(memoized, {
|
||||
__memoized__: d(true),
|
||||
delete: d(extDel),
|
||||
clear: d(conf.clear)
|
||||
});
|
||||
return conf;
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function () { return ''; };
|
||||
@@ -1,32 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
var memoize = require('../..');
|
||||
|
||||
module.exports = {
|
||||
"": function (a) {
|
||||
var i = 0, fn = function () { ++i; return 3; };
|
||||
|
||||
fn = memoize(fn);
|
||||
a(fn(), 3, "First");
|
||||
a(fn(1), 3, "Second");
|
||||
a(fn(5), 3, "Third");
|
||||
a(i, 1, "Called once");
|
||||
},
|
||||
"Delete": function (a) {
|
||||
var i = 0, fn, mfn, x = {};
|
||||
|
||||
fn = function (a, b, c) {
|
||||
return a + (++i);
|
||||
};
|
||||
mfn = memoize(fn, { length: 0 });
|
||||
a(mfn(3), 4, "Init");
|
||||
a(mfn(5), 4, "Other");
|
||||
a(i, 1, "Pre clear");
|
||||
mfn.delete(6, x, 1);
|
||||
a(i, 1, "After clear");
|
||||
a(mfn(6, x, 1), 8, "Reinit");
|
||||
a(i, 2, "Reinit count");
|
||||
a(mfn(3, x, 1), 8, "Reinit Cached");
|
||||
a(i, 2, "Reinit count");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user