-
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 2 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,82 @@ | ||
import invariant from 'invariant'; | ||
songlang1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import _ from 'underscore'; | ||
|
||
import * as NumberFormatUtils from './NumberFormatUtils'; | ||
|
||
const STANDARD_DIGITS = Object.freeze(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-', ',']); | ||
songlang1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 Object.freeze(localeDigits); | ||
}); | ||
|
||
/** | ||
* Checks whether the given character is a valid locale digit. | ||
* | ||
* @param {String} locale | ||
* @param {String} char A single character | ||
* @returns {Boolean} | ||
*/ | ||
function isValidLocaleDigit(locale, char) { | ||
return getLocaleDigits(locale).includes(char); | ||
} | ||
|
||
/** | ||
* 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); | ||
invariant(index > -1, `"${digit}" must be in ${JSON.stringify(STANDARD_DIGITS)}`); | ||
songlang1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
invariant(index > -1, `"${localeDigit}" must be in ${JSON.stringify(getLocaleDigits(locale))}`); | ||
songlang1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return STANDARD_DIGITS[index]; | ||
} | ||
|
||
export { | ||
isValidLocaleDigit, | ||
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 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 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
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.
I can't think of any other place in the app that will use these functions so I don't think it is a good idea to pollute the WithLocalize context at this time. Maybe directly use them in the IOUAmountPage for now and pass the locale from
this.props.preferredLocale
. We can refactor these in future if needed.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.
Currently
toLocaleDigit
is also used byBigNumberPad
.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.
Also, I cleaned up the
isValidLocaleDigit
function because it's not used. So onlytoLocaleDigit
andfromLocaleDigit
is added in that context.