@@ -43,9 +43,9 @@ Globalize = function( cultureSelector ) {
43
43
return new Globalize . prototype . init ( cultureSelector ) ;
44
44
} ;
45
45
46
- if ( typeof require !== "undefined"
47
- && typeof exports !== "undefined"
48
- && typeof module !== "undefined" ) {
46
+ if ( typeof require !== "undefined" &&
47
+ typeof exports !== "undefined" &&
48
+ typeof module !== "undefined" ) {
49
49
// Assume CommonJS
50
50
module . exports = Globalize ;
51
51
} else {
@@ -66,11 +66,11 @@ Globalize.prototype = {
66
66
} ;
67
67
Globalize . prototype . init . prototype = Globalize . prototype ;
68
68
69
- // 1. When defining a culture, all fields are required except the ones stated as optional.
70
- // 2. Each culture should have a ".calendars" object with at least one calendar named "standard"
71
- // which serves as the default calendar in use by that culture.
72
- // 3. Each culture should have a ".calendar" object which is the current calendar being used,
73
- // it may be dynamically changed at any time to one of the calendars in ".calendars".
69
+ // 1. When defining a culture, all fields are required except the ones stated as optional.
70
+ // 2. Each culture should have a ".calendars" object with at least one calendar named "standard"
71
+ // which serves as the default calendar in use by that culture.
72
+ // 3. Each culture should have a ".calendar" object which is the current calendar being used,
73
+ // it may be dynamically changed at any time to one of the calendars in ".calendars".
74
74
Globalize . cultures [ "default" ] = {
75
75
// A unique name for the culture in the form <language code>-<country/region code>
76
76
name : "en" ,
@@ -120,7 +120,7 @@ Globalize.cultures[ "default" ] = {
120
120
// symbol used for negative numbers
121
121
"-" : "-" ,
122
122
// symbol used for NaN (Not-A-Number)
123
- NaN : "NaN" ,
123
+ " NaN" : "NaN" ,
124
124
// symbol used for Negative Infinity
125
125
negativeInfinity : "-Infinity" ,
126
126
// symbol used for Positive Infinity
@@ -262,7 +262,7 @@ Globalize.cultures[ "default" ] = {
262
262
263
263
Globalize . cultures [ "default" ] . calendar = Globalize . cultures [ "default" ] . calendars . standard ;
264
264
265
- Globalize . cultures [ "en" ] = Globalize . cultures [ "default" ] ;
265
+ Globalize . cultures . en = Globalize . cultures [ "default" ] ;
266
266
267
267
Globalize . cultureSelector = "en" ;
268
268
@@ -271,8 +271,8 @@ Globalize.cultureSelector = "en";
271
271
//
272
272
273
273
regexHex = / ^ 0 x [ a - f 0 - 9 ] + $ / i;
274
- regexInfinity = / ^ [ + - ] ? i n f i n i t y $ / i;
275
- regexParseFloat = / ^ [ + - ] ? \d * \. ? \d * ( e [ + - ] ? \d + ) ? $ / ;
274
+ regexInfinity = / ^ [ + \ -] ? i n f i n i t y $ / i;
275
+ regexParseFloat = / ^ [ + \ -] ? \d * \. ? \d * ( e [ + \ -] ? \d + ) ? $ / ;
276
276
regexTrim = / ^ \s + | \s + $ / g;
277
277
278
278
//
@@ -295,7 +295,7 @@ endsWith = function( value, pattern ) {
295
295
return value . substr ( value . length - pattern . length ) === pattern ;
296
296
} ;
297
297
298
- extend = function ( deep ) {
298
+ extend = function ( ) {
299
299
var options , name , src , copy , copyIsArray , clone ,
300
300
target = arguments [ 0 ] || { } ,
301
301
i = 1 ,
@@ -358,8 +358,8 @@ isArray = Array.isArray || function( obj ) {
358
358
} ;
359
359
360
360
isFunction = function ( obj ) {
361
- return Object . prototype . toString . call ( obj ) === "[object Function]"
362
- }
361
+ return Object . prototype . toString . call ( obj ) === "[object Function]" ;
362
+ } ;
363
363
364
364
isObject = function ( obj ) {
365
365
return Object . prototype . toString . call ( obj ) === "[object Object]" ;
@@ -374,7 +374,10 @@ trim = function( value ) {
374
374
} ;
375
375
376
376
truncate = function ( value ) {
377
- return value | 0 ;
377
+ if ( isNaN ( value ) ) {
378
+ return NaN ;
379
+ }
380
+ return Math [ value < 0 ? "ceil" : "floor" ] ( value ) ;
378
381
} ;
379
382
380
383
zeroPad = function ( str , count , left ) {
@@ -444,10 +447,10 @@ expandFormat = function( cal, format ) {
444
447
445
448
formatDate = function ( value , format , culture ) {
446
449
var cal = culture . calendar ,
447
- convert = cal . convert ;
450
+ convert = cal . convert ,
451
+ ret ;
448
452
449
453
if ( ! format || ! format . length || format === "i" ) {
450
- var ret ;
451
454
if ( culture && culture . name . length ) {
452
455
if ( convert ) {
453
456
// non-gregorian calendar, so we cannot use built-in toLocaleString()
@@ -507,9 +510,14 @@ formatDate = function( value, format, culture ) {
507
510
return converted [ part ] ;
508
511
}
509
512
switch ( part ) {
510
- case 0 : return date . getFullYear ( ) ;
511
- case 1 : return date . getMonth ( ) ;
512
- case 2 : return date . getDate ( ) ;
513
+ case 0 :
514
+ return date . getFullYear ( ) ;
515
+ case 1 :
516
+ return date . getMonth ( ) ;
517
+ case 2 :
518
+ return date . getDate ( ) ;
519
+ default :
520
+ throw "Invalid part value " + part ;
513
521
}
514
522
}
515
523
@@ -563,11 +571,9 @@ formatDate = function( value, format, culture ) {
563
571
// Month, using the full name
564
572
var part = getPart ( value , 1 ) ;
565
573
ret . push (
566
- ( cal . monthsGenitive && hasDay ( ) )
567
- ?
568
- cal . monthsGenitive [ clength === 3 ? "namesAbbr" : "names" ] [ part ]
569
- :
570
- cal . months [ clength === 3 ? "namesAbbr" : "names" ] [ part ]
574
+ ( cal . monthsGenitive && hasDay ( ) ) ?
575
+ ( cal . monthsGenitive [ clength === 3 ? "namesAbbr" : "names" ] [ part ] ) :
576
+ ( cal . months [ clength === 3 ? "namesAbbr" : "names" ] [ part ] )
571
577
) ;
572
578
break ;
573
579
case "M" :
@@ -656,10 +662,10 @@ formatDate = function( value, format, culture ) {
656
662
// Time zone offset with leading zero
657
663
hour = value . getTimezoneOffset ( ) / 60 ;
658
664
ret . push (
659
- ( hour <= 0 ? "+" : "-" ) + padZeros ( Math . floor ( Math . abs ( hour ) ) , 2 )
665
+ ( hour <= 0 ? "+" : "-" ) + padZeros ( Math . floor ( Math . abs ( hour ) ) , 2 ) +
660
666
// Hard coded ":" separator, rather than using cal.TimeSeparator
661
667
// Repeated here for consistency, plus ":" was already assumed in date parsing.
662
- + ":" + padZeros ( Math . abs ( value . getTimezoneOffset ( ) % 60 ) , 2 )
668
+ ":" + padZeros ( Math . abs ( value . getTimezoneOffset ( ) % 60 ) , 2 )
663
669
) ;
664
670
break ;
665
671
case "g" :
@@ -675,7 +681,6 @@ formatDate = function( value, format, culture ) {
675
681
break ;
676
682
default :
677
683
throw "Invalid date format pattern \'" + current + "\'." ;
678
- break ;
679
684
}
680
685
}
681
686
return ret . join ( "" ) ;
@@ -706,15 +711,14 @@ formatDate = function( value, format, culture ) {
706
711
numberString = split [ 0 ] ;
707
712
right = split . length > 1 ? split [ 1 ] : "" ;
708
713
709
- var l ;
710
714
if ( exponent > 0 ) {
711
715
right = zeroPad ( right , exponent , false ) ;
712
716
numberString += right . slice ( 0 , exponent ) ;
713
717
right = right . substr ( exponent ) ;
714
718
}
715
719
else if ( exponent < 0 ) {
716
720
exponent = - exponent ;
717
- numberString = zeroPad ( numberString , exponent + 1 ) ;
721
+ numberString = zeroPad ( numberString , exponent + 1 , true ) ;
718
722
right = numberString . slice ( - exponent , numberString . length ) + right ;
719
723
numberString = numberString . slice ( 0 , - exponent ) ;
720
724
}
@@ -783,10 +787,10 @@ formatDate = function( value, format, culture ) {
783
787
break ;
784
788
case "N" :
785
789
formatInfo = nf ;
786
- // fall through
790
+ /* falls through */
787
791
case "C" :
788
792
formatInfo = formatInfo || nf . currency ;
789
- // fall through
793
+ /* falls through */
790
794
case "P" :
791
795
formatInfo = formatInfo || nf . percent ;
792
796
pattern = value < 0 ? formatInfo . pattern [ 0 ] : ( formatInfo . pattern [ 1 ] || "n" ) ;
@@ -835,7 +839,7 @@ formatDate = function( value, format, culture ) {
835
839
836
840
getTokenRegExp = function ( ) {
837
841
// regular expression for matching date and time tokens in format strings.
838
- return / \/ | d d d d | d d d | d d | d | M M M M | M M M | M M | M | y y y y | y y | y | h h | h | H H | H | m m | m | s s | s | t t | t | f f f | f f | f | z z z | z z | z | g g | g / g;
842
+ return ( / \/ | d d d d | d d d | d d | d | M M M M | M M M | M M | M | y y y y | y y | y | h h | h | H H | H | m m | m | s s | s | t t | t | f f f | f f | f | z z z | z z | z | g g | g / g) ;
839
843
} ;
840
844
841
845
getEra = function ( date , eras ) {
@@ -872,12 +876,12 @@ getEraYear = function( date, cal, era, sortable ) {
872
876
873
877
expandYear = function ( cal , year ) {
874
878
// expands 2-digit year into 4 digits.
875
- var now = new Date ( ) ,
876
- era = getEra ( now ) ;
877
879
if ( year < 100 ) {
878
- var twoDigitYearMax = cal . twoDigitYearMax ;
880
+ var now = new Date ( ) ,
881
+ era = getEra ( now ) ,
882
+ curr = getEraYear ( now , cal , era ) ,
883
+ twoDigitYearMax = cal . twoDigitYearMax ;
879
884
twoDigitYearMax = typeof twoDigitYearMax === "string" ? new Date ( ) . getFullYear ( ) % 100 + parseInt ( twoDigitYearMax , 10 ) : twoDigitYearMax ;
880
- var curr = getEraYear ( now , cal , era ) ;
881
885
year += curr - ( curr % 100 ) ;
882
886
if ( year > twoDigitYearMax ) {
883
887
year -= 100 ;
@@ -1004,11 +1008,10 @@ getEraYear = function( date, cal, era, sortable ) {
1004
1008
add = "([+-]?\\d\\d?)" ;
1005
1009
break ;
1006
1010
case "/" :
1007
- add = "(\\" + cal [ "/" ] + " )";
1011
+ add = "(\\/ )" ;
1008
1012
break ;
1009
1013
default :
1010
1014
throw "Invalid date format pattern \'" + m + "\'." ;
1011
- break ;
1012
1015
}
1013
1016
if ( add ) {
1014
1017
regexp . push ( add ) ;
@@ -1212,7 +1215,7 @@ getEraYear = function( date, cal, era, sortable ) {
1212
1215
if ( tzMinOffset !== null ) {
1213
1216
// adjust timezone to utc before applying local offset.
1214
1217
var adjustedMin = result . getMinutes ( ) - ( tzMinOffset + result . getTimezoneOffset ( ) ) ;
1215
- // Safari limits hours and minutes to the range of -127 to 127. We need to use setHours
1218
+ // Safari limits hours and minutes to the range of -127 to 127. We need to use setHours
1216
1219
// to ensure both these fields will not exceed this range. adjustedMin will range
1217
1220
// somewhere between -1440 and 1500, so we only need to split this into hours.
1218
1221
result . setHours ( result . getHours ( ) + parseInt ( adjustedMin / 60 , 10 ) , adjustedMin % 60 ) ;
@@ -1229,7 +1232,7 @@ parseNegativePattern = function( value, nf, negativePattern ) {
1229
1232
case "n -" :
1230
1233
neg = " " + neg ;
1231
1234
pos = " " + pos ;
1232
- // fall through
1235
+ /* falls through */
1233
1236
case "n-" :
1234
1237
if ( endsWith ( value , neg ) ) {
1235
1238
ret = [ "-" , value . substr ( 0 , value . length - neg . length ) ] ;
@@ -1241,7 +1244,7 @@ parseNegativePattern = function( value, nf, negativePattern ) {
1241
1244
case "- n" :
1242
1245
neg += " " ;
1243
1246
pos += " " ;
1244
- // fall through
1247
+ /* falls through */
1245
1248
case "-n" :
1246
1249
if ( startsWith ( value , neg ) ) {
1247
1250
ret = [ "-" , value . substr ( neg . length ) ] ;
@@ -1329,7 +1332,7 @@ Globalize.addCultureInfo = function( cultureName, baseCultureName, info ) {
1329
1332
Globalize . findClosestCulture = function ( name ) {
1330
1333
var match ;
1331
1334
if ( ! name ) {
1332
- return this . cultures [ this . cultureSelector ] || this . cultures [ "default" ] ;
1335
+ return this . findClosestCulture ( this . cultureSelector ) || this . cultures [ "default" ] ;
1333
1336
}
1334
1337
if ( typeof name === "string" ) {
1335
1338
name = name . split ( "," ) ;
@@ -1361,9 +1364,13 @@ Globalize.findClosestCulture = function( name ) {
1361
1364
prioritized . push ( { lang : lang , pri : pri } ) ;
1362
1365
}
1363
1366
prioritized . sort ( function ( a , b ) {
1364
- return a . pri < b . pri ? 1 : - 1 ;
1367
+ if ( a . pri < b . pri ) {
1368
+ return 1 ;
1369
+ } else if ( a . pri > b . pri ) {
1370
+ return - 1 ;
1371
+ }
1372
+ return 0 ;
1365
1373
} ) ;
1366
-
1367
1374
// exact match
1368
1375
for ( i = 0 ; i < l ; i ++ ) {
1369
1376
lang = prioritized [ i ] . lang ;
@@ -1396,7 +1403,7 @@ Globalize.findClosestCulture = function( name ) {
1396
1403
lang = prioritized [ i ] . lang ;
1397
1404
for ( var cultureKey in cultures ) {
1398
1405
var culture = cultures [ cultureKey ] ;
1399
- if ( culture . language == lang ) {
1406
+ if ( culture . language === lang ) {
1400
1407
return culture ;
1401
1408
}
1402
1409
}
@@ -1409,7 +1416,7 @@ Globalize.findClosestCulture = function( name ) {
1409
1416
} ;
1410
1417
1411
1418
Globalize . format = function ( value , format , cultureSelector ) {
1412
- culture = this . findClosestCulture ( cultureSelector ) ;
1419
+ var culture = this . findClosestCulture ( cultureSelector ) ;
1413
1420
if ( value instanceof Date ) {
1414
1421
value = formatDate ( value , format , culture ) ;
1415
1422
}
@@ -1478,8 +1485,13 @@ Globalize.parseFloat = function( value, radix, cultureSelector ) {
1478
1485
value = value . replace ( culture . numberFormat . currency [ "." ] , culture . numberFormat [ "." ] ) ;
1479
1486
}
1480
1487
1481
- // trim leading and trailing whitespace
1482
- value = trim ( value ) ;
1488
+ //Remove percentage character from number string before parsing
1489
+ if ( value . indexOf ( culture . numberFormat . percent . symbol ) > - 1 ) {
1490
+ value = value . replace ( culture . numberFormat . percent . symbol , "" ) ;
1491
+ }
1492
+
1493
+ // remove spaces: leading, trailing and between - and number. Used for negative currency pt-BR
1494
+ value = value . replace ( / / g, "" ) ;
1483
1495
1484
1496
// allow infinity or hexidecimal
1485
1497
if ( regexInfinity . test ( value ) ) {
@@ -1567,7 +1579,7 @@ Globalize.culture = function( cultureSelector ) {
1567
1579
this . cultureSelector = cultureSelector ;
1568
1580
}
1569
1581
// getter
1570
- return this . findClosestCulture ( cultureSelector ) || this . culture [ "default" ] ;
1582
+ return this . findClosestCulture ( cultureSelector ) || this . cultures [ "default" ] ;
1571
1583
} ;
1572
1584
1573
- } ( this ) ) ;
1585
+ } ( this ) ) ;
0 commit comments