-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
218 lines (187 loc) · 4.98 KB
/
index.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
/**
* See <https://tools.ietf.org/html/rfc4647#section-3.1>
* for more information on the algorithms.
*/
/**
* @typedef {string} Tag
* @typedef {Array<Tag>} Tags
* @typedef {string} Range
* @typedef {Array<Range>} Ranges
*/
/**
* @callback Check
* @param {Tag} tag
* @param {Range} range
* @returns {boolean}
*/
/**
* @callback Filter
* @param {Tag|Tags} tag
* @param {Range|Ranges} [ranges]
* @returns {Tag}
*/
/**
* @callback Lookup
* @param {Tag|Tags} tag
* @param {Range|Ranges} [ranges]
* @returns {Tag}
*/
/**
* Factory to perform a filter or a lookup.
* This factory creates a function that accepts a list of tags and a list of
* ranges, and contains logic to exit early for lookups.
* `check` just has to deal with one tag and one range.
* This match function iterates over ranges, and for each range,
* iterates over tags. That way, earlier ranges matching any tag have
* precedence over later ranges.
*
* @type {{
* (check: Check, filter: true): Filter
* (check: Check, filter?: false): Lookup
* }}
*/
// prettier-ignore
const factory = (
/**
* @param {Check} check
* @param {boolean} [filter=false]
*/
function (check, filter) {
return match
/**
* @param {Tag|Tags} tags
* @param {Range|Ranges} [ranges='*']
* @returns {Tag|Tags|undefined}
*/
function match(tags, ranges) {
let left = cast(tags, 'tag')
const right = cast(
ranges === null || ranges === undefined ? '*' : ranges,
'range'
)
/** @type {Tags} */
const matches = []
let rightIndex = -1
while (++rightIndex < right.length) {
const range = right[rightIndex].toLowerCase()
// Ignore wildcards in lookup mode.
if (!filter && range === '*') continue
let leftIndex = -1
/** @type {Tags} */
const next = []
while (++leftIndex < left.length) {
if (check(left[leftIndex].toLowerCase(), range)) {
// Exit if this is a lookup and we have a match.
if (!filter) return left[leftIndex]
matches.push(left[leftIndex])
} else {
next.push(left[leftIndex])
}
}
left = next
}
// If this is a filter, return the list. If it’s a lookup, we didn’t find
// a match, so return `undefined`.
return filter ? matches : undefined
}
}
)
/**
* Basic Filtering (Section 3.3.1) matches a language priority list consisting
* of basic language ranges (Section 2.1) to sets of language tags.
* @param {Tag|Tags} tags
* @param {Range|Ranges} [ranges]
* @returns {Tags}
*/
export const basicFilter = factory(
/** @type {Check} */
function (tag, range) {
return range === '*' || tag === range || tag.includes(range + '-')
},
true
)
/**
* Extended Filtering (Section 3.3.2) matches a language priority list
* consisting of extended language ranges (Section 2.2) to sets of language
* tags.
* @param {Tag|Tags} tags
* @param {Range|Ranges} [ranges]
* @returns {Tags}
*/
export const extendedFilter = factory(
/** @type {Check} */
function (tag, range) {
// 3.3.2.1
const left = tag.split('-')
const right = range.split('-')
let leftIndex = 0
let rightIndex = 0
// 3.3.2.2
if (right[rightIndex] !== '*' && left[leftIndex] !== right[rightIndex]) {
return false
}
leftIndex++
rightIndex++
// 3.3.2.3
while (rightIndex < right.length) {
// 3.3.2.3.A
if (right[rightIndex] === '*') {
rightIndex++
continue
}
// 3.3.2.3.B
if (!left[leftIndex]) return false
// 3.3.2.3.C
if (left[leftIndex] === right[rightIndex]) {
leftIndex++
rightIndex++
continue
}
// 3.3.2.3.D
if (left[leftIndex].length === 1) return false
// 3.3.2.3.E
leftIndex++
}
// 3.3.2.4
return true
},
true
)
/**
* Lookup (Section 3.4) matches a language priority list consisting of basic
* language ranges to sets of language tags to find the one exact language tag
* that best matches the range.
* @param {Tag|Tags} tags
* @param {Range|Ranges} [ranges]
* @returns {Tag}
*/
export const lookup = factory(
/** @type {Check} */
function (tag, range) {
let right = range
/* eslint-disable-next-line no-constant-condition */
while (true) {
if (right === '*' || tag === right) return true
let index = right.lastIndexOf('-')
if (index < 0) return false
if (right.charAt(index - 2) === '-') index -= 2
right = right.slice(0, index)
}
}
)
/**
* Validate tags or ranges, and cast them to arrays.
*
* @param {string|Array<string>} values
* @param {string} name
* @returns {Array<string>}
*/
function cast(values, name) {
const value = values && typeof values === 'string' ? [values] : values
if (!value || typeof value !== 'object' || !('length' in value)) {
throw new Error(
'Invalid ' + name + ' `' + value + '`, expected non-empty string'
)
}
return value
}