Skip to content

Commit

Permalink
Merge branch 'mastermunj:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
eta1993 authored Jun 20, 2023
2 parents 9cb3c82 + 48304d8 commit f16fea8
Show file tree
Hide file tree
Showing 12 changed files with 653 additions and 498 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [3.6.0](https://github.com/mastermunj/to-words/compare/v3.5.1...v3.6.0) (2023-06-19)


### Features

* add support for only text to be in front of number words ([e96ca2f](https://github.com/mastermunj/to-words/commit/e96ca2fcd80f2366723b7ad43f8ecc8c046355c9))
* **locale:** add estonian (ee-EE) language support ([df44a3f](https://github.com/mastermunj/to-words/commit/df44a3f4d456edd83363393524a8a4e01e21bfef))

### [3.5.1](https://github.com/mastermunj/to-words/compare/v3.5.0...v3.5.1) (2023-04-24)

## [3.5.0](https://github.com/mastermunj/to-words/compare/v3.4.0...v3.5.0) (2023-02-27)
Expand Down
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,27 @@ let words = toWords.convert(0.572, { currency: true, ignoreZeroCurrency: true })
| currencyOptions | object | undefined | By default currency options are taken from the specified locale.<br/>This option allows to specify different currency options while keeping the language details from the selected locale (e.g. convert to English text but use EUR as a currency). You can define different currencies for each call to `convert()` so it works also if you need to dynamically support multiple currencies.<br/>*This works only when `currency = true`* |

## Supported Locale
| Country | Language | Locale |
| ------------- | ------------- | ------------- |
| UAE | English | en-AE |
| Bangladesh | English | en-BD |
| UK | English | en-GB |
| Ghana | English | en-GH |
| India | English | en-IN (default) |
| Myanmar | English | en-MM |
| Mauritius | English | en-MU |
| Nigeria | English | en-NG |
| Nepal | English | en-NP |
| USA | English | en-US |
| Iran | Persian | fa-IR |
| France | French | fr-FR |
| India | Gujarati | gu-IN |
| India | Hindi | hi-IN |
| India | Marathi | mr-IN |
| Suriname | Dutch | nl-SR |
| Brazil | Portuguese | pt-BR |
| Turkey | Turkish | tr-TR |
| Country | Language | Locale |
|------------|------------|-----------------|
| UAE | English | en-AE |
| Bangladesh | English | en-BD |
| UK | English | en-GB |
| Ghana | English | en-GH |
| India | English | en-IN (default) |
| Myanmar | English | en-MM |
| Mauritius | English | en-MU |
| Nigeria | English | en-NG |
| Nepal | English | en-NP |
| USA | English | en-US |
| Estonia | Estonian | ee-EE |
| Iran | Persian | fa-IR |
| France | French | fr-FR |
| India | Gujarati | gu-IN |
| India | Hindi | hi-IN |
| India | Marathi | mr-IN |
| Suriname | Dutch | nl-SR |
| Brazil | Portuguese | pt-BR |
| Turkey | Turkish | tr-TR |


## Inspiration for core logic
Expand Down
252 changes: 252 additions & 0 deletions __tests__/ee-EE.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
import { cloneDeep } from 'lodash';
import { ToWords } from '../src/ToWords';
import eeEe from '../src/locales/ee-EE';

const localeCode = 'ee-EE';
const toWords = new ToWords({
localeCode,
});

describe('Test Locale', () => {
test(`Locale Class: ${localeCode}`, () => {
expect(toWords.getLocaleClass()).toBe(eeEe);
});

const wrongLocaleCode = localeCode + '-wrong';
test(`Wrong Locale: ${wrongLocaleCode}`, () => {
const toWordsWrongLocale = new ToWords({
localeCode: wrongLocaleCode,
});
expect(() => toWordsWrongLocale.convert(1)).toThrow(/Unknown Locale/);
});
});

const testIntegers = [
[0, 'Null'],
[100, 'Ükssada'],
[137, 'Sada Kolmkümmend Seitse'],
[700, 'Seitsesada'],
[1100, 'Üks Tuhat Ükssada'],
[4680, 'Neli Tuhat Kuussada Kaheksakümmend'],
[63892, 'Kuuskümmend Kolm Tuhat Kaheksasada Üheksakümmend Kaks'],
[792581, 'Seitsesada Üheksakümmend Kaks Tuhat Viissada Kaheksakümmend Üks'],
[2741034, 'Kaks Miljonit Seitsesada Nelikümmend Üks Tuhat Kolmkümmend Neli'],
[86429753, 'Kaheksakümmend Kuus Miljonit Nelisada Kakskümmend Üheksa Tuhat Seitsesada Viiskümmend Kolm'],
[975310864, 'Üheksasada Seitsekümmend Viis Miljonit Kolmsada Kümme Tuhat Kaheksasada Kuuskümmend Neli'],
[
9999923128,
'Üheksa Miljardit Üheksasada Üheksakümmend Üheksa Miljonit Üheksasada Kakskümmend Kolm Tuhat Sada Kakskümmend Kaheksa',
],
[100000000000, 'Ükssada Miljardit'],
[1000000000000, 'Üks Triljon'],
];

describe('Test Integers with options = {}', () => {
test.concurrent.each(testIntegers)('convert %d => %s', (input, expected) => {
expect(toWords.convert(input as number)).toBe(expected);
});
});

describe('Test Negative Integers with options = {}', () => {
const testNegativeIntegers = cloneDeep(testIntegers);
testNegativeIntegers.map((row, i) => {
if (i === 0) {
return;
}
row[0] = -row[0];
row[1] = `Miinus ${row[1]}`;
});

test.concurrent.each(testNegativeIntegers)('convert %d => %s', (input, expected) => {
expect(toWords.convert(input as number)).toBe(expected);
});
});

describe('Test Integers with options = { currency: true }', () => {
const testIntegersWithCurrency = cloneDeep(testIntegers);
testIntegersWithCurrency.map((row) => {
row[1] = `Ainult ${row[1]} Eurot`;
});

test.concurrent.each(testIntegersWithCurrency)('convert %d => %s', (input, expected) => {
expect(toWords.convert(input as number, { currency: true })).toBe(expected);
});
});

describe('Test Integers with options = { currency: true, doNotAddOnly: true }', () => {
const testIntegersWithCurrency = cloneDeep(testIntegers);
testIntegersWithCurrency.map((row) => {
row[1] = `${row[1]} Eurot`;
});

test.concurrent.each(testIntegersWithCurrency)('convert %d => %s', (input, expected) => {
expect(toWords.convert(input as number, { currency: true, doNotAddOnly: true })).toBe(expected);
});
});

describe('Test Negative Integers with options = { currency: true }', () => {
const testNegativeIntegersWithCurrency = cloneDeep(testIntegers);
testNegativeIntegersWithCurrency.map((row, i) => {
if (i === 0) {
row[1] = `Ainult ${row[1]} Eurot`;
return;
}
row[0] = -row[0];
row[1] = `Ainult Miinus ${row[1]} Eurot`;
});

test.concurrent.each(testNegativeIntegersWithCurrency)('convert %d => %s', (input, expected) => {
expect(toWords.convert(input as number, { currency: true })).toBe(expected);
});
});

describe('Test Integers with options = { currency: true, ignoreZeroCurrency: true }', () => {
const testIntegersWithCurrencyAndIgnoreZeroCurrency = cloneDeep(testIntegers);
testIntegersWithCurrencyAndIgnoreZeroCurrency.map((row, i) => {
row[1] = i === 0 ? '' : `Ainult ${row[1]} Eurot`;
});

test.concurrent.each(testIntegersWithCurrencyAndIgnoreZeroCurrency)('convert %d => %s', (input, expected) => {
expect(
toWords.convert(input as number, {
currency: true,
ignoreZeroCurrency: true,
}),
).toBe(expected);
});
});

const testFloats = [
[0.0, 'Null'],
[0.04, 'Null Koma Null Neli'],
[0.0468, 'Null Koma Null Neli Kuus Kaheksa'],
[0.4, 'Null Koma Neli'],
[0.63, 'Null Koma Kuuskümmend Kolm'],
[0.973, 'Null Koma Üheksasada Seitsekümmend Kolm'],
[0.999, 'Null Koma Üheksasada Üheksakümmend Üheksa'],
[37.06, 'Kolmkümmend Seitse Koma Null Kuus'],
[37.068, 'Kolmkümmend Seitse Koma Null Kuus Kaheksa'],
[37.0684, 'Kolmkümmend Seitse Koma Null Kuus Kaheksa Neli'],
];

describe('Test Floats with options = {}', () => {
test.concurrent.each(testFloats)('convert %d => %s', (input, expected) => {
expect(toWords.convert(input as number)).toBe(expected);
});
});

const testFloatsWithCurrency: [number, string][] = [
[0.0, `Ainult Null Eurot`],
[0.04, `Ainult Null Eurot Ja Neli Senti`],
[0.0468, `Ainult Null Eurot Ja Viis Senti`],
[0.4, `Ainult Null Eurot Ja Nelikümmend Senti`],
[0.63, `Ainult Null Eurot Ja Kuuskümmend Kolm Senti`],
[0.973, `Ainult Null Eurot Ja Üheksakümmend Seitse Senti`],
[0.999, `Ainult Üks Eurot`],
[37.06, `Ainult Kolmkümmend Seitse Eurot Ja Kuus Senti`],
[37.068, `Ainult Kolmkümmend Seitse Eurot Ja Seitse Senti`],
[37.0684, `Ainult Kolmkümmend Seitse Eurot Ja Seitse Senti`],
];

describe('Test Floats with options = { currency: true }', () => {
test.concurrent.each(testFloatsWithCurrency)('convert %d => %s', (input, expected) => {
expect(toWords.convert(input as number, { currency: true })).toBe(expected);
});
});

describe('Test Floats with options = { currency: true, ignoreZeroCurrency: true }', () => {
const testFloatsWithCurrencyAndIgnoreZeroCurrency = cloneDeep(testFloatsWithCurrency);
testFloatsWithCurrencyAndIgnoreZeroCurrency[0][1] = '';
testFloatsWithCurrencyAndIgnoreZeroCurrency.map((row, i) => {
if (i === 0) {
row[1] = '';
return;
}
if (row[0] > 0 && row[0] < 1) {
row[1] = (row[1] as string).replace(`Null Eurot Ja `, '');
}
});

test.concurrent.each(testFloatsWithCurrencyAndIgnoreZeroCurrency)('convert %d => %s', (input, expected) => {
expect(
toWords.convert(input as number, {
currency: true,
ignoreZeroCurrency: true,
}),
).toBe(expected);
});
});

describe('Test Floats with options = { currency: true, ignoreDecimal: true }', () => {
const testFloatsWithCurrencyAndIgnoreDecimal = cloneDeep(testFloatsWithCurrency);
testFloatsWithCurrencyAndIgnoreDecimal.map((row) => {
if (row[0] === 0.999 || row[0] === 0.63 || row[0] === 0.4 || row[0] === 0.973) {
row[1] = `Ainult Null Eurot`;
} else {
row[1] = (row[1] as string).replace(new RegExp(` Ja [\\w ]+ Senti`), '');
}
});

test.concurrent.each(testFloatsWithCurrencyAndIgnoreDecimal)('convert %d => %s', (input, expected) => {
expect(
toWords.convert(input as number, {
currency: true,
ignoreDecimal: true,
}),
).toBe(expected);
});
});

describe('Test Floats with options = { currency: true, ignoreZeroCurrency: true, ignoreDecimal: true }', () => {
const testFloatsWithCurrencyAndIgnoreZeroCurrencyAndIgnoreDecimals = cloneDeep(testFloatsWithCurrency);
testFloatsWithCurrencyAndIgnoreZeroCurrencyAndIgnoreDecimals[0][1] = '';
testFloatsWithCurrencyAndIgnoreZeroCurrencyAndIgnoreDecimals.map((row) => {
if (row[0] > 0 && row[0] < 1) {
row[1] = '';
}
row[1] = (row[1] as string).replace(new RegExp(` Ja [\\w ]+ Senti`), '');
});

test.concurrent.each(testFloatsWithCurrencyAndIgnoreZeroCurrencyAndIgnoreDecimals)(
'convert %d => %s',
(input, expected) => {
expect(
toWords.convert(input as number, {
currency: true,
ignoreZeroCurrency: true,
ignoreDecimal: true,
}),
).toBe(expected);
},
);
});

const testFloatsWithDollarCurrency = [
[0.0, `Ainult Null Dollarit`],
[0.04, `Ainult Null Dollarit Ja Neli Senti`],
[0.0468, `Ainult Null Dollarit Ja Viis Senti`],
[0.4, `Ainult Null Dollarit Ja Nelikümmend Senti`],
[0.63, `Ainult Null Dollarit Ja Kuuskümmend Kolm Senti`],
[0.973, `Ainult Null Dollarit Ja Üheksakümmend Seitse Senti`],
[0.999, `Ainult Üks Dollarit`],
[37.06, `Ainult Kolmkümmend Seitse Dollarit Ja Kuus Senti`],
[37.068, `Ainult Kolmkümmend Seitse Dollarit Ja Seitse Senti`],
[37.0684, `Ainult Kolmkümmend Seitse Dollarit Ja Seitse Senti`],
];

const dollarCurrencyOption = {
name: 'Dollar',
plural: 'Dollarit',
symbol: '$',
fractionalUnit: {
name: 'Sent',
plural: 'Senti',
symbol: '¢',
},
};

describe('Test Floats with options = { currency: true, currencyOptions }', () => {
test.concurrent.each(testFloatsWithDollarCurrency)('convert %d => %s', (input, expected) => {
expect(toWords.convert(input as number, { currency: true, currencyOptions: dollarCurrencyOption })).toBe(expected);
});
});
8 changes: 7 additions & 1 deletion dist/ToWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const mr_IN_1 = __importDefault(require("./locales/mr-IN"));
const pt_BR_1 = __importDefault(require("./locales/pt-BR"));
const tr_TR_1 = __importDefault(require("./locales/tr-TR"));
const nl_SR_1 = __importDefault(require("./locales/nl-SR"));
const ee_EE_1 = __importDefault(require("./locales/ee-EE"));
exports.DefaultConverterOptions = {
currency: false,
ignoreDecimal: false,
Expand All @@ -41,6 +42,8 @@ class ToWords {
getLocaleClass() {
/* eslint-disable @typescript-eslint/no-var-requires */
switch (this.options.localeCode) {
case 'ee-EE':
return ee_EE_1.default;
case 'en-AE':
return en_AE_1.default;
case 'en-BD':
Expand Down Expand Up @@ -186,12 +189,15 @@ class ToWords {
if (!isEmpty && isNegativeNumber) {
words.unshift(locale.config.texts.minus);
}
if (!isEmpty && locale.config.texts.only && !options.doNotAddOnly) {
if (!isEmpty && locale.config.texts.only && !options.doNotAddOnly && !locale.config.onlyInFront) {
wordsWithDecimal.push(locale.config.texts.only);
}
if (wordsWithDecimal.length) {
words.push(...wordsWithDecimal);
}
if (!isEmpty && !options.doNotAddOnly && locale.config.onlyInFront) {
words.splice(0, 0, locale.config.texts.only);
}
return words;
}
convertInternal(number) {
Expand Down
4 changes: 4 additions & 0 deletions dist/locales/ee-EE.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { LocaleConfig, LocaleInterface } from '../types';
export default class Locale implements LocaleInterface {
config: LocaleConfig;
}
Loading

0 comments on commit f16fea8

Please sign in to comment.