UNPKG

jsii

Version:

[![Join the chat at https://cdk.Dev](https://img.shields.io/static/v1?label=Slack&message=cdk.dev&color=brightgreen&logo=slack)](https://cdk.dev) [![Build Status](https://github.com/aws/jsii-compiler/workflows/build/badge.svg)](https://github.com/aws/jsii

52 lines 1.95 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.kebab = exports.snake = exports.pascal = exports.constant = exports.camel = void 0; const case_1 = __importDefault(require("case")); const withCache = (func) => (text) => Cache.fetch(text, func); exports.camel = withCache(case_1.default.camel); exports.constant = withCache(case_1.default.constant); exports.pascal = withCache(case_1.default.pascal); exports.snake = withCache(case_1.default.snake); exports.kebab = withCache(case_1.default.kebab); class Cache { static fetch(text, func) { // Check whether we have a cache for this function... const cacheKey = CacheKey.for(func); let cache = this.CACHES.get(cacheKey); if (cache == null) { // If not, create one... cache = new Map(); this.CACHES.set(cacheKey, cache); } // Check if the current cache has a value for this text... const cached = cache.get(text); if (cached != null) { return cached; } // If not, compute one... const result = func(text); cache.set(text, result); return result; } // Cache is indexed on a weak CacheKey so the cache can be purged under memory pressure static CACHES = new WeakMap(); constructor() { } } class CacheKey { static for(data) { const entry = this.STORE.get(data)?.deref(); if (entry != null) { return entry; } const newKey = new CacheKey(); this.STORE.set(data, new WeakRef(newKey)); return newKey; } // Storing cache keys as weak references to allow garbage collection if there is memory pressure. static STORE = new Map(); constructor() { } } //# sourceMappingURL=case.js.map