forked from hapijs/accept
-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.js
executable file
·137 lines (89 loc) · 3.69 KB
/
language.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
'use strict';
const Accept = require('..');
const Code = require('@hapi/code');
const Lab = require('@hapi/lab');
const internals = {};
const { describe, it } = exports.lab = Lab.script();
const expect = Code.expect;
describe('language()', () => {
it('parses the header', () => {
const language = Accept.language('da, en-GB, en');
expect(language).to.equal('da');
});
it('respects weights', () => {
const language = Accept.language('en;q=0.6, en-GB;q=0.8');
expect(language).to.equal('en-gb');
});
it('requires the preferences parameter to be an array', () => {
expect(() => {
Accept.language('en;q=0.6, en-GB;q=0.8', 'en');
}).to.throw('Preferences must be an array');
});
it('returns empty string with header is empty', () => {
const language = Accept.language('');
expect(language).to.equal('');
});
it('returns empty string if header is missing', () => {
const language = Accept.language();
expect(language).to.equal('');
});
it('ignores an empty preferences array', () => {
const language = Accept.language('da, en-GB, en', []);
expect(language).to.equal('da');
});
it('returns empty string if none of the preferences match', () => {
const language = Accept.language('da, en-GB, en', ['es']);
expect(language).to.equal('');
});
it('returns first preference if header has *', () => {
const language = Accept.language('da, en-GB, en, *', ['en-US']);
expect(language).to.equal('en-US');
});
it('returns first found preference that header includes', () => {
const language = Accept.language('da, en-GB, en', ['en-US', 'en-GB']);
expect(language).to.equal('en-GB');
});
it('returns preference with highest specificity', () => {
expect(Accept.language('da, en, en-GB', ['en', 'en-GB'])).to.equal('en-GB');
expect(Accept.language('da, en, en-GB', ['en-GB', 'en'])).to.equal('en-GB');
expect(Accept.language('en, en-GB, en-US')).to.equal('en-gb');
});
it('return language with heighest weight', () => {
const language = Accept.language('da;q=0.5, en;q=1', ['da', 'en']);
expect(language).to.equal('en');
});
it('ignores preference case when matching', () => {
const language = Accept.language('da, en-GB, en', ['en-us', 'en-gb']); // en-GB vs en-gb
expect(language).to.equal('en-gb');
});
});
describe('languages()', () => {
it('parses the header', () => {
const languages = Accept.languages('da, en-GB, en');
expect(languages).to.equal(['da', 'en-gb', 'en']);
});
it('orders by weight(q)', () => {
const languages = Accept.languages('da, en;q=0.7, en-GB;q=0.8');
expect(languages).to.equal(['da', 'en-gb', 'en']);
});
it('maintains case', () => {
const languages = Accept.languages('da, en-GB, en');
expect(languages).to.equal(['da', 'en-gb', 'en']);
});
it('drops zero weighted charsets', () => {
const languages = Accept.languages('da, en-GB, es;q=0, en');
expect(languages).to.equal(['da', 'en-gb', 'en']);
});
it('ignores invalid weights', () => {
const languages = Accept.languages('da, en-GB;q=1.1, es;q=a, en;q=0.0001');
expect(languages).to.equal(['da', 'en-gb', 'es', 'en']);
});
it('return empty array when no header is present', () => {
const languages = Accept.languages();
expect(languages).to.equal([]);
});
it('return empty array when header is empty', () => {
const languages = Accept.languages('');
expect(languages).to.equal([]);
});
});