-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathresolver.ts
356 lines (312 loc) · 8.27 KB
/
resolver.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import { isObject } from '@intlify/shared'
/** @VueI18nGeneral */
export type Path = string
// actions
const enum Actions {
APPEND,
PUSH,
INC_SUB_PATH_DEPTH,
PUSH_SUB_PATH
}
// states
const enum States {
BEFORE_PATH,
IN_PATH,
BEFORE_IDENT,
IN_IDENT,
IN_SUB_PATH,
IN_SINGLE_QUOTE,
IN_DOUBLE_QUOTE,
AFTER_PATH,
ERROR
}
type StateAction = [States, Actions?]
const enum PathCharTypes {
WORKSPACE = 'w',
IDENT = 'i',
LEFT_BRACKET = '[',
RIGHT_BRACKET = ']',
DOT = '.',
ASTARISK = '*',
ZERO = '0',
ELSE = 'l',
END_OF_FAIL = 'o',
SINGLE_QUOTE = "'",
DOUBLE_QUOTE = '"'
}
type PathState = StateAction | States.ERROR
type PathStateMachine = Record<string, PathState>
const pathStateMachine = /* #__PURE__*/ [] as PathStateMachine[]
pathStateMachine[States.BEFORE_PATH] = {
[PathCharTypes.WORKSPACE]: [States.BEFORE_PATH],
[PathCharTypes.IDENT]: [States.IN_IDENT, Actions.APPEND],
[PathCharTypes.LEFT_BRACKET]: [States.IN_SUB_PATH],
[PathCharTypes.END_OF_FAIL]: [States.AFTER_PATH]
}
pathStateMachine[States.IN_PATH] = {
[PathCharTypes.WORKSPACE]: [States.IN_PATH],
[PathCharTypes.DOT]: [States.BEFORE_IDENT],
[PathCharTypes.LEFT_BRACKET]: [States.IN_SUB_PATH],
[PathCharTypes.END_OF_FAIL]: [States.AFTER_PATH]
}
pathStateMachine[States.BEFORE_IDENT] = {
[PathCharTypes.WORKSPACE]: [States.BEFORE_IDENT],
[PathCharTypes.IDENT]: [States.IN_IDENT, Actions.APPEND],
[PathCharTypes.ZERO]: [States.IN_IDENT, Actions.APPEND]
}
pathStateMachine[States.IN_IDENT] = {
[PathCharTypes.IDENT]: [States.IN_IDENT, Actions.APPEND],
[PathCharTypes.ZERO]: [States.IN_IDENT, Actions.APPEND],
[PathCharTypes.WORKSPACE]: [States.IN_PATH, Actions.PUSH],
[PathCharTypes.DOT]: [States.BEFORE_IDENT, Actions.PUSH],
[PathCharTypes.LEFT_BRACKET]: [States.IN_SUB_PATH, Actions.PUSH],
[PathCharTypes.END_OF_FAIL]: [States.AFTER_PATH, Actions.PUSH]
}
pathStateMachine[States.IN_SUB_PATH] = {
[PathCharTypes.SINGLE_QUOTE]: [States.IN_SINGLE_QUOTE, Actions.APPEND],
[PathCharTypes.DOUBLE_QUOTE]: [States.IN_DOUBLE_QUOTE, Actions.APPEND],
[PathCharTypes.LEFT_BRACKET]: [
States.IN_SUB_PATH,
Actions.INC_SUB_PATH_DEPTH
],
[PathCharTypes.RIGHT_BRACKET]: [States.IN_PATH, Actions.PUSH_SUB_PATH],
[PathCharTypes.END_OF_FAIL]: States.ERROR,
[PathCharTypes.ELSE]: [States.IN_SUB_PATH, Actions.APPEND]
}
pathStateMachine[States.IN_SINGLE_QUOTE] = {
[PathCharTypes.SINGLE_QUOTE]: [States.IN_SUB_PATH, Actions.APPEND],
[PathCharTypes.END_OF_FAIL]: States.ERROR,
[PathCharTypes.ELSE]: [States.IN_SINGLE_QUOTE, Actions.APPEND]
}
pathStateMachine[States.IN_DOUBLE_QUOTE] = {
[PathCharTypes.DOUBLE_QUOTE]: [States.IN_SUB_PATH, Actions.APPEND],
[PathCharTypes.END_OF_FAIL]: States.ERROR,
[PathCharTypes.ELSE]: [States.IN_DOUBLE_QUOTE, Actions.APPEND]
}
/**
* Check if an expression is a literal value.
*/
const literalValueRE = /^\s?(?:true|false|-?[\d.]+|'[^']*'|"[^"]*")\s?$/
function isLiteral(exp: string): boolean {
return literalValueRE.test(exp)
}
/**
* Strip quotes from a string
*/
function stripQuotes(str: string): string {
const a = str.charCodeAt(0)
const b = str.charCodeAt(str.length - 1)
return a === b && (a === 0x22 || a === 0x27) ? str.slice(1, -1) : str
}
/**
* Determine the type of a character in a keypath.
*/
function getPathCharType(ch?: string): string {
if (ch === undefined || ch === null) {
return PathCharTypes.END_OF_FAIL
}
const code = ch.charCodeAt(0)
switch (code) {
case 0x5b: // [
case 0x5d: // ]
case 0x2e: // .
case 0x22: // "
case 0x27: // '
return ch
case 0x5f: // _
case 0x24: // $
case 0x2d: // -
return PathCharTypes.IDENT
case 0x09: // Tab (HT)
case 0x0a: // Newline (LF)
case 0x0d: // Return (CR)
case 0xa0: // No-break space (NBSP)
case 0xfeff: // Byte Order Mark (BOM)
case 0x2028: // Line Separator (LS)
case 0x2029: // Paragraph Separator (PS)
return PathCharTypes.WORKSPACE
}
return PathCharTypes.IDENT
}
/**
* Format a subPath, return its plain form if it is
* a literal string or number. Otherwise prepend the
* dynamic indicator (*).
*/
function formatSubPath(path: string): boolean | string {
const trimmed = path.trim()
// invalid leading 0
if (path.charAt(0) === '0' && isNaN(parseInt(path))) {
return false
}
return isLiteral(trimmed)
? stripQuotes(trimmed)
: PathCharTypes.ASTARISK + trimmed
}
/**
* Parse a string path into an array of segments
*/
export function parse(path: Path): string[] | undefined {
const keys = [] as string[]
let index = -1
let mode = States.BEFORE_PATH
let subPathDepth = 0
let c: string | undefined
let key: any // eslint-disable-line
let newChar: string
let type: string
let transition: PathState
let action: Function
let typeMap: PathStateMachine
const actions = [] as Function[]
actions[Actions.APPEND] = (): void => {
if (key === undefined) {
key = newChar
} else {
key += newChar
}
}
actions[Actions.PUSH] = () => {
if (key !== undefined) {
keys.push(key)
key = undefined
}
}
actions[Actions.INC_SUB_PATH_DEPTH] = () => {
actions[Actions.APPEND]()
subPathDepth++
}
actions[Actions.PUSH_SUB_PATH] = () => {
if (subPathDepth > 0) {
subPathDepth--
mode = States.IN_SUB_PATH
actions[Actions.APPEND]()
} else {
subPathDepth = 0
if (key === undefined) {
return false
}
key = formatSubPath(key)
if (key === false) {
return false
} else {
actions[Actions.PUSH]()
}
}
}
function maybeUnescapeQuote() {
const nextChar = path[index + 1]
if (
(mode === States.IN_SINGLE_QUOTE &&
nextChar === PathCharTypes.SINGLE_QUOTE) ||
(mode === States.IN_DOUBLE_QUOTE &&
nextChar === PathCharTypes.DOUBLE_QUOTE)
) {
index++
newChar = '\\' + nextChar
actions[Actions.APPEND]()
return true
}
}
while (mode !== null) {
index++
c = path[index]
if (c === '\\' && maybeUnescapeQuote()) {
continue
}
type = getPathCharType(c)
typeMap = pathStateMachine[mode]
transition = typeMap[type] || typeMap[PathCharTypes.ELSE] || States.ERROR
// check parse error
if (transition === States.ERROR) {
return
}
mode = transition[0]
if (transition[1] !== undefined) {
action = actions[transition[1]]
if (action) {
newChar = c
if (action() === false) {
return
}
}
}
// check parse finish
if (mode === States.AFTER_PATH) {
return keys
}
}
}
/** @VueI18nGeneral */
export type PathValue =
| string
| number
| boolean
| Function
| null
| { [key: string]: PathValue }
| PathValue[]
/** @VueI18nGeneral */
export type MessageResolver = (obj: unknown, path: Path) => PathValue
// path token cache
const cache = new Map<Path, string[]>()
/**
* key-value message resolver
*
* @remarks
* Resolves messages with the key-value structure. Note that messages with a hierarchical structure such as objects cannot be resolved
*
* @param obj - A target object to be resolved with path
* @param path - A {@link Path | path} to resolve the value of message
*
* @returns A resolved {@link PathValue | path value}
*
* @VueI18nGeneral
*/
export function resolveWithKeyValue(obj: unknown, path: Path): PathValue {
return isObject(obj) ? obj[path] : null
}
/**
* message resolver
*
* @remarks
* Resolves messages. messages with a hierarchical structure such as objects can be resolved. This resolver is used in VueI18n as default.
*
* @param obj - A target object to be resolved with path
* @param path - A {@link Path | path} to resolve the value of message
*
* @returns A resolved {@link PathValue | path value}
*
* @VueI18nGeneral
*/
export function resolveValue(obj: unknown, path: Path): PathValue {
// check object
if (!isObject(obj)) {
return null
}
// parse path
let hit = cache.get(path)
if (!hit) {
hit = parse(path)
if (hit) {
cache.set(path, hit)
}
}
// check hit
if (!hit) {
return null
}
// resolve path value
const len = hit.length
let last = obj
let i = 0
while (i < len) {
const val = last[hit[i]]
if (val === undefined) {
return null
}
last = val
i++
}
return last
}