Skip to content

Commit

Permalink
min update
Browse files Browse the repository at this point in the history
  • Loading branch information
fl3xice committed Jul 2, 2022
1 parent 8abc48b commit ab3fa02
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
6 changes: 5 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export { default as Humanity } from "./src/Humanity.ts";
export { createCustomHumanity, createHumanity } from "./src/Humanity.ts";
export type { LocaleHumanity } from "./src/Humanity.ts";
export const DefaultLocales = { de_DE, en_US, ru_RU };
export type { DeclinationLocale, LocaleObject } from "./src/Locales.ts";
export type {
DeclinationLocale,
LocaleObject,
Numbers,
} from "./src/Locales.ts";
3 changes: 2 additions & 1 deletion src/Humanity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class Humanity {
}

private switchWord(lenZeros: number): string {
if (lenZeros < 5 || lenZeros > 18) {
const numbers = Object.keys(this.localeObject.numbers);
if (lenZeros < 5 || lenZeros > (numbers.length + 1) * 3) {
return lenZeros.toString();
}

Expand Down
23 changes: 15 additions & 8 deletions src/Locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ export interface DeclinationLocale {
};
}

export type Numbers =
| "thousand"
| "million"
| "billion"
| "trillion"
| "quadrillion"
| "quintillion"
| "sexillion";

export interface LocaleObject extends DeclinationLocale {
locale: string;
numbers: {
thousand: string;
million: string;
billion: string;
trillion: string;
quadrillion: string;
quintillion: string;
};
excludeNumbers?: Numbers[];
numbers: Record<Numbers, string>;
}

export const ru_RU: LocaleObject = {
locale: "ru_RU",
excludeNumbers: ["billion", "million", "thousand"],
useDeclination: true,
useCountZerosAfterFirstDigit: true,
declinations: {
Expand All @@ -40,6 +44,7 @@ export const ru_RU: LocaleObject = {
trillion: "триллион",
quadrillion: "квадриллион",
quintillion: "квинтиллион",
sexillion: "сиксилион",
},
};

Expand All @@ -52,6 +57,7 @@ export const en_US: LocaleObject = {
trillion: "trillion",
quadrillion: "quadrillion",
quintillion: "quintillion",
sexillion: "sextillion",
},
};

Expand All @@ -64,5 +70,6 @@ export const de_DE: LocaleObject = {
trillion: "billion",
quadrillion: "quadrilliarde",
quintillion: "quintillion",
sexillion: "sextillion",
},
};
23 changes: 20 additions & 3 deletions tests/humanity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ Deno.test("Test humanity class", () => {
"1 quadrillion"
);
assertEquals(
Humanity.number(10000000000000000),
Humanity.number(10000000000000000n),
"10 quadrillion",
"10 quadrillion"
);
assertEquals(
Humanity.number(100000000000000000),
Humanity.number(100000000000000000n),
"100 quadrillion",
"100 quadrillion"
);
assertEquals(
Humanity.number(1000000000000000000),
Humanity.number(1000000000000000000n),
"1 quintillion",
"1 quintillion"
);
Expand All @@ -46,6 +46,22 @@ Deno.test("Test humanity class", () => {
assertEquals(Humanity.number(2500000000), "2 billion", "2 billion");
// 200 000 000 000
assertEquals(Humanity.number(250000000000), "200 billion", "200 billion");

assertEquals(
Humanity.number(10000000000000000000n),
"10 quintillion",
"10 quintillion"
);
assertEquals(
Humanity.number(100000000000000000000n),
"100 quintillion",
"100 quintillion"
);
assertEquals(
Humanity.number(1000000000000000000000n),
"1 sextillion",
"1 sextillion"
);
});

Deno.test("Ru Locale with declinations", () => {
Expand Down Expand Up @@ -78,6 +94,7 @@ Deno.test("Custom Locale", () => {
trillion: "xe",
quadrillion: "fa",
quintillion: "ier",
sexillion: "sex",
},
});

Expand Down

0 comments on commit ab3fa02

Please sign in to comment.