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

currency pricerange formatter #943

Merged
merged 5 commits into from
Sep 9, 2021
Merged

currency pricerange formatter #943

merged 5 commits into from
Sep 9, 2021

Conversation

yen-tt
Copy link
Contributor

@yen-tt yen-tt commented Sep 8, 2021

  • Added Formatter.priceRange() function that utilize locale-currency and currency-symbol-map npm library to retrieve currency symbol based on location's country code (ISO format), and construct the price range accordingly.
  • exclude currency-symbol-map from the node_modules exclusion in webpack babel for production build so it can be transpile to es5

J=SLAP-1552
TEST=manual&auto

  • added jest test for Formatter.priceRange
  • add priceRange for a UK location entity. Launched test site and see that it's displayed in £ instead of default $

- use iso-country-currency npm library to retrieve currency symbol based on location's country code (ISO format), and construct the price range accordingly.

J=SLAP-1552
TEST=manual&auto

- added jest test for Formatter.priceRange
- add priceRange for a UK location. See that it's displayed in £ instead of default $
@coveralls
Copy link

coveralls commented Sep 8, 2021

Coverage Status

Coverage increased (+0.1%) to 8.206% when pulling 1f6c74c on dev/currency-formatter into a426812 on develop.

* @param {string} countrycode The country code from LiveAPI entity (e.g. profile.address.countryCode)
* @return {string} The price range with correct currency symbol formatting according to country code
*/
export function priceRange(priceRange, countryCode) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Minor nit, but I find it confusing to have a function called priceRange that takes in a priceRange. How about naming this something like localizePriceRange?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think keeping the function name as priceRange would be more consistent with the other formatter functions like Formatter.price(). I can rename priceRange param to defaultPriceRange or entityPriceRange?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure, defaultPriceRange or priceRangeFieldValue sgtm!

}
}
console.warn(`Unable to determine currency symbol from ISO country code ${countryCode}.`);
return '';
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this case, would it be better to simply return the original price range, non-localized?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought it might be a bit misleading to have default dollar signs on international locations if we can't localized it so I left it blank. But I can return the original price range if you think that's fine!

Copy link
Collaborator

Choose a reason for hiding this comment

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

My first inclination is that we return the original price range. That way, there's at least something displayed. The formatter is then strictly better than just plopping the original price range on the page. Let's go with that approach for now and see if we get QA feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it. Updated!

if (currencyCode) {
const currencySymbol = getSymbolFromCurrency(currencyCode);
if (currencySymbol) {
return Array(priceRange.length+1).join(currencySymbol);
Copy link
Collaborator

Choose a reason for hiding this comment

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

For my own edification, what does the priceRange object look like?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the given priceRange from entity, if it's defined, is like this: '$', '$$', '$$$', or '$$$$'.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So it's a string? The param type listed in the method signature is object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops, I missed that. Updated!

console.warn(`No price range or country code given.`);
return '';
}
const currencyCode = LocaleCurrency.getCurrency(countryCode);
if (currencyCode) {
const currencySymbol = getSymbolFromCurrency(currencyCode);
if (currencySymbol) {
return Array(priceRange.length+1).join(currencySymbol);
return Array(defaultPriceRange.length+1).join(currencySymbol);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we just do defaultPriceRange.replaceAll('$", currencySymbol) instead? Seems a bit simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm you are right. I guess the backend will only have $ for now. I will update this!

@yen-tt yen-tt merged commit dd9a616 into develop Sep 9, 2021
@yen-tt yen-tt deleted the dev/currency-formatter branch September 9, 2021 13:35
@tmeyer2115 tmeyer2115 mentioned this pull request Sep 14, 2021
tmeyer2115 added a commit that referenced this pull request Sep 14, 2021
## Version 1.24.0
### Features
- A new formatter was added that prints the list of categories applied to an entity. (#946)
- Added a new localized price range formatter. (#943)

### Enhancements
- Google's text highlighting was added to the "View Details" link on the `document-search` Direct Answer card. Note that
the highlighting is only applied when the user's browser is Chrome. (#945)
- The Open Status translations are no longer hard-coded in `static` JS files. Instead, they are sourced from the Theme's
.PO files. This means they can be overridden in custom .PO files. 

### Bugfixes
- Styling was added to the `prominent-image` and `prominent-video` cards to ensure that images and videos, respectively, 
are the same size. (#941, #950)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants