UNPKG

@visulima/string

Version:

Functions for manipulating strings.

58 lines (55 loc) 2.04 kB
import { stripVTControlCharacters } from 'node:util'; import { s as stripEmoji } from '../packem_shared/constants-CKNmLDBQ.mjs'; import LRUCache from '../packem_shared/LRUCache-udNErhWw.mjs'; import { g as generateCacheKey } from '../packem_shared/generate-cache-key-YSju5pj2.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const defaultCacheStore = new LRUCache(1e3); const flipCase = /* @__PURE__ */ __name((value, options) => { if (typeof value !== "string" || value.length === 0) { return ""; } const shouldCache = options?.cache ?? false; const cacheStore = options?.cacheStore ?? defaultCacheStore; let cacheKey; if (shouldCache) { cacheKey = generateCacheKey(value, options); } if (shouldCache && cacheKey && cacheStore.has(cacheKey)) { return cacheStore.get(cacheKey); } let processedValue = value; if (options?.stripEmoji) { processedValue = stripEmoji(processedValue); } if (options?.stripAnsi) { processedValue = stripVTControlCharacters(processedValue); } let result = ""; let index = 0; while (index < processedValue.length) { if (options?.handleAnsi && processedValue[index] === "\x1B") { let ansiSequence = processedValue[index]; index++; while (index < processedValue.length && processedValue[index] !== "m") { ansiSequence += processedValue[index]; index++; } if (index < processedValue.length) { ansiSequence += processedValue[index]; result += ansiSequence; } index++; continue; } const char = processedValue[index]; const lowerChar = options?.locale ? char.toLocaleLowerCase(options.locale) : char.toLowerCase(); result += char === lowerChar ? options?.locale ? char.toLocaleUpperCase(options.locale) : char.toUpperCase() : lowerChar; index++; } if (shouldCache && cacheKey) { cacheStore.set(cacheKey, result); } return result; }, "flipCase"); export { flipCase };