Skip to content

Commit

Permalink
Incorporate PR #57 from the mother branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-Hung Lin committed Feb 5, 2015
1 parent 9b73d76 commit 3e2242d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
20 changes: 15 additions & 5 deletions accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,23 @@
* Alias: `accounting.parse(string)`
*
* Decimal must be included in the regular expression to match floats (defaults to
* accounting.settings.number.decimal), so if the number uses a non-standard decimal
* accounting.settings.number.decimal), so if the number uses a non-standard decimal
* separator, provide it as the second argument.
*
* Also matches bracketed negatives (eg. "$ (1.99)" => -1.99)
*
* Doesn't throw any errors (`NaN`s become 0) but this may change in future
* options:
* returnNaN: if truthy unformat() will return NaN instead of returning 0
* in the case of an error.
*
* Doesn't throw any errors (`NaN`s become 0 unless options.returnNaN is truthy)
*/
var unformat = lib.unformat = lib.parse = function(value, decimal) {
var unformat = lib.unformat = lib.parse = function(value, decimal, options) {
// parse options
var returnNaN = (typeof options == 'object')
&& typeof(options['returnNaN'] != undefined)
&& options.returnNaN;

// Recursively unformat arrays:
if (isArray(value)) {
return map(value, function(val) {
Expand All @@ -197,13 +206,14 @@
var regex = new RegExp("[^0-9-" + decimal + "]", ["g"]),
unformatted = parseFloat(
("" + value)
.replace(/\((.*)\)/, "-$1") // replace bracketed values with negatives
.replace(/\((^[\s]*)\)/, "-$1") // replace bracketed values with negatives when
// there are non-whitespace values in the backets
.replace(regex, '') // strip out any cruft
.replace(decimal, '.') // make sure decimal point is standard
);

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


Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"dependencies" : [],
"repository" : {"type": "git", "url": "git://github.com/openexchangerates/accounting.js.git"},
"main" : "accounting.js",
"version" : "0.4.1"
}
"version" : "0.4.2"
}
8 changes: 6 additions & 2 deletions tests/jasmine/core/unformatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('unformat()', function(){
expect( accounting.unformat('&*()$ -123,456') ).toBe( -123456 );
expect( accounting.unformat(';$@#$%^&-123,456.78') ).toBe( -123456.78 );
});
it('should accept different decimal separators', function(){

it('should accept different decimal separators', function(){
expect( accounting.unformat('$ 123,456', ',') ).toBe( 123.456 );
expect( accounting.unformat('$ 123456|78', '|') ).toBe( 123456.78 );
expect( accounting.unformat('&*()$ 123>456', '>') ).toBe( 123.456 );
Expand All @@ -28,4 +28,8 @@ describe('unformat()', function(){
expect( vals[2] ).toBe( 12345678.901 );
});

it('should return NaN when returnNaN option is truthy', function(){
var val = accounting.unformat('abcdefg', '.', {returnNaN: true});
expect( isNaN(val) ).toBe(true);
});
});

0 comments on commit 3e2242d

Please sign in to comment.