Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cldrjs

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cldrjs - npm Package Compare versions

Comparing version 0.3.11 to 0.4.0

CHANGELOG.md

2

bower.json
{
"name": "cldrjs",
"version": "0.3.11",
"version": "0.4.0",
"dependencies": {

@@ -5,0 +5,0 @@ "cldr-data": ">=25"

/**
* CLDR JavaScript Library v0.3.11
* CLDR JavaScript Library v0.4.0
* http://jquery.com/

@@ -9,6 +9,6 @@ *

*
* Date: 2014-12-03T12:00Z
* Date: 2015-01-20T20:32Z
*/
/*!
* CLDR JavaScript Library v0.3.11 2014-12-03T12:00Z MIT license © Rafael Xavier
* CLDR JavaScript Library v0.4.0 2015-01-20T20:32Z MIT license © Rafael Xavier
* http://git.io/h4lmVg

@@ -32,80 +32,2 @@ */

var arrayForEach = function( array, callback ) {
var i, length;
if ( array.forEach ) {
return array.forEach( callback );
}
for ( i = 0, length = array.length; i < length; i++ ) {
callback( array[ i ], i, array );
}
};
var objectKeys = function( object ) {
var i,
result = [];
if ( Object.keys ) {
return Object.keys( object );
}
for ( i in object ) {
result.push( i );
}
return result;
};
var createError = function( code, attributes ) {
var error, message;
message = code + ( attributes && JSON ? ": " + JSON.stringify( attributes ) : "" );
error = new Error( message );
error.code = code;
// extend( error, attributes );
arrayForEach( objectKeys( attributes ), function( attribute ) {
error[ attribute ] = attributes[ attribute ];
});
return error;
};
var validate = function( code, check, attributes ) {
if ( !check ) {
throw createError( code, attributes );
}
};
var validatePresence = function( value, name ) {
validate( "E_MISSING_PARAMETER", typeof value !== "undefined", {
name: name
});
};
var validateType = function( value, name, check, expected ) {
validate( "E_INVALID_PAR_TYPE", check, {
expected: expected,
name: name,
value: value
});
};
var arrayIsArray = Array.isArray || function( obj ) {

@@ -118,33 +40,2 @@ return Object.prototype.toString.call( obj ) === "[object Array]";

var validateTypePath = function( value, name ) {
validateType( value, name, typeof value === "string" || arrayIsArray( value ), "String or Array" );
};
/**
* Function inspired by jQuery Core, but reduced to our use case.
*/
var isPlainObject = function( obj ) {
return obj !== null && "" + obj === "[object Object]";
};
var validateTypePlainObject = function( value, name ) {
validateType( value, name, typeof value === "undefined" || isPlainObject( value ), "Plain Object" );
};
var validateTypeString = function( value, name ) {
validateType( value, name, typeof value === "string", "a string" );
};
var pathNormalize = function( path, attributes ) {

@@ -320,2 +211,196 @@ if ( arrayIsArray( path ) ) {

/**
* subtags( locale )
*
* @locale [String]
*/
var coreSubtags = function( locale ) {
var aux, unicodeLanguageId,
subtags = [];
locale = locale.replace( /_/, "-" );
// Unicode locale extensions.
aux = locale.split( "-u-" );
if ( aux[ 1 ] ) {
aux[ 1 ] = aux[ 1 ].split( "-t-" );
locale = aux[ 0 ] + ( aux[ 1 ][ 1 ] ? "-t-" + aux[ 1 ][ 1 ] : "");
subtags[ 4 /* unicodeLocaleExtensions */ ] = aux[ 1 ][ 0 ];
}
// TODO normalize transformed extensions. Currently, skipped.
// subtags[ x ] = locale.split( "-t-" )[ 1 ];
unicodeLanguageId = locale.split( "-t-" )[ 0 ];
// unicode_language_id = "root"
// | unicode_language_subtag
// (sep unicode_script_subtag)?
// (sep unicode_region_subtag)?
// (sep unicode_variant_subtag)* ;
//
// Although unicode_language_subtag = alpha{2,8}, I'm using alpha{2,3}. Because, there's no language on CLDR lengthier than 3.
aux = unicodeLanguageId.match( /^(([a-z]{2,3})(-([A-Z][a-z]{3}))?(-([A-Z]{2}|[0-9]{3}))?)(-[a-zA-Z0-9]{5,8}|[0-9][a-zA-Z0-9]{3})*$|^(root)$/ );
if ( aux === null ) {
return [ "und", "Zzzz", "ZZ" ];
}
subtags[ 0 /* language */ ] = aux[ 9 ] /* root */ || aux[ 2 ] || "und";
subtags[ 1 /* script */ ] = aux[ 4 ] || "Zzzz";
subtags[ 2 /* territory */ ] = aux[ 6 ] || "ZZ";
subtags[ 3 /* variant */ ] = aux[ 7 ];
// 0: language
// 1: script
// 2: territory (aka region)
// 3: variant
// 4: unicodeLocaleExtensions
return subtags;
};
var arrayForEach = function( array, callback ) {
var i, length;
if ( array.forEach ) {
return array.forEach( callback );
}
for ( i = 0, length = array.length; i < length; i++ ) {
callback( array[ i ], i, array );
}
};
/**
* bundleLookup( minLanguageId )
*
* @Cldr [Cldr class]
*
* @cldr [Cldr instance]
*
* @minLanguageId [String] requested languageId after applied remove likely subtags.
*/
var bundleLookup = function( Cldr, cldr, minLanguageId ) {
var availableBundleMap = Cldr._availableBundleMap,
availableBundleMapQueue = Cldr._availableBundleMapQueue;
if ( availableBundleMapQueue.length ) {
arrayForEach( availableBundleMapQueue, function( bundle ) {
var existing, maxBundle, minBundle, subtags;
subtags = coreSubtags( bundle );
maxBundle = coreLikelySubtags( Cldr, cldr, subtags, { force: true } ) || subtags;
minBundle = coreRemoveLikelySubtags( Cldr, cldr, maxBundle );
minBundle = minBundle.join( Cldr.localeSep );
existing = availableBundleMapQueue[ minBundle ];
if ( existing && existing.length < bundle.length ) {
return;
}
availableBundleMap[ minBundle ] = bundle;
});
Cldr._availableBundleMapQueue = [];
}
return availableBundleMap[ minLanguageId ] || null;
};
var objectKeys = function( object ) {
var i,
result = [];
if ( Object.keys ) {
return Object.keys( object );
}
for ( i in object ) {
result.push( i );
}
return result;
};
var createError = function( code, attributes ) {
var error, message;
message = code + ( attributes && JSON ? ": " + JSON.stringify( attributes ) : "" );
error = new Error( message );
error.code = code;
// extend( error, attributes );
arrayForEach( objectKeys( attributes ), function( attribute ) {
error[ attribute ] = attributes[ attribute ];
});
return error;
};
var validate = function( code, check, attributes ) {
if ( !check ) {
throw createError( code, attributes );
}
};
var validatePresence = function( value, name ) {
validate( "E_MISSING_PARAMETER", typeof value !== "undefined", {
name: name
});
};
var validateType = function( value, name, check, expected ) {
validate( "E_INVALID_PAR_TYPE", check, {
expected: expected,
name: name,
value: value
});
};
var validateTypePath = function( value, name ) {
validateType( value, name, typeof value === "string" || arrayIsArray( value ), "String or Array" );
};
/**
* Function inspired by jQuery Core, but reduced to our use case.
*/
var isPlainObject = function( obj ) {
return obj !== null && "" + obj === "[object Object]";
};
var validateTypePlainObject = function( value, name ) {
validateType( value, name, typeof value === "undefined" || isPlainObject( value ), "Plain Object" );
};
var validateTypeString = function( value, name ) {
validateType( value, name, typeof value === "string", "a string" );
};
// @path: normalized path

@@ -339,7 +424,23 @@ var resourceGet = function( data, path ) {

var itemGetResolved = function( Cldr, path, attributes ) {
// Resolve path
var normalizedPath = pathNormalize( path, attributes );
/**
* setAvailableBundles( Cldr, json )
*
* @Cldr [Cldr class]
*
* @json resolved/unresolved cldr data.
*
* Set available bundles queue based on passed json CLDR data. Considers a bundle as any String at /main/{bundle}.
*/
var coreSetAvailableBundles = function( Cldr, json ) {
var bundle,
availableBundleMapQueue = Cldr._availableBundleMapQueue,
main = resourceGet( json, [ "main" ] );
return resourceGet( Cldr._resolved, normalizedPath );
if ( main ) {
for ( bundle in main ) {
if ( main.hasOwnProperty( bundle ) ) {
availableBundleMapQueue.push( bundle );
}
}
}
};

@@ -349,3 +450,2 @@

var alwaysArray = function( somethingOrArray ) {

@@ -399,2 +499,44 @@ return arrayIsArray( somethingOrArray ) ? somethingOrArray : [ somethingOrArray ];

/**
* load( Cldr, source, jsons )
*
* @Cldr [Cldr class]
*
* @source [Object]
*
* @jsons [arguments]
*/
var coreLoad = function( Cldr, source, jsons ) {
var i, j, json;
validatePresence( jsons[ 0 ], "json" );
// Support arbitrary parameters, e.g., `Cldr.load({...}, {...})`.
for ( i = 0; i < jsons.length; i++ ) {
// Support array parameters, e.g., `Cldr.load([{...}, {...}])`.
json = alwaysArray( jsons[ i ] );
for ( j = 0; j < json.length; j++ ) {
validateTypePlainObject( json[ j ], "json" );
source = jsonMerge( source, json[ j ] );
coreSetAvailableBundles( Cldr, json[ j ] );
}
}
return source;
};
var itemGetResolved = function( Cldr, path, attributes ) {
// Resolve path
var normalizedPath = pathNormalize( path, attributes );
return resourceGet( Cldr._resolved, normalizedPath );
};
/**
* new Cldr()

@@ -408,2 +550,3 @@ */

Cldr._alwaysArray = alwaysArray;
Cldr._coreLoad = coreLoad;
Cldr._createError = createError;

@@ -419,2 +562,4 @@ Cldr._itemGetResolved = itemGetResolved;

Cldr._availableBundleMap = {};
Cldr._availableBundleMapQueue = [];
Cldr._resolved = {};

@@ -425,20 +570,11 @@

// Load resolved cldr data
// @json [JSON]
Cldr.load = function( json ) {
var i, j;
validatePresence( json, "json" );
// Support arbitrary parameters, e.g., `Cldr.load({...}, {...})`.
for ( i = 0; i < arguments.length; i++ ) {
// Support array parameters, e.g., `Cldr.load([{...}, {...}])`.
json = alwaysArray( arguments[ i ] );
for ( j = 0; j < json.length; j++ ) {
validateTypePlainObject( json[ j ], "json" );
Cldr._resolved = jsonMerge( Cldr._resolved, json[ j ] );
}
}
/**
* Cldr.load( json [, json, ...] )
*
* @json [JSON] CLDR data or [Array] Array of @json's.
*
* Load resolved cldr data.
*/
Cldr.load = function() {
Cldr._resolved = coreLoad( Cldr, Cldr._resolved, arguments );
};

@@ -450,3 +586,3 @@

Cldr.prototype.init = function( locale ) {
var attributes, aux, language, languageId, maxLanguageId, script, territory, unicodeLanguageId, unicodeLocaleExtensions, variant,
var attributes, language, maxLanguageId, minLanguageId, script, subtags, territory, unicodeLocaleExtensions, variant,
sep = Cldr.localeSep;

@@ -457,2 +593,7 @@

subtags = coreSubtags( locale );
unicodeLocaleExtensions = subtags[ 4 ];
variant = subtags[ 3 ];
// Normalize locale code.

@@ -466,70 +607,16 @@ // Get (or deduce) the "triple subtags": language, territory (also aliased as region), and script subtags.

locale = locale.replace( /_/, "-" );
// Unicode locale extensions.
aux = locale.split( "-u-" );
locale = aux[ 0 ];
unicodeLocaleExtensions = aux[ 1 ];
// TODO normalize transformed extensions. Currently, skipped.
// transformedExtensions = locale.split( "-t-" )[ 1 ];
locale = locale.split( "-t-" )[ 0 ];
unicodeLanguageId = locale;
// unicodeLanguageId = ...
switch ( true ) {
// language_script_territory..
case /^[a-z]{2,3}-[A-Z][a-z]{3}-[A-Z0-9]{2,3}(\b|-)/.test( unicodeLanguageId ):
language = unicodeLanguageId.split( "-" )[ 0 ];
script = unicodeLanguageId.split( "-" )[ 1 ];
territory = unicodeLanguageId.split( "-" )[ 2 ];
variant = unicodeLanguageId.split( "-" )[ 3 ];
break;
// language_script..
case /^[a-z]{2,3}-[A-Z][a-z]{3}(\b|-)/.test( unicodeLanguageId ):
language = unicodeLanguageId.split( "-" )[ 0 ];
script = unicodeLanguageId.split( "-" )[ 1 ];
territory = "ZZ";
variant = unicodeLanguageId.split( "-" )[ 2 ];
break;
// language_territory..
case /^[a-z]{2,3}-[A-Z0-9]{2,3}(\b|-)/.test( unicodeLanguageId ):
language = unicodeLanguageId.split( "-" )[ 0 ];
script = "Zzzz";
territory = unicodeLanguageId.split( "-" )[ 1 ];
variant = unicodeLanguageId.split( "-" )[ 2 ];
break;
// language.., or root
case /^([a-z]{2,3}|root)(\b|-)/.test( unicodeLanguageId ):
language = unicodeLanguageId.split( "-" )[ 0 ];
script = "Zzzz";
territory = "ZZ";
variant = unicodeLanguageId.split( "-" )[ 1 ];
break;
default:
language = "und";
script = "Zzzz";
territory = "ZZ";
break;
}
// When a locale id does not specify a language, or territory (region), or script, they are obtained by Likely Subtags.
maxLanguageId = coreLikelySubtags( Cldr, this, [ language, script, territory ], { force: true } ) || unicodeLanguageId.split( "-" );
maxLanguageId = coreLikelySubtags( Cldr, this, subtags, { force: true } ) || subtags;
language = maxLanguageId[ 0 ];
script = maxLanguageId[ 1 ];
territory = maxLanguageId[ 2 ];
territory = maxLanguageId[ 2 ];
languageId = coreRemoveLikelySubtags( Cldr, this, maxLanguageId ).join( sep );
minLanguageId = coreRemoveLikelySubtags( Cldr, this, maxLanguageId ).join( sep );
// Set attributes
this.attributes = attributes = {
bundle: bundleLookup( Cldr, this, minLanguageId ),
// Unicode Language Id
languageId: languageId,
minlanguageId: minLanguageId,
maxLanguageId: maxLanguageId.join( sep ),

@@ -559,3 +646,3 @@

this.locale = variant ? [ languageId, variant ].join( sep ) : languageId;
this.locale = locale;
};

@@ -581,4 +668,8 @@

validate( "E_MISSING_BUNDLE", this.attributes.bundle !== null, {
locale: this.locale
});
path = alwaysArray( path );
return this.get( [ "main/{languageId}" ].concat( path ) );
return this.get( [ "main/{bundle}" ].concat( path ) );
};

@@ -585,0 +676,0 @@

/**
* CLDR JavaScript Library v0.3.11
* CLDR JavaScript Library v0.4.0
* http://jquery.com/

@@ -9,6 +9,6 @@ *

*
* Date: 2014-12-03T12:00Z
* Date: 2015-01-20T20:32Z
*/
/*!
* CLDR JavaScript Library v0.3.11 2014-12-03T12:00Z MIT license © Rafael Xavier
* CLDR JavaScript Library v0.4.0 2015-01-20T20:32Z MIT license © Rafael Xavier
* http://git.io/h4lmVg

@@ -15,0 +15,0 @@ */

/**
* CLDR JavaScript Library v0.3.11
* CLDR JavaScript Library v0.4.0
* http://jquery.com/

@@ -9,6 +9,6 @@ *

*
* Date: 2014-12-03T12:00Z
* Date: 2015-01-20T20:32Z
*/
/*!
* CLDR JavaScript Library v0.3.11 2014-12-03T12:00Z MIT license © Rafael Xavier
* CLDR JavaScript Library v0.4.0 2015-01-20T20:32Z MIT license © Rafael Xavier
* http://git.io/h4lmVg

@@ -15,0 +15,0 @@ */

/**
* CLDR JavaScript Library v0.3.11
* CLDR JavaScript Library v0.4.0
* http://jquery.com/

@@ -9,6 +9,6 @@ *

*
* Date: 2014-12-03T12:00Z
* Date: 2015-01-20T20:32Z
*/
/*!
* CLDR JavaScript Library v0.3.11 2014-12-03T12:00Z MIT license © Rafael Xavier
* CLDR JavaScript Library v0.4.0 2015-01-20T20:32Z MIT license © Rafael Xavier
* http://git.io/h4lmVg

@@ -32,3 +32,3 @@ */

// Build optimization hack to avoid duplicating functions across modules.
var alwaysArray = Cldr._alwaysArray;
var coreLoad = Cldr._coreLoad;
var jsonMerge = Cldr._jsonMerge;

@@ -39,3 +39,2 @@ var pathNormalize = Cldr._pathNormalize;

var validateTypePath = Cldr._validateTypePath;
var validateTypePlainObject = Cldr._validateTypePlainObject;

@@ -116,3 +115,3 @@

parent = bundleParentLookup( Cldr, locale );
value = lookup( Cldr, parent, path, jsonMerge( attributes, { languageId: parent }), locale );
value = lookup( Cldr, parent, path, jsonMerge( attributes, { bundle: parent }), locale );
}

@@ -136,23 +135,11 @@

/**
* Load resolved or unresolved cldr data
* @json [JSON]
*
* Cldr.load( json [, json, ...] )
*
* @json [JSON] CLDR data or [Array] Array of @json's.
*
* Load resolved or unresolved cldr data.
* Overwrite Cldr.load().
*/
Cldr.load = function( json ) {
var i, j;
validatePresence( json, "json" );
// Support arbitrary parameters, e.g., `Cldr.load({...}, {...})`.
for ( i = 0; i < arguments.length; i++ ) {
// Support array parameters, e.g., `Cldr.load([{...}, {...}])`.
json = alwaysArray( arguments[ i ] );
for ( j = 0; j < json.length; j++ ) {
validateTypePlainObject( json[ j ], "json" );
Cldr._raw = jsonMerge( Cldr._raw, json[ j ] );
}
}
Cldr.load = function() {
Cldr._raw = coreLoad( Cldr, Cldr._raw, arguments );
};

@@ -167,5 +154,5 @@

// 1: use languageId as locale on item lookup for simplification purposes, because no other extended subtag is used anyway on bundle parent lookup.
// 2: during init(), this method is called, but languageId is yet not defined. Use "" as a workaround in this very specific scenario.
return itemLookup( Cldr, this.attributes && this.attributes.languageId /* 1 */ || "" /* 2 */, path, this.attributes );
// 1: use bundle as locale on item lookup for simplification purposes, because no other extended subtag is used anyway on bundle parent lookup.
// 2: during init(), this method is called, but bundle is yet not defined. Use "" as a workaround in this very specific scenario.
return itemLookup( Cldr, this.attributes && this.attributes.bundle /* 1 */ || "" /* 2 */, path, this.attributes );
};

@@ -172,0 +159,0 @@

/**
* CLDR JavaScript Library v0.3.11
* CLDR JavaScript Library v0.4.0
* http://jquery.com/

@@ -9,6 +9,6 @@ *

*
* Date: 2014-12-03T12:00Z
* Date: 2015-01-20T20:32Z
*/
/*!
* CLDR JavaScript Library v0.3.11 2014-12-03T12:00Z MIT license © Rafael Xavier
* CLDR JavaScript Library v0.4.0 2015-01-20T20:32Z MIT license © Rafael Xavier
* http://git.io/h4lmVg

@@ -15,0 +15,0 @@ */

{
"name": "cldrjs",
"version": "0.3.11",
"version": "0.4.0",
"description": "Simple CLDR API",

@@ -5,0 +5,0 @@ "keywords": [

@@ -1,8 +0,8 @@

# cldr.js - Simple CLDR API
# cldr.js - Simple CLDR traverser
[CLDR (unicode.org)](http://cldr.unicode.org/) provides locale content for I18n software. The data is provided in two formats: LDML (XML format), and JSON. Our goal is to provide a simple layer to facilitate I18n softwares to access and use the [official CLDR JSON data](http://cldr.unicode.org/index/cldr-spec/json).
[CLDR (unicode.org)](http://cldr.unicode.org/) provides locale content for i18n software. The data is provided in two formats: LDML (XML format) and JSON. Our goal is to provide a simple layer to facilitate i18n software to access and use the [official CLDR JSON data](http://cldr.unicode.org/index/cldr-spec/json).
| File | Minified + gzipped size | Summary |
|---|--:|---|
| cldr.js | 1.7KB | Core library |
| cldr.js | 2.1KB | Core library |
| cldr/event.js | +1.4KB | Provides methods to allow listening to events, eg. `get` |

@@ -32,6 +32,10 @@ | cldr/supplemental.js | +0.5KB | Provides supplemental helper methods |

It's designed to work both in the [browser](#usage-and-installation), or in [node.js](#commonjs--nodejs). It supports [AMD](#usage-and-installation), and [CommonJs](#usage-and-installation);
It's designed to work both in the [browser](#usage-and-installation) and in [Node.js](#commonjs--nodejs). It supports [AMD](#usage-and-installation) and [CommonJs](#usage-and-installation);
See [Usage and installation](#usage-and-installation).
### What changed from 0.3.x to 0.4.x?
See our [changelogs](./CHANGELOG.md).
### Load only the CLDR portion you need

@@ -56,3 +60,4 @@

// {
// "languageId": "en",
// "bundle": "en",
// "minLanguageId": "en",
// "maxLanguageId": "en-Latn-US",

@@ -68,3 +73,4 @@ // "language": "en",

// {
// "languageId": "zh",
// "bundle": "zh-Hant",
// "minLanguageId": "zh",
// "maxLanguageId": "zh-Hans-CN",

@@ -81,4 +87,4 @@ // "language": "zh",

- `language`, `script`, `territory` (also aliased as `region`), and `maxLanguageId` are computed by [adding likely subtags](./src/likely-subtags.js) according to the [specification](http://www.unicode.org/reports/tr35/#Likely_Subtags).
- `languageId` is always in the succinct form, obtained by [removing the likely subtags from `maxLanguageId`](./src/remove-likely-subtags.js) according to the [specification](http://www.unicode.org/reports/tr35/#Likely_Subtags).
- `language`, `script`, `territory` (also aliased as `region`), `maxLanguageId` (computed by [adding likely subtags](./src/core/likely_subtags.js)) and `minLanguageId` (computed by [removing likely subtags](./src/core/remove_likely_subtags.js)) according to the [specification](http://www.unicode.org/reports/tr35/#Likely_Subtags).
- `bundle` holds the bundle lookup match based on the available loaded CLDR data, obtained by following [Bundle Lookup Matcher][].
- [Unicode locale extensions](http://www.unicode.org/reports/tr35/#u_Extension).

@@ -88,3 +94,3 @@

| locale | languageId | maxLanguageId | language | script | region |
| locale | minLanguageId | maxLanguageId | language | script | region |
| --- | --- | --- | --- | --- | --- |

@@ -109,3 +115,3 @@ | **en** | `"en"` | `"en-Latn-US"` | `"en"` | `"Latn"` | `"US"` |

// Equivalent to:
// .get( "main/{languageId}/numbers/symbols-numberSystem-latn/decimal" );
// .get( "main/{bundle}/numbers/symbols-numberSystem-latn/decimal" );

@@ -115,6 +121,6 @@ ptBr.main( "numbers/symbols-numberSystem-latn/decimal" );

// Equivalent to:
// .get( "main/{languageId}/numbers/symbols-numberSystem-latn/decimal" );
// .get( "main/{bundle}/numbers/symbols-numberSystem-latn/decimal" );
```
Have any [locale attributes](#cldrattributes) replaced with their corresponding values by embracing it with `{}`. In the example below, `{language}` is replaced with `"en"`, and `{territory}` with `"US"`.
Have any [locale attributes](#cldrattributes) replaced with their corresponding values by embracing it with `{}`. In the example below, `{language}` is replaced with `"en"` and `{territory}` with `"US"`.

@@ -237,3 +243,3 @@ ```javascript

// Equivalent to:
// .get( "main/{languageId}/numbers/symbols-numberSystem-latn/decimal" );
// .get( "main/{bundle}/numbers/symbols-numberSystem-latn/decimal" );
```

@@ -277,3 +283,3 @@

You can choose to generate unresolved data to save space or bandwidth (`-r false` option of the conversion tool), and instead have it resolve at runtime.
You can choose to generate unresolved data to save space or bandwidth (`-r false` option of the conversion tool) and instead have it resolve at runtime.

@@ -330,5 +336,5 @@ For the examples below, first fetch CLDR JSON data:

#### Atention: library owners, do not embed data
#### Attention: library owners, do not embed data
It's NOT recommended that libraries embed data into its code logic for some reasons: avoid forcing a certain data version on users, avoid maintaining locale changes, avoid duplicating data among different i18n libraries.
It's NOT recommended that libraries embed data into their code logic for several reasons: avoid forcing a certain data version on users, avoid maintaining locale changes, avoid duplicating data among different i18n libraries.

@@ -369,3 +375,3 @@ We recommend loading CLDR data must be performed by end user code.

Attributes is an Object created during instance initialization (construction), and are used internally by `.get()` to replace dynamic parts of an item path.
Attributes is an Object created during instance initialization (construction) and are used internally by `.get()` to replace dynamic parts of an item path.

@@ -382,3 +388,3 @@ [Read more...](doc/api/core/attributes.md)

It's an alias for `.get([ "main/{languageId}", ... ])`.
It's an alias for `.get([ "main/{bundle}", ... ])`.

@@ -473,4 +479,19 @@ [Read more...](doc/api/core/main.md)

### Error reference
## Error reference
### CLDR Errors
#### `E_MISSING_BUNDLE`
Thrown when none of the loaded CLDR data can be used as a bundle for the corresponding locale. See more information on [Bundle Lookup Matcher][].
Error object:
| Attribute | Value |
| --- | --- |
| code | `E_MISSING_BUNDLE` |
| locale | Locale whose bundle could not be found |
### Parameter Errors
#### `E_MISSING_PARAMETER`

@@ -517,4 +538,6 @@

[Bundle Lookup Matcher]: ./doc/bundle_lookup_matcher.md
## License
MIT © [Rafael Xavier de Souza](http://rafael.xavier.blog.br)

@@ -31,3 +31,3 @@ /**

// Build optimization hack to avoid duplicating functions across modules.
var alwaysArray = Cldr._alwaysArray;
var coreLoad = Cldr._coreLoad;
var jsonMerge = Cldr._jsonMerge;

@@ -38,3 +38,2 @@ var pathNormalize = Cldr._pathNormalize;

var validateTypePath = Cldr._validateTypePath;
var validateTypePlainObject = Cldr._validateTypePlainObject;
define([
"./bundle/lookup",
"./common/create_error",
"./common/validate",
"./common/validate/presence",

@@ -9,3 +11,5 @@ "./common/validate/type",

"./core/likely_subtags",
"./core/load",
"./core/remove_likely_subtags",
"./core/subtags",
"./item/get_resolved",

@@ -16,4 +20,3 @@ "./path/normalize",

"./util/json/merge"
], function( createError, validatePresence, validateType, validateTypePath,
validateTypePlainObject, validateTypeString, coreLikelySubtags, coreRemoveLikelySubtags, itemGetResolved, pathNormalize, resourceGet, alwaysArray, jsonMerge ) {
], function( bundleLookup, createError, validate, validatePresence, validateType, validateTypePath, validateTypePlainObject, validateTypeString, coreLikelySubtags, coreLoad, coreRemoveLikelySubtags, coreSubtags, itemGetResolved, pathNormalize, resourceGet, alwaysArray, jsonMerge ) {

@@ -29,2 +32,3 @@ /**

Cldr._alwaysArray = alwaysArray;
Cldr._coreLoad = coreLoad;
Cldr._createError = createError;

@@ -40,2 +44,4 @@ Cldr._itemGetResolved = itemGetResolved;

Cldr._availableBundleMap = {};
Cldr._availableBundleMapQueue = [];
Cldr._resolved = {};

@@ -46,20 +52,11 @@

// Load resolved cldr data
// @json [JSON]
Cldr.load = function( json ) {
var i, j;
validatePresence( json, "json" );
// Support arbitrary parameters, e.g., `Cldr.load({...}, {...})`.
for ( i = 0; i < arguments.length; i++ ) {
// Support array parameters, e.g., `Cldr.load([{...}, {...}])`.
json = alwaysArray( arguments[ i ] );
for ( j = 0; j < json.length; j++ ) {
validateTypePlainObject( json[ j ], "json" );
Cldr._resolved = jsonMerge( Cldr._resolved, json[ j ] );
}
}
/**
* Cldr.load( json [, json, ...] )
*
* @json [JSON] CLDR data or [Array] Array of @json's.
*
* Load resolved cldr data.
*/
Cldr.load = function() {
Cldr._resolved = coreLoad( Cldr, Cldr._resolved, arguments );
};

@@ -71,3 +68,3 @@

Cldr.prototype.init = function( locale ) {
var attributes, aux, language, languageId, maxLanguageId, script, territory, unicodeLanguageId, unicodeLocaleExtensions, variant,
var attributes, language, maxLanguageId, minLanguageId, script, subtags, territory, unicodeLocaleExtensions, variant,
sep = Cldr.localeSep;

@@ -78,2 +75,7 @@

subtags = coreSubtags( locale );
unicodeLocaleExtensions = subtags[ 4 ];
variant = subtags[ 3 ];
// Normalize locale code.

@@ -87,70 +89,16 @@ // Get (or deduce) the "triple subtags": language, territory (also aliased as region), and script subtags.

locale = locale.replace( /_/, "-" );
// Unicode locale extensions.
aux = locale.split( "-u-" );
locale = aux[ 0 ];
unicodeLocaleExtensions = aux[ 1 ];
// TODO normalize transformed extensions. Currently, skipped.
// transformedExtensions = locale.split( "-t-" )[ 1 ];
locale = locale.split( "-t-" )[ 0 ];
unicodeLanguageId = locale;
// unicodeLanguageId = ...
switch ( true ) {
// language_script_territory..
case /^[a-z]{2,3}-[A-Z][a-z]{3}-[A-Z0-9]{2,3}(\b|-)/.test( unicodeLanguageId ):
language = unicodeLanguageId.split( "-" )[ 0 ];
script = unicodeLanguageId.split( "-" )[ 1 ];
territory = unicodeLanguageId.split( "-" )[ 2 ];
variant = unicodeLanguageId.split( "-" )[ 3 ];
break;
// language_script..
case /^[a-z]{2,3}-[A-Z][a-z]{3}(\b|-)/.test( unicodeLanguageId ):
language = unicodeLanguageId.split( "-" )[ 0 ];
script = unicodeLanguageId.split( "-" )[ 1 ];
territory = "ZZ";
variant = unicodeLanguageId.split( "-" )[ 2 ];
break;
// language_territory..
case /^[a-z]{2,3}-[A-Z0-9]{2,3}(\b|-)/.test( unicodeLanguageId ):
language = unicodeLanguageId.split( "-" )[ 0 ];
script = "Zzzz";
territory = unicodeLanguageId.split( "-" )[ 1 ];
variant = unicodeLanguageId.split( "-" )[ 2 ];
break;
// language.., or root
case /^([a-z]{2,3}|root)(\b|-)/.test( unicodeLanguageId ):
language = unicodeLanguageId.split( "-" )[ 0 ];
script = "Zzzz";
territory = "ZZ";
variant = unicodeLanguageId.split( "-" )[ 1 ];
break;
default:
language = "und";
script = "Zzzz";
territory = "ZZ";
break;
}
// When a locale id does not specify a language, or territory (region), or script, they are obtained by Likely Subtags.
maxLanguageId = coreLikelySubtags( Cldr, this, [ language, script, territory ], { force: true } ) || unicodeLanguageId.split( "-" );
maxLanguageId = coreLikelySubtags( Cldr, this, subtags, { force: true } ) || subtags;
language = maxLanguageId[ 0 ];
script = maxLanguageId[ 1 ];
territory = maxLanguageId[ 2 ];
territory = maxLanguageId[ 2 ];
languageId = coreRemoveLikelySubtags( Cldr, this, maxLanguageId ).join( sep );
minLanguageId = coreRemoveLikelySubtags( Cldr, this, maxLanguageId ).join( sep );
// Set attributes
this.attributes = attributes = {
bundle: bundleLookup( Cldr, this, minLanguageId ),
// Unicode Language Id
languageId: languageId,
minlanguageId: minLanguageId,
maxLanguageId: maxLanguageId.join( sep ),

@@ -180,3 +128,3 @@

this.locale = variant ? [ languageId, variant ].join( sep ) : languageId;
this.locale = locale;
};

@@ -202,4 +150,8 @@

validate( "E_MISSING_BUNDLE", this.attributes.bundle !== null, {
locale: this.locale
});
path = alwaysArray( path );
return this.get( [ "main/{languageId}" ].concat( path ) );
return this.get( [ "main/{bundle}" ].concat( path ) );
};

@@ -206,0 +158,0 @@

@@ -37,3 +37,3 @@ define([

parent = bundleParentLookup( Cldr, locale );
value = lookup( Cldr, parent, path, jsonMerge( attributes, { languageId: parent }), locale );
value = lookup( Cldr, parent, path, jsonMerge( attributes, { bundle: parent }), locale );
}

@@ -40,0 +40,0 @@

define([
"./common/validate/presence",
"./common/validate/type/path",
"./common/validate/type/plain_object",
"./core",
"./item/lookup",
"./util/always_array",
"./util/json/merge"
], function( validatePresence, validateTypePath, validateTypePlainObject, Cldr, itemLookup, alwaysArray, jsonMerge ) {
"./core/load",
"./item/lookup"
], function( validatePresence, validateTypePath, Cldr, coreLoad, itemLookup ) {

@@ -14,23 +12,11 @@ Cldr._raw = {};

/**
* Load resolved or unresolved cldr data
* @json [JSON]
*
* Cldr.load( json [, json, ...] )
*
* @json [JSON] CLDR data or [Array] Array of @json's.
*
* Load resolved or unresolved cldr data.
* Overwrite Cldr.load().
*/
Cldr.load = function( json ) {
var i, j;
validatePresence( json, "json" );
// Support arbitrary parameters, e.g., `Cldr.load({...}, {...})`.
for ( i = 0; i < arguments.length; i++ ) {
// Support array parameters, e.g., `Cldr.load([{...}, {...}])`.
json = alwaysArray( arguments[ i ] );
for ( j = 0; j < json.length; j++ ) {
validateTypePlainObject( json[ j ], "json" );
Cldr._raw = jsonMerge( Cldr._raw, json[ j ] );
}
}
Cldr.load = function() {
Cldr._raw = coreLoad( Cldr, Cldr._raw, arguments );
};

@@ -45,5 +31,5 @@

// 1: use languageId as locale on item lookup for simplification purposes, because no other extended subtag is used anyway on bundle parent lookup.
// 2: during init(), this method is called, but languageId is yet not defined. Use "" as a workaround in this very specific scenario.
return itemLookup( Cldr, this.attributes && this.attributes.languageId /* 1 */ || "" /* 2 */, path, this.attributes );
// 1: use bundle as locale on item lookup for simplification purposes, because no other extended subtag is used anyway on bundle parent lookup.
// 2: during init(), this method is called, but bundle is yet not defined. Use "" as a workaround in this very specific scenario.
return itemLookup( Cldr, this.attributes && this.attributes.bundle /* 1 */ || "" /* 2 */, path, this.attributes );
};

@@ -50,0 +36,0 @@

@@ -99,4 +99,12 @@ define([

it( "should throw error on missing bundle", function() {
var cldr = new Cldr( "sr-RS" );
expect( cldr.attributes.bundle ).to.be.null;
expect(function() {
cldr.main( "numbers" );
}).to.throw( Error, /E_MISSING_BUNDLE/ );
});
});
});

@@ -19,9 +19,16 @@ require.config({

// 2nd unit tests group.
"./unit/core/load",
"./unit/core/set_available_bundles",
// 3rd unit tests group.
"./unit/core",
"./unit/core/likely_subtags",
"./unit/core/remove_likely_subtags",
"./unit/supplemental"
"./unit/supplemental",
// 4th unit tests group.
"./unit/bundle/lookup"
], function() {
mocha.run();
});

@@ -7,6 +7,8 @@ define([

Cldr.load( parentLocalesJson );
describe( "Bundle Parent Lookup", function() {
before(function() {
Cldr.load( parentLocalesJson );
});
it( "should truncate locale", function() {

@@ -13,0 +15,0 @@ expect( parentLookup( Cldr, [ "pt", "BR" ].join( Cldr.localeSep ) ) ).to.equal( "pt" );

@@ -7,54 +7,67 @@ define([

Cldr.load( enNumbersJson, likelySubtagsJson );
describe( "Cldr (core)", function() {
var cldr;
before(function() {
Cldr.load( enNumbersJson, likelySubtagsJson );
});
it( "should normalize a locale on initialization", function() {
cldr = new Cldr( "pt-BR" );
expect( cldr.attributes.language ).to.equal( "pt" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "BR" );
describe( "Cldr.load()", function() {
cldr = new Cldr( "en" );
expect( cldr.attributes.language ).to.equal( "en" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "US" );
it( "should load json", function() {
expect( Cldr._resolved && Cldr._resolved.supplemental ).to.be.ok;
});
cldr = new Cldr( "en-GB" );
expect( cldr.attributes.language ).to.equal( "en" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "GB" );
});
cldr = new Cldr( "lkt" );
expect( cldr.attributes.language ).to.equal( "lkt" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "US" );
describe( "Constructor", function() {
var cldr;
cldr = new Cldr( "root" );
expect( cldr.attributes.language ).to.equal( "en" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "US" );
it( "should normalize a locale on initialization", function() {
cldr = new Cldr( "pt-BR" );
expect( cldr.attributes.language ).to.equal( "pt" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "BR" );
// Should not identify nonExistent as a language subtag, due to its length. So, it should become `und_Zzzz_ZZ`. Then, the `und` likelySubtags value.
cldr = new Cldr( "nonExistent" );
expect( cldr.attributes.language ).to.equal( "en" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "US" );
});
cldr = new Cldr( "en" );
expect( cldr.attributes.language ).to.equal( "en" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "US" );
it( "should set unicode locale extensions attributes", function() {
cldr = new Cldr( "en-u-cu-usd" );
expect( cldr.attributes[ "u-cu" ] ).to.equal( "usd" );
cldr = new Cldr( "en-GB" );
expect( cldr.attributes.language ).to.equal( "en" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "GB" );
cldr = new Cldr( "en-u-foo-bar-nu-arab-cu-usd" );
expect( cldr.attributes[ "u-foo" ] ).to.be.true;
expect( cldr.attributes[ "u-bar" ] ).to.be.true;
expect( cldr.attributes[ "u-cu" ] ).to.equal( "usd" );
expect( cldr.attributes[ "u-nu" ] ).to.equal( "arab" );
});
cldr = new Cldr( "lkt" );
expect( cldr.attributes.language ).to.equal( "lkt" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "US" );
it( "should implement cldr.main as an alias of get( \"main/{languageId}...\" )", function() {
cldr = new Cldr( "en" );
expect( cldr.main( "numbers/symbols-numberSystem-latn/decimal" ) ).to.equal( "." );
cldr = new Cldr( "root" );
expect( cldr.attributes.language ).to.equal( "en" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "US" );
// Should not identify nonExistent as a language subtag, due to its length. So, it should become `und_Zzzz_ZZ`. Then, the `und` likelySubtags value.
cldr = new Cldr( "nonExistent" );
expect( cldr.attributes.language ).to.equal( "en" );
expect( cldr.attributes.script ).to.equal( "Latn" );
expect( cldr.attributes.territory ).to.equal( "US" );
});
it( "should set unicode locale extensions attributes", function() {
cldr = new Cldr( "en-u-cu-usd" );
expect( cldr.attributes[ "u-cu" ] ).to.equal( "usd" );
cldr = new Cldr( "en-u-foo-bar-nu-arab-cu-usd" );
expect( cldr.attributes[ "u-foo" ] ).to.be.true;
expect( cldr.attributes[ "u-bar" ] ).to.be.true;
expect( cldr.attributes[ "u-cu" ] ).to.equal( "usd" );
expect( cldr.attributes[ "u-nu" ] ).to.equal( "arab" );
});
it( "should implement cldr.main as an alias of get( \"main/{bundle}...\" )", function() {
cldr = new Cldr( "en" );
expect( cldr.main( "numbers/symbols-numberSystem-latn/decimal" ) ).to.equal( "." );
});
});

@@ -61,0 +74,0 @@

@@ -7,7 +7,9 @@ define([

Cldr.load( likelySubtagsJson );
describe( "Likely Subtags", function() {
var cldr = new Cldr( "root" );
before(function() {
Cldr.load( likelySubtagsJson );
});
it( "should skip empty language tag", function() {

@@ -14,0 +16,0 @@ expect( likelySubtags( Cldr, cldr, [ "en", "Latn", "US" ] ) ).to.eql( [ "en", "Latn", "US" ] );

@@ -7,8 +7,9 @@ define([

Cldr.load( likelySubtagsJson );
describe( "Remove Likely Subtags", function() {
var cldr = new Cldr( "root" );
before(function() {
Cldr.load( likelySubtagsJson );
});
it( "Should reduce \"en_Latn_US\" into \"en\"", function() {

@@ -15,0 +16,0 @@ expect( removeLikelySubtags( Cldr, cldr, [ "en", "Latn", "US" ] ) ).to.eql( [ "en" ] );

@@ -10,16 +10,18 @@ define([

Cldr.load(
genderJson,
likelySubtagsJson,
ptNumbersJson,
{
"lookup-test": {
a: 1,
b: 2
}
}
);
describe( "Item Lookup", function() {
before(function() {
Cldr.load(
genderJson,
likelySubtagsJson,
ptNumbersJson,
{
"lookup-test": {
a: 1,
b: 2
}
}
);
});
it( "should get resolved items", function() {

@@ -32,3 +34,3 @@ var cldr = new Cldr( "root" );

var cldr = new Cldr( "pt_BR" );
expect( itemLookup( Cldr, cldr.locale, "/main/{languageId}/numbers/symbols-numberSystem-latn/decimal", cldr.attributes ) ).to.equal( "," );
expect( itemLookup( Cldr, cldr.locale, "/main/{bundle}/numbers/symbols-numberSystem-latn/decimal", cldr.attributes ) ).to.equal( "," );
});

@@ -35,0 +37,0 @@

@@ -9,10 +9,13 @@ define([

Cldr.load(
likelySubtagsJson,
timeDataJson,
weekDataJson
);
describe( "Supplemental", function() {
var en, enGb, fr, ptBr, ty;
describe( "Supplemental", function() {
var en = new Cldr( "en" ),
before(function() {
Cldr.load(
likelySubtagsJson,
timeDataJson,
weekDataJson
);
en = new Cldr( "en" ),
enGb = new Cldr( "en_GB" ),

@@ -23,7 +26,8 @@ fr = new Cldr( "fr" ),

en.supplemental = supplemental( en );
enGb.supplemental = supplemental( enGb );
fr.supplemental = supplemental( fr );
ptBr.supplemental = supplemental( ptBr );
ty.supplemental = supplemental( ty );
en.supplemental = supplemental( en );
enGb.supplemental = supplemental( enGb );
fr.supplemental = supplemental( fr );
ptBr.supplemental = supplemental( ptBr );
ty.supplemental = supplemental( ty );
});

@@ -30,0 +34,0 @@ it( "should get weekData.firstDay", function() {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc