|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
803 bytes
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | function memoize(func, resolver) {
|
|---|
| 6 | if (typeof func !== 'function' || (resolver != null && typeof resolver !== 'function')) {
|
|---|
| 7 | throw new TypeError('Expected a function');
|
|---|
| 8 | }
|
|---|
| 9 | const memoized = function (...args) {
|
|---|
| 10 | const key = resolver ? resolver.apply(this, args) : args[0];
|
|---|
| 11 | const cache = memoized.cache;
|
|---|
| 12 | if (cache.has(key)) {
|
|---|
| 13 | return cache.get(key);
|
|---|
| 14 | }
|
|---|
| 15 | const result = func.apply(this, args);
|
|---|
| 16 | memoized.cache = cache.set(key, result) || cache;
|
|---|
| 17 | return result;
|
|---|
| 18 | };
|
|---|
| 19 | const CacheConstructor = memoize.Cache || Map;
|
|---|
| 20 | memoized.cache = new CacheConstructor();
|
|---|
| 21 | return memoized;
|
|---|
| 22 | }
|
|---|
| 23 | memoize.Cache = Map;
|
|---|
| 24 |
|
|---|
| 25 | exports.memoize = memoize;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.