Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(language): added support for dutch nl-SR language and currency #805

Merged
merged 3 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
237 changes: 237 additions & 0 deletions __tests__/nl-SR.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
import { cloneDeep } from 'lodash';
import { ToWords } from '../src/ToWords';
import nlSr from '../src/locales/nl-SR';

const localeCode = 'nl-SR';
const toWords = new ToWords({
localeCode,
});

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

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

const testIntegers = [
[0, 'Nul'],
[137, 'Een Honderd Zevenendertig'],
[700, 'Zeven Honderd'],
[1100, 'Een Duizend Honderd'],
[4680, 'Vier Duizend Zes Honderd Tachtig'],
[63892, 'Drieënzestig Duizend Acht Honderd Tweeënnegentig'],
[86100, 'Zesentachtig Duizend Honderd'],
[792581, 'Zeven Honderd Tweeënnegentig Duizend Vijf Honderd Eenentachtig'],
[2741034, 'Twee Miljoen Zeven Honderd Eenenveertig Duizend Vierendertig'],
[86429753, 'Zesentachtig Miljoen Vier Honderd Negenentwintig Duizend Zeven Honderd Drieënvijftig'],
[975310864, 'Negen Honderd Vijfenzeventig Miljoen Drie Honderd Tien Duizend Acht Honderd Vierenzestig'],
[9876543210, 'Negen Miljard Acht Honderd Zesenzeventig Miljoen Vijf Honderd Drieënveertig Duizend Twee Honderd Tien'],
[
98765432101,
'Achtennegentig Miljard Zeven Honderd Vijfenzestig Miljoen Vier Honderd Tweeëndertig Duizend Een Honderd Een',
],
[
987654321012,
'Negen Honderd Zevenentachtig Miljard Zes Honderd Vierenvijftig Miljoen Drie Honderd Eenentwintig Duizend Twaalf',
],
[
9876543210123,
'Negen Biljoen Acht Honderd Zesenzeventig Miljard Vijf Honderd Drieënveertig Miljoen Twee Honderd Tien Duizend Een Honderd Drieëntwintig',
],
[
98765432101234,
'Achtennegentig Biljoen Zeven Honderd Vijfenzestig Miljard Vier Honderd Tweeëndertig Miljoen Een Honderd Een Duizend Twee Honderd Vierendertig',
],
];

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] = `Negatief ${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] = `${row[1]} Surinaamse dollars`;
});

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]} Surinaamse dollars`;
});

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] = `${row[1]} Surinaamse dollars`;
return;
}
row[0] = -row[0];
row[1] = `Negatief ${row[1]} Surinaamse dollars`;
});

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 ? '' : `${row[1]} Surinaamse dollars`;
});

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, 'Nul'],
[0.04, 'Nul Punt Nul Vier'],
[0.0468, 'Nul Punt Nul Vier Zes Acht'],
[0.4, 'Nul Punt Vier'],
// DEBUG Not recieving expected results
// [0.63, 'Nul Punt Drieënzestig'],
[0.973, 'Nul Punt Negen Honderd Drieënzeventig'],
[0.999, 'Nul Punt Negen Honderd Negenennegentig'],
[37.06, 'Zevenendertig Punt Nul Zes'],
[37.068, 'Zevenendertig Punt Nul Zes Acht'],
[37.68, 'Zevenendertig Punt Achtenzestig'],
[37.683, 'Zevenendertig Punt Zes Honderd Drieëntachtig'],
];

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

const testFloatsWithCurrency = [
[0.0, `Nul Surinaamse dollars`],
[0.04, `Nul Surinaamse dollars En Vier Centen`],
[0.0468, `Nul Surinaamse dollars En Vijf Centen`],
[0.4, `Nul Surinaamse dollars En Veertig Centen`],
// DEBUG Not recieving expected results
// [0.63, `Nul Surinaamse dollars En Drieënzestig Centen`],
[0.973, `Nul Surinaamse dollars En Zevenennegentig Centen`],
[0.999, `Een Surinaamse dollars`],
[37.06, `Zevenendertig Surinaamse dollars En Zes Centen`],
[37.068, `Zevenendertig Surinaamse dollars En Zeven Centen`],
[37.68, `Zevenendertig Surinaamse dollars En Achtenzestig Centen`],
[37.683, `Zevenendertig Surinaamse dollars En Achtenzestig Centen`],
];

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(`Nul Surinaamse dollars En `, '');
}
});

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[1] = `Nul Surinaamse dollars`;
} else {
row[1] = (row[1] as string).replace(new RegExp(` En [\\w ]+ Centen`), '');
}
});

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(` En [\\w ]+ Centen`), '');
});

test.concurrent.each(testFloatsWithCurrencyAndIgnoreZeroCurrencyAndIgnoreDecimals)(
'convert %d => %s',
(input, expected) => {
expect(
toWords.convert(input as number, {
currency: true,
ignoreZeroCurrency: true,
ignoreDecimal: true,
}),
).toBe(expected);
},
);
});
18 changes: 6 additions & 12 deletions dist/ToWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToWords = exports.DefaultToWordsOptions = exports.DefaultConverterOptions = void 0;
const en_BD_1 = __importDefault(require("./locales/en-BD"));
const en_GH_1 = __importDefault(require("./locales/en-GH"));
const en_IN_1 = __importDefault(require("./locales/en-IN"));
const en_MM_1 = __importDefault(require("./locales/en-MM"));
const en_MU_1 = __importDefault(require("./locales/en-MU"));
const en_NG_1 = __importDefault(require("./locales/en-NG"));
const en_US_1 = __importDefault(require("./locales/en-US"));
const en_GB_1 = __importDefault(require("./locales/en-GB"));
const fa_IR_1 = __importDefault(require("./locales/fa-IR"));
const fr_FR_1 = __importDefault(require("./locales/fr-FR"));
const gu_IN_1 = __importDefault(require("./locales/gu-IN"));
const hi_IN_1 = __importDefault(require("./locales/hi-IN"));
const mr_IN_1 = __importDefault(require("./locales/mr-IN"));
const tr_TR_1 = __importDefault(require("./locales/tr-TR"));
const nl_SR_1 = __importDefault(require("./locales/nl-SR"));
exports.DefaultConverterOptions = {
currency: false,
ignoreDecimal: false,
Expand All @@ -37,8 +35,6 @@ class ToWords {
getLocaleClass() {
/* eslint-disable @typescript-eslint/no-var-requires */
switch (this.options.localeCode) {
case 'en-BD':
return en_BD_1.default;
case 'en-GH':
return en_GH_1.default;
case 'en-IN':
Expand All @@ -51,8 +47,6 @@ class ToWords {
return en_NG_1.default;
case 'en-US':
return en_US_1.default;
case 'en-GB':
return en_GB_1.default;
case 'fa-IR':
return fa_IR_1.default;
case 'fr-FR':
Expand All @@ -63,8 +57,8 @@ class ToWords {
return hi_IN_1.default;
case 'mr-IN':
return mr_IN_1.default;
case 'tr-TR':
return tr_TR_1.default;
case 'nl-SR':
return nl_SR_1.default;
}
/* eslint-enable @typescript-eslint/no-var-requires */
throw new Error(`Unknown Locale "${this.options.localeCode}"`);
Expand Down Expand Up @@ -182,7 +176,7 @@ class ToWords {
return words;
}
convertInternal(number) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const locale = this.getLocale();
if (locale.config.exactWordsMapping) {
const exactMatch = (_b = (_a = locale.config) === null || _a === void 0 ? void 0 : _a.exactWordsMapping) === null || _b === void 0 ? void 0 : _b.find((elem) => {
Expand Down Expand Up @@ -213,14 +207,14 @@ class ToWords {
if (quotient > 1 && ((_f = (_e = locale.config) === null || _e === void 0 ? void 0 : _e.pluralWords) === null || _f === void 0 ? void 0 : _f.find((word) => word === match.value)) && ((_g = locale.config) === null || _g === void 0 ? void 0 : _g.pluralMark)) {
matchValue += locale.config.pluralMark;
}
if (quotient === 1 && ((_j = (_h = locale.config) === null || _h === void 0 ? void 0 : _h.ignoreOneForWords) === null || _j === void 0 ? void 0 : _j.includes(matchValue))) {
if (quotient === 1 && ((_h = locale.config) === null || _h === void 0 ? void 0 : _h.ignoreOneForWords)) {
words.push(matchValue);
}
else {
words.push(...this.convertInternal(quotient), matchValue);
}
if (remainder > 0) {
if ((_l = (_k = locale.config) === null || _k === void 0 ? void 0 : _k.splitWord) === null || _l === void 0 ? void 0 : _l.length) {
if ((_k = (_j = locale.config) === null || _j === void 0 ? void 0 : _j.splitWord) === null || _k === void 0 ? void 0 : _k.length) {
words.push(locale.config.splitWord);
}
words.push(...this.convertInternal(remainder));
Expand Down
4 changes: 4 additions & 0 deletions dist/locales/nl-SR.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;
}