-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse.js
302 lines (253 loc) · 6.76 KB
/
parse.js
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
'use strict'
var alphanumeric = require('is-alphanumerical')
var alphabetical = require('is-alphabetical')
var decimal = require('is-decimal')
var regular = require('./regular')
var normal = require('./normalize')
module.exports = parse
var own = {}.hasOwnProperty
var maxSubtag = 8
var maxReserved = 4
var maxIso639 = 3
var minIso639 = 2
var maxExtendedLanguageSubtagCount = 3
var maxVariant = 8
var minAlphanumericVariant = 5
var minVariant = 4
var maxExtension = 8
var maxPrivateUse = 8
var errVariantTooLong = 1
var errExtensionTooLong = 2
var errTooManySubtags = 3
var errEmptyExtension = 4
var errPrivateUseTooLong = 5
var errExtraContent = 6
var errors = {}
errors[errVariantTooLong] = 'Too long variant, expected at most 8 characters'
errors[errExtensionTooLong] =
'Too long extension, expected at most 8 characters'
errors[errTooManySubtags] =
'Too many extended language subtags, expected at most 3 subtags'
errors[errEmptyExtension] =
'Empty extension, extensions must have at least 2 characters of content'
errors[errPrivateUseTooLong] =
'Too long private-use area, expected at most 8 characters'
errors[errExtraContent] = 'Found superfluous content after tag'
// Parse a BCP 47 language tag.
/* eslint-disable complexity */
function parse(tag, options) {
var settings = options || {}
var warning = settings.warning
var forgiving = settings.forgiving
var normalize = settings.normalize
var result = {}
var sensitive
var code
var index
var count
var groupCount
var offset
// Check input.
if (tag === null || tag === undefined) {
throw new Error('Expected string, got `' + tag + '`')
}
normalize = normalize === null || normalize === undefined ? true : normalize
// Let’s start.
result = empty()
tag = String(tag)
sensitive = tag
tag = tag.toLowerCase()
if (own.call(normal, tag)) {
if (normalize && normal[tag]) {
return parse(normal[tag])
}
result[regular.indexOf(tag) === -1 ? 'irregular' : 'regular'] = sensitive
return result
}
index = 0
code = char(index)
while (alphabetical(code)) {
index++
code = char(index)
}
// Parse a normal tag.
if (index < minIso639 || index > maxSubtag) {
index = 0
} else {
if (index > maxReserved) {
// Subtag language.
result.language = sensitive.slice(0, index)
} else if (index > maxIso639) {
// Reserved language.
result.language = sensitive.slice(0, index)
} else {
groupCount = 0
// ISO 639 language.
result.language = sensitive.slice(0, index)
while (
dash(char(index)) &&
alphabetical(char(index + 1)) &&
alphabetical(char(index + 2)) &&
alphabetical(char(index + 3)) &&
!alphabetical(char(index + 4))
) {
if (groupCount >= maxExtendedLanguageSubtagCount) {
return fail(index, errTooManySubtags)
}
// Extended language subtag.
result.extendedLanguageSubtags.push(
sensitive.slice(index + 1, index + 4)
)
index += 4
groupCount++
}
}
if (
dash(char(index)) &&
alphabetical(char(index + 1)) &&
alphabetical(char(index + 2)) &&
alphabetical(char(index + 3)) &&
alphabetical(char(index + 4)) &&
!alphabetical(char(index + 5))
) {
// ISO 15924 script.
result.script = sensitive.slice(index + 1, index + 5)
index += 5
}
if (dash(char(index))) {
if (
alphabetical(char(index + 1)) &&
alphabetical(char(index + 2)) &&
!alphabetical(char(index + 3))
) {
// ISO 3166-1 region.
result.region = sensitive.slice(index + 1, index + 3)
index += 3
} else if (
decimal(char(index + 1)) &&
decimal(char(index + 2)) &&
decimal(char(index + 3)) &&
!decimal(char(index + 4))
) {
// UN M49 region.
result.region = sensitive.slice(index + 1, index + 4)
index += 4
}
}
while (dash(char(index))) {
offset = index + 1
count = 0
while (alphanumeric(char(offset))) {
offset++
count++
if (count > maxVariant) {
return fail(offset, errVariantTooLong)
}
}
if (count >= minAlphanumericVariant) {
// Long variant.
result.variants.push(sensitive.slice(index + 1, offset))
index = offset
} else if (decimal(char(index + 1)) && count >= minVariant) {
// Short variant.
result.variants.push(sensitive.slice(index + 1, offset))
index = offset
} else {
break
}
}
while (dash(char(index))) {
if (
x(char(index + 1)) ||
!alphanumeric(char(index + 1)) ||
!dash(char(index + 2)) ||
!alphanumeric(char(index + 3))
) {
break
}
offset = index + 2
groupCount = 0
while (
dash(char(offset)) &&
alphanumeric(char(offset + 1)) &&
alphanumeric(char(offset + 2))
) {
offset += 2
count = 2
groupCount++
while (alphanumeric(char(offset))) {
if (count > maxExtension) {
return fail(offset, errExtensionTooLong)
}
offset++
count++
}
}
if (!groupCount) {
return fail(offset, errEmptyExtension)
}
// Extension.
result.extensions.push({
singleton: sensitive.charAt(index + 1),
extensions: sensitive.slice(index + 3, offset).split('-')
})
index = offset
}
}
if (
(index === 0 && x(char(0))) ||
(index !== 1 && dash(char(index)) && x(char(index + 1)))
) {
index = index ? index + 2 : 1
offset = index
while (dash(char(offset)) && alphanumeric(char(offset + 1))) {
offset += 2
count = 1
while (alphanumeric(char(offset))) {
if (count >= maxPrivateUse) {
return fail(offset, errPrivateUseTooLong)
}
offset++
count++
}
result.privateuse.push(sensitive.slice(index + 1, offset))
index = offset
}
}
if (index !== tag.length) {
return fail(index, errExtraContent)
}
return result
function char(pos) {
return tag.charCodeAt(pos)
}
function fail(offset, code) {
if (warning) {
warning(errors[code], code, offset)
}
return forgiving ? result : empty()
}
}
/* eslint-enable complexity */
// Check if `code` is `x` (lower-case).
function x(code) {
return code === 120 // 'x'
}
// Check if `code` is `-`.
function dash(code) {
return code === 45 // '-'
}
// Create an empty results object.
function empty() {
return {
language: null,
extendedLanguageSubtags: [],
script: null,
region: null,
variants: [],
extensions: [],
privateuse: [],
irregular: null,
regular: null
}
}