-
Notifications
You must be signed in to change notification settings - Fork 0
/
createRuntime.ts
88 lines (77 loc) · 2.73 KB
/
createRuntime.ts
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
import {
ArgumentRenderer,
ElementRenderer,
FragmentRenderer,
FunctionRenderer,
IRuntime,
LocaleMatcher,
PluralMatcher,
RuntimeMethod,
SelectMatcher,
SelectOrdinalMatcher,
} from './runtime-types';
import {matchLocale} from 'locale-matcher';
import {selectMatcher} from './selectMatcher';
import {createPluralMatcher} from './createPluralMatcher';
export interface IRuntimeOptions<Result> {
renderElement: ElementRenderer<Result | string>;
renderFragment: FragmentRenderer<Result | string>;
renderArgument: ArgumentRenderer<Result | string>;
/**
* By default, {@link renderArgument} is used.
*/
renderFunction?: FunctionRenderer<Result | string>;
/**
* Renders a fragment nested inside an attribute.
*
* By default, {@link renderFragment} is used.
*/
renderAttributeFragment?: FragmentRenderer<Result | string>;
/**
* Renders an argument nested inside an attribute.
*
* By default, {@link renderArgument} is used.
*/
renderAttributeArgument?: ArgumentRenderer<Result | string>;
/**
* Renders an argument value formatted using a function inside an attribute.
*
* By default, {@link renderFunction} is used.
*/
renderAttributeFunction?: FunctionRenderer<Result | string>;
/**
* By default, [`locale-matcher`](https://github.com/smikhalevski/locale-matcher) is used.
*/
matchLocale?: LocaleMatcher;
/**
* By default, uses exact match algorithm to match select keys.
*/
matchSelect?: SelectMatcher;
/**
* By default, uses `Intl.PluralRules` to resolve the plural category.
*/
matchPlural?: PluralMatcher;
/**
* By default, uses `Intl.PluralRules` to resolve the plural category.
*/
matchSelectOrdinal?: SelectOrdinalMatcher;
}
/**
* Creates the new runtime.
*/
export function createRuntime<Result>(options: IRuntimeOptions<Result>): IRuntime<Result> {
const {renderFragment, renderFunction, renderArgument} = options;
return {
[RuntimeMethod.ELEMENT]: options.renderElement,
[RuntimeMethod.FRAGMENT]: options.renderFragment,
[RuntimeMethod.ARGUMENT]: renderArgument,
[RuntimeMethod.FUNCTION]: renderFunction || renderArgument,
[RuntimeMethod.ATTRIBUTE_FRAGMENT]: options.renderAttributeFragment || renderFragment,
[RuntimeMethod.ATTRIBUTE_ARGUMENT]: options.renderAttributeArgument || renderArgument,
[RuntimeMethod.ATTRIBUTE_FUNCTION]: options.renderAttributeFunction || renderFunction || renderArgument,
[RuntimeMethod.LOCALE]: options.matchLocale || matchLocale,
[RuntimeMethod.SELECT]: options.matchSelect || selectMatcher,
[RuntimeMethod.PLURAL]: options.matchPlural || createPluralMatcher('cardinal'),
[RuntimeMethod.SELECT_ORDINAL]: options.matchSelectOrdinal || createPluralMatcher('ordinal'),
};
}