Skip to content

Commit

Permalink
feat(GB): added tests for GB support
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyso committed Feb 7, 2022
1 parent b2d446d commit b9f488f
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 1 deletion.
235 changes: 235 additions & 0 deletions __tests__/en-GB.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
import { cloneDeep } from 'lodash';
import { ToWords } from '../src/ToWords';
import enGb from '../src/locales/en-GB';

const localeCode = 'en-GB';
const toWords = new ToWords({
localeCode,
});

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

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

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]} Pounds Only`;
return;
}
row[0] = -row[0];
row[1] = `Minus ${row[1]} Pounds 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]} Pounds 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 = [
[0.0, `Zero Pounds Only`],
[0.04, `Zero Pounds And Four Pence Only`],
[0.0468, `Zero Pounds And Five Pence Only`],
[0.4, `Zero Pounds And Forty Pence Only`],
[0.63, `Zero Pounds And Sixty Three Pence Only`],
[0.973, `Zero Pounds And Ninety Seven Pence Only`],
[0.999, `One Pounds Only`],
[37.06, `Thirty Seven Pounds And Six Pence Only`],
[37.068, `Thirty Seven Pounds And Seven Pence Only`],
[37.68, `Thirty Seven Pounds And Sixty Eight Pence Only`],
[37.683, `Thirty Seven Pounds And Sixty Eight Pence 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 Pounds 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 Pounds Only`;
} else {
row[1] = (row[1] as string).replace(new RegExp(` And [\\w ]+ Pence`), '');
}
});

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 ]+ Pence`), '');
});

test.concurrent.each(testFloatsWithCurrencyAndIgnoreZeroCurrencyAndIgnoreDecimals)(
'convert %d => %s',
(input, expected) => {
expect(
toWords.convert(input as number, {
currency: true,
ignoreZeroCurrency: true,
ignoreDecimal: true,
}),
).toBe(expected);
},
);
});
3 changes: 3 additions & 0 deletions dist/ToWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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"));
Expand Down Expand Up @@ -50,6 +51,8 @@ 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 Down
4 changes: 4 additions & 0 deletions dist/locales/en-GB.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;
}
62 changes: 62 additions & 0 deletions dist/locales/en-GB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Locale {
constructor() {
this.config = {
currency: {
name: 'Pound',
plural: 'Pounds',
symbol: '£',
fractionalUnit: {
name: 'Pence',
plural: 'Pence',
symbol: '',
},
},
texts: {
and: 'And',
minus: 'Minus',
only: 'Only',
point: 'Point',
},
numberWordsMapping: [
{ number: 1000000000000000, value: 'Quadrillion' },
{ number: 1000000000000, value: 'Trillion' },
{ number: 1000000000, value: 'Billion' },
{ number: 1000000, value: 'Million' },
{ number: 1000, value: 'Thousand' },
{ number: 100, value: 'Hundred' },
{ number: 90, value: 'Ninety' },
{ number: 80, value: 'Eighty' },
{ number: 70, value: 'Seventy' },
{ number: 60, value: 'Sixty' },
{ number: 50, value: 'Fifty' },
{ number: 40, value: 'Forty' },
{ number: 30, value: 'Thirty' },
{ number: 20, value: 'Twenty' },
{ number: 19, value: 'Nineteen' },
{ number: 18, value: 'Eighteen' },
{ number: 17, value: 'Seventeen' },
{ number: 16, value: 'Sixteen' },
{ number: 15, value: 'Fifteen' },
{ number: 14, value: 'Fourteen' },
{ number: 13, value: 'Thirteen' },
{ number: 12, value: 'Twelve' },
{ number: 11, value: 'Eleven' },
{ number: 10, value: 'Ten' },
{ number: 9, value: 'Nine' },
{ number: 8, value: 'Eight' },
{ number: 7, value: 'Seven' },
{ number: 6, value: 'Six' },
{ number: 5, value: 'Five' },
{ number: 4, value: 'Four' },
{ number: 3, value: 'Three' },
{ number: 2, value: 'Two' },
{ number: 1, value: 'One' },
{ number: 0, value: 'Zero' },
],
exactWordsMapping: [{ number: 100, value: 'One Hundred' }],
};
}
}
exports.default = Locale;
2 changes: 1 addition & 1 deletion src/ToWords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ToWords {
case 'en-US':
return enUs;
case 'en-GB':
return enGb;
return enGb;
case 'fa-IR':
return faIr;
case 'fr-FR':
Expand Down

0 comments on commit b9f488f

Please sign in to comment.