isLocale.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isLocale;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. /*
  9. = 3ALPHA ; selected ISO 639 codes
  10. *2("-" 3ALPHA) ; permanently reserved
  11. */
  12. var extlang = '([A-Za-z]{3}(-[A-Za-z]{3}){0,2})';
  13. /*
  14. = 2*3ALPHA ; shortest ISO 639 code
  15. ["-" extlang] ; sometimes followed by
  16. ; extended language subtags
  17. / 4ALPHA ; or reserved for future use
  18. / 5*8ALPHA ; or registered language subtag
  19. */
  20. var language = "(([a-zA-Z]{2,3}(-".concat(extlang, ")?)|([a-zA-Z]{5,8}))");
  21. /*
  22. = 4ALPHA ; ISO 15924 code
  23. */
  24. var script = '([A-Za-z]{4})';
  25. /*
  26. = 2ALPHA ; ISO 3166-1 code
  27. / 3DIGIT ; UN M.49 code
  28. */
  29. var region = '([A-Za-z]{2}|\\d{3})';
  30. /*
  31. = 5*8alphanum ; registered variants
  32. / (DIGIT 3alphanum)
  33. */
  34. var variant = '([A-Za-z0-9]{5,8}|(\\d[A-Z-a-z0-9]{3}))';
  35. /*
  36. = DIGIT ; 0 - 9
  37. / %x41-57 ; A - W
  38. / %x59-5A ; Y - Z
  39. / %x61-77 ; a - w
  40. / %x79-7A ; y - z
  41. */
  42. var singleton = '(\\d|[A-W]|[Y-Z]|[a-w]|[y-z])';
  43. /*
  44. = singleton 1*("-" (2*8alphanum))
  45. ; Single alphanumerics
  46. ; "x" reserved for private use
  47. */
  48. var extension = "(".concat(singleton, "(-[A-Za-z0-9]{2,8})+)");
  49. /*
  50. = "x" 1*("-" (1*8alphanum))
  51. */
  52. var privateuse = '(x(-[A-Za-z0-9]{1,8})+)'; // irregular tags do not match the 'langtag' production and would not
  53. // otherwise be considered 'well-formed'. These tags are all valid, but
  54. // most are deprecated in favor of more modern subtags or subtag combination
  55. var irregular = '((en-GB-oed)|(i-ami)|(i-bnn)|(i-default)|(i-enochian)|' + '(i-hak)|(i-klingon)|(i-lux)|(i-mingo)|(i-navajo)|(i-pwn)|(i-tao)|' + '(i-tay)|(i-tsu)|(sgn-BE-FR)|(sgn-BE-NL)|(sgn-CH-DE))'; // regular tags match the 'langtag' production, but their subtags are not
  56. // extended language or variant subtags: their meaning is defined by
  57. // their registration and all of these are deprecated in favor of a more
  58. // modern subtag or sequence of subtags
  59. var regular = '((art-lojban)|(cel-gaulish)|(no-bok)|(no-nyn)|(zh-guoyu)|' + '(zh-hakka)|(zh-min)|(zh-min-nan)|(zh-xiang))';
  60. /*
  61. = irregular ; non-redundant tags registered
  62. / regular ; during the RFC 3066 era
  63. */
  64. var grandfathered = "(".concat(irregular, "|").concat(regular, ")");
  65. /*
  66. RFC 5646 defines delimitation of subtags via a hyphen:
  67. "Subtag" refers to a specific section of a tag, delimited by a
  68. hyphen, such as the subtags 'zh', 'Hant', and 'CN' in the tag "zh-
  69. Hant-CN". Examples of subtags in this document are enclosed in
  70. single quotes ('Hant')
  71. However, we need to add "_" to maintain the existing behaviour.
  72. */
  73. var delimiter = '(-|_)';
  74. /*
  75. = language
  76. ["-" script]
  77. ["-" region]
  78. *("-" variant)
  79. *("-" extension)
  80. ["-" privateuse]
  81. */
  82. var langtag = "".concat(language, "(").concat(delimiter).concat(script, ")?(").concat(delimiter).concat(region, ")?(").concat(delimiter).concat(variant, ")*(").concat(delimiter).concat(extension, ")*(").concat(delimiter).concat(privateuse, ")?");
  83. /*
  84. Regex implementation based on BCP RFC 5646
  85. Tags for Identifying Languages
  86. https://www.rfc-editor.org/rfc/rfc5646.html
  87. */
  88. var languageTagRegex = new RegExp("(^".concat(privateuse, "$)|(^").concat(grandfathered, "$)|(^").concat(langtag, "$)"));
  89. function isLocale(str) {
  90. (0, _assertString.default)(str);
  91. return languageTagRegex.test(str);
  92. }
  93. module.exports = exports.default;
  94. module.exports.default = exports.default;