UNPKG

3.86 kBTypeScriptView Raw
1export declare enum TYPE {
2 /**
3 * Raw text
4 */
5 literal = 0,
6 /**
7 * Variable w/o any format, e.g `var` in `this is a {var}`
8 */
9 argument = 1,
10 /**
11 * Variable w/ number format
12 */
13 number = 2,
14 /**
15 * Variable w/ date format
16 */
17 date = 3,
18 /**
19 * Variable w/ time format
20 */
21 time = 4,
22 /**
23 * Variable w/ select format
24 */
25 select = 5,
26 /**
27 * Variable w/ plural format
28 */
29 plural = 6
30}
31export declare const enum SKELETON_TYPE {
32 number = 0,
33 dateTime = 1
34}
35export interface LocationDetails {
36 offset: number;
37 line: number;
38 column: number;
39}
40export interface Location {
41 start: LocationDetails;
42 end: LocationDetails;
43}
44export interface BaseElement<T extends TYPE> {
45 type: T;
46 value: string;
47 location?: Location;
48}
49export declare type LiteralElement = BaseElement<TYPE.literal>;
50export declare type ArgumentElement = BaseElement<TYPE.argument>;
51export interface SimpleFormatElement<T extends TYPE, S extends Skeleton> extends BaseElement<T> {
52 style?: string | S | null;
53}
54export declare type NumberElement = SimpleFormatElement<TYPE.number, NumberSkeleton>;
55export declare type DateElement = SimpleFormatElement<TYPE.date, DateTimeSkeleton>;
56export declare type TimeElement = SimpleFormatElement<TYPE.time, DateTimeSkeleton>;
57export interface SelectOption {
58 id: string;
59 value: MessageFormatElement[];
60 location?: Location;
61}
62export declare type ValidPluralRule = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other' | string;
63export interface PluralOrSelectOption {
64 value: MessageFormatElement[];
65 location?: Location;
66}
67export interface SelectElement extends BaseElement<TYPE.select> {
68 options: Record<string, PluralOrSelectOption>;
69}
70export interface PluralElement extends BaseElement<TYPE.plural> {
71 options: Record<ValidPluralRule, PluralOrSelectOption>;
72 offset: number;
73 pluralType: Intl.PluralRulesOptions['type'];
74}
75export declare type MessageFormatElement = LiteralElement | ArgumentElement | NumberElement | DateElement | TimeElement | SelectElement | PluralElement;
76export interface NumberSkeletonToken {
77 stem: string;
78 options: string[];
79}
80export interface NumberSkeleton {
81 type: SKELETON_TYPE.number;
82 tokens: NumberSkeletonToken[];
83 location?: Location;
84}
85export interface DateTimeSkeleton {
86 type: SKELETON_TYPE.dateTime;
87 pattern: string;
88 location?: Location;
89}
90export declare type Skeleton = NumberSkeleton | DateTimeSkeleton;
91/**
92 * Type Guards
93 */
94export declare function isLiteralElement(el: MessageFormatElement): el is LiteralElement;
95export declare function isArgumentElement(el: MessageFormatElement): el is ArgumentElement;
96export declare function isNumberElement(el: MessageFormatElement): el is NumberElement;
97export declare function isDateElement(el: MessageFormatElement): el is DateElement;
98export declare function isTimeElement(el: MessageFormatElement): el is TimeElement;
99export declare function isSelectElement(el: MessageFormatElement): el is SelectElement;
100export declare function isPluralElement(el: MessageFormatElement): el is PluralElement;
101export declare function isNumberSkeleton(el: NumberElement['style']): el is NumberSkeleton;
102export declare function isDateTimeSkeleton(el?: DateElement['style'] | TimeElement['style']): el is DateTimeSkeleton;
103export declare function createLiteralElement(value: string): LiteralElement;
104export declare function createNumberElement(value: string, style?: string | null): NumberElement;
105export interface Options {
106 /**
107 * Whether to convert `#` in plural rule options
108 * to `{var, number}`
109 * Default is true
110 */
111 normalizeHashtagInPlural?: boolean;
112 /**
113 * Capture location info in AST
114 * Default is false
115 */
116 captureLocation?: boolean;
117}