MCPcopy
hub / github.com/formatjs/formatjs / constructor

Method constructor

packages/intl-segmenter/segmenter.ts:123–197  ·  view source on GitHub ↗
(
    locales: string | string[] | undefined,
    options: SegmenterOptions
  )

Source from the content-addressed store, hash-verified

121 private readonly mergedSegmentationTypeValue: SegmentationTypeTypeRaw
122
123 constructor(
124 locales: string | string[] | undefined,
125 options: SegmenterOptions
126 ) {
127 if (new.target === undefined) {
128 throw TypeError(`Constructor Intl.Segmenter requires 'new'`)
129 }
130 const requestedLocales = CanonicalizeLocaleList(locales)
131 options = GetOptionsObject(options)
132
133 const opt = Object.create(null)
134 const matcher = GetOption(
135 options,
136 'localeMatcher',
137 'string',
138 ['lookup', 'best fit'],
139 'best fit'
140 )
141 opt.localeMatcher = matcher
142
143 const granularity = GetOption(
144 options,
145 'granularity',
146 'string',
147 ['word', 'sentence', 'grapheme'],
148 'grapheme'
149 ) as keyof typeof SegmentationRules.root
150 setSlot(this, 'granularity', granularity)
151
152 //TODO: figure out correct availible locales
153 const r = ResolveLocale(
154 Segmenter.availableLocales, //availible locales
155 requestedLocales,
156 opt,
157 [], // there is no relevantExtensionKeys
158 {},
159 () => '' //use only root rules
160 )
161 setSlot(this, 'locale', r.locale)
162
163 //root rules based on granularity
164 this.mergedSegmentationTypeValue = SegmentationRules.root[granularity]
165
166 //merge root rules with locale ones if locale is specified
167 if (r.locale.length) {
168 const localeOverrides =
169 SegmentationRules[r.locale as keyof typeof SegmentationRules]
170 if (granularity in localeOverrides) {
171 const localeSegmentationTypeValue: SegmentationTypeTypeRaw =
172 localeOverrides[granularity as keyof typeof localeOverrides]
173 this.mergedSegmentationTypeValue.variables = {
174 ...this.mergedSegmentationTypeValue.variables,
175 ...localeSegmentationTypeValue.variables,
176 }
177 this.mergedSegmentationTypeValue.segmentRules = {
178 ...this.mergedSegmentationTypeValue.segmentRules,
179 ...localeSegmentationTypeValue.segmentRules,
180 }

Callers

nothing calls this directly

Calls 6

ResolveLocaleFunction · 0.90
GetOptionsObjectFunction · 0.85
GetOptionFunction · 0.85
setSlotFunction · 0.70
CanonicalizeLocaleListFunction · 0.50

Tested by

no test coverage detected