| 75 | } |
| 76 | |
| 77 | function stringify(obj: unknown): string { |
| 78 | if (obj === null) { |
| 79 | return 'null'; |
| 80 | } |
| 81 | if (obj === undefined) { |
| 82 | return 'undefined'; |
| 83 | } |
| 84 | if (typeof obj === 'boolean') { |
| 85 | return obj ? 'true' : 'false'; |
| 86 | } |
| 87 | if (typeof obj === 'number') { |
| 88 | return String(obj); |
| 89 | } |
| 90 | if (typeof obj === 'string') { |
| 91 | return obj; |
| 92 | } |
| 93 | if (typeof obj === 'function') { |
| 94 | return obj.toString(); |
| 95 | } |
| 96 | |
| 97 | if (Array.isArray(obj)) { |
| 98 | return `[${obj.map(stringify).join(',')}]`; |
| 99 | } |
| 100 | |
| 101 | if (typeof obj === 'object') { |
| 102 | const pairs = Object.entries(obj) |
| 103 | .sort(([a], [b]) => a.localeCompare(b)) |
| 104 | .map(([key, value]) => `${key}:${stringify(value)}`); |
| 105 | return pairs.join(':'); |
| 106 | } |
| 107 | |
| 108 | // Fallback for any other types |
| 109 | return String(obj); |
| 110 | } |
| 111 | |
| 112 | export interface CacheableOptions { |
| 113 | cacheEmptyArray?: boolean; |