UNPKG

1.72 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.kebab = exports.snake = exports.pascal = exports.constant = exports.camel = void 0;
4const Case = require("case");
5const withCache = (func) => (text) => Cache.fetch(text, func);
6exports.camel = withCache(Case.camel);
7exports.constant = withCache(Case.constant);
8exports.pascal = withCache(Case.pascal);
9exports.snake = withCache(Case.snake);
10exports.kebab = withCache(Case.kebab);
11class Cache {
12 static fetch(text, func) {
13 // Check whether we have a cache for this function...
14 const cacheKey = CacheKey.for(func);
15 let cache = this.CACHES.get(cacheKey);
16 if (cache == null) {
17 // If not, create one...
18 cache = new Map();
19 this.CACHES.set(cacheKey, cache);
20 }
21 // Check if the current cache has a value for this text...
22 const cached = cache.get(text);
23 if (cached != null) {
24 return cached;
25 }
26 // If not, compute one...
27 const result = func(text);
28 cache.set(text, result);
29 return result;
30 }
31 constructor() { }
32}
33// Cache is indexed on a weak CacheKey so the cache can be purged under memory pressure
34Cache.CACHES = new WeakMap();
35class CacheKey {
36 static for(data) {
37 const entry = this.STORE.get(data)?.deref();
38 if (entry != null) {
39 return entry;
40 }
41 const newKey = new CacheKey();
42 this.STORE.set(data, new WeakRef(newKey));
43 return newKey;
44 }
45 constructor() { }
46}
47// Storing cache keys as weak references to allow garbage collection if there is memory pressure.
48CacheKey.STORE = new Map();
49//# sourceMappingURL=case.js.map
\No newline at end of file