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

Handle currency symbols with a period in fromUSDToNumber #561

Merged
merged 4 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 6 additions & 0 deletions __tests__/Str-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,9 @@ describe('Str.sanitizeURL', () => {
expect(Str.sanitizeURL('HTtp://FOO.com/blah_BLAH')).toBe('http://foo.com/blah_BLAH');
});
});

describe('Str.fromUSDToNumber', () => {
it('Handles currency symbols with a period', () => {
expect(Str.fromUSDToNumber('Bs.S2.48')).toBe('248');
Li357 marked this conversation as resolved.
Show resolved Hide resolved
});
});
11 changes: 7 additions & 4 deletions lib/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@
},

/**
* Converts a USD string into th number of cents it represents.
* Converts a currency string into the number of cents it represents.
*
* @param {String} amountStr A string representing a USD value.
* @param {String} amountStr A string representing a currency symbol and value, like $4.02 or Bs.S97.9
* @param {Boolean} allowFraction Flag indicating if fractions of cents should be
* allowed in the output.
* allowed in the output.
Li357 marked this conversation as resolved.
Show resolved Hide resolved
*
* @return {Number} The cent value of the @p amountStr.
*/
fromUSDToNumber(amountStr, allowFraction) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we merge, we should consider if we want to rename this method since it now does double-duty by handling all types of currency strings (symbols + values) instead of just USD.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I would. fromCurrencyToNumber maybe? Just need to make sure we replace it everywhere

let amount = String(amountStr).replace(/[^\d.\-()]+/g, '');
amountStr = String(amountStr);

Check failure on line 36 in lib/str.js

View workflow job for this annotation

GitHub Actions / lint

Assignment to function parameter 'amountStr'
// Currently, no currency symbol has a number in it, so we find the first number

Check failure on line 37 in lib/str.js

View workflow job for this annotation

GitHub Actions / lint

Expected line before comment
let withoutCurrency = amountStr.substring(amountStr.search(/\d/));

Check failure on line 38 in lib/str.js

View workflow job for this annotation

GitHub Actions / lint

'withoutCurrency' is never reassigned. Use 'const' instead
Li357 marked this conversation as resolved.
Show resolved Hide resolved
let amount = withoutCurrency.replace(/[^\d.\-()]+/g, '');
if (amount.match(/\(.*\)/)) {
const modifiedAmount = amount.replace(/[()]/g, '');
amount = `-${modifiedAmount}`;
Expand Down
Loading