diff --git a/test.html b/test.html index 7bbb3e1..4e8c22b 100644 --- a/test.html +++ b/test.html @@ -3,7 +3,7 @@
- + @@ -175,7 +175,7 @@ undo: function( value ){ return value; } - }); + }); equal ( Tester.to ( 15000 ), '15000k' ); equal ( Tester.from ( '15000k' ), 15000 ); @@ -186,4 +186,34 @@ equal ( Tester.to ( 1058009 ), '1058009m' ); }); - + test( "Testing negative In Bracket, negativeInBracket", function(){ + var Tester = wNumb({ + negativeInBracket: '()', + }); + equal ( Tester.to ( -10.35 ), '(10.35)' ); + equal ( Tester.from ( 'eur (100.15)' ), -100.15 ); + }); + + test( "Testing Positive before, positiveBefore", function(){ + var Tester = wNumb({ + positiveBefore: '+', + }); + equal ( Tester.to ( 10.35 ), '+10.35' ); + equal ( Tester.from ( 'eur (+100.15)' ), +100.15 ); + }); + + + + var wNumbTest2 = wNumb({ + positiveBefore: ' +' + }); + console.log(wNumbTest2.to(35.14)); + console.log(wNumbTest2.from('+35.14')); + + var wNumbTest = wNumb({ + prefix: 'Eur ', + negativeInBracket: '()', + }); + console.log(wNumbTest.to(-35.14)); + // console.log(wNumbTest.from('EUR shiju (35.14)')); + \ No newline at end of file diff --git a/wNumb.js b/wNumb.js index 4fef4f0..d0e9252 100644 --- a/wNumb.js +++ b/wNumb.js @@ -20,7 +20,9 @@ "suffix", "encoder", "decoder", - "negativeBefore", + "positiveBefore", + "negativeInBracket", + "negativeBefore", "negative", "edit", "undo" @@ -78,6 +80,8 @@ suffix, encoder, decoder, + positiveBefore, + negativeInBracket, negativeBefore, negative, edit, @@ -87,6 +91,7 @@ var originalInput = input, inputIsNegative, inputPieces, + bracketPieces, inputBase, inputDecimals = "", output = ""; @@ -113,7 +118,7 @@ if (input < 0) { inputIsNegative = true; input = Math.abs(input); - } + } // Reduce the number of decimals to the specified option. if (decimals !== false) { @@ -153,11 +158,22 @@ output += prefix; } - // Normal negative option comes after the prefix. Defaults to '-'. + //If the number is negative, add the negation symbol after the prefix if (inputIsNegative && negative) { output += negative; } + // Instead of using a negation symbol enclose negative numbers in a brackets.. + if (inputIsNegative && negativeInBracket) { + bracketPieces = negativeInBracket.split(''); + inputBase = bracketPieces[0]+inputBase; + inputDecimals = inputDecimals+ bracketPieces[1]; + } + + if (input > 0 && positiveBefore) { + output += positiveBefore; + } + // Append the actual number. output += inputBase; output += inputDecimals; @@ -171,7 +187,7 @@ if (edit) { output = edit(output, originalInput); } - + // All done. return output; } @@ -185,6 +201,8 @@ suffix, encoder, decoder, + positiveBefore, + negativeInBracket, negativeBefore, negative, edit, @@ -193,6 +211,7 @@ ) { var originalInput = input, inputIsNegative, + bracketPieces, output = ""; // User defined pre-decoder. Result must be a non empty string. @@ -238,6 +257,15 @@ if (mark) { input = input.replace(mark, "."); } + + // And again for negative in brackets . + if (negativeInBracket){ + bracketPieces = negativeInBracket.split(''); + if((input.indexOf(bracketPieces[0]) > -1) && (input.indexOf(bracketPieces[1]) > -1)){ + input = input.replace(/[()]/g, ""); + inputIsNegative = true; + } + } // Prepend the negative symbol. if (inputIsNegative) { @@ -290,7 +318,7 @@ if (optionValue === undefined) { // Only default if negativeBefore isn't set. - if (optionName === "negative" && !filteredOptions.negativeBefore) { + if (optionName === "negative" && !filteredOptions.negativeBefore && !filteredOptions.negativeInBracket) { filteredOptions[optionName] = "-"; // Don't set a default for mark when 'thousand' is set. } else if (optionName === "mark" && filteredOptions.thousand !== ".") { diff --git a/wNumb.min.js b/wNumb.min.js index bb8f177..fec2e12 100644 --- a/wNumb.min.js +++ b/wNumb.min.js @@ -1 +1 @@ -!function(e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():window.wNumb=e()}(function(){"use strict";var o=["decimals","thousand","mark","prefix","suffix","encoder","decoder","negativeBefore","negative","edit","undo"];function w(e){return e.split("").reverse().join("")}function h(e,t){return e.substring(0,t.length)===t}function f(e,t,n){if((e[t]||e[n])&&e[t]===e[n])throw new Error(t)}function x(e){return"number"==typeof e&&isFinite(e)}function n(e,t,n,r,i,o,f,u,s,c,a,p){var d,l,h,g=p,v="",m="";return o&&(p=o(p)),!!x(p)&&(!1!==e&&0===parseFloat(p.toFixed(e))&&(p=0),p<0&&(d=!0,p=Math.abs(p)),!1!==e&&(p=function(e,t){return e=e.toString().split("e"),(+((e=(e=Math.round(+(e[0]+"e"+(e[1]?+e[1]+t:t)))).toString().split("e"))[0]+"e"+(e[1]?e[1]-t:-t))).toFixed(t)}(p,e)),-1!==(p=p.toString()).indexOf(".")?(h=(l=p.split("."))[0],n&&(v=n+l[1])):h=p,t&&(h=w((h=w(h).match(/.{1,3}/g)).join(w(t)))),d&&u&&(m+=u),r&&(m+=r),d&&s&&(m+=s),m+=h,m+=v,i&&(m+=i),c&&(m=c(m,g)),m)}function r(e,t,n,r,i,o,f,u,s,c,a,p){var d,l="";return a&&(p=a(p)),!(!p||"string"!=typeof p)&&(u&&h(p,u)&&(p=p.replace(u,""),d=!0),r&&h(p,r)&&(p=p.replace(r,"")),s&&h(p,s)&&(p=p.replace(s,""),d=!0),i&&function(e,t){return e.slice(-1*t.length)===t}(p,i)&&(p=p.slice(0,-1*i.length)),t&&(p=p.split(t).join("")),n&&(p=p.replace(n,".")),d&&(l+="-"),""!==(l=(l+=p).replace(/[^0-9\.\-.]/g,""))&&(l=Number(l),f&&(l=f(l)),!!x(l)&&l))}function i(e,t,n){var r,i=[];for(r=0;r