Path: blob/trunk/third_party/closure/goog/i18n/collation.js
2868 views
// Copyright 2013 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.131415/**16* @fileoverview Contains helper functions for performing locale-sensitive17* collation.18*/192021goog.provide('goog.i18n.collation');222324/**25* Returns the comparator for a locale. If a locale is not explicitly specified,26* a comparator for the user's locale will be returned. Note that if the browser27* does not support locale-sensitive string comparisons, the comparator returned28* will be a simple codepoint comparator.29*30* @param {string=} opt_locale the locale that the comparator is used for.31* @param {{usage: (string|undefined), localeMatcher: (string|undefined),32* sensitivity: (string|undefined), ignorePunctuation: (boolean|undefined),33* numeric: (boolean|undefined), caseFirst: (string|undefined)}=}34* opt_options the optional set of options for use with the native35* collator.36* @return {function(string, string): number} The locale-specific comparator.37*/38goog.i18n.collation.createComparator = function(opt_locale, opt_options) {39// See http://code.google.com/p/v8-i18n.40if (goog.i18n.collation.hasNativeComparator()) {41var intl = goog.global.Intl;42return new intl.Collator([opt_locale || goog.LOCALE], opt_options || {})43.compare;44} else {45return function(arg1, arg2) { return arg1.localeCompare(arg2); };46}47};484950/**51* Returns true if a locale-sensitive comparator is available for a locale. If52* a locale is not explicitly specified, the user's locale is used instead.53*54* @param {string=} opt_locale The locale to be checked.55* @return {boolean} Whether there is a locale-sensitive comparator available56* for the locale.57*/58goog.i18n.collation.hasNativeComparator = function(opt_locale) {59var intl = goog.global.Intl;60return !!(intl && intl.Collator);61};626364