UNPKG

@visulima/string

Version:

Functions for manipulating strings.

57 lines (54 loc) 2.04 kB
import { R as RE_FAST_ANSI } from '../packem_shared/constants-CKNmLDBQ.mjs'; 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 joinSegments from '../packem_shared/joinSegments-B2-yNNIj.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); const sentenceCase = /* @__PURE__ */ __name((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); } let firstWord = true; const result = joinSegments( 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?.handleAnsi && RE_FAST_ANSI.test(word)) { return word; } word = options?.locale?.startsWith("de") ? normalizeGermanEszett(word) : word; word = options?.locale ? word.toLocaleLowerCase(options.locale) : word.toLowerCase(); if (firstWord) { firstWord = false; return upperFirst(word, options); } return word; }), " " ); if (shouldCache && cacheKey) { cacheStore.set(cacheKey, result); } return result; }, "sentenceCase"); export { sentenceCase as default };