/
intl.collator.test.js
117 lines (101 loc) 路 4.32 KB
/
intl.collator.test.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
/*
* Titanium SDK
* Copyright TiDev, Inc. 04/07/2022-Present. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';
const should = require('./utilities/assertions');
describe('Intl.Collator', function () {
it('#constructor()', () => {
should(Intl).not.be.undefined();
should(Intl.Collator).not.be.undefined();
should(Intl.Collator).be.a.Function();
should(Intl.Collator.prototype.constructor).not.be.undefined();
should(Intl.Collator.prototype.constructor).be.a.Function();
should(Intl.Collator.prototype.constructor === Intl.Collator).be.true();
should(new Intl.Collator()).be.an.Object();
should(new Intl.Collator('en-US')).be.an.Object();
should(new Intl.Collator([ 'en-US' ])).be.an.Object();
should(new Intl.Collator([ 'en-US', 'de-DE' ])).be.an.Object();
should(new Intl.Collator(undefined, { sensitivity: 'variant' })).be.an.Object();
should(new Intl.Collator('en-US', { sensitivity: 'variant' })).be.an.Object();
should(new Intl.Collator([ 'en-US' ], { sensitivity: 'variant' })).be.an.Object();
should(new Intl.Collator([ 'en-US', 'de-DE' ], { sensitivity: 'variant' })).be.an.Object();
});
describe('#compare()', () => {
it('validate function', () => {
const formatter = new Intl.Collator();
should(formatter.compare).not.be.undefined();
should(formatter.compare).be.a.Function();
should(formatter.compare('', '')).be.a.Number();
});
it('sort callback', () => {
const elements = [ 'c', 'b', 'a' ];
elements.sort(new Intl.Collator().compare);
should(elements).be.eql([ 'a', 'b', 'c' ]);
});
it('localized compare', () => {
// In German, '盲' sorts before 'z'.
should(new Intl.Collator('de').compare('盲', 'z')).be.below(0);
// In Swedish, '盲' sorts after 'z'.
should(new Intl.Collator('sv').compare('盲', 'z')).be.above(0);
});
it('sensitivity - variant', () => {
const collator = new Intl.Collator(Ti.Locale.currentLocale, { sensitivity: 'variant' });
should(collator.compare('a', 'a')).be.eql(0);
should(collator.compare('a', '谩')).not.be.eql(0);
should(collator.compare('a', 'A')).not.be.eql(0);
should(collator.compare('a', 'b')).not.be.eql(0);
});
it('sensitivity - case', () => {
const collator = new Intl.Collator(Ti.Locale.currentLocale, { sensitivity: 'case' });
should(collator.compare('a', 'a')).be.eql(0);
should(collator.compare('a', '谩')).be.eql(0);
should(collator.compare('a', 'A')).not.be.eql(0);
should(collator.compare('a', 'b')).not.be.eql(0);
});
it('sensitivity - accent', () => {
const collator = new Intl.Collator(Ti.Locale.currentLocale, { sensitivity: 'accent' });
should(collator.compare('a', 'a')).be.eql(0);
should(collator.compare('a', '谩')).not.be.eql(0);
should(collator.compare('a', 'A')).be.eql(0);
should(collator.compare('a', 'b')).not.be.eql(0);
});
it('sensitivity - base', () => {
const collator = new Intl.Collator(Ti.Locale.currentLocale, { sensitivity: 'base' });
should(collator.compare('a', 'a')).be.eql(0);
should(collator.compare('a', '谩')).be.eql(0);
should(collator.compare('a', 'A')).be.eql(0);
should(collator.compare('a', 'b')).not.be.eql(0);
});
});
it('#resolvedOptions()', () => {
const formatter = new Intl.Collator();
should(formatter.resolvedOptions).not.be.undefined();
should(formatter.resolvedOptions).be.a.Function();
should(formatter.resolvedOptions()).be.an.Object();
});
it('#supportedLocalesOf()', () => {
should(Intl.Collator.supportedLocalesOf).not.be.undefined();
should(Intl.Collator.supportedLocalesOf).be.a.Function();
let locales = Intl.Collator.supportedLocalesOf([]);
should(locales).be.an.Array();
should(locales.length).be.eql(0);
locales = Intl.Collator.supportedLocalesOf([ 'en' ]);
should(locales).be.an.Array();
should(locales.length).be.eql(1);
should(locales[0]).be.eql('en');
locales = Intl.Collator.supportedLocalesOf([ 'en', 'de' ]);
should(locales).be.an.Array();
should(locales.length).be.eql(2);
should(locales[0]).be.eql('en');
should(locales[1]).be.eql('de');
locales = Intl.Collator.supportedLocalesOf([ 'xx', 'en' ]);
should(locales).be.an.Array();
should(locales.length).be.eql(1);
should(locales[0]).be.eql('en');
});
});