UNPKG

6.72 kBPlain TextView Raw
1import { BigNumber } from "bignumber.js";
2
3import { I18n } from "./I18n";
4
5export type MakePlural = (count: number, ordinal?: boolean) => string;
6
7export interface Dict {
8 [key: string]: any;
9}
10
11export type DateTime = string | number | Date;
12
13export interface TimeAgoInWordsOptions {
14 includeSeconds?: boolean;
15 scope?: Scope;
16}
17
18export type Numeric = BigNumber | string | number;
19
20/**
21 * Controls handling of arithmetic exceptions and rounding.
22 *
23 * - "up": round away from zero
24 * - "down" or "truncate": round towards zero (truncate)
25 * - "halfUp" or "default": round towards the nearest neighbor, unless both
26 * neighbors are equidistant, in which case round away from zero.
27 * - "halfDown": round towards the nearest neighbor, unless both neighbors are
28 * equidistant, in which case round towards zero.
29 * - "halfEven" or "banker": round towards the nearest neighbor, unless both
30 * neighbors are equidistant, in which case round towards the even neighbor
31 * (Banker’s rounding)
32 * - "ceiling" or "ceil": round towards positive infinity
33 * - "floor": round towards negative infinity
34 *
35 * @type {string}
36 */
37export type RoundingMode =
38 | "up"
39 | "down"
40 | "truncate"
41 | "halfUp"
42 | "default"
43 | "halfDown"
44 | "halfEven"
45 | "banker"
46 | "ceiling"
47 | "ceil"
48 | "floor";
49
50export interface FormatNumberOptions {
51 format: string;
52 negativeFormat: string;
53 precision: number | null;
54 roundMode: RoundingMode;
55 significant: boolean;
56 separator: string;
57 delimiter: string;
58 stripInsignificantZeros: boolean;
59 raise: boolean;
60 unit: string;
61}
62
63// I18n#numberToHumanSize options.
64export type NumberToHumanSizeOptions = Omit<
65 FormatNumberOptions,
66 "format" | "negativeFormat" | "raise"
67>;
68
69export type NumberToHumanUnits = {
70 [key: string]: string;
71};
72
73export type NumberToHumanOptions = Omit<
74 FormatNumberOptions,
75 "negativeFormat" | "unit" | "raise"
76> & {
77 units: NumberToHumanUnits | string;
78};
79
80export type NumberToDelimitedOptions = {
81 delimiterPattern: RegExp;
82 delimiter: string;
83 separator: string;
84};
85
86export type NumberToPercentageOptions = Omit<FormatNumberOptions, "raise">;
87
88export type NumberToRoundedOptions = Omit<
89 FormatNumberOptions,
90 "format" | "negativeFormat" | "raise"
91> & { precision: number };
92
93export type NumberToCurrencyOptions = FormatNumberOptions;
94
95export interface ToSentenceOptions {
96 wordsConnector: string;
97 twoWordsConnector: string;
98 lastWordConnector: string;
99}
100
101// Default primitive types.
102export type PrimitiveType = number | string | null | undefined | boolean;
103export type ArrayType = AnyObject[];
104export type AnyObject = PrimitiveType | ArrayType | ObjectType;
105
106export interface ObjectType {
107 [key: string]: PrimitiveType | ArrayType | ObjectType;
108}
109
110/**
111 * Possible missing translation behavior.
112 * @type {String}
113 */
114export type MissingBehavior = "message" | "guess" | "error";
115
116// The I18n class initializer options.
117export interface I18nOptions {
118 /**
119 * Set default locale. This locale will be used when fallback is enabled and
120 * the translation doesn't exist in a particular locale. Defaults to `en`.
121 *
122 * @type {string}
123 */
124 defaultLocale: string;
125
126 /**
127 * Set the default string separator. Defaults to `.`, as in
128 * `scope.translation`.
129 *
130 * @type {string}
131 */
132 defaultSeparator: string;
133
134 /**
135 * Set if engine should fallback to the default locale when a translation is
136 * missing. Defaults to `false`.
137 *
138 * When enabled, missing translations will first be looked for in less
139 * specific versions of the requested locale and if that fails by taking them
140 * from your `I18n#defaultLocale`.
141 *
142 * @type {boolean}
143 */
144 enableFallback: boolean;
145
146 /**
147 * Set the current locale. Defaults to `en`.
148 *
149 * @type {string}
150 */
151 locale: string;
152
153 /**
154 * Set missing translation behavior.
155 *
156 * - `message` will display a message that the translation is missing.
157 * - `guess` will try to guess the string.
158 * - `error` will raise an exception whenever a translation is not defined.
159 *
160 * See {@link MissingTranslation.register} for instructions on how to define
161 * your own behavior.
162 *
163 * @type {MissingBehavior}
164 */
165 missingBehavior: MissingBehavior;
166
167 /**
168 * Return a missing placeholder message for given parameters.
169 *
170 * @type {MissingPlaceholderHandler}
171 */
172 missingPlaceholder: MissingPlaceholderHandler;
173
174 /**
175 * Return a placeholder message for null values. Defaults to the same behavior
176 * as `I18n.missingPlaceholder`.
177 *
178 * @type {NullPlaceholderHandler}
179 */
180 nullPlaceholder: NullPlaceholderHandler;
181
182 /**
183 * If you use missingBehavior with 'message', but want to know that the string
184 * is actually missing for testing purposes, you can prefix the guessed string
185 * by setting the value here. By default, no prefix is used.
186 *
187 * @type {string}
188 */
189 missingTranslationPrefix: string;
190
191 /**
192 * Set the placeholder format. Accepts `{{placeholder}}` and `%{placeholder}`.
193 *
194 * @type {RegExp}
195 */
196 placeholder: RegExp;
197
198 /**
199 * Transform keys. By default, it returns the key as it is, but allows for
200 * overriding. For instance, you can set a function to receive the camelcase
201 * key, and convert it to snake case.
202 *
203 * @type {function}
204 */
205 transformKey: (key: string) => string;
206}
207
208// The translation scope.
209export type Scope = Readonly<string | string[]>;
210
211// The locale resolver.
212export type LocaleResolver = (i18n: I18n, locale: string) => string[];
213
214// The pluralizer function.
215export type Pluralizer = (i18n: I18n, count: number) => string[];
216
217// The missing translation strategy.
218export type MissingTranslationStrategy = (
219 i18n: I18n,
220 scope: Scope,
221 options: Dict,
222) => string;
223
224export interface TranslateOptions {
225 defaultValue?: any;
226 count?: number;
227 scope?: Scope;
228 defaults?: Dict[];
229 missingBehavior?: MissingBehavior | string;
230 [key: string]: any;
231}
232
233export type MissingPlaceholderHandler = (
234 i18n: I18n,
235 placeholder: string,
236 message: string,
237 options: Dict,
238) => string;
239
240export type NullPlaceholderHandler = (
241 i18n: I18n,
242 placeholder: string,
243 message: string,
244 options: Dict,
245) => string;
246
247export type DayNames = [string, string, string, string, string, string, string];
248export type MonthNames = [
249 null,
250 string,
251 string,
252 string,
253 string,
254 string,
255 string,
256 string,
257 string,
258 string,
259 string,
260 string,
261 string,
262];
263
264export interface StrftimeOptions {
265 meridian: {
266 am: string;
267 pm: string;
268 };
269
270 dayNames: DayNames;
271 abbrDayNames: DayNames;
272 monthNames: MonthNames;
273 abbrMonthNames: MonthNames;
274
275 // Set how month and year will be retrieved.
276 utc?: boolean;
277}
278
279export type OnChangeHandler = (i18n: I18n) => void;