Skip to content

Commit

Permalink
excludeNumbers feature
Browse files Browse the repository at this point in the history
  • Loading branch information
fl3xice committed Jul 2, 2022
1 parent ab3fa02 commit 5d3adee
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export const DefaultLocales = { de_DE, en_US, ru_RU };
export type {
DeclinationLocale,
LocaleObject,
Numbers,
NumberType,
} from "./src/Locales.ts";
12 changes: 9 additions & 3 deletions src/Humanity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ class Humanity {
return lenZeros.toString();
}

return Object.values(this.localeObject.numbers)[
Math.floor(lenZeros / 3) - 1
];
const found = Math.floor(lenZeros / 3) - 1;
if (
this.localeObject.excludeNumbers &&
this.localeObject.excludeNumbers.some((x) => x == numbers[found])
) {
return lenZeros.toString();
}

return Object.values(this.localeObject.numbers)[found];
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface DeclinationLocale {
};
}

export type Numbers =
export type NumberType =
| "thousand"
| "million"
| "billion"
Expand All @@ -23,13 +23,12 @@ export type Numbers =

export interface LocaleObject extends DeclinationLocale {
locale: string;
excludeNumbers?: Numbers[];
numbers: Record<Numbers, string>;
excludeNumbers?: NumberType[];
numbers: Record<NumberType, string>;
}

export const ru_RU: LocaleObject = {
locale: "ru_RU",
excludeNumbers: ["billion", "million", "thousand"],
useDeclination: true,
useCountZerosAfterFirstDigit: true,
declinations: {
Expand Down
72 changes: 72 additions & 0 deletions tests/humanity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,75 @@ Deno.test("Disabling features", () => {
Humanity.disable("spacing");
assertEquals(Humanity.number(100000), "100thousand", "100 thousand");
});

Deno.test("Test humanity class", () => {
const Humanity = createCustomHumanity({
locale: "custom",
excludeNumbers: ["billion"],
numbers: {
thousand: "thousand",
million: "million",
billion: "billion",
trillion: "trillion",
quadrillion: "quadrillion",
quintillion: "quintillion",
sexillion: "sextillion",
},
});
assertEquals(Humanity.number(1), "1", "one");
assertEquals(Humanity.number(100000), "100 thousand", "100 thousand");
assertEquals(Humanity.number(1000000), "1 million", "1 million");
assertEquals(Humanity.number(10000000), "10 million", "10 million");
assertEquals(Humanity.number(100000000), "100 million", "100 million");
assertEquals(Humanity.number(1000000000), "1000000000", "1 billion");
assertEquals(Humanity.number(10000000000), "10000000000", "10 billion");
assertEquals(Humanity.number(100000000000), "100000000000", "100 billion");
assertEquals(Humanity.number(1000000000000), "1 trillion", "1 trillion");
assertEquals(Humanity.number(10000000000000), "10 trillion", "10 trillion");
assertEquals(
Humanity.number(100000000000000),
"100 trillion",
"100 trillion"
);
assertEquals(
Humanity.number(1000000000000000),
"1 quadrillion",
"1 quadrillion"
);
assertEquals(
Humanity.number(10000000000000000n),
"10 quadrillion",
"10 quadrillion"
);
assertEquals(
Humanity.number(100000000000000000n),
"100 quadrillion",
"100 quadrillion"
);
assertEquals(
Humanity.number(1000000000000000000n),
"1 quintillion",
"1 quintillion"
);
assertEquals(Humanity.number(2500000), "2 million", "2 million");
// 2 000 000 000
assertEquals(Humanity.number(2500000000), "2500000000", "2 billion");
// 200 000 000 000
assertEquals(Humanity.number(250000000000), "250000000000", "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"
);
});

0 comments on commit 5d3adee

Please sign in to comment.