-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from AndreVale69/feat/add-number-format-71
feat: add number format
- Loading branch information
Showing
4 changed files
with
126 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* A useful enumeration of common locales. | ||
* | ||
* The values are supported by the BCP 47 standard. | ||
*/ | ||
export enum CommonLocales { | ||
EnglishUnitedStates = 'en-US', | ||
EnglishUnitedKingdom = 'en-GB', | ||
French = 'fr-FR', | ||
German = 'de-DE', | ||
Spanish = 'es-ES', | ||
Italian = 'it-IT', | ||
Japanese = 'ja-JP', | ||
Chinese = 'zh-CN', | ||
Russian = 'ru-RU' | ||
} | ||
|
||
/** | ||
* Formats a number using the specified locale. | ||
* For example, 1000 will be formatted as 1,000 in English (United States). | ||
* | ||
* @param number - The number to format. | ||
* @param locales - The default locale to use for formatting is English (United States). | ||
* You can use the {@link CommonLocales} enumeration to specify a locale, | ||
* or you can hardcode a locale string using the | ||
* {@link https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry|BCP 47 standard}. | ||
* @returns The formatted number. | ||
*/ | ||
export function formatNumber( | ||
number: number, | ||
locales: CommonLocales | string = CommonLocales.EnglishUnitedStates | ||
): string { | ||
return number.toLocaleString(locales); | ||
} |
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,89 @@ | ||
import {assertEquals} from "@std/assert"; | ||
import {describe, it} from "@std/testing/bdd"; | ||
import {CommonLocales, formatNumber} from "../../../src/utils/format-number"; | ||
|
||
describe("Correct Format Number Utils", () => { | ||
// Use a set to store all common locales. | ||
// It is useful to invalidate the test if a new locale is added and not tested. | ||
const commonLocales: Set<string> = new Set(Object.values(CommonLocales)); | ||
it("should format number using default, the English (United States) locale", () => { | ||
const expected: string = "1,000"; | ||
const result: string = formatNumber(1000, CommonLocales.EnglishUnitedStates); | ||
const resultWithDefault: string = formatNumber(1000); | ||
assertEquals(result, expected); | ||
assertEquals(resultWithDefault, expected); | ||
commonLocales.delete(CommonLocales.EnglishUnitedStates); | ||
}); | ||
|
||
it("should format number using English (United Kingdom) locale", () => { | ||
const expected: string = "1,000"; | ||
const result: string = formatNumber(1000, CommonLocales.EnglishUnitedKingdom); | ||
assertEquals(result, expected); | ||
commonLocales.delete(CommonLocales.EnglishUnitedKingdom); | ||
}); | ||
|
||
it("should format number using French locale", () => { | ||
const expected: string = "1" + "\u202F" + "000"; // non-breaking space (NBSP), not equal to 1 000 | ||
const result: string = formatNumber(1000, CommonLocales.French); | ||
assertEquals(result, expected); | ||
commonLocales.delete(CommonLocales.French); | ||
}); | ||
|
||
it("should format number using German locale", () => { | ||
const expected: string = "1.000"; | ||
const result: string = formatNumber(1000, CommonLocales.German); | ||
assertEquals(result, expected); | ||
commonLocales.delete(CommonLocales.German); | ||
}); | ||
|
||
it("should format number using Spanish locale", () => { | ||
const expected: string = "1000"; | ||
const result: string = formatNumber(1000, CommonLocales.Spanish); | ||
assertEquals(result, expected); | ||
commonLocales.delete(CommonLocales.Spanish); | ||
}); | ||
|
||
it("should format number using Italian locale", () => { | ||
const expected: string = "1.000"; | ||
const result: string = formatNumber(1000, CommonLocales.Italian); | ||
assertEquals(result, expected); | ||
commonLocales.delete(CommonLocales.Italian); | ||
}); | ||
|
||
it("should format number using Japanese locale", () => { | ||
const expected: string = "1,000"; | ||
const result: string = formatNumber(1000, CommonLocales.Japanese); | ||
assertEquals(result, expected); | ||
commonLocales.delete(CommonLocales.Japanese); | ||
}); | ||
|
||
it("should format number using Chinese (Simplified) locale", () => { | ||
const expected: string = "1,000"; | ||
const result: string = formatNumber(1000, CommonLocales.Chinese); | ||
assertEquals(result, expected); | ||
commonLocales.delete(CommonLocales.Chinese); | ||
}); | ||
|
||
it("should format number using Russian locale", () => { | ||
const expected: string = "1" + "\u00A0" + "000"; // non-breaking space | ||
const result: string = formatNumber(1000, CommonLocales.Russian); | ||
assertEquals(result, expected); | ||
commonLocales.delete(CommonLocales.Russian); | ||
}); | ||
|
||
it("should test all common locales", () => { | ||
assertEquals(commonLocales.size, 0); | ||
}); | ||
}); | ||
|
||
describe("All format numbers should be the same", () => { | ||
it("should each format number be the same because the digits are less than 1000", () => { | ||
const number: number = 999; | ||
const expected: string = number.toLocaleString("en-US"); | ||
// Test all common locales. | ||
Object.values(CommonLocales).forEach((locale: string) => { | ||
const result: string = formatNumber(number, locale); | ||
assertEquals(result, expected); | ||
}); | ||
}); | ||
}); |