-
Notifications
You must be signed in to change notification settings - Fork 3k
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
IOU - Localize amount text input #7221
Merged
Luke9389
merged 7 commits into
Expensify:main
from
songlang1994:iou-amount-localization
Jan 19, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
55b2372
Localize amount input
songlang1994 2d6d46a
Update README
songlang1994 294c201
Code style
songlang1994 3857085
Clean up unused code
songlang1994 3d7488f
Code style
songlang1994 195891f
Code style
songlang1994 faa5c84
Merge branch 'main' into iou-amount-localization
songlang1994 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import _ from 'underscore'; | ||
|
||
import * as NumberFormatUtils from './NumberFormatUtils'; | ||
|
||
const STANDARD_DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-', ',']; | ||
|
||
const INDEX_DECIMAL = 10; | ||
const INDEX_MINUS_SIGN = 11; | ||
const INDEX_GROUP = 12; | ||
|
||
const getLocaleDigits = _.memoize((locale) => { | ||
const localeDigits = _.clone(STANDARD_DIGITS); | ||
for (let i = 0; i <= 9; i++) { | ||
localeDigits[i] = NumberFormatUtils.format(locale, i); | ||
} | ||
_.forEach(NumberFormatUtils.formatToParts(locale, 1000000.5), (part) => { | ||
switch (part.type) { | ||
case 'decimal': | ||
localeDigits[INDEX_DECIMAL] = part.value; | ||
break; | ||
case 'minusSign': | ||
localeDigits[INDEX_MINUS_SIGN] = part.value; | ||
break; | ||
case 'group': | ||
localeDigits[INDEX_GROUP] = part.value; | ||
break; | ||
default: | ||
break; | ||
} | ||
}); | ||
return localeDigits; | ||
}); | ||
|
||
/** | ||
* Gets the locale digit corresponding to a standard digit. | ||
* | ||
* @param {String} locale | ||
* @param {String} digit - Character of a single standard digit . It may be "0" ~ "9" (digits), | ||
* "," (group separator), "." (decimal separator) or "-" (minus sign). | ||
* @returns {String} | ||
* | ||
* @throws If `digit` is not a valid standard digit. | ||
*/ | ||
function toLocaleDigit(locale, digit) { | ||
const index = _.indexOf(STANDARD_DIGITS, digit); | ||
if (index < 0) { | ||
throw new Error(`"${digit}" must be in ${JSON.stringify(STANDARD_DIGITS)}`); | ||
} | ||
return getLocaleDigits(locale)[index]; | ||
} | ||
|
||
/** | ||
* Gets the standard digit corresponding to a locale digit. | ||
* | ||
* @param {String} locale | ||
* @param {String} localeDigit - Character of a single locale digit. It may be **the localized version** of | ||
* "0" ~ "9" (digits), "," (group separator), "." (decimal separator) or "-" (minus sign). | ||
* @returns {String} | ||
* | ||
* @throws If `localeDigit` is not a valid locale digit. | ||
*/ | ||
function fromLocaleDigit(locale, localeDigit) { | ||
const index = _.indexOf(getLocaleDigits(locale), localeDigit); | ||
if (index < 0) { | ||
throw new Error(`"${localeDigit}" must be in ${JSON.stringify(getLocaleDigits(locale))}`); | ||
} | ||
return STANDARD_DIGITS[index]; | ||
} | ||
|
||
export { | ||
toLocaleDigit, | ||
fromLocaleDigit, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function format(locale, number, options) { | ||
return new Intl.NumberFormat(locale, options).format(number); | ||
} | ||
|
||
function formatToParts(locale, number, options) { | ||
return new Intl.NumberFormat(locale, options).formatToParts(number); | ||
} | ||
|
||
export { | ||
format, | ||
formatToParts, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can't we just use,
numberFormat
prop directly for the whole input value?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.
Unfortunately we can't. Because the amount might not be a valid number yet while inputing (e.g. "12.").
If we do need to use numberFormat the code will be
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.
Ok. What is more performant?
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.
There is no performance issue here since the max length of
amount
is just 8. ButreplaceAllDigits
here is more readable.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.
Sounds, good. I will give it a go.