-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtag.js
50 lines (41 loc) · 1.19 KB
/
subtag.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
!function(root, name, make) {
if (typeof module != 'undefined' && module.exports) module.exports = make()
else root[name] = make()
}(this, 'subtag', function() {
var empty = ''
var pattern = /^([a-zA-Z]{2,3})(?:[_-]+([a-zA-Z]{3})(?=$|[_-]+))?(?:[_-]+([a-zA-Z]{4})(?=$|[_-]+))?(?:[_-]+([a-zA-Z]{2}|[0-9]{3})(?=$|[_-]+))?/
function match(tag) {
return tag.match(pattern) || []
}
function split(tag) {
return match(tag).filter(function(v, i) { return v && i })
}
function api(tag) {
tag = match(tag)
return {
language: tag[1] || empty,
extlang: tag[2] || empty,
script: tag[3] || empty,
region: tag[4] || empty
}
}
function expose(target, key, value) {
Object.defineProperty(target, key, {
value: value,
enumerable: true
})
}
function part(position, pattern, type) {
function method(tag) {
return match(tag)[position] || empty
}
expose(method, 'pattern', pattern)
expose(api, type, method)
}
part(1, /^[a-zA-Z]{2,3}$/, 'language')
part(2, /^[a-zA-Z]{3}$/, 'extlang')
part(3, /^[a-zA-Z]{4}$/, 'script')
part(4, /^[a-zA-Z]{2}$|^[0-9]{3}$/, 'region')
expose(api, 'split', split)
return api
});