Brought in another asset for Ray's eye form: moment
[openemr.git] / public / assets / moment-2-13-0 / src / lib / locale / locales.js
blob9cf6ef4f7027ebca3eefc1929944904ed5f5e283
1 import isArray from '../utils/is-array';
2 import isUndefined from '../utils/is-undefined';
3 import compareArrays from '../utils/compare-arrays';
4 import { deprecateSimple } from '../utils/deprecate';
5 import { mergeConfigs } from './set';
6 import { Locale } from './constructor';
7 import keys from '../utils/keys';
9 // internal storage for locale config files
10 var locales = {};
11 var globalLocale;
13 function normalizeLocale(key) {
14     return key ? key.toLowerCase().replace('_', '-') : key;
17 // pick the locale from the array
18 // try ['en-au', 'en-gb'] as 'en-au', 'en-gb', 'en', as in move through the list trying each
19 // substring from most specific to least, but move to the next array item if it's a more specific variant than the current root
20 function chooseLocale(names) {
21     var i = 0, j, next, locale, split;
23     while (i < names.length) {
24         split = normalizeLocale(names[i]).split('-');
25         j = split.length;
26         next = normalizeLocale(names[i + 1]);
27         next = next ? next.split('-') : null;
28         while (j > 0) {
29             locale = loadLocale(split.slice(0, j).join('-'));
30             if (locale) {
31                 return locale;
32             }
33             if (next && next.length >= j && compareArrays(split, next, true) >= j - 1) {
34                 //the next array item is better than a shallower substring of this one
35                 break;
36             }
37             j--;
38         }
39         i++;
40     }
41     return null;
44 function loadLocale(name) {
45     var oldLocale = null;
46     // TODO: Find a better way to register and load all the locales in Node
47     if (!locales[name] && (typeof module !== 'undefined') &&
48             module && module.exports) {
49         try {
50             oldLocale = globalLocale._abbr;
51             require('./locale/' + name);
52             // because defineLocale currently also sets the global locale, we
53             // want to undo that for lazy loaded locales
54             getSetGlobalLocale(oldLocale);
55         } catch (e) { }
56     }
57     return locales[name];
60 // This function will load locale and then set the global locale.  If
61 // no arguments are passed in, it will simply return the current global
62 // locale key.
63 export function getSetGlobalLocale (key, values) {
64     var data;
65     if (key) {
66         if (isUndefined(values)) {
67             data = getLocale(key);
68         }
69         else {
70             data = defineLocale(key, values);
71         }
73         if (data) {
74             // moment.duration._locale = moment._locale = data;
75             globalLocale = data;
76         }
77     }
79     return globalLocale._abbr;
82 export function defineLocale (name, config) {
83     if (config !== null) {
84         config.abbr = name;
85         if (locales[name] != null) {
86             deprecateSimple('defineLocaleOverride',
87                     'use moment.updateLocale(localeName, config) to change ' +
88                     'an existing locale. moment.defineLocale(localeName, ' +
89                     'config) should only be used for creating a new locale');
90             config = mergeConfigs(locales[name]._config, config);
91         } else if (config.parentLocale != null) {
92             if (locales[config.parentLocale] != null) {
93                 config = mergeConfigs(locales[config.parentLocale]._config, config);
94             } else {
95                 // treat as if there is no base config
96                 deprecateSimple('parentLocaleUndefined',
97                         'specified parentLocale is not defined yet');
98             }
99         }
100         locales[name] = new Locale(config);
102         // backwards compat for now: also set the locale
103         getSetGlobalLocale(name);
105         return locales[name];
106     } else {
107         // useful for testing
108         delete locales[name];
109         return null;
110     }
113 export function updateLocale(name, config) {
114     if (config != null) {
115         var locale;
116         if (locales[name] != null) {
117             config = mergeConfigs(locales[name]._config, config);
118         }
119         locale = new Locale(config);
120         locale.parentLocale = locales[name];
121         locales[name] = locale;
123         // backwards compat for now: also set the locale
124         getSetGlobalLocale(name);
125     } else {
126         // pass null for config to unupdate, useful for tests
127         if (locales[name] != null) {
128             if (locales[name].parentLocale != null) {
129                 locales[name] = locales[name].parentLocale;
130             } else if (locales[name] != null) {
131                 delete locales[name];
132             }
133         }
134     }
135     return locales[name];
138 // returns locale data
139 export function getLocale (key) {
140     var locale;
142     if (key && key._locale && key._locale._abbr) {
143         key = key._locale._abbr;
144     }
146     if (!key) {
147         return globalLocale;
148     }
150     if (!isArray(key)) {
151         //short-circuit everything else
152         locale = loadLocale(key);
153         if (locale) {
154             return locale;
155         }
156         key = [key];
157     }
159     return chooseLocale(key);
162 export function listLocales() {
163     return keys(locales);