MCPcopy Create free account
hub / github.com/denoland/std / titleCaseSegment

Function titleCaseSegment

text/_title_case_util.ts:99–122  ·  view source on GitHub ↗
(
  s: string,
  opts: ResolvedOptions,
  _cache = titleCaseCache,
)

Source from the content-addressed store, hash-verified

97export const CACHE_MAX_SEGMENT_LENGTH = 0x20; // max cache size for titleCaseSegment
98const titleCaseCache = new Map<string, string>();
99export function titleCaseSegment(
100 s: string,
101 opts: ResolvedOptions,
102 _cache = titleCaseCache,
103): string {
104 // skip cache for long segments as they're highly likely to be unique
105 if (s.length > CACHE_MAX_SEGMENT_LENGTH) return _titleCaseSegment(s, opts);
106
107 // for other segments, caching is beneficial as performance can be slow otherwise
108 const cacheKey = opts.cacheKey + s;
109 let result = _cache.get(cacheKey);
110 if (result != null) {
111 // least-recently-used
112 _cache.delete(cacheKey);
113 _cache.set(cacheKey, result);
114 return result;
115 }
116 result = _titleCaseSegment(s, opts);
117 if (_cache.size >= 0x1000) {
118 _cache.delete(_cache.keys().next().value!);
119 }
120 _cache.set(cacheKey, result);
121 return result;
122}
123
124// https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/text/CaseMap.Title.html#adjustToCased--
125// Since ICU 60, the default index adjustment is to the next character that is a letter, number, symbol, or private use

Callers 3

toSentenceCaseFunction · 0.90
toTitleCaseFunction · 0.90

Calls 6

_titleCaseSegmentFunction · 0.85
keysMethod · 0.80
getMethod · 0.65
deleteMethod · 0.65
setMethod · 0.65
nextMethod · 0.45

Tested by

no test coverage detected