]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
incorporating ichernev comments
authorIsaac Cambron <isaac@isaaccambron.com>
Mon, 14 Jul 2014 05:12:05 +0000 (01:12 -0400)
committerIsaac Cambron <isaac@isaaccambron.com>
Mon, 14 Jul 2014 05:12:05 +0000 (01:12 -0400)
moment.js
test/moment/locale.js

index 74953103a5836b3414de7f6c2da9151b587daaa2..3b2fd4a589fd075b013a5b90d18c7b432dbe8364 100644 (file)
--- a/moment.js
+++ b/moment.js
         return key ? key.toLowerCase().replace('_', '-') : key;
     }
 
+    // pick the locale from the array
+    // try ['en-au', 'en-gb'] as 'en-au', 'en-gb', 'en', as in move through the list trying each
+    // substring from most specific to least, but move to the next array item if it's a more specific variant than the current root
+    function chooseLocale(names) {
+        var i = 0, j, next, locale, split;
+
+        while (i < names.length) {
+            split = normalizeLocale(names[i]).split('-');
+            j = split.length;
+            next = normalizeLocale(names[i + 1]);
+            next = next ? next.split('-') : null;
+            while (j > 0) {
+                locale = loadLocale(split.slice(0, j).join('-'));
+                if (locale) {
+                    return locale;
+                }
+                if (next && next.length >= j && compareArrays(split, next, true) >= j - 1) {
+                    //the next array item is better than a shallower substring of this one
+                    break;
+                }
+                j--;
+            }
+            i++;
+        }
+        return null;
+    }
+
+    function loadLocale(name) {
+        if (!locales[name] && hasModule) {
+            try {
+                require('./locale/' + name);
+            } catch (e) { }
+        }
+        return locales[name];
+    }
+
     // Return a moment from input, that is local/utc/zone equivalent to model.
     function makeAs(input, model) {
         return model._isUTC ? moment(input).zone(model._offset || 0) :
 
     // returns locale data
     moment.localeData = function (key) {
-        var i = 0, j, locale, next, split,
-            get = function (k) {
-                if (!locales[k] && hasModule) {
-                    try {
-                        require('./locale/' + k);
-                    } catch (e) { }
-                }
-                return locales[k];
-            };
+        var locale;
 
         if (key && key._locale && key._locale._abbr) {
             key = key._locale._abbr;
 
         if (!isArray(key)) {
             //short-circuit everything else
-            locale = get(key);
+            locale = loadLocale(key);
             if (locale) {
                 return locale;
             }
             key = [key];
         }
 
-        //pick the locale from the array
-        //try ['en-au', 'en-gb'] as 'en-au', 'en-gb', 'en', as in move through the list trying each
-        //substring from most specific to least, but move to the next array item if it's a more specific variant than the current root
-        while (i < key.length) {
-            split = normalizeLocale(key[i]).split('-');
-            j = split.length;
-            next = normalizeLocale(key[i + 1]);
-            next = next ? next.split('-') : null;
-            while (j > 0) {
-                locale = get(split.slice(0, j).join('-'));
-                if (locale) {
-                    return locale;
-                }
-                if (next && next.length >= j && compareArrays(split, next, true) >= j - 1) {
-                    //the next array item is better than a shallower substring of this one
-                    break;
-                }
-                j--;
-            }
-            i++;
-        }
-        return null;
+        return chooseLocale(key);
     };
 
     // compare moment object
         lang : deprecate(
             "moment().lang is deprecated. Use moment().locale instead.",
             function (key) {
-                return this.locale(key);
+                return this.localeData(key);
             }
         ),
 
index 9edbf3e4cfe0aded3b32c47023f27213df4daf60..c9feb31f4c8bccf7a2ba7ced117ef8bd9f5cb8ab 100644 (file)
@@ -219,7 +219,7 @@ exports.locale = {
     },
 
     "duration deprecations" : function (test) {
-        test.equal(moment.duration().lang(), moment.duration().locale(), "duration.lang is the same as duration.locale");
+        test.equal(moment.duration().lang(), moment.duration().localeData(), "duration.lang is the same as duration.localeData");
         test.done();
     },