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

Unformat invalid #122

Open
wants to merge 3 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
6 changes: 4 additions & 2 deletions accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@
*
* Doesn't throw any errors (`NaN`s become 0) but this may change in future
*/
var unformat = lib.unformat = lib.parse = function(value, decimal) {
var unformat = lib.unformat = lib.parse = function(value, decimal, failure_result) {
if (failure_result === undefined)
failure_result = 0;
// Recursively unformat arrays:
if (isArray(value)) {
return map(value, function(val) {
Expand All @@ -203,7 +205,7 @@
);

// This will fail silently which may cause trouble, let's wait and see:
return !isNaN(unformatted) ? unformatted : 0;
return !isNaN(unformatted) ? unformatted : failure_result;
};


Expand Down
10 changes: 8 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,20 @@ <h4><strong>accounting.toFixed()</strong></h4>
<h4><strong>accounting.unformat()</strong></h4>

<pre class="prettyprint lang-js">// Standard usage and parameters (returns number):
accounting.unformat(string<em>, [decimal]</em>);
accounting.unformat(string<em>, [decimal], [failure_result]</em>);

// Example usage:
accounting.unformat("GBP &pound; 12,345,678.90"); // 12345678.9

// If a non-standard decimal separator was used (eg. a comma) unformat() will need it in order to work out
// which part of the number is a decimal/float:
accounting.unformat("&euro; 1.000.000,00", ","); // 1000000</pre>
accounting.unformat("&euro; 1.000.000,00", ","); // 1000000

// if passed string is not a number convertible, unformat fails and return 0
// you can specify failure_result response of your choice for flexibility and avoid ambiguity.
accounting.unformat("&euro; Not a Number", ","); // 0
accounting.unformat("&euro; Not a Number", ",", "failed"); // "failed"
</pre>

</section>

Expand Down
2 changes: 2 additions & 0 deletions tests/qunit/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ $(document).ready(function() {
equals(accounting.unformat(1234567890), 1234567890, 'Returns same value when passed an integer');
equals(accounting.unformat("string"), 0, 'Returns 0 for a string with no numbers');
equals(accounting.unformat({joss:1}), 0, 'Returns 0 for object');
equals(accounting.unformat("string", ',', null), null, 'Returns user specified failure response for a string with no numbers');
equals(accounting.unformat("string", ',', "failed"), "failed", 'Returns user specified failure response for a string with no numbers');

accounting.settings.number.decimal = ',';
equals(accounting.unformat("100,00"), 100, 'Uses decimal separator from settings');
Expand Down