myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gcrawf...@apache.org
Subject svn commit: r1452924 - in /myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs: NumberConverter.js NumberFormat.js
Date Tue, 05 Mar 2013 18:09:01 GMT
Author: gcrawford
Date: Tue Mar  5 18:09:01 2013
New Revision: 1452924

URL: http://svn.apache.org/r1452924
Log:
TRINIDAD-2371 nls:client side currency numberconverter rejects formatted-well value in no_no


thanks to anand

Modified:
    myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
    myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js?rev=1452924&r1=1452923&r2=1452924&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
(original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
Tue Mar  5 18:09:01 2013
@@ -236,28 +236,47 @@ TrNumberConverter.prototype.getAsObject 
     // are removed from the user input number string for all types number, currency and percent.
   
     var parsedValue;
     var localeSymbols = getLocaleSymbols(this._locale);
-      
-    // TODO matzew - see TRINIDAD-682
-    // Remove the thousands separator - which Javascript doesn't want to see
-    var groupingSeparator = localeSymbols.getGroupingSeparator();
-      
-    if (groupingSeparator == "\xa0")
-    {
-      var normalSpace = new RegExp("\\ " , "g");
-      numberString = numberString.replace(normalSpace, "\xa0");
-    }
-      
-    var grouping = new RegExp("\\" + groupingSeparator, "g");
-    numberString = numberString.replace(grouping, "");
+    var isPosNum = false;
+    var processCurrency = (this._type == "currency" && this._numberFormat.hasCurrencyPrefixAndSuffix(numberString));
 
-    // Then change the decimal separator into a period, the only
-    // decimal separator allowed by JS
-    var decimalSeparator = localeSymbols.getDecimalSeparator();
-    var decimal = new RegExp("\\" + decimalSeparator, "g");
-    numberString = numberString.replace(decimal, ".");
-      
     try
     {
+      // remove and preserve the currency prefix and suffix if one exists
+      // this is done to avoid any corruption of prefix and suffix while performing
+      // other string replacement operations that follows.
+      if (processCurrency)
+      {
+        var arr = this._numberFormat.removeCurrencyPrefixAndSuffix(numberString);
+        numberString = arr[0];
+        isPosNum = arr[1];
+      }
+
+
+      // TODO matzew - see TRINIDAD-682
+      // Remove the thousands separator - which Javascript doesn't want to see
+      var groupingSeparator = localeSymbols.getGroupingSeparator();
+
+      if (groupingSeparator == "\xa0")
+      {
+        var normalSpace = new RegExp("\\ " , "g");
+        numberString = numberString.replace(normalSpace, "\xa0");
+      }
+
+      var grouping = new RegExp("\\" + groupingSeparator, "g");
+      numberString = numberString.replace(grouping, "");
+
+      // Then change the decimal separator into a period, the only
+      // decimal separator allowed by JS
+      var decimalSeparator = localeSymbols.getDecimalSeparator();
+      var decimal = new RegExp("\\" + decimalSeparator, "g");
+      numberString = numberString.replace(decimal, ".");
+
+      // put the prefix and suffix back for currency if required
+      if (processCurrency)
+      {
+        numberString = this._numberFormat.addCurrencyPrefixAndSuffix(numberString, isPosNum);
+      }
+
       // parse the numberString
       numberString = this._numberFormat.parse(numberString)+"";     
     }

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js?rev=1452924&r1=1452923&r2=1452924&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js
(original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js
Tue Mar  5 18:09:01 2013
@@ -271,38 +271,96 @@ TrNumberFormat.prototype.stringToNumber 
 }
 
 /**
+ * Returns true if there is a currency prefix and suffix in the currency string
+ */
+TrNumberFormat.prototype.hasCurrencyPrefixAndSuffix = function(currencyString)
+{
+  var negP = currencyString.indexOf(this._nPre);
+  var nSufNoSpace = this._nSuf;
+  if (nSufNoSpace.charAt(0) == ' ' || nSufNoSpace.charAt(0) == '\xa0')
+    nSufNoSpace = nSufNoSpace.substring(1);
+  var negS = currencyString.indexOf(nSufNoSpace);
+
+  // insist that prefix always starts at 0
+  if(negP == 0 && negS != -1)
+  {
+    return true;
+  }
+  else
+  {
+    var posP = currencyString.indexOf(this._pPre);
+    var pSufNoSpace = this._pSuf;
+    if (pSufNoSpace.charAt(0) == ' ' || pSufNoSpace.charAt(0) == '\xa0')
+      pSufNoSpace = pSufNoSpace.substring(1);
+    var posS = currencyString.indexOf(pSufNoSpace);
+
+    // insist that prefix always starts at 0
+    if(posP == 0 && posS != -1)
+    {
+      return true;
+    }
+  }
+
+  return false;
+}
+
+/**
  * Formats a currency string into a number.
  */
 TrNumberFormat.prototype.stringToCurrency = function(numberString)
 {
+  var arr = this.removeCurrencyPrefixAndSuffix(numberString);
+  numberString = arr[0]
+  var isPosNum = arr[1];
+
+  if (isPosNum)
+  {
+    return this.stringToNumber(numberString);
+  }
+  else
+  {
+    return (this.stringToNumber(numberString) * -1);
+  }
+}
+
+/**
+ * Removes the locale specific currency prefix and suffix from a currencyString
+ * and returns an array with 2 elements,
+ *     arr[0]  -> converted string
+ *     arr[1]  -> boolean, true if number is positive, false if number is negative
+ */
+TrNumberFormat.prototype.removeCurrencyPrefixAndSuffix = function(currencyString)
+{
   //is the string negative ?
-  var negP = numberString.indexOf(this._nPre);
+  var retArr = [];
+  var negP = currencyString.indexOf(this._nPre);
   var nSufNoSpace = this._nSuf;
   if (nSufNoSpace.charAt(0) == ' ' || nSufNoSpace.charAt(0) == '\xa0')
     nSufNoSpace = nSufNoSpace.substring(1);
-  var negS = numberString.indexOf(nSufNoSpace);
+  var negS = currencyString.indexOf(nSufNoSpace);
 
 
   // TRINIDAD-1958: In Arabic the values for negPrefix and posPrefix are the same, so it
is insufficient to test for
-  // the presence of (only) negPrefix to determine if the number is negative. 
+  // the presence of (only) negPrefix to determine if the number is negative.
   if(negP != -1 && negS != -1)
   {
-    numberString = numberString.substr(this._nPre.length, numberString.length - (this._nPre.length
+ nSufNoSpace.length));
-    return (this.stringToNumber(numberString) * -1);
+    retArr.push(currencyString.substr(this._nPre.length, currencyString.length - (this._nPre.length
+ nSufNoSpace.length)));
+    retArr.push(false);
+    return retArr;
   }
   else
   {
-    var posP = numberString.indexOf(this._pPre);
+    var posP = currencyString.indexOf(this._pPre);
     var pSufNoSpace = this._pSuf;
     if (pSufNoSpace.charAt(0) == ' ' || pSufNoSpace.charAt(0) == '\xa0')
       pSufNoSpace = pSufNoSpace.substring(1);
-    var posS = numberString.indexOf(pSufNoSpace);
+    var posS = currencyString.indexOf(pSufNoSpace);
 
     if(posP != -1 && posS != -1)
     {
-      numberString = numberString.substr (this._pPre.length, numberString.length - (this._pPre.length
+ pSufNoSpace.length));
-      numberString = this.stringToNumber(numberString);
-      return numberString;
+      retArr.push(currencyString.substr(this._pPre.length, currencyString.length - (this._pPre.length
+ pSufNoSpace.length)));
+      retArr.push(true);
+      return retArr;
     }
     else
     {
@@ -374,6 +432,21 @@ TrNumberFormat.prototype.numberToString 
 }
 
 /**
+ * Adds locale specific currency prefix and suffix to the number string
+ */
+TrNumberFormat.prototype.addCurrencyPrefixAndSuffix = function(numberString, hasPositivePrefix)
+{
+  if(hasPositivePrefix)
+  {
+    return this._pPre + numberString + this._pSuf;
+  }
+  else
+  {
+    return this._nPre + numberString + this._nSuf;
+  }
+}
+
+/**
  * Formats a number into a a formatted currency string.
  */
 TrNumberFormat.prototype.currencyToString = function(number)
@@ -383,12 +456,12 @@ TrNumberFormat.prototype.currencyToStrin
   {
     number = (number*-1)+"";
     number = this.numberToString(number);
-    return this._nPre + number + this._nSuf;
+    return this.addCurrencyPrefixAndSuffix(number, false);
   }
   else
   {
     number = this.numberToString(number);
-    return this._pPre + number + this._pSuf;
+    return this.addCurrencyPrefixAndSuffix(number, true);
   }
 }
 



Mime
View raw message