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

Method constructor

packages/intl-durationformat/core.ts:140–253  ·  view source on GitHub ↗
(locales?: string | string[], options?: DurationFormatOptions)

Source from the content-addressed store, hash-verified

138 */
139export class DurationFormat implements DurationFormatType {
140 constructor(locales?: string | string[], options?: DurationFormatOptions) {
141 // Ensure constructor is called with 'new' keyword
142 // test262/test/intl402/ListFormat/constructor/constructor/newtarget-undefined.js
143 // Cannot use `new.target` bc of IE11 & TS transpiles it to something else
144 const newTarget =
145 this && this instanceof DurationFormat ? this.constructor : void 0
146 if (!newTarget) {
147 throw new TypeError("Intl.DurationFormat must be called with 'new'")
148 }
149 // Canonicalize the requested locales into a standard format
150 const requestedLocales = CanonicalizeLocaleList(locales)
151 const opt: any = Object.create(null)
152 const opts = options === undefined ? Object.create(null) : ToObject(options)
153
154 // Get locale matching algorithm preference ('best fit' or 'lookup')
155 const matcher = GetOption(
156 opts,
157 'localeMatcher',
158 'string',
159 ['best fit', 'lookup'],
160 'best fit'
161 )
162
163 // Get numbering system (e.g., 'latn', 'arab', 'deva')
164 const numberingSystem = GetOption(
165 opts,
166 'numberingSystem',
167 'string',
168 undefined,
169 undefined
170 )
171 if (
172 numberingSystem !== undefined &&
173 numberingSystemNames.indexOf(numberingSystem) < 0
174 ) {
175 // Validate that the numbering system is recognized
176 throw RangeError(`Invalid numberingSystems: ${numberingSystem}`)
177 }
178 opt.nu = numberingSystem
179 opt.localeMatcher = matcher
180 // Resolve the best matching locale from available locales
181 const {localeData, availableLocales} = DurationFormat
182 const r = ResolveLocale(
183 availableLocales,
184 requestedLocales,
185 opt,
186 // [[RelevantExtensionKeys]] slot - only 'nu' (numbering system) is supported
187 ['nu'],
188 localeData,
189 DurationFormat.getDefaultLocale
190 )
191 const locale = r.locale
192
193 // Initialize internal slots for this formatter instance
194 const internalSlots = getInternalSlots(this)
195 internalSlots.initializedDurationFormat = true
196 internalSlots.locale = locale
197 internalSlots.numberingSystem = r.nu

Callers

nothing calls this directly

Calls 7

ResolveLocaleFunction · 0.90
ToObjectFunction · 0.85
GetOptionFunction · 0.85
GetDurationUnitOptionsFunction · 0.85
GetNumberOptionFunction · 0.85
getInternalSlotsFunction · 0.70
CanonicalizeLocaleListFunction · 0.50

Tested by

no test coverage detected