@ultraq/icu-message-formatter 0.14.2 → 0.15.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +0 -0
- package/dist/icu-message-formatter.browser.es.min.js +180 -1
- package/dist/icu-message-formatter.browser.es.min.js.map +1 -1
- package/dist/icu-message-formatter.browser.min.js +1 -1
- package/dist/icu-message-formatter.browser.min.js.map +1 -1
- package/dist/icu-message-formatter.cjs +44 -260
- package/dist/icu-message-formatter.cjs.map +1 -1
- package/dist/icu-message-formatter.d.ts +54 -49
- package/dist/icu-message-formatter.js +50 -259
- package/dist/icu-message-formatter.js.map +1 -1
- package/package.json +19 -27
- package/dist/icu-message-formatter.d.cts +0 -153
@@ -1,153 +0,0 @@
|
|
1
|
-
export type ParseCasesResult = {
|
2
|
-
/**
|
3
|
-
* A list of prepended arguments.
|
4
|
-
*/
|
5
|
-
args: string[];
|
6
|
-
/**
|
7
|
-
* A map of all cases.
|
8
|
-
*/
|
9
|
-
cases: Record<string, string>;
|
10
|
-
};
|
11
|
-
export type FormatValues = Record<string, any>;
|
12
|
-
export type ProcessFunction = (message: string, values?: FormatValues) => any[];
|
13
|
-
export type TypeHandler = (value: any, matches: string, locale: string, values: FormatValues, process: ProcessFunction) => any | any[];
|
14
|
-
/**
|
15
|
-
* @typedef {Record<string,any>} FormatValues
|
16
|
-
*/
|
17
|
-
/**
|
18
|
-
* @callback ProcessFunction
|
19
|
-
* @param {string} message
|
20
|
-
* @param {FormatValues} [values={}]
|
21
|
-
* @return {any[]}
|
22
|
-
*/
|
23
|
-
/**
|
24
|
-
* @callback TypeHandler
|
25
|
-
* @param {any} value
|
26
|
-
* The object which matched the key of the block being processed.
|
27
|
-
* @param {string} matches
|
28
|
-
* Any format options associated with the block being processed.
|
29
|
-
* @param {string} locale
|
30
|
-
* The locale to use for formatting.
|
31
|
-
* @param {FormatValues} values
|
32
|
-
* The object of placeholder data given to the original `format`/`process`
|
33
|
-
* call.
|
34
|
-
* @param {ProcessFunction} process
|
35
|
-
* The `process` function itself so that sub-messages can be processed by type
|
36
|
-
* handlers.
|
37
|
-
* @return {any | any[]}
|
38
|
-
*/
|
39
|
-
/**
|
40
|
-
* The main class for formatting messages.
|
41
|
-
*
|
42
|
-
* @author Emanuel Rabina
|
43
|
-
*/
|
44
|
-
export class MessageFormatter {
|
45
|
-
/**
|
46
|
-
* Creates a new formatter that can work using any of the custom type handlers
|
47
|
-
* you register.
|
48
|
-
*
|
49
|
-
* @param {string} locale
|
50
|
-
* @param {Record<string,TypeHandler>} [typeHandlers]
|
51
|
-
* Optional object where the keys are the names of the types to register,
|
52
|
-
* their values being the functions that will return a nicely formatted
|
53
|
-
* string for the data and locale they are given.
|
54
|
-
*/
|
55
|
-
constructor(locale: string, typeHandlers?: Record<string, TypeHandler>);
|
56
|
-
locale: string;
|
57
|
-
typeHandlers: Record<string, TypeHandler>;
|
58
|
-
/**
|
59
|
-
* Formats an ICU message syntax string using `values` for placeholder data
|
60
|
-
* and any currently-registered type handlers.
|
61
|
-
*
|
62
|
-
* @type {(message: string, values?: FormatValues) => string}
|
63
|
-
*/
|
64
|
-
format: (message: string, values?: FormatValues) => string;
|
65
|
-
/**
|
66
|
-
* Process an ICU message syntax string using `values` for placeholder data
|
67
|
-
* and any currently-registered type handlers. The result of this method is
|
68
|
-
* an array of the component parts after they have been processed in turn by
|
69
|
-
* their own type handlers. This raw output is useful for other renderers,
|
70
|
-
* eg: React where components can be used instead of being forced to return
|
71
|
-
* raw strings.
|
72
|
-
*
|
73
|
-
* This method is used by {@link MessageFormatter#format} where it acts as a
|
74
|
-
* string renderer.
|
75
|
-
*
|
76
|
-
* @param {string} message
|
77
|
-
* @param {FormatValues} [values]
|
78
|
-
* @return {any[]}
|
79
|
-
*/
|
80
|
-
process(message: string, values?: FormatValues): any[];
|
81
|
-
}
|
82
|
-
/**
|
83
|
-
* Finds the index of the matching closing curly bracket, including through
|
84
|
-
* strings that could have nested brackets.
|
85
|
-
*
|
86
|
-
* @param {string} string
|
87
|
-
* @param {number} fromIndex
|
88
|
-
* @return {number}
|
89
|
-
* The index of the matching closing bracket, or -1 if no closing bracket
|
90
|
-
* could be found.
|
91
|
-
*/
|
92
|
-
export function findClosingBracket(string: string, fromIndex: number): number;
|
93
|
-
/**
|
94
|
-
* @typedef ParseCasesResult
|
95
|
-
* @property {string[]} args
|
96
|
-
* A list of prepended arguments.
|
97
|
-
* @property {Record<string,string>} cases
|
98
|
-
* A map of all cases.
|
99
|
-
*/
|
100
|
-
/**
|
101
|
-
* Most branch-based type handlers are based around "cases". For example,
|
102
|
-
* `select` and `plural` compare compare a value to "case keys" to choose a
|
103
|
-
* subtranslation.
|
104
|
-
*
|
105
|
-
* This util splits "matches" portions provided to the aforementioned handlers
|
106
|
-
* into case strings, and extracts any prepended arguments (for example,
|
107
|
-
* `plural` supports an `offset:n` argument used for populating the magic `#`
|
108
|
-
* variable).
|
109
|
-
*
|
110
|
-
* @param {string} string
|
111
|
-
* @return {ParseCasesResult}
|
112
|
-
*/
|
113
|
-
export function parseCases(string?: string): ParseCasesResult;
|
114
|
-
/**
|
115
|
-
* Handler for `plural` statements within ICU message syntax strings. Returns
|
116
|
-
* a formatted string for the branch that closely matches the current value.
|
117
|
-
*
|
118
|
-
* See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more
|
119
|
-
* details on how the `plural` statement works.
|
120
|
-
*
|
121
|
-
* @param {string} value
|
122
|
-
* @param {string} matches
|
123
|
-
* @param {string} locale
|
124
|
-
* @param {import('./MessageFormatter.js').FormatValues} values
|
125
|
-
* @param {import('./MessageFormatter.js').ProcessFunction} process
|
126
|
-
* @return {any | any[]}
|
127
|
-
*/
|
128
|
-
export function pluralTypeHandler(value: string, matches: string, locale: string, values: any, process: any): any | any[];
|
129
|
-
/**
|
130
|
-
* Handler for `select` statements within ICU message syntax strings. Returns
|
131
|
-
* a formatted string for the branch that closely matches the current value.
|
132
|
-
*
|
133
|
-
* See https://formatjs.io/docs/core-concepts/icu-syntax#select-format for more
|
134
|
-
* details on how the `select` statement works.
|
135
|
-
*
|
136
|
-
* @param {string} value
|
137
|
-
* @param {string} matches
|
138
|
-
* @param {string} locale
|
139
|
-
* @param {import('./MessageFormatter.js').FormatValues} values
|
140
|
-
* @param {import('./MessageFormatter.js').ProcessFunction} process
|
141
|
-
* @return {any | any[]}
|
142
|
-
*/
|
143
|
-
export function selectTypeHandler(value: string, matches: string, locale: string, values: any, process: any): any | any[];
|
144
|
-
/**
|
145
|
-
* Split a `{key, type, format}` block into those 3 parts, taking into account
|
146
|
-
* nested message syntax that can exist in the `format` part.
|
147
|
-
*
|
148
|
-
* @param {string} block
|
149
|
-
* @return {string[]}
|
150
|
-
* An array with `key`, `type`, and `format` items in that order, if present
|
151
|
-
* in the formatted argument block.
|
152
|
-
*/
|
153
|
-
export function splitFormattedArgument(block: string): string[];
|