Skip to content

Commit

Permalink
Merge pull request #26935 from software-mansion-labs/ts-migration/loc…
Browse files Browse the repository at this point in the history
…ale-digit-utils-lib

[No QA] [TS migration] Migrate 'LocaleDigitUtils.js' lib to TypeScript
  • Loading branch information
NikkiWines authored Sep 13, 2023
2 parents d494433 + a30f45c commit 0f4e4fe
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/libs/LocaleDigitUtils.js → src/libs/LocaleDigitUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from 'underscore';
import _ from 'lodash';

import * as NumberFormatUtils from './NumberFormatUtils';

Expand All @@ -8,12 +8,12 @@ const INDEX_DECIMAL = 10;
const INDEX_MINUS_SIGN = 11;
const INDEX_GROUP = 12;

const getLocaleDigits = _.memoize((locale) => {
const localeDigits = _.clone(STANDARD_DIGITS);
const getLocaleDigits = _.memoize((locale: string): string[] => {
const localeDigits = [...STANDARD_DIGITS];
for (let i = 0; i <= 9; i++) {
localeDigits[i] = NumberFormatUtils.format(locale, i);
}
_.forEach(NumberFormatUtils.formatToParts(locale, 1000000.5), (part) => {
NumberFormatUtils.formatToParts(locale, 1000000.5).forEach((part) => {
switch (part.type) {
case 'decimal':
localeDigits[INDEX_DECIMAL] = part.value;
Expand All @@ -34,15 +34,13 @@ const getLocaleDigits = _.memoize((locale) => {
/**
* 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),
* @param 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);
function toLocaleDigit(locale: string, digit: string): string {
const index = STANDARD_DIGITS.indexOf(digit);
if (index < 0) {
throw new Error(`"${digit}" must be in ${JSON.stringify(STANDARD_DIGITS)}`);
}
Expand All @@ -52,15 +50,13 @@ function toLocaleDigit(locale, digit) {
/**
* 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
* @param 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);
function fromLocaleDigit(locale: string, localeDigit: string): string {
const index = getLocaleDigits(locale).indexOf(localeDigit);
if (index < 0) {
throw new Error(`"${localeDigit}" must be in ${JSON.stringify(getLocaleDigits(locale))}`);
}
Expand Down

0 comments on commit 0f4e4fe

Please sign in to comment.