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

Straight revert of 2 PRs without involving any resolve conflict situation #568

Merged
merged 2 commits into from
Aug 23, 2023
Merged
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
43 changes: 0 additions & 43 deletions __tests__/Str-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,49 +96,6 @@ describe('Str.sanitizeURL', () => {
});
});

describe('Str.fromCurrencyToNumber', () => {
it('Handles negative amounts with minus sign', () => {
expect(Str.fromCurrencyToNumber('-$5.23')).toBe(-523);
expect(Str.fromCurrencyToNumber('$-5.23')).toBe(-523);
});

it('Handles negative amounts with ()', () => {
expect(Str.fromCurrencyToNumber('($5.23)')).toBe(-523);
});

it('Handles fractional cents when allowed', () => {
expect(Str.fromCurrencyToNumber('$5.223', true)).toBe(522.3);
});

it('Handles amounts without leading zeros', () => {
expect(Str.fromCurrencyToNumber('$.23')).toBe(23);
});

it('Handles amounts without cents', () => {
expect(Str.fromCurrencyToNumber('$5')).toBe(500);
});

it('Handles currency symbols with a period', () => {
expect(Str.fromCurrencyToNumber('Bs.S2.48')).toBe(248);
expect(Str.fromCurrencyToNumber('Bs.S-2.48')).toBe(-248);
expect(Str.fromCurrencyToNumber('-Bs.S2.48')).toBe(-248);
expect(Str.fromCurrencyToNumber('(Bs.S2.48)')).toBe(-248);
expect(Str.fromCurrencyToNumber('Bs.S.48')).toBe(48);
expect(Str.fromCurrencyToNumber('Bs.S2')).toBe(200);
});

it('Handles amounts with currency symbols', () => {
expect(Str.fromCurrencyToNumber('-5.8')).toBe(-580);
expect(Str.fromCurrencyToNumber(-5.812)).toBe(-581);
expect(Str.fromCurrencyToNumber('24.342')).toBe(2434);
expect(Str.fromCurrencyToNumber(24.342, true)).toBe(2434.2);
});

it('Defaults to 0 when no amountStr is passed', () => {
expect(Str.fromCurrencyToNumber()).toBe(0);
});
});

describe('Str.isValidEmail', () => {
it('Correctly detects a valid email', () => {
expect(Str.isValidEmail('[email protected]')).toBeTruthy();
Expand Down
49 changes: 9 additions & 40 deletions lib/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,19 @@ const Str = {
},

/**
* Converts a currency string into the number of cents it represents.
* Converts a USD string into th number of cents it represents.
*
* @param {String} amountStr 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.
* @param {String} amountStr A string representing a USD value.
* @param {Boolean} allowFraction Flag indicating if fractions of cents should be
* allowed in the output.
*
* @return {Number} The cent value of the @p amountStr.
*/
fromCurrencyToNumber(amountStr, allowFraction) {
if (!amountStr) {
return 0;
}

// eslint-disable-next-line no-param-reassign
amountStr = String(amountStr);

// Ideally, we would pass the currency symbol directly here the same string
// could be interpreted differently based on currency symbol but for now
// we match the amount from the end of the string. This matches just the numeric
// value of the string before we check for negative signs or (...) later
const matches = amountStr.match(/(\.\d+|\d+\.?\d*)\)?$/);
let amount = amountStr;
let matchIndex = 0;
if (matches) {
amount = matches[1];
matchIndex = matches.index;
}

// Now we apply the minus sign if it comes before the numeric value
const minusIndex = amountStr.indexOf('-');
if (amountStr.match(/\(.*\)/) || (minusIndex >= 0 && minusIndex < matchIndex)) {
amount = `-${amount}`;
fromUSDToNumber(amountStr, allowFraction) {
let amount = String(amountStr).replace(/[^\d.\-()]+/g, '');
if (amount.match(/\(.*\)/)) {
const modifiedAmount = amount.replace(/[()]/g, '');
amount = `-${modifiedAmount}`;
}
amount = Number(amount) * 100;

Expand All @@ -68,19 +50,6 @@ const Str = {
return allowFraction ? amount : Math.round(amount);
},

/**
* Wrapper around fromCurrencyToNumber
*
* @deprecated
* @param {String} amountStr 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.
*
* @return {Number} The cent value of the @p amountStr.
*/
fromUSDToNumber(...args) {
return Str.fromCurrencyToNumber(...args);
},

/**
* Truncates the middle section of a string based on the max allowed length
* @param {string} fullStr
Expand Down
Loading