Skip to content

Commit ab8efdd

Browse files
committed
Build
1 parent e7e0850 commit ab8efdd

File tree

7 files changed

+6017
-0
lines changed

7 files changed

+6017
-0
lines changed

dist/globalize.js

Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
/**
2+
* Globalize v1.0.0-alpha.17
3+
*
4+
* http://github.com/jquery/globalize
5+
*
6+
* Copyright 2010, 2014 jQuery Foundation, Inc. and other contributors
7+
* Released under the MIT license
8+
* http://jquery.org/license
9+
*
10+
* Date: 2015-02-21T22:13Z
11+
*/
12+
/*!
13+
* Globalize v1.0.0-alpha.17 2015-02-21T22:13Z Released under the MIT license
14+
* http://git.io/TrdQbw
15+
*/
16+
(function( root, factory ) {
17+
18+
// UMD returnExports
19+
if ( typeof define === "function" && define.amd ) {
20+
21+
// AMD
22+
define([
23+
"cldr",
24+
"cldr/event"
25+
], factory );
26+
} else if ( typeof exports === "object" ) {
27+
28+
// Node, CommonJS
29+
module.exports = factory( require( "cldrjs" ) );
30+
} else {
31+
32+
// Global
33+
root.Globalize = factory( root.Cldr );
34+
}
35+
}( this, function( Cldr ) {
36+
37+
38+
/**
39+
* A toString method that outputs meaningful values for objects or arrays and
40+
* still performs as fast as a plain string in case variable is string, or as
41+
* fast as `"" + number` in case variable is a number.
42+
* Ref: http://jsperf.com/my-stringify
43+
*/
44+
var toString = function( variable ) {
45+
return typeof variable === "string" ? variable : ( typeof variable === "number" ? "" +
46+
variable : JSON.stringify( variable ) );
47+
};
48+
49+
50+
51+
52+
/**
53+
* formatMessage( message, data )
54+
*
55+
* @message [String] A message with optional {vars} to be replaced.
56+
*
57+
* @data [Array or JSON] Object with replacing-variables content.
58+
*
59+
* Return the formatted message. For example:
60+
*
61+
* - formatMessage( "{0} second", [ 1 ] ); // 1 second
62+
*
63+
* - formatMessage( "{0}/{1}", ["m", "s"] ); // m/s
64+
*
65+
* - formatMessage( "{name} <{email}>", {
66+
* name: "Foo",
67+
* email: "bar@baz.qux"
68+
* }); // Foo <bar@baz.qux>
69+
*/
70+
var formatMessage = function( message, data ) {
71+
72+
// Replace {attribute}'s
73+
message = message.replace( /{[0-9a-zA-Z-_. ]+}/g, function( name ) {
74+
name = name.replace( /^{([^}]*)}$/, "$1" );
75+
return toString( data[ name ] );
76+
});
77+
78+
return message;
79+
};
80+
81+
82+
83+
84+
var objectExtend = function() {
85+
var destination = arguments[ 0 ],
86+
sources = [].slice.call( arguments, 1 );
87+
88+
sources.forEach(function( source ) {
89+
var prop;
90+
for ( prop in source ) {
91+
destination[ prop ] = source[ prop ];
92+
}
93+
});
94+
95+
return destination;
96+
};
97+
98+
99+
100+
101+
var createError = function( code, message, attributes ) {
102+
var error;
103+
104+
message = code + ( message ? ": " + formatMessage( message, attributes ) : "" );
105+
error = new Error( message );
106+
error.code = code;
107+
108+
objectExtend( error, attributes );
109+
110+
return error;
111+
};
112+
113+
114+
115+
116+
var validate = function( code, message, check, attributes ) {
117+
if ( !check ) {
118+
throw createError( code, message, attributes );
119+
}
120+
};
121+
122+
123+
124+
125+
var alwaysArray = function( stringOrArray ) {
126+
return Array.isArray( stringOrArray ) ? stringOrArray : stringOrArray ? [ stringOrArray ] : [];
127+
};
128+
129+
130+
131+
132+
var validateCldr = function( path, value, options ) {
133+
var skipBoolean;
134+
options = options || {};
135+
136+
skipBoolean = alwaysArray( options.skip ).some(function( pathRe ) {
137+
return pathRe.test( path );
138+
});
139+
140+
validate( "E_MISSING_CLDR", "Missing required CLDR content `{path}`.", value || skipBoolean, {
141+
path: path
142+
});
143+
};
144+
145+
146+
147+
148+
var validateDefaultLocale = function( value ) {
149+
validate( "E_DEFAULT_LOCALE_NOT_DEFINED", "Default locale has not been defined.",
150+
value !== undefined, {} );
151+
};
152+
153+
154+
155+
156+
var validateParameterPresence = function( value, name ) {
157+
validate( "E_MISSING_PARAMETER", "Missing required parameter `{name}`.",
158+
value !== undefined, { name: name });
159+
};
160+
161+
162+
163+
164+
/**
165+
* range( value, name, minimum, maximum )
166+
*
167+
* @value [Number].
168+
*
169+
* @name [String] name of variable.
170+
*
171+
* @minimum [Number]. The lowest valid value, inclusive.
172+
*
173+
* @maximum [Number]. The greatest valid value, inclusive.
174+
*/
175+
var validateParameterRange = function( value, name, minimum, maximum ) {
176+
validate(
177+
"E_PAR_OUT_OF_RANGE",
178+
"Parameter `{name}` has value `{value}` out of range [{minimum}, {maximum}].",
179+
value === undefined || value >= minimum && value <= maximum,
180+
{
181+
maximum: maximum,
182+
minimum: minimum,
183+
name: name,
184+
value: value
185+
}
186+
);
187+
};
188+
189+
190+
191+
192+
var validateParameterType = function( value, name, check, expected ) {
193+
validate(
194+
"E_INVALID_PAR_TYPE",
195+
"Invalid `{name}` parameter ({value}). {expected} expected.",
196+
check,
197+
{
198+
expected: expected,
199+
name: name,
200+
value: value
201+
}
202+
);
203+
};
204+
205+
206+
207+
208+
var validateParameterTypeLocale = function( value, name ) {
209+
validateParameterType(
210+
value,
211+
name,
212+
value === undefined || typeof value === "string" || value instanceof Cldr,
213+
"String or Cldr instance"
214+
);
215+
};
216+
217+
218+
219+
220+
/**
221+
* Function inspired by jQuery Core, but reduced to our use case.
222+
*/
223+
var isPlainObject = function( obj ) {
224+
return obj !== null && "" + obj === "[object Object]";
225+
};
226+
227+
228+
229+
230+
var validateParameterTypePlainObject = function( value, name ) {
231+
validateParameterType(
232+
value,
233+
name,
234+
value === undefined || isPlainObject( value ),
235+
"Plain Object"
236+
);
237+
};
238+
239+
240+
241+
242+
var alwaysCldr = function( localeOrCldr ) {
243+
return localeOrCldr instanceof Cldr ? localeOrCldr : new Cldr( localeOrCldr );
244+
};
245+
246+
247+
248+
249+
// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FRegular_Expressions
250+
var regexpEscape = function( string ) {
251+
return string.replace( /([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1" );
252+
};
253+
254+
255+
256+
257+
var stringPad = function( str, count, right ) {
258+
var length;
259+
if ( typeof str !== "string" ) {
260+
str = String( str );
261+
}
262+
for ( length = str.length; length < count; length += 1 ) {
263+
str = ( right ? ( str + "0" ) : ( "0" + str ) );
264+
}
265+
return str;
266+
};
267+
268+
269+
270+
271+
function validateLikelySubtags( cldr ) {
272+
cldr.once( "get", validateCldr );
273+
cldr.get( "supplemental/likelySubtags" );
274+
}
275+
276+
/**
277+
* [new] Globalize( locale|cldr )
278+
*
279+
* @locale [String]
280+
*
281+
* @cldr [Cldr instance]
282+
*
283+
* Create a Globalize instance.
284+
*/
285+
function Globalize( locale ) {
286+
if ( !( this instanceof Globalize ) ) {
287+
return new Globalize( locale );
288+
}
289+
290+
validateParameterPresence( locale, "locale" );
291+
validateParameterTypeLocale( locale, "locale" );
292+
293+
this.cldr = alwaysCldr( locale );
294+
295+
validateLikelySubtags( this.cldr );
296+
}
297+
298+
/**
299+
* Globalize.load( json, ... )
300+
*
301+
* @json [JSON]
302+
*
303+
* Load resolved or unresolved cldr data.
304+
* Somewhat equivalent to previous Globalize.addCultureInfo(...).
305+
*/
306+
Globalize.load = function() {
307+
// validations are delegated to Cldr.load().
308+
Cldr.load.apply( Cldr, arguments );
309+
};
310+
311+
/**
312+
* Globalize.locale( [locale|cldr] )
313+
*
314+
* @locale [String]
315+
*
316+
* @cldr [Cldr instance]
317+
*
318+
* Set default Cldr instance if locale or cldr argument is passed.
319+
*
320+
* Return the default Cldr instance.
321+
*/
322+
Globalize.locale = function( locale ) {
323+
validateParameterTypeLocale( locale, "locale" );
324+
325+
if ( arguments.length ) {
326+
this.cldr = alwaysCldr( locale );
327+
validateLikelySubtags( this.cldr );
328+
}
329+
return this.cldr;
330+
};
331+
332+
/**
333+
* Optimization to avoid duplicating some internal functions across modules.
334+
*/
335+
Globalize._alwaysArray = alwaysArray;
336+
Globalize._createError = createError;
337+
Globalize._formatMessage = formatMessage;
338+
Globalize._isPlainObject = isPlainObject;
339+
Globalize._objectExtend = objectExtend;
340+
Globalize._regexpEscape = regexpEscape;
341+
Globalize._stringPad = stringPad;
342+
Globalize._validate = validate;
343+
Globalize._validateCldr = validateCldr;
344+
Globalize._validateDefaultLocale = validateDefaultLocale;
345+
Globalize._validateParameterPresence = validateParameterPresence;
346+
Globalize._validateParameterRange = validateParameterRange;
347+
Globalize._validateParameterTypePlainObject = validateParameterTypePlainObject;
348+
Globalize._validateParameterType = validateParameterType;
349+
350+
return Globalize;
351+
352+
353+
354+
355+
}));

0 commit comments

Comments
 (0)