-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ import HoursStringsLocalizer from './hours/stringslocalizer.js'; | |
import HoursTableBuilder from './hours/table/builder.js'; | ||
import { DayNames } from './hours/constants.js'; | ||
import { generateCTAFieldTypeLink } from './formatters/generate-cta-field-type-link'; | ||
|
||
import LocaleCurrency from 'locale-currency' | ||
import getSymbolFromCurrency from 'currency-symbol-map' | ||
|
||
export function address(profile) { | ||
if (!profile.address) { | ||
|
@@ -522,6 +523,28 @@ export function price(fieldValue = {}, locale) { | |
return price.toLocaleString(localeForFormatting, { style: 'currency', currency: currencyCode }); | ||
} | ||
|
||
/** | ||
* Returns a localized price range string for the given price range ($-$$$$) and country code (ISO format) | ||
* @param {Object} pricerange The price range from LiveAPI entity | ||
* @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) { | ||
if (!priceRange || !countryCode) { | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For my own edification, what does the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 '$$$$'. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, I missed that. Updated! |
||
} | ||
} | ||
console.warn(`Unable to determine currency symbol from ISO country code ${countryCode}.`); | ||
return ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it. Updated! |
||
} | ||
|
||
/** | ||
* Highlights snippets of the provided fieldValue according to the matched substrings. | ||
* Each match will be wrapped in <mark> tags. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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 apriceRange
. How about naming this something likelocalizePriceRange
?There was a problem hiding this comment.
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
orentityPriceRange
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure,
defaultPriceRange
orpriceRangeFieldValue
sgtm!