openexchangerates / accounting.js

A lightweight JavaScript library for number, money and currency formatting - fully localisable, zero dependencies.
http://openexchangerates.github.io/accounting.js
MIT License
4.96k stars 530 forks source link

update regExp literal to fix failing accounting.unformat() jasmine tests. #162

Closed jjsquillante closed 7 years ago

jjsquillante commented 7 years ago

@wjcrowcroft Hope all's well!

I'm submitting a pull request designed to fix all of the failing jasmine tests for unformatSpect.js.

I noticed a pattern in the 3 unformatSpec failures where the regular expression literal (within the replace method) was not considering the case when parentheses are simply passed as a special character. In this case (ie. accounting.unformat('&*()$ 123,456')), the expected value is a positive number but unformat returns a negative value.

I propose updating the regular expression literal to match only if an open parentheses is followed by a numeric character, then strip out both parentheses and add the negative symbol.
IE. accounting.unformat('&*()$ 123,456') // returns 123456 accounting.unformat('&*()$ (123,456)') // returns -123456

The 3 failing tests, now pass. I've also added an extra test below to confirm the regExp works as designed:

expect( accounting.unformat('&*()$(123,456)A$@P') ).toBe( -123456 );

Please reach out if there's anything that I can help further clarify in my PR. Thanks!

wjcrowcroft commented 7 years ago

@jjsquillante Thanks for submitting this PR and the clear examples!

jjsquillante commented 7 years ago

@wjcrowcroft glad to help! thank you!