From 5d3adee4b4e005ff0ec5656d9dee27ffa6d52f10 Mon Sep 17 00:00:00 2001 From: fl3xice Date: Sat, 2 Jul 2022 07:16:14 +0400 Subject: [PATCH] excludeNumbers feature --- mod.ts | 2 +- src/Humanity.ts | 12 +++++-- src/Locales.ts | 7 ++-- tests/humanity.test.ts | 72 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 8 deletions(-) diff --git a/mod.ts b/mod.ts index c86fcd5..1989967 100644 --- a/mod.ts +++ b/mod.ts @@ -7,5 +7,5 @@ export const DefaultLocales = { de_DE, en_US, ru_RU }; export type { DeclinationLocale, LocaleObject, - Numbers, + NumberType, } from "./src/Locales.ts"; diff --git a/src/Humanity.ts b/src/Humanity.ts index 411472a..dc2b1d1 100644 --- a/src/Humanity.ts +++ b/src/Humanity.ts @@ -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]; } /** diff --git a/src/Locales.ts b/src/Locales.ts index 10fdc59..6b10108 100644 --- a/src/Locales.ts +++ b/src/Locales.ts @@ -12,7 +12,7 @@ export interface DeclinationLocale { }; } -export type Numbers = +export type NumberType = | "thousand" | "million" | "billion" @@ -23,13 +23,12 @@ export type Numbers = export interface LocaleObject extends DeclinationLocale { locale: string; - excludeNumbers?: Numbers[]; - numbers: Record; + excludeNumbers?: NumberType[]; + numbers: Record; } export const ru_RU: LocaleObject = { locale: "ru_RU", - excludeNumbers: ["billion", "million", "thousand"], useDeclination: true, useCountZerosAfterFirstDigit: true, declinations: { diff --git a/tests/humanity.test.ts b/tests/humanity.test.ts index c0350e0..b4def2f 100644 --- a/tests/humanity.test.ts +++ b/tests/humanity.test.ts @@ -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" + ); +});