polyfill_Intl_bcp47.js.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: polyfill/Intl/bcp47.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: polyfill/Intl/bcp47.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>// Licensed Materials - Property of IBM
  20. //
  21. // IBM Watson Analytics
  22. //
  23. // (C) Copyright IBM Corp. 2018
  24. //
  25. // US Government Users Restricted Rights - Use, duplication or
  26. // disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  27. ( function( exports, ObjectPolyfill )
  28. {
  29. "use strict";
  30. // Instead of fully validating the tag, we match any string that starts with two or three [a-z] characters,
  31. // followed by any number of -[a-z0-9] suffixes.
  32. var reTag = /^(i|[a-zA-Z]{2,3})((?:\-[a-zA-Z0-9]+)*)$/;
  33. var reFormat = /^[a-zA-Z]+|\-\w+/g;
  34. function bcp47Replace( _match, _offset )
  35. {
  36. /*
  37. * Formatting follows simple rules:
  38. * > if NOT the first entry
  39. * > Length 2: UPPER case
  40. * > Length 4: Title case
  41. * > lower case
  42. * For any match that is not the first (_offset is 0), take into account '-' is in the match
  43. */
  44. var len = _offset ? _match.length : 0;
  45. return len === 3 ? // +1 for '-'
  46. _match.toUpperCase() :
  47. len === 5 ? // +1 for '-'
  48. _match.substr( 0, 2 ).toUpperCase() + _match.substr( 2 ).toLowerCase() :
  49. _match.toLowerCase();
  50. }
  51. function isStructurallyValidLanguageTag( _tag )
  52. {
  53. // With the current implementation, the type check for string is unnecessary.
  54. return /*typeof _tag === "string" &amp;&amp;*/ reTag.test( _tag );
  55. }
  56. function canonicalizeLanguageTag( _tag )
  57. {
  58. // Officially this validation should be performed before calling this function,
  59. // but with this limited implementation it's more convenient to perform it here.
  60. var tag = String( _tag );
  61. if ( !isStructurallyValidLanguageTag( tag ) )
  62. throw new RangeError( "Invalid language tag: " + tag );
  63. return tag.replace( reFormat, bcp47Replace );
  64. }
  65. /**
  66. * @memberof module:barejs/polyfill/Intl
  67. * @param {String|Array} _locales
  68. * @return {String[]} List of canonical locale tags.
  69. */
  70. exports.getCanonicalLocales = function getCanonicalLocales( _locales )
  71. {
  72. var seen;
  73. if ( _locales === undefined )
  74. {
  75. seen = new Array( 0 );
  76. }
  77. else if ( typeof _locales === "string" )
  78. {
  79. seen = new Array( 1 );
  80. seen[ 0 ] = canonicalizeLanguageTag( _locales );
  81. }
  82. else
  83. {
  84. seen = [];
  85. var locales = ObjectPolyfill.toObject( _locales );
  86. var len = locales.length || 0;
  87. for ( var i = 0; i &lt; len; ++i )
  88. {
  89. var tag = canonicalizeLanguageTag( locales[ i ] );
  90. if ( seen.indexOf( tag ) &lt; 0 )
  91. seen.push( tag );
  92. }
  93. }
  94. return seen;
  95. };
  96. }( module.exports, require( "../Object" ) ) );
  97. </code></pre>
  98. </article>
  99. </section>
  100. </div>
  101. <nav>
  102. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-barejs.html">barejs</a></li><li><a href="module-barejs_polyfill.html">barejs/polyfill</a></li><li><a href="module-barejs_polyfill_Intl.html">barejs/polyfill/Intl</a></li></ul><h3>Classes</h3><ul><li><a href="module-barejs.decl.html">decl</a></li><li><a href="module-barejs.decl-Enum.html">Enum</a></li><li><a href="module-barejs.decl-Interface.html">Interface</a></li><li><a href="module-barejs.decl-SpecialType.html">SpecialType</a></li><li><a href="module-barejs.Destroyable.html">Destroyable</a></li><li><a href="module-barejs.EventArgs.html">EventArgs</a></li><li><a href="module-barejs.Evented.html">Evented</a></li><li><a href="module-barejs.Evented-EventedHandle.html">EventedHandle</a></li><li><a href="module-barejs.Exception.html">Exception</a></li><li><a href="module-barejs_polyfill.Array.html">Array</a></li><li><a href="module-barejs_polyfill.Date.html">Date</a></li><li><a href="module-barejs_polyfill.EntryStore.html">EntryStore</a></li><li><a href="module-barejs_polyfill.EntryStore.Iterator.html">Iterator</a></li><li><a href="module-barejs_polyfill.Function.html">Function</a></li><li><a href="module-barejs_polyfill.Map.html">Map</a></li><li><a href="module-barejs_polyfill.Map-MapIterator.html">MapIterator</a></li><li><a href="module-barejs_polyfill.Math.html">Math</a></li><li><a href="module-barejs_polyfill.Number.html">Number</a></li><li><a href="module-barejs_polyfill.Object.html">Object</a></li><li><a href="module-barejs_polyfill.Promise.html">Promise</a></li><li><a href="module-barejs_polyfill.Set.html">Set</a></li><li><a href="module-barejs_polyfill.Set-SetIterator.html">SetIterator</a></li><li><a href="module-barejs_polyfill.String.html">String</a></li><li><a href="module-barejs_polyfill.Symbol.html">Symbol</a></li><li><a href="module-barejs_polyfill.WeakMap.html">WeakMap</a></li><li><a href="module-barejs_polyfill.WeakSet.html">WeakSet</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat.html">DateTimeFormat</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat-DateTimeFormatOptions.html">DateTimeFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat.html">NumberFormat</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat-NumberFormatOptions.html">NumberFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl-Format.html">Format</a></li></ul>
  103. </nav>
  104. <br class="clear">
  105. <footer>
  106. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Oct 03 2018 15:59:34 GMT+0200 (W. Europe Daylight Time)
  107. </footer>
  108. <script> prettyPrint(); </script>
  109. <script src="scripts/linenumber.js"> </script>
  110. </body>
  111. </html>