Skip to content

Commit

Permalink
Merge pull request #72 from AndreVale69/feat/add-number-format-71
Browse files Browse the repository at this point in the history
feat: add number format
  • Loading branch information
Zhykos authored Dec 27, 2024
2 parents 3262331 + 521b751 commit 8cc4a7f
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ This software uses the following open source packages:
## Authors

- **Thomas Cicognani** - *Original creator* - [Zhykos](/~https://github.com/Zhykos)
- **Andrea Valentini** - *Contributor* - [AndreVale69](/~https://github.com/AndreVale69)

## License

Expand Down
3 changes: 2 additions & 1 deletion src/prescan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { VideoGamePlatform } from "./scanner/domain/valueobject/VideoGamePlatfor
import { getFileInfo } from "./utils/file.ts";
import { pluralFinalS } from "./utils/plural-final-s.ts";
import { scanDirectory } from "./utils/scan-directory.ts";
import { formatNumber } from "./utils/format-number.ts";

const blueskyMaxFileSize = 976.56 * 1024;

Expand Down Expand Up @@ -50,7 +51,7 @@ export const preScan = (configuration: Configuration, logger: Log): boolean => {
}

logger.log("Pre-scan completed!");
logger.log(`Found ${pluralFinalS(filesCount, "file")}.`);
logger.log(`Found ${formatNumber(filesCount)} ${pluralFinalS(filesCount, "file", false)}.`);

if (errorsCount === 0) {
logger.log("No errors found.");
Expand Down
34 changes: 34 additions & 0 deletions src/utils/format-number.ts
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);
}
89 changes: 89 additions & 0 deletions test/common/utils/formatNumber.test.ts
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);
});
});
});

0 comments on commit 8cc4a7f

Please sign in to comment.