@visulima/string
Version:
Functions for manipulating strings.
46 lines (43 loc) • 1.65 kB
JavaScript
import LRUCache from '../packem_shared/LRUCache-udNErhWw.mjs';
import { splitByCase } from './split-by-case.mjs';
import upperFirst from './upper-first.mjs';
import { g as generateCacheKey } from '../packem_shared/generate-cache-key-YSju5pj2.mjs';
import { n as normalizeGermanEszett } from '../packem_shared/normalize-german-eszett-fpJ4lZL6.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const defaultCacheStore = new LRUCache(1e3);
function titleCase(value, options) {
if (typeof value !== "string") {
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);
}
const result = splitByCase(value, {
handleAnsi: options?.handleAnsi,
handleEmoji: options?.handleEmoji,
knownAcronyms: options?.knownAcronyms,
locale: options?.locale,
normalize: options?.normalize,
separators: void 0,
stripAnsi: options?.stripAnsi,
stripEmoji: options?.stripEmoji
}).map((word) => {
if (options?.locale?.startsWith("de")) {
word = normalizeGermanEszett(word);
}
return upperFirst(options?.locale ? word.toLocaleLowerCase(options.locale) : word.toLowerCase(), { locale: options?.locale });
}).join(" ");
if (shouldCache && cacheKey) {
cacheStore.set(cacheKey, result);
}
return result;
}
__name(titleCase, "titleCase");
export { titleCase as default };