@@ -43,9 +43,9 @@ Globalize = function( cultureSelector ) {
4343 return new Globalize . prototype . init ( cultureSelector ) ;
4444} ;
4545
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" ) {
4949 // Assume CommonJS
5050 module . exports = Globalize ;
5151} else {
@@ -66,11 +66,11 @@ Globalize.prototype = {
6666} ;
6767Globalize . prototype . init . prototype = Globalize . prototype ;
6868
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".
7474Globalize . cultures [ "default" ] = {
7575 <
F7FB
span class=pl-c>// A unique name for the culture in the form <language code>-<country/region code>
7676 name : "en" ,
@@ -120,7 +120,7 @@ Globalize.cultures[ "default" ] = {
120120 // symbol used for negative numbers
121121 "-" : "-" ,
122122 // symbol used for NaN (Not-A-Number)
123- NaN : "NaN" ,
123+ " NaN" : "NaN" ,
124124 // symbol used for Negative Infinity
125125 negativeInfinity : "-Infinity" ,
126126 // symbol used for Positive Infinity
@@ -262,7 +262,7 @@ Globalize.cultures[ "default" ] = {
262262
263263Globalize . cultures [ "default" ] . calendar = Globalize . cultures [ "default" ] . calendars . standard ;
264264
265- Globalize . cultures [ "en" ] = Globalize . cultures [ "default" ] ;
265+ Globalize . cultures . en = Globalize . cultures [ "default" ] ;
266266
267267Globalize . cultureSelector = "en" ;
268268
@@ -271,8 +271,8 @@ Globalize.cultureSelector = "en";
271271//
272272
273273regexHex = / ^ 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 + ) ? $ / ;
276276regexTrim = / ^ \s + | \s + $ / g;
277277
278278//
@@ -295,7 +295,7 @@ endsWith = function( value, pattern ) {
295295 return value . substr ( value . length - pattern . length ) === pattern ;
296296} ;
297297
298- extend = function ( deep ) {
298+ extend = function ( ) {
299299 var options , name , src , copy , copyIsArray , clone ,
300300 target = arguments [ 0 ] || { } ,
301301 i = 1 ,
@@ -358,8 +358,8 @@ isArray = Array.isArray || function( obj ) {
358358} ;
359359
360360isFunction = function ( obj ) {
361- return Object . prototype . toString . call ( obj ) === "[object Function]"
362- }
361+ return Object . prototype . toString . call ( obj ) === "[object Function]" ;
362+ } ;
363363
364364isObject = function ( obj ) {
365365 return Object . prototype . toString . call ( obj ) === "[object Object]" ;
@@ -374,7 +374,10 @@ trim = function( value ) {
374374} ;
375375
376376truncate = function ( value ) {
377- return value | 0 ;
377+ if ( isNaN ( value ) ) {
378+ return NaN ;
379+ }
380+ return Math [ value < 0 ? "ceil" : "floor" ] ( value ) ;
378381} ;
379382
380383zeroPad = function ( str , count , left ) {
@@ -444,10 +447,10 @@ expandFormat = function( cal, format ) {
444447
445448formatDate = function ( value , format , culture ) {
446449 var cal = culture . calendar ,
447- convert = cal . convert ;
450+ convert = cal . convert ,
451+ ret ;
448452
449453 if ( ! format || ! format . length || format === "i" ) {
450- var ret ;
451454 if ( culture && culture . name . length ) {
452455 if ( convert ) {
453456 // non-gregorian calendar, so we cannot use built-in toLocaleString()
@@ -507,9 +510,14 @@ formatDate = function( value, format, culture ) {
507510 return converted [ part ] ;
508511 }
509512 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 ;
513521 }
514522 }
515523
@@ -563,11 +571,9 @@ formatDate = function( value, format, culture ) {
563571 // Month, using the full name
564572 var part = getPart ( value , 1 ) ;
565573 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 ] )
571577 ) ;
572578 break ;
573579 case "M" :
@@ -656,10 +662,10 @@ formatDate = function( value, format, culture ) {
656662 // Time zone offset with leading zero
657663 hour = value . getTimezoneOffset ( ) / 60 ;
658664 ret . push (
659- ( hour <= 0 ? "+" : "-" ) + padZeros ( Math . floor ( Math . abs ( hour ) ) , 2 )
665+ ( hour <= 0 ? "+" : "-" ) + padZeros ( Math . floor ( Math . abs ( hour ) ) , 2 ) +
660666 // Hard coded ":" separator, rather than using cal.TimeSeparator
661667 // 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 )
663669 ) ;
664670 break ;
665671 case "g" :
@@ -675,7 +681,6 @@ formatDate = function( value, format, culture ) {
675681 break ;
676682 default :
677683 throw "Invalid date format pattern \'" + current + "\'." ;
678- break ;
679684 }
680685 }
681686 return ret . join ( "" ) ;
@@ -706,15 +711,14 @@ formatDate = function( value, format, culture ) {
706711 numberString = split [ 0 ] ;
707712 right = split . length > 1 ? split [ 1 ] : "" ;
708713
709- var l ;
710714 if ( exponent > 0 ) {
711715 right = zeroPad ( right , exponent , false ) ;
712716 numberString += right . slice ( 0 , exponent ) ;
713717 right = right . substr ( exponent ) ;
714718 }
715719 else if ( exponent < 0 ) {
716720 exponent = - exponent ;
717- numberString = zeroPad ( numberString , exponent + 1 ) ;
721+ numberString = zeroPad ( numberString , exponent + 1 , true ) ;
718722 right = numberString . slice ( - exponent , numberString . length ) + right ;
719723 numberString = numberString . slice ( 0 , - exponent ) ;
720724 }
@@ -783,10 +787,10 @@ formatDate = function( value, format, culture ) {
783787 break ;
784788 case "N" :
785789 formatInfo = nf ;
786- // fall through
790+ /* falls through */
787791 case "C" :
788792 formatInfo = formatInfo || nf . currency ;
789- // fall through
793+ /* falls through */
790794 case "P" :
791795 formatInfo = formatInfo || nf . percent ;
792796 pattern = value < 0 ? formatInfo . pattern [ 0 ] : ( formatInfo . pattern [ 1 ] || "n" ) ;
@@ -835,7 +839,7 @@ formatDate = function( value, format, culture ) {
835839
836840getTokenRegExp = function ( ) {
837841 // 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) ;
839843} ;
840844
841845getEra = function ( date , eras ) {
@@ -872,12 +876,12 @@ getEraYear = function( date, cal, era, sortable ) {
872876
873877 expandYear = function ( cal , year ) {
874878 // expands 2-digit year into 4 digits.
875- var now = new Date ( ) ,
876- era = getEra ( now ) ;
877879 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 ;
879884 twoDigitYearMax = typeof twoDigitYearMax === "string" ? new Date ( ) . getFullYear ( ) % 100 + parseInt ( twoDigitYearMax , 10 ) : twoDigitYearMax ;
880- var curr = getEraYear ( now , cal , era ) ;
881885 year += curr - ( curr % 100 ) ;
882886 if ( year > twoDigitYearMax ) {
883887 year -= 100 ;
@@ -1004,11 +1008,10 @@ getEraYear = function( date, cal, era, sortable ) {
10041008 add = "([+-]?\\d\\d?)" ;
10051009 break ;
10061010 case "/" :
1007- add = "(\\" + cal [ "/" ] + " )";
1011+ add = "(\\/ )" ;
10081012 break ;
10091013 default :
10101014 throw "Invalid date format pattern \'" + m + "\'." ;
1011- break ;
10121015 }
10131016 if ( add ) {
10141017 regexp . push ( add ) ;
@@ -1212,7 +1215,7 @@ getEraYear = function( date, cal, era, sortable ) {
12121215 if ( tzMinOffset !== null ) {
12131216 // adjust timezone to utc before applying local offset.
12141217 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
12161219 // to ensure both these fields will not exceed this range. adjustedMin will range
12171220 // somewhere between -1440 and 1500, so we only need to split this into hours.
12181221 result . setHours ( result . getHours ( ) + parseInt ( adjustedMin / 60 , 10 ) , adjustedMin % 60 ) ;
@@ -1229,7 +1232,7 @@ parseNegativePattern = function( value, nf, negativePattern ) {
12291232 case "n -" :
12301233 neg = " " + neg ;
12311234 pos = " " + pos ;
1232- // fall through
1235+ /* falls through */
12331236 case "n-" :
12341237 if ( endsWith ( value , neg ) ) {
12351238 ret = [ "-" , value . substr ( 0 , value . length - neg . length ) ] ;
@@ -1241,7 +1244,7 @@ parseNegativePattern = function( value, nf, negativePattern ) {
12411244 case "- n" :
12421245 neg += " " ;
12431246 pos += " " ;
1244- // fall through
1247+ /* falls through */
12451248 case "-n" :
12461249 if ( startsWith ( value , neg ) ) {
12471250 ret = [ "-" , value . substr ( neg . length ) ] ;
@@ -1329,7 +1332,7 @@ Globalize.addCultureInfo = function( cultureName, baseCultureName, info ) {
13291332Globalize . findClosestCulture = function ( name ) {
13301333 var match ;
13311334 if ( ! name ) {
1332- return this . cultures [ this . cultureSelector ] || this . cultures [ "default" ] ;
1335+ return this . findClosestCulture ( this . cultureSelector ) || this . cultures [ "default" ] ;
13331336 }
13341337 if ( typeof name === "string" ) {
13351338 name = name . split ( "," ) ;
@@ -1361,9 +1364,13 @@ Globalize.findClosestCulture = function( name ) {
13611364 prioritized . push ( { lang : lang , pri : pri } ) ;
13621365 }
13631366 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 ;
13651373 } ) ;
1366-
13671374 // exact match
13681375 for ( i = 0 ; i < l ; i ++ ) {
13691376 lang = prioritized [ i ] . lang ;
@@ -1396,7 +1403,7 @@ Globalize.findClosestCulture = function( name ) {
13961403 lang = prioritized [ i ] . lang ;
13971404 for ( var cultureKey in cultures ) {
13981405 var culture = cultures [ cultureKey ] ;
1399- if ( culture . language == lang ) {
1406+ if ( culture . language === lang ) {
14001407 return culture ;
14011408 }
14021409 }
@@ -1409,7 +1416,7 @@ Globalize.findClosestCulture = function( name ) {
14091416} ;
14101417
14111418Globalize . format = function ( value , format , cultureSelector ) {
1412- culture = this . findClosestCulture ( cultureSelector ) ;
1419+ var culture = this . findClosestCulture ( cultureSelector ) ;
14131420 if ( value instanceof Date ) {
14141421 value = formatDate ( value , format , culture ) ;
14151422 }
@@ -1478,8 +1485,13 @@ Globalize.parseFloat = function( value, radix, cultureSelector ) {
14781485 value = value . replace ( culture . numberFormat . currency [ "." ] , culture . numberFormat [ "." ] ) ;
14791486 }
14801487
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, "" ) ;
14831495
14841496 // allow infinity or hexidecimal
14851497 if ( regexInfinity . test ( value ) ) {
@@ -1567,7 +1579,7 @@ Globalize.culture = function( cultureSelector ) {
15671579 this . cultureSelector = cultureSelector ;
15681580 }
15691581 // getter
1570- return this . findClosestCulture ( cultureSelector ) || this . culture [ "default" ] ;
1582+ return this . findClosestCulture ( cultureSelector ) || this . cultures [ "default" ] ;
15711583} ;
15721584
1573- } ( this ) ) ;
1585+ } ( this ) ) ;
0 commit comments