Debian Codegraph @

closure-library/closure/goog/i18n/collation.js

raw
1 // Copyright 2013 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 /**
17 * @fileoverview Contains helper functions for performing locale-sensitive
18 * collation.
19 */
20
21
22 goog.provide('goog.i18n.collation');
23
24
25 /**
26 * Returns the comparator for a locale. If a locale is not explicitly specified,
27 * a comparator for the user's locale will be returned. Note that if the browser
28 * does not support locale-sensitive string comparisons, the comparator returned
29 * will be a simple codepoint comparator.
30 *
31 * @param {string=} opt_locale the locale that the comparator is used for.
32 * @param {{usage: (string|undefined), localeMatcher: (string|undefined),
33 * sensitivity: (string|undefined), ignorePunctuation: (boolean|undefined),
34 * numeric: (boolean|undefined), caseFirst: (string|undefined)}=}
35 * opt_options the optional set of options for use with the native
36 * collator.
37 * @return {function(string, string): number} The locale-specific comparator.
38 */
39 goog.i18n.collation.createComparator = function(opt_locale, opt_options) {
40 // See http://code.google.com/p/v8-i18n.
41 if (goog.i18n.collation.hasNativeComparator()) {
42 var intl = goog.global.Intl;
43 return new intl.Collator([opt_locale || goog.LOCALE], opt_options || {})
44 .compare;
45 } else {
46 return function(arg1, arg2) { return arg1.localeCompare(arg2); };
47 }
48 };
49
50
51 /**
52 * Returns true if a locale-sensitive comparator is available for a locale. If
53 * a locale is not explicitly specified, the user's locale is used instead.
54 *
55 * @param {string=} opt_locale The locale to be checked.
56 * @return {boolean} Whether there is a locale-sensitive comparator available
57 * for the locale.
58 */
59 goog.i18n.collation.hasNativeComparator = function(opt_locale) {
60 var intl = goog.global.Intl;
61 return !!(intl && intl.Collator);
62 };