UNPKG

5.16 kBTypeScriptView Raw
1import { NumberFormatOptions } from '@formatjs/ecma402-abstract';
2export interface ExtendedNumberFormatOptions extends NumberFormatOptions {
3 scale?: number;
4}
5export declare enum TYPE {
6 /**
7 * Raw text
8 */
9 literal = 0,
10 /**
11 * Variable w/o any format, e.g `var` in `this is a {var}`
12 */
13 argument = 1,
14 /**
15 * Variable w/ number format
16 */
17 number = 2,
18 /**
19 * Variable w/ date format
20 */
21 date = 3,
22 /**
23 * Variable w/ time format
24 */
25 time = 4,
26 /**
27 * Variable w/ select format
28 */
29 select = 5,
30 /**
31 * Variable w/ plural format
32 */
33 plural = 6,
34 /**
35 * Only possible within plural argument.
36 * This is the `#` symbol that will be substituted with the count.
37 */
38 pound = 7,
39 /**
40 * XML-like tag
41 */
42 tag = 8
43}
44export declare enum SKELETON_TYPE {
45 number = 0,
46 dateTime = 1
47}
48export interface LocationDetails {
49 offset: number;
50 line: number;
51 column: number;
52}
53export interface Location {
54 start: LocationDetails;
55 end: LocationDetails;
56}
57export interface BaseElement<T extends TYPE> {
58 type: T;
59 value: string;
60 location?: Location;
61}
62export declare type LiteralElement = BaseElement<TYPE.literal>;
63export declare type ArgumentElement = BaseElement<TYPE.argument>;
64export interface TagElement {
65 type: TYPE.tag;
66 value: string;
67 children: MessageFormatElement[];
68 location?: Location;
69}
70export interface SimpleFormatElement<T extends TYPE, S extends Skeleton> extends BaseElement<T> {
71 style?: string | S | null;
72}
73export declare type NumberElement = SimpleFormatElement<TYPE.number, NumberSkeleton>;
74export declare type DateElement = SimpleFormatElement<TYPE.date, DateTimeSkeleton>;
75export declare type TimeElement = SimpleFormatElement<TYPE.time, DateTimeSkeleton>;
76export interface SelectOption {
77 id: string;
78 value: MessageFormatElement[];
79 location?: Location;
80}
81export declare type ValidPluralRule = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other' | string;
82export interface PluralOrSelectOption {
83 value: MessageFormatElement[];
84 location?: Location;
85}
86export interface SelectElement extends BaseElement<TYPE.select> {
87 options: Record<string, PluralOrSelectOption>;
88}
89export interface PluralElement extends BaseElement<TYPE.plural> {
90 options: Record<ValidPluralRule, PluralOrSelectOption>;
91 offset: number;
92 pluralType: Intl.PluralRulesOptions['type'];
93}
94export interface PoundElement {
95 type: TYPE.pound;
96 location?: Location;
97}
98export declare type MessageFormatElement = LiteralElement | ArgumentElement | NumberElement | DateElement | TimeElement | SelectElement | PluralElement | TagElement | PoundElement;
99export interface NumberSkeletonToken {
100 stem: string;
101 options: string[];
102}
103export interface NumberSkeleton {
104 type: SKELETON_TYPE.number;
105 tokens: NumberSkeletonToken[];
106 location?: Location;
107 parsedOptions: ExtendedNumberFormatOptions;
108}
109export interface DateTimeSkeleton {
110 type: SKELETON_TYPE.dateTime;
111 pattern: string;
112 location?: Location;
113 parsedOptions: Intl.DateTimeFormatOptions;
114}
115export declare type Skeleton = NumberSkeleton | DateTimeSkeleton;
116/**
117 * Type Guards
118 */
119export declare function isLiteralElement(el: MessageFormatElement): el is LiteralElement;
120export declare function isArgumentElement(el: MessageFormatElement): el is ArgumentElement;
121export declare function isNumberElement(el: MessageFormatElement): el is NumberElement;
122export declare function isDateElement(el: MessageFormatElement): el is DateElement;
123export declare function isTimeElement(el: MessageFormatElement): el is TimeElement;
124export declare function isSelectElement(el: MessageFormatElement): el is SelectElement;
125export declare function isPluralElement(el: MessageFormatElement): el is PluralElement;
126export declare function isPoundElement(el: MessageFormatElement): el is PoundElement;
127export declare function isTagElement(el: MessageFormatElement): el is TagElement;
128export declare function isNumberSkeleton(el: NumberElement['style'] | Skeleton): el is NumberSkeleton;
129export declare function isDateTimeSkeleton(el?: DateElement['style'] | TimeElement['style'] | Skeleton): el is DateTimeSkeleton;
130export declare function createLiteralElement(value: string): LiteralElement;
131export declare function createNumberElement(value: string, style?: string | null): NumberElement;
132export interface Options {
133 /**
134 * Whether to convert `#` in plural rule options
135 * to `{var, number}`
136 * Default is true
137 */
138 normalizeHashtagInPlural?: boolean;
139 /**
140 * Whether to parse number/datetime skeleton
141 * into Intl.NumberFormatOptions & Intl.DateTimeFormatOptions
142 */
143 shouldParseSkeletons?: boolean;
144 /**
145 * Capture location info in AST
146 * Default is false
147 */
148 captureLocation?: boolean;
149 /**
150 * Whether to treat HTML/XML tags as string literal
151 * instead of parsing them as tag token.
152 * When this is false we only allow simple tags without
153 * any attributes
154 */
155 ignoreTag?: boolean;
156}
157//# sourceMappingURL=types.d.ts.map
\No newline at end of file