-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMessageBox.ts
160 lines (133 loc) · 4.93 KB
/
MessageBox.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
import { template } from 'lodash-es'
import merge from './merge.js'
import { GetMessageOptions, MessageBoxConstructorOptions, MessageFactoryFunction, type MessageList, TrackerDepLike, TrackerLike, ValidationError } from './types.js'
// Default lodash templates regular expressions
// https://regex101.com/r/ce27tA/5
export const DEFAULT_INTERPOLATE = /{{{([^{}#][\s\S]+?)}}}/g
// https://regex101.com/r/8sRC8b/8
export const DEFAULT_ESCAPE = /{{([^{}#][\s\S]+?)}}/g
// https://regex101.com/r/ndDqxg/4
export const SUGGESTED_EVALUATE = /{{#([^{}].*?)}}/g
class MessageBox {
static language: string | null | undefined
static interpolate: RegExp | undefined
static evaluate: RegExp | undefined
static escape: RegExp | undefined
static messages: MessageList = {}
public language: string
public readonly interpolate: RegExp | undefined
public readonly evaluate: RegExp | undefined
public readonly escape: RegExp | undefined
public readonly messageList: MessageList
public readonly tracker: TrackerLike | undefined
public readonly trackerDep: TrackerDepLike | undefined
constructor ({
escape,
evaluate,
initialLanguage,
interpolate,
messages,
tracker
}: MessageBoxConstructorOptions = {}) {
this.language = initialLanguage ?? MessageBox.language ?? 'en'
this.messageList = messages ?? {}
if (tracker != null) {
this.tracker = tracker
this.trackerDep = new tracker.Dependency()
}
// Template options
this.interpolate = interpolate ?? MessageBox.interpolate ?? DEFAULT_INTERPOLATE
this.evaluate = evaluate ?? MessageBox.evaluate
this.escape = escape ?? MessageBox.escape ?? DEFAULT_ESCAPE
}
clone (): MessageBox {
const copy = new MessageBox({
escape: this.escape,
evaluate: this.evaluate,
initialLanguage: this.language,
interpolate: this.interpolate,
tracker: this.tracker
})
copy.messages(this.messageList)
return copy
}
getMessages (language?: string | undefined): { messages: Record<string, string | MessageFactoryFunction | Record<string, string | MessageFactoryFunction>>, language: string } {
if (language == null) {
language = this.language
if (this.trackerDep != null) this.trackerDep.depend()
}
const globalMessages = MessageBox.messages[language]
let messages = this.messageList[language]
if (messages != null) {
if (globalMessages != null) messages = merge({}, globalMessages, messages) as Record<string, string | MessageFactoryFunction | Record<string, string | MessageFactoryFunction>>
} else {
messages = globalMessages
}
if (messages == null) throw new Error(`No messages found for language "${language}"`)
return {
messages,
language
}
}
message (errorInfo: ValidationError, {
context,
language
}: GetMessageOptions = {}): string {
// Error objects can optionally include a preformatted message,
// in which case we use that.
if (typeof errorInfo.message === 'string') return errorInfo.message
const fieldName = errorInfo.name
const errorType = errorInfo.type
const genericName = MessageBox.makeNameGeneric(fieldName)
const { messages } = this.getMessages(language)
const messageOrMessages = messages[errorType]
let message: string | MessageFactoryFunction | undefined
if (typeof messageOrMessages === 'string' || typeof messageOrMessages === 'function') {
message = messageOrMessages
} else {
message = genericName != null ? messageOrMessages[genericName] : undefined
if (message == null) message = messageOrMessages._default
}
if (typeof message === 'string') {
message = template(message, {
interpolate: this.interpolate,
evaluate: this.evaluate,
escape: this.escape
})
}
if (typeof message !== 'function') return `${fieldName} is invalid`
return message({
genericName,
...(context ?? {}),
...errorInfo
})
}
messages (messages: MessageList): void {
merge(this.messageList, messages)
}
setLanguage (language: string): void {
this.language = language
if (this.trackerDep != null) this.trackerDep.changed()
}
static makeNameGeneric (name: string | null | undefined): string | null {
if (typeof name !== 'string') return null
return name.replace(/\.[0-9]+(?=\.|$)/g, '.$')
}
static defaults ({
escape,
evaluate,
initialLanguage,
interpolate,
messages
}: Omit<MessageBoxConstructorOptions, 'tracker'> = {}): void {
if (typeof initialLanguage === 'string') MessageBox.language = initialLanguage
if (interpolate instanceof RegExp) MessageBox.interpolate = interpolate
if (evaluate instanceof RegExp) MessageBox.evaluate = evaluate
if (escape instanceof RegExp) MessageBox.escape = escape
if (messages != null) {
if (MessageBox.messages == null) MessageBox.messages = {}
merge(MessageBox.messages, messages)
}
}
}
export default MessageBox