1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.memoize = void 0;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function memoize(target, propertyKey, descriptor) {
|
19 | const descriptorPropertyName = descriptor.get ? 'get' : 'value';
|
20 | const originalMethod = descriptor[descriptorPropertyName];
|
21 | if (typeof originalMethod !== 'function') {
|
22 | throw new Error('Memoize decorator can only be used on methods or get accessors.');
|
23 | }
|
24 | const cache = new Map();
|
25 | return {
|
26 | ...descriptor,
|
27 | [descriptorPropertyName]: function (...args) {
|
28 | for (const arg of args) {
|
29 | if (!isJSONSerializable(arg)) {
|
30 | throw new Error(`Argument ${isNonPrimitive(arg) ? arg.toString() : arg} is JSON serializable.`);
|
31 | }
|
32 | }
|
33 | const key = JSON.stringify(args);
|
34 | if (cache.has(key)) {
|
35 | return cache.get(key);
|
36 | }
|
37 | const result = originalMethod.apply(this, args);
|
38 | cache.set(key, result);
|
39 | return result;
|
40 | },
|
41 | };
|
42 | }
|
43 | exports.memoize = memoize;
|
44 |
|
45 | function isNonPrimitive(value) {
|
46 | return ((value !== null && typeof value === 'object') ||
|
47 | typeof value === 'function' ||
|
48 | typeof value === 'symbol');
|
49 | }
|
50 |
|
51 | function isJSONSerializable(value) {
|
52 | if (!isNonPrimitive(value)) {
|
53 |
|
54 | return true;
|
55 | }
|
56 | let nestedValues;
|
57 | if (Array.isArray(value)) {
|
58 |
|
59 | nestedValues = value;
|
60 | }
|
61 | else if (Object.prototype.toString.call(value) === '[object Object]') {
|
62 |
|
63 | nestedValues = Object.values(value);
|
64 | }
|
65 | if (!nestedValues || nestedValues.some((v) => !isJSONSerializable(v))) {
|
66 | return false;
|
67 | }
|
68 | return true;
|
69 | }
|