Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some format updates #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<link href="http://code.jquery.com/qunit/qunit-1.12.0.css" rel="stylesheet">
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script src="wNumb.min.js"></script>
<script src="wNumb.js"></script>
</head>

<div id="qunit"></div>
Expand Down Expand Up @@ -175,7 +175,7 @@
undo: function( value ){
return value;
}
});
});

equal ( Tester.to ( 15000 ), '15000k' );
equal ( Tester.from ( '15000k' ), 15000 );
Expand All @@ -186,4 +186,34 @@
equal ( Tester.to ( 1058009 ), '1058009m' );
});

</script>
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)'));
</script>
38 changes: 33 additions & 5 deletions wNumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"suffix",
"encoder",
"decoder",
"negativeBefore",
"positiveBefore",
"negativeInBracket",
"negativeBefore",
"negative",
"edit",
"undo"
Expand Down Expand Up @@ -78,6 +80,8 @@
suffix,
encoder,
decoder,
positiveBefore,
negativeInBracket,
negativeBefore,
negative,
edit,
Expand All @@ -87,6 +91,7 @@
var originalInput = input,
inputIsNegative,
inputPieces,
bracketPieces,
inputBase,
inputDecimals = "",
output = "";
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -171,7 +187,7 @@
if (edit) {
output = edit(output, originalInput);
}

// All done.
return output;
}
Expand All @@ -185,6 +201,8 @@
suffix,
encoder,
decoder,
positiveBefore,
negativeInBracket,
negativeBefore,
negative,
edit,
Expand All @@ -193,6 +211,7 @@
) {
var originalInput = input,
inputIsNegative,
bracketPieces,
output = "";

// User defined pre-decoder. Result must be a non empty string.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 !== ".") {
Expand Down
2 changes: 1 addition & 1 deletion wNumb.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.