12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- function isPrimitive(value) {
- var type = typeof value;
- return (
- type === 'boolean' ||
- type === 'number' ||
- type === 'string' ||
- type === 'undefined' ||
- value === null
- );
- }
- function call(fn, args) {
- if (args.length === 2) {
- return fn(args[0], args[1]);
- }
- if (args.length === 1) {
- return fn(args[0]);
- }
- return fn();
- }
- function serializer(args) {
- if (args.length === 1 && isPrimitive(args[0])) {
- return args[0];
- }
- var obj = {};
- for (var i = 0; i < args.length; i++) {
- obj['key' + i] = args[i];
- }
- return JSON.stringify(obj);
- }
- function memoize(fn) {
- var cache = {};
- return function() {
- var key = serializer(arguments);
- if (cache[key] === undefined) {
- cache[key] = call(fn, arguments);
- }
- return cache[key];
- };
- }
- module.exports = memoize;
|