UNPKG

3.21 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../source/resolveLocale.js"],"names":["resolveLocale","locale","options","localeMatcher","resolveLocaleLookup","RangeError","resolvedLocale","parts","split","length","pop","join"],"mappings":";;;;;;;;AAAA;;AAIA;;;;;;;;;;;;AAYe,SAASA,aAAT,CAAuBC,MAAvB,EAA6C;AAAA,MAAdC,OAAc,uEAAJ,EAAI;AAC1D,MAAMC,aAAa,GAAGD,OAAO,CAACC,aAAR,IAAyB,QAA/C;;AACA,UAAQA,aAAR;AACE,SAAK,QAAL;AACE,aAAOC,mBAAmB,CAACH,MAAD,CAA1B;AACF;AACA;;AACA,SAAK,UAAL;AACE;AACA,aAAOG,mBAAmB,CAACH,MAAD,CAA1B;;AACF;AACE,YAAM,IAAII,UAAJ,6CAAkDF,aAAlD,EAAN;AATJ;AAWD;AAED;;;;;;;;;;;;;;;;AAcO,SAASC,mBAAT,CAA6BH,MAA7B,EAAqC;AAC1C,MAAMK,cAAc,GAAG,oCAAqBL,MAArB,CAAvB;;AACA,MAAIK,cAAJ,EAAoB;AAClB,WAAOA,cAAP;AACD,GAJyC,CAK1C;;;AACA,MAAMC,KAAK,GAAGN,MAAM,CAACO,KAAP,CAAa,GAAb,CAAd;;AACA,SAAOP,MAAM,CAACQ,MAAP,GAAgB,CAAvB,EAA0B;AACxBF,IAAAA,KAAK,CAACG,GAAN;AACAT,IAAAA,MAAM,GAAGM,KAAK,CAACI,IAAN,CAAW,GAAX,CAAT;;AACA,QAAML,eAAc,GAAG,oCAAqBL,MAArB,CAAvB;;AACA,QAAIK,eAAJ,EAAoB;AAClB,aAAOA,eAAP;AACD;AACF;AACF","sourcesContent":["import {\r\n resolveLocale as resolveLocaleForData\r\n} from './LocaleDataStore'\r\n\r\n/**\r\n * Resolves a locale to a supported one (if any).\r\n * @param {string} locale\r\n * @param {Object} [options] - An object that may have the following property:\r\n * @param {string} [options.localeMatcher=\"lookup\"] - The locale matching algorithm to use. Possible values are \"lookup\" and \"best fit\". Currently only \"lookup\" is supported.\r\n * @return {string} [locale]\r\n * @example\r\n * // Returns \"sr\"\r\n * resolveLocale(\"sr-Cyrl-BA\")\r\n * // Returns `undefined`\r\n * resolveLocale(\"xx-Latn\")\r\n */\r\nexport default function resolveLocale(locale, options = {}) {\r\n const localeMatcher = options.localeMatcher || 'lookup'\r\n switch (localeMatcher) {\r\n case 'lookup':\r\n return resolveLocaleLookup(locale)\r\n // \"best fit\" locale matching is not supported.\r\n // https://github.com/catamphetamine/relative-time-format/issues/2\r\n case 'best fit':\r\n // return resolveLocaleBestFit(locale)\r\n return resolveLocaleLookup(locale)\r\n default:\r\n throw new RangeError(`Invalid \"localeMatcher\" option: ${localeMatcher}`)\r\n }\r\n}\r\n\r\n/**\r\n * Resolves a locale to a supported one (if any).\r\n * Starts from the most specific locale and gradually\r\n * falls back to less specific ones.\r\n * This is a basic implementation of the \"lookup\" algorithm.\r\n * https://tools.ietf.org/html/rfc4647#section-3.4\r\n * @param {string} locale\r\n * @return {string} [locale]\r\n * @example\r\n * // Returns \"sr\"\r\n * resolveLocaleLookup(\"sr-Cyrl-BA\")\r\n * // Returns `undefined`\r\n * resolveLocaleLookup(\"xx-Latn\")\r\n */\r\nexport function resolveLocaleLookup(locale) {\r\n const resolvedLocale = resolveLocaleForData(locale)\r\n if (resolvedLocale) {\r\n return resolvedLocale\r\n }\r\n // `sr-Cyrl-BA` -> `sr-Cyrl` -> `sr`.\r\n const parts = locale.split('-')\r\n while (locale.length > 1) {\r\n parts.pop()\r\n locale = parts.join('-')\r\n const resolvedLocale = resolveLocaleForData(locale)\r\n if (resolvedLocale) {\r\n return resolvedLocale\r\n }\r\n }\r\n}\r\n"],"file":"resolveLocale.js"}
\No newline at end of file