195 lines
7.8 KiB
JavaScript
195 lines
7.8 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
var tslib_1 = require("tslib");
|
||
|
var memoize_1 = require("./memoize");
|
||
|
describe('memoizeFunction', function () {
|
||
|
it('can return a cached result with a no args function', function () {
|
||
|
var _timesCalled = 0;
|
||
|
var memoizeFunctiondTimesCalled = (0, memoize_1.memoizeFunction)(function () { return ++_timesCalled; });
|
||
|
expect(memoizeFunctiondTimesCalled()).toEqual(1);
|
||
|
expect(memoizeFunctiondTimesCalled()).toEqual(1);
|
||
|
});
|
||
|
it('can return a cached result with a 2 arg function', function () {
|
||
|
var _timesCalled = 0;
|
||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||
|
var combine = (0, memoize_1.memoizeFunction)(function (obj1, obj2) { return obj1.val + obj2.val + ++_timesCalled; });
|
||
|
var objA = { val: 'a' };
|
||
|
var objB = { val: 'b' };
|
||
|
expect(combine(objA, objA)).toEqual('aa1');
|
||
|
expect(combine(objA, objA)).toEqual('aa1');
|
||
|
expect(combine(objA, objB)).toEqual('ab2');
|
||
|
expect(combine(objA, objB)).toEqual('ab2');
|
||
|
expect(combine(objB, objA)).toEqual('ba3');
|
||
|
expect(combine(objB, objA)).toEqual('ba3');
|
||
|
});
|
||
|
it('do not use a cached result if the function is different', function () {
|
||
|
var _timesCalled = 0;
|
||
|
var test = (0, memoize_1.memoizeFunction)(function (fn) {
|
||
|
fn();
|
||
|
return ++_timesCalled;
|
||
|
});
|
||
|
function createFunctionArg() {
|
||
|
return function () { return 'test'; };
|
||
|
}
|
||
|
var fnA = createFunctionArg();
|
||
|
var fnB = createFunctionArg();
|
||
|
expect(test(fnA)).toEqual(1);
|
||
|
expect(test(fnA)).toEqual(1);
|
||
|
expect(test(fnB)).toEqual(2);
|
||
|
expect(test(fnA)).toEqual(1);
|
||
|
expect(test(fnB)).toEqual(2);
|
||
|
expect(test(fnB)).toEqual(2);
|
||
|
});
|
||
|
it('can return a cached result with falsy args', function () {
|
||
|
var _timesCalled = 0;
|
||
|
var combine = (0, memoize_1.memoizeFunction)(
|
||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||
|
function (obj1, obj2) { return (obj1 ? obj1.val : '') + (obj2 ? obj2.val : '') + ++_timesCalled; });
|
||
|
var objA = { val: 'a' };
|
||
|
var objB = { val: 'b' };
|
||
|
expect(combine(objA, objA)).toEqual('aa1');
|
||
|
expect(combine(objA, undefined)).toEqual('a2');
|
||
|
expect(combine(null, objB)).toEqual('b3');
|
||
|
expect(combine(false, 0)).toEqual('4');
|
||
|
});
|
||
|
it('works if you pass less arguments on subsequent calls', function () {
|
||
|
var count = 0;
|
||
|
var func = (0, memoize_1.memoizeFunction)(function (a, b) {
|
||
|
if (a === void 0) { a = ''; }
|
||
|
if (b === void 0) { b = ''; }
|
||
|
return a + b + count++;
|
||
|
});
|
||
|
expect(func('hi', 'world')).toEqual('hiworld0');
|
||
|
expect(func('hi', 'world')).toEqual('hiworld0');
|
||
|
expect(func('hi')).toEqual('hi1');
|
||
|
expect(func('hi')).toEqual('hi1');
|
||
|
expect(func()).toEqual('2');
|
||
|
expect(func()).toEqual('2');
|
||
|
});
|
||
|
it('works if you pass more arguments on subsequent calls', function () {
|
||
|
var count = 0;
|
||
|
var func = (0, memoize_1.memoizeFunction)(function (a, b) {
|
||
|
if (a === void 0) { a = ''; }
|
||
|
if (b === void 0) { b = ''; }
|
||
|
return a + b + count++;
|
||
|
});
|
||
|
expect(func()).toEqual('0');
|
||
|
expect(func()).toEqual('0');
|
||
|
expect(func('hi')).toEqual('hi1');
|
||
|
expect(func('hi')).toEqual('hi1');
|
||
|
expect(func('hi', 'world')).toEqual('hiworld2');
|
||
|
expect(func('hi', 'world')).toEqual('hiworld2');
|
||
|
});
|
||
|
it('resets after resetCount limit is reached.', function () {
|
||
|
var count = 0;
|
||
|
var func = (0, memoize_1.memoizeFunction)(function (a) { return a + count++; }, 1);
|
||
|
expect(func('a')).toEqual('a0');
|
||
|
expect(func('a')).toEqual('a0');
|
||
|
expect(func('b')).toEqual('b1');
|
||
|
expect(func('b')).toEqual('b2');
|
||
|
expect(func('b')).toEqual('b2');
|
||
|
expect(func('a')).toEqual('a3');
|
||
|
expect(func('a')).toEqual('a4');
|
||
|
expect(func('a')).toEqual('a4');
|
||
|
});
|
||
|
it('updates the cache if the cached value is null', function () {
|
||
|
var returnNull = true;
|
||
|
var callback = function () {
|
||
|
if (returnNull) {
|
||
|
return null;
|
||
|
}
|
||
|
return 1;
|
||
|
};
|
||
|
var func = (0, memoize_1.memoizeFunction)(function () { return callback(); }, undefined, true /*ignoreNullOrUndefinedResult */);
|
||
|
expect(func()).toEqual(null);
|
||
|
returnNull = false;
|
||
|
expect(func()).toEqual(1);
|
||
|
});
|
||
|
it('updates the cache if the cached value is undefined', function () {
|
||
|
var returnUndefined = true;
|
||
|
var callback = function () {
|
||
|
if (returnUndefined) {
|
||
|
return undefined;
|
||
|
}
|
||
|
return 1;
|
||
|
};
|
||
|
var func = (0, memoize_1.memoizeFunction)(function () { return callback(); }, undefined, true /*ignoreNullOrUndefinedResult */);
|
||
|
expect(func()).toEqual(undefined);
|
||
|
returnUndefined = false;
|
||
|
expect(func()).toEqual(1);
|
||
|
});
|
||
|
it('caches and preserves if the falsey value returned by the callback method is 0', function () {
|
||
|
var returnZero = true;
|
||
|
var callback = function () {
|
||
|
if (returnZero) {
|
||
|
return 0;
|
||
|
}
|
||
|
return 1;
|
||
|
};
|
||
|
var func = (0, memoize_1.memoizeFunction)(function () { return callback(); }, undefined, true /*ignoreNullOrUndefinedResult */);
|
||
|
expect(func()).toEqual(0);
|
||
|
returnZero = false;
|
||
|
expect(func()).toEqual(0);
|
||
|
});
|
||
|
it('caches and preserves if the falsey value returned by the callback method is NaN', function () {
|
||
|
var returnNaN = true;
|
||
|
var callback = function () {
|
||
|
if (returnNaN) {
|
||
|
return NaN;
|
||
|
}
|
||
|
return 1;
|
||
|
};
|
||
|
var func = (0, memoize_1.memoizeFunction)(function () { return callback(); }, undefined, true /*ignoreNullOrUndefinedResult */);
|
||
|
expect(func()).toEqual(NaN);
|
||
|
returnNaN = false;
|
||
|
expect(func()).toEqual(NaN);
|
||
|
});
|
||
|
it('caches and preserves if the falsey value returned by the callback method is false', function () {
|
||
|
var returnFalse = true;
|
||
|
var callback = function () {
|
||
|
if (returnFalse) {
|
||
|
return false;
|
||
|
}
|
||
|
return 1;
|
||
|
};
|
||
|
var func = (0, memoize_1.memoizeFunction)(function () { return callback(); }, undefined, true /*ignoreNullOrUndefinedResult */);
|
||
|
expect(func()).toEqual(false);
|
||
|
returnFalse = false;
|
||
|
expect(func()).toEqual(false);
|
||
|
});
|
||
|
it('caches and preserves if the falsey value returned by the callback method is empty string', function () {
|
||
|
var returnEmptyString = true;
|
||
|
var callback = function () {
|
||
|
if (returnEmptyString) {
|
||
|
return '';
|
||
|
}
|
||
|
return 1;
|
||
|
};
|
||
|
var func = (0, memoize_1.memoizeFunction)(function () { return callback(); }, undefined, true /*ignoreNullOrUndefinedResult */);
|
||
|
expect(func()).toEqual('');
|
||
|
returnEmptyString = false;
|
||
|
expect(func()).toEqual('');
|
||
|
});
|
||
|
});
|
||
|
describe('memoize', function () {
|
||
|
it('can work on multiple instances of a class', function () {
|
||
|
var _count = 0;
|
||
|
var Foo = /** @class */ (function () {
|
||
|
function Foo() {
|
||
|
}
|
||
|
Foo.prototype.bar = function (val) {
|
||
|
return val + _count++;
|
||
|
};
|
||
|
tslib_1.__decorate([
|
||
|
memoize_1.memoize
|
||
|
], Foo.prototype, "bar", null);
|
||
|
return Foo;
|
||
|
}());
|
||
|
var f = new Foo();
|
||
|
expect(f.bar('hi')).toEqual('hi0');
|
||
|
expect(f.bar('hi')).toEqual('hi0');
|
||
|
expect(f.bar('bye')).toEqual('bye1');
|
||
|
expect(f.bar('bye')).toEqual('bye1');
|
||
|
});
|
||
|
});
|
||
|
//# sourceMappingURL=memoize.test.js.map
|