MCPcopy
hub / github.com/formatjs/formatjs / GetOption

Function GetOption

packages/ecma402-abstract/GetOption.ts:11–38  ·  view source on GitHub ↗
(
  opts: T,
  prop: K,
  type: 'string' | 'boolean',
  values: readonly T[K][] | undefined,
  fallback: F
)

Source from the content-addressed store, hash-verified

9 * @param fallback
10 */
11export function GetOption<T extends object, K extends keyof T, F>(
12 opts: T,
13 prop: K,
14 type: 'string' | 'boolean',
15 values: readonly T[K][] | undefined,
16 fallback: F
17): Exclude<T[K], undefined> | F {
18 if (typeof opts !== 'object') {
19 throw new TypeError('Options must be an object')
20 }
21 let value: any = opts[prop]
22 if (value !== undefined) {
23 if (type !== 'boolean' && type !== 'string') {
24 throw new TypeError('invalid type')
25 }
26 if (type === 'boolean') {
27 value = Boolean(value)
28 }
29 if (type === 'string') {
30 value = ToString(value)
31 }
32 if (values !== undefined && !values.filter(val => val == value).length) {
33 throw new RangeError(`${value} is not within ${values.join(', ')}`)
34 }
35 return value
36 }
37 return fallback
38}

Callers 15

core.tsFile · 0.85
constructorMethod · 0.85
SupportedLocalesFunction · 0.85
InitializeDateTimeFormatFunction · 0.85
GetDurationUnitOptionsFunction · 0.85
InitializePluralRulesFunction · 0.85
InitializeNumberFormatFunction · 0.85
constructorMethod · 0.85
constructorMethod · 0.85

Calls 1

ToStringFunction · 0.85

Tested by

no test coverage detected