@visulima/string
Version:
Functions for manipulating strings.
62 lines (57 loc) • 2.2 kB
JavaScript
;
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
const node_util = require('node:util');
const constants = require('../packem_shared/constants-nihvIvk5.cjs');
const LRUCache = require('../packem_shared/LRUCache-Crb-m7cw.cjs');
const generateCacheKey = require('../packem_shared/generate-cache-key-D8f1auDB.cjs');
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.generateCacheKey(value, options);
}
if (shouldCache && cacheKey && cacheStore.has(cacheKey)) {
return cacheStore.get(cacheKey);
}
let processedValue = value;
if (options?.stripEmoji) {
processedValue = constants.stripEmoji(processedValue);
}
if (options?.stripAnsi) {
processedValue = node_util.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");
exports.flipCase = flipCase;