@ww-samuel/to-words
Version:
Converts numbers (including decimal points) into words & currency.
267 lines (238 loc) • 9.38 kB
text/typescript
import { cloneDeep } from 'lodash';
import { ToWords } from '../src/ToWords';
import enPh from '../src/locales/en-PH';
const localeCode = 'en-PH';
const toWords = new ToWords({
localeCode,
});
describe('Test Locale', () => {
test(`Locale Class: ${localeCode}`, () => {
expect(toWords.getLocaleClass()).toBe(enPh);
});
const wrongLocaleCode = localeCode + '-wrong';
test(`Wrong Locale: ${wrongLocaleCode}`, () => {
const toWordsWrongLocale = new ToWords({
localeCode: wrongLocaleCode,
});
expect(() => toWordsWrongLocale.convert(1)).toThrow(/Unknown Locale/);
});
});
const testIntegers = [
[0, 'Zero'],
[137, 'One Hundred Thirty Seven'],
[700, 'Seven Hundred'],
[1100, 'One Thousand One Hundred'],
[4680, 'Four Thousand Six Hundred Eighty'],
[63892, 'Sixty Three Thousand Eight Hundred Ninety Two'],
[86100, 'Eighty Six Thousand One Hundred'],
[792581, 'Seven Hundred Ninety Two Thousand Five Hundred Eighty One'],
[2741034, 'Two Million Seven Hundred Forty One Thousand Thirty Four'],
[86429753, 'Eighty Six Million Four Hundred Twenty Nine Thousand Seven Hundred Fifty Three'],
[975310864, 'Nine Hundred Seventy Five Million Three Hundred Ten Thousand Eight Hundred Sixty Four'],
[9876543210, 'Nine Billion Eight Hundred Seventy Six Million Five Hundred Forty Three Thousand Two Hundred Ten'],
[
98765432101,
'Ninety Eight Billion Seven Hundred Sixty Five Million Four Hundred Thirty Two Thousand One Hundred One',
],
[
987654321012,
'Nine Hundred Eighty Seven Billion Six Hundred Fifty Four Million Three Hundred Twenty One Thousand Twelve',
],
[
9876543210123,
'Nine Trillion Eight Hundred Seventy Six Billion Five Hundred Forty Three Million Two Hundred Ten Thousand One Hundred Twenty Three',
],
[
98765432101234,
'Ninety Eight Trillion Seven Hundred Sixty Five Billion Four Hundred Thirty Two Million One Hundred One Thousand Two Hundred Thirty Four',
],
];
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] = `Minus ${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]} Pesos Only`;
});
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]} Pesos`;
});
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]} Pesos Only`;
return;
}
row[0] = -row[0];
row[1] = `Minus ${row[1]} Pesos Only`;
});
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]} Pesos Only`;
});
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, 'Zero'],
[0.04, 'Zero Point Zero Four'],
[0.0468, 'Zero Point Zero Four Six Eight'],
[0.4, 'Zero Point Four'],
[0.63, 'Zero Point Sixty Three'],
[0.973, 'Zero Point Nine Hundred Seventy Three'],
[0.999, 'Zero Point Nine Hundred Ninety Nine'],
[37.06, 'Thirty Seven Point Zero Six'],
[37.068, 'Thirty Seven Point Zero Six Eight'],
[37.68, 'Thirty Seven Point Sixty Eight'],
[37.683, 'Thirty Seven Point Six Hundred Eighty Three'],
];
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, `Zero Pesos Only`],
[0.04, `Zero Pesos And Four Cents Only`],
[0.0468, `Zero Pesos And Five Cents Only`],
[0.4, `Zero Pesos And Forty Cents Only`],
[0.63, `Zero Pesos And Sixty Three Cents Only`],
[0.973, `Zero Pesos And Ninety Seven Cents Only`],
[0.999, `One Pesos Only`],
[37.06, `Thirty Seven Pesos And Six Cents Only`],
[37.068, `Thirty Seven Pesos And Seven Cents Only`],
[37.68, `Thirty Seven Pesos And Sixty Eight Cents Only`],
[37.683, `Thirty Seven Pesos And Sixty Eight Cents Only`],
];
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(`Zero Pesos And `, '');
}
});
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] = `Zero Pesos Only`;
} else {
row[1] = (row[1] as string).replace(new RegExp(` And [\\w ]+ Cents`), '');
}
});
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(` And [\\w ]+ Cents`), '');
});
test.concurrent.each(testFloatsWithCurrencyAndIgnoreZeroCurrencyAndIgnoreDecimals)(
'convert %d => %s',
(input, expected) => {
expect(
toWords.convert(input as number, {
currency: true,
ignoreZeroCurrency: true,
ignoreDecimal: true,
}),
).toBe(expected);
},
);
});
const testFloatsWithEuroCurrency = [
[0.0, `Zero Euros Only`],
[0.04, `Zero Euros And Four Eurocents Only`],
[0.0468, `Zero Euros And Five Eurocents Only`],
[0.4, `Zero Euros And Forty Eurocents Only`],
[0.63, `Zero Euros And Sixty Three Eurocents Only`],
[0.973, `Zero Euros And Ninety Seven Eurocents Only`],
[0.999, `One Euros Only`],
[37.06, `Thirty Seven Euros And Six Eurocents Only`],
[37.068, `Thirty Seven Euros And Seven Eurocents Only`],
[37.68, `Thirty Seven Euros And Sixty Eight Eurocents Only`],
[37.683, `Thirty Seven Euros And Sixty Eight Eurocents Only`],
];
const euroCurrencyOptions = {
name: 'Euro',
plural: 'Euros',
symbol: '€',
fractionalUnit: {
name: 'Eurocent',
plural: 'Eurocents',
symbol: '¢',
},
};
describe('Test Floats with options = { currency: true, currencyOptions }', () => {
test.concurrent.each(testFloatsWithEuroCurrency)('convert %d => %s', (input, expected) => {
expect(toWords.convert(input as number, { currency: true, currencyOptions: euroCurrencyOptions })).toBe(expected);
});
});