<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: polyfill/Intl/bcp47.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: polyfill/Intl/bcp47.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>// Licensed Materials - Property of IBM
//
// IBM Watson Analytics
//
// (C) Copyright IBM Corp. 2018
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

( function( exports, ObjectPolyfill )
{
"use strict";

// Instead of fully validating the tag, we match any string that starts with two or three [a-z] characters,
// followed by any number of -[a-z0-9] suffixes.
var reTag = /^(i|[a-zA-Z]{2,3})((?:\-[a-zA-Z0-9]+)*)$/;

var reFormat = /^[a-zA-Z]+|\-\w+/g;

function bcp47Replace( _match, _offset )
{
    /*
     * Formatting follows simple rules:
     *  > if NOT the first entry
     *      > Length 2: UPPER case
     *      > Length 4: Title case
     *  > lower case
     * For any match that is not the first (_offset is 0), take into account '-' is in the match
     */
    var len = _offset ? _match.length : 0;
    return len === 3 ? // +1 for '-'
        _match.toUpperCase() :
        len === 5 ? // +1 for '-'
            _match.substr( 0, 2 ).toUpperCase() + _match.substr( 2 ).toLowerCase() :
            _match.toLowerCase();
}

function isStructurallyValidLanguageTag( _tag )
{
    // With the current implementation, the type check for string is unnecessary.
    return /*typeof _tag === "string" &amp;&amp;*/ reTag.test( _tag );
}

function canonicalizeLanguageTag( _tag )
{
    // Officially this validation should be performed before calling this function,
    // but with this limited implementation it's more convenient to perform it here.
    var tag = String( _tag );
    if ( !isStructurallyValidLanguageTag( tag ) )
        throw new RangeError( "Invalid language tag: " + tag );
    return tag.replace( reFormat, bcp47Replace );
}

/**
 * @memberof module:barejs/polyfill/Intl
 * @param {String|Array} _locales
 * @return {String[]} List of canonical locale tags.
 */
exports.getCanonicalLocales = function getCanonicalLocales( _locales )
{
    var seen;

    if ( _locales === undefined )
    {
        seen = new Array( 0 );
    }
    else if ( typeof _locales === "string" )
    {
        seen = new Array( 1 );
        seen[ 0 ] = canonicalizeLanguageTag( _locales );
    }
    else
    {
        seen = [];
        var locales = ObjectPolyfill.toObject( _locales );
        var len = locales.length || 0;
        for ( var i = 0; i &lt; len; ++i )
        {
            var tag = canonicalizeLanguageTag( locales[ i ] );
            if ( seen.indexOf( tag ) &lt; 0 )
                seen.push( tag );
        }
    }

    return seen;
};

}( module.exports, require( "../Object" ) ) );
</code></pre>
        </article>
    </section>




</div>

<nav>
    <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>
</nav>

<br class="clear">

<footer>
    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)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>