Swadhin Biswas feat: implement mervelas cli engine, built-in tools, and reactive UI · 4/3/2026
0358986
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Shared Intl object instances with lazy initialization.
*
* Intl constructors are expensive (~0.05-0.1ms each), so we cache instances
* for reuse across the codebase instead of creating new ones each time.
* Lazy initialization ensures we only pay the cost when actually needed.
*/
// Segmenters for Unicode text processing (lazily initialized)
let graphemeSegmenter: Intl.Segmenter | null = null
let wordSegmenter: Intl.Segmenter | null = null
export function getGraphemeSegmenter(): Intl.Segmenter {
if (!graphemeSegmenter) {
graphemeSegmenter = new Intl.Segmenter(undefined, {
granularity: 'grapheme',
})
}
return graphemeSegmenter
}
/**
* Extract the first grapheme cluster from a string.
* Returns '' for empty strings.
*/
export function firstGrapheme(text: string): string {
if (!text) return ''
const segments = getGraphemeSegmenter().segment(text)
const first = segments[Symbol.iterator]().next().value
return first?.segment ?? ''
}
/**
* Extract the last grapheme cluster from a string.
* Returns '' for empty strings.
*/
export function lastGrapheme(text: string): string {
if (!text) return ''
let last = ''
for (const { segment } of getGraphemeSegmenter().segment(text)) {
last = segment
}
return last
}
export function getWordSegmenter(): Intl.Segmenter {
if (!wordSegmenter) {
wordSegmenter = new Intl.Segmenter(undefined, { granularity: 'word' })
}
return wordSegmenter
}
// RelativeTimeFormat cache (keyed by style:numeric)
const rtfCache = new Map<string, Intl.RelativeTimeFormat>()
export function getRelativeTimeFormat(
style: 'long' | 'short' | 'narrow',
numeric: 'always' | 'auto',
): Intl.RelativeTimeFormat {
const key = `${style}:${numeric}`
let rtf = rtfCache.get(key)
if (!rtf) {
rtf = new Intl.RelativeTimeFormat('en', { style, numeric })
rtfCache.set(key, rtf)
}
return rtf
}
// Timezone is constant for the process lifetime
let cachedTimeZone: string | null = null
export function getTimeZone(): string {
if (!cachedTimeZone) {
cachedTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
}
return cachedTimeZone
}
// System locale language subtag (e.g. 'en', 'ja') is constant for the process
// lifetime. null = not yet computed; undefined = computed but unavailable (so
// a stripped-ICU environment fails once instead of retrying on every call).
let cachedSystemLocaleLanguage: string | undefined | null = null
export function getSystemLocaleLanguage(): string | undefined {
if (cachedSystemLocaleLanguage === null) {
try {
const locale = Intl.DateTimeFormat().resolvedOptions().locale
cachedSystemLocaleLanguage = new Intl.Locale(locale).language
} catch {
cachedSystemLocaleLanguage = undefined
}
}
return cachedSystemLocaleLanguage
}