Skip to content

Commit

Permalink
feat: support added for french (fr-FR) (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
florianchappaz authored Aug 6, 2021
1 parent 7932af0 commit ae19f85
Show file tree
Hide file tree
Showing 9 changed files with 532 additions and 10 deletions.
185 changes: 185 additions & 0 deletions __tests__/fr-FR.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { ToWords } from '../src/to-words';
import cloneDeep from 'lodash/cloneDeep';

const toWords = new ToWords({
localeCode: 'fr-FR',
});

const testIntegers = [
[0, 'Zéro'],
[137, 'Cent Trente-Sept'],
[700, 'Sept Cent'],
[4680, 'Quatre Mille Six Cent Quatre-Vingt'],
[63892, 'Soixante-Trois Mille Huit Cent Quatre-Vingt-Douze'],
[792581, 'Sept Cent Quatre-Vingt-Douze Mille Cinq Cent Quatre-Vingt-Un'],
[2741034, 'Deux Millions Sept Cent Quarante Et Un Mille Trente-Quatre'],
[86429753, 'Quatre-Vingt-Six Millions Quatre Cent Vingt-Neuf Mille Sept Cent Cinquante-Trois'],
[975310864, 'Neuf Cent Soixante-Quinze Millions Trois Cent Dix Mille Huit Cent Soixante-Quatre'],
[9876543210, 'Neuf Milliards Huit Cent Soixante-Seize Millions Cinq Cent Quarante-Trois Mille Deux Cent Dix'],
[
98765432101,
'Quatre-Vingt-Dix-Huit Milliards Sept Cent Soixante-Cinq Millions Quatre Cent Trente-Deux Mille Cent Un',
],
[
987654321012,
'Neuf Cent Quatre-Vingt-Sept Milliards Six Cent Cinquante-Quatre Millions Trois Cent Vingt Et Un Mille Douze',
],
[
9876543210123,
'Neuf Billions Huit Cent Soixante-Seize Milliards Cinq Cent Quarante-Trois Millions Deux Cent Dix Mille Cent Vingt-Trois',
],
[
98765432101234,
'Quatre-Vingt-Dix-Huit Billions Sept Cent Soixante-Cinq Milliards Quatre Cent Trente-Deux Millions Cent Un Mille Deux Cent Trente-Quatre',
],
];

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

const testNegativeIntegers = cloneDeep(testIntegers);
testNegativeIntegers.map((row, i) => {
if (i === 0) {
return;
}
row[0] = -row[0];
row[1] = `Moins ${row[1]}`;
});

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

const testIntegersWithCurrency = cloneDeep(testIntegers);
testIntegersWithCurrency.map((row) => {
row[1] = `${row[1]} Euros`;
});

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

const testIntegersWithCurrencyAndIgnoreZeroCurrency = cloneDeep(testIntegersWithCurrency);
testIntegersWithCurrencyAndIgnoreZeroCurrency[0][1] = '';

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

const testFloats = [
[0.0, 'Zéro'],
[0.04, 'Zéro Virgule Zéro Quatre'],
[0.0468, 'Zéro Virgule Zéro Quatre Six Huit'],
[0.4, 'Zéro Virgule Quatre'],
[0.63, 'Zéro Virgule Soixante-Trois'],
[0.973, 'Zéro Virgule Neuf Cent Soixante-Treize'],
[0.999, 'Zéro Virgule Neuf Cent Quatre-Vingt-Dix-Neuf'],
[37.06, 'Trente-Sept Virgule Zéro Six'],
[37.068, 'Trente-Sept Virgule Zéro Six Huit'],
[37.68, 'Trente-Sept Virgule Soixante-Huit'],
[37.683, 'Trente-Sept Virgule Six Cent Quatre-Vingt-Trois'],
];

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

const testFloatsWithCurrency = [
[0.0, `Zéro Euros`],
[0.04, `Zéro Euros Et Quatre Centimes`],
[0.0468, `Zéro Euros Et Cinq Centimes`],
[0.4, `Zéro Euros Et Quarante Centimes`],
[0.63, `Zéro Euros Et Soixante-Trois Centimes`],
[0.973, `Zéro Euros Et Quatre-Vingt-Dix-Sept Centimes`],
[0.999, `Un Euros`],
[37.06, `Trente-Sept Euros Et Six Centimes`],
[37.068, `Trente-Sept Euros Et Sept Centimes`],
[37.68, `Trente-Sept Euros Et Soixante-Huit Centimes`],
[37.683, `Trente-Sept Euros Et Soixante-Huit Centimes`],
];

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

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(`Zéro Euros Et `, '');
}
});

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

const testFloatsWithCurrencyAndIgnoreDecimal = cloneDeep(testFloatsWithCurrency);
testFloatsWithCurrencyAndIgnoreDecimal.map((row) => {
if (row[0] === 0.999) {
row[1] = `Zéro Euros`;
} else {
row[1] = (row[1] as string).replace(new RegExp(` Et [\\w\\- ]+ Centimes`), '');
}
});

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

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

describe('Test Floats with options = { currency: true, ignoreZeroCurrency: true, ignoreDecimal: true }', () => {
test.each(testFloatsWithCurrencyAndIgnoreZeroCurrencyAndIgnoreDecimals)('convert %d => %s', (input, expected) => {
expect(
toWords.convert(input as number, {
currency: true,
ignoreZeroCurrency: true,
ignoreDecimal: true,
}),
).toBe(expected);
});
});
28 changes: 28 additions & 0 deletions dist/locales/fr-FR.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { LocaleInterface } from './locale.interface';
export declare class Locale implements LocaleInterface {
currency: {
name: string;
plural: string;
symbol: string;
fractionalUnit: {
name: string;
plural: string;
symbol: string;
};
};
options: {
ignoreOneForWords: string[];
pluralMark: string;
pluralWords: string[];
};
texts: {
and: string;
minus: string;
only: string;
point: string;
};
numberWordsMapping: {
number: number;
value: string;
}[];
}
137 changes: 137 additions & 0 deletions dist/locales/fr-FR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Locale = void 0;
class Locale {
constructor() {
this.currency = {
name: 'Euro',
plural: 'Euros',
symbol: '€',
fractionalUnit: {
name: 'Centime',
plural: 'Centimes',
symbol: 'Cts',
},
};
this.options = {
ignoreOneForWords: ['Cent', 'Mille'],
pluralMark: 's',
pluralWords: ['Billiard', 'Billion', 'Milliard', 'Million'],
};
this.texts = {
and: 'Et',
minus: 'Moins',
only: '',
point: 'Virgule',
};
this.numberWordsMapping = [
{ number: 1000000000000000, value: 'Billiard' },
{ number: 1000000000000, value: 'Billion' },
{ number: 1000000000, value: 'Milliard' },
{ number: 1000000, value: 'Million' },
{ number: 1000, value: 'Mille' },
{ number: 100, value: 'Cent' },
{ number: 99, value: 'Quatre-Vingt-Dix-Neuf' },
{ number: 98, value: 'Quatre-Vingt-Dix-Huit' },
{ number: 97, value: 'Quatre-Vingt-Dix-Sept' },
{ number: 96, value: 'Quatre-Vingt-Seize' },
{ number: 95, value: 'Quatre-Vingt-Quinze' },
{ number: 94, value: 'Quatre-Vingt-Quatorze' },
{ number: 93, value: 'Quatre-Vingt-Treize' },
{ number: 92, value: 'Quatre-Vingt-Douze' },
{ number: 91, value: 'Quatre-Vingt-Onze' },
{ number: 90, value: 'Quatre-Vingt-Dix' },
{ number: 89, value: 'Quatre-Vingt-Neuf' },
{ number: 88, value: 'Quatre-Vingt-Huit' },
{ number: 87, value: 'Quatre-Vingt-Sept' },
{ number: 86, value: 'Quatre-Vingt-Six' },
{ number: 85, value: 'Quatre-Vingt-Cinq' },
{ number: 84, value: 'Quatre-Vingt-Quatre' },
{ number: 83, value: 'Quatre-Vingt-Trois' },
{ number: 82, value: 'Quatre-Vingt-Deux' },
{ number: 81, value: 'Quatre-Vingt-Un' },
{ number: 80, value: 'Quatre-Vingt' },
{ number: 79, value: 'Soixante-Dix-Neuf' },
{ number: 78, value: 'Soixante-Dix-Huit' },
{ number: 77, value: 'Soixante-Dix-Sept' },
{ number: 76, value: 'Soixante-Seize' },
{ number: 75, value: 'Soixante-Quinze' },
{ number: 74, value: 'Soixante-Quatorze' },
{ number: 73, value: 'Soixante-Treize' },
{ number: 72, value: 'Soixante-Douze' },
{ number: 71, value: 'Soixante Et Onze' },
{ number: 70, value: 'Soixante-dix' },
{ number: 69, value: 'Soixante-Neuf' },
{ number: 68, value: 'Soixante-Huit' },
{ number: 67, value: 'Soixante-Sept' },
{ number: 66, value: 'Soixante-Six' },
{ number: 65, value: 'Soixante-Cinq' },
{ number: 64, value: 'Soixante-Quatre' },
{ number: 63, value: 'Soixante-Trois' },
{ number: 62, value: 'Soixante-Deux' },
{ number: 61, value: 'Soixante Et Un' },
{ number: 60, value: 'Soixante' },
{ number: 59, value: 'Cinquante-Neuf' },
{ number: 58, value: 'Cinquante-Huit' },
{ number: 57, value: 'Cinquante-Sept' },
{ number: 56, value: 'Cinquante-Six' },
{ number: 55, value: 'Cinquante-Cinq' },
{ number: 54, value: 'Cinquante-Quatre' },
{ number: 53, value: 'Cinquante-Trois' },
{ number: 52, value: 'Cinquante-Deux' },
{ number: 51, value: 'Cinquante Et Un' },
{ number: 50, value: 'Cinquante' },
{ number: 49, value: 'Quarante-Neuf' },
{ number: 48, value: 'Quarante-Huit' },
{ number: 47, value: 'Quarante-Sept' },
{ number: 46, value: 'Quarante-Six' },
{ number: 45, value: 'Quarante-Cinq' },
{ number: 44, value: 'Quarante-Quatre' },
{ number: 43, value: 'Quarante-Trois' },
{ number: 42, value: 'Quarante-Deux' },
{ number: 41, value: 'Quarante Et Un' },
{ number: 40, value: 'Quarante' },
{ number: 39, value: 'Trente-Neuf' },
{ number: 38, value: 'Trente-Huit' },
{ number: 37, value: 'Trente-Sept' },
{ number: 36, value: 'Trente-Six' },
{ number: 35, value: 'Trente-Cinq' },
{ number: 34, value: 'Trente-Quatre' },
{ number: 33, value: 'Trente-Trois' },
{ number: 32, value: 'Trente-Deux' },
{ number: 31, value: 'Trente Et Un' },
{ number: 30, value: 'Trente' },
{ number: 29, value: 'Vingt-Neuf' },
{ number: 28, value: 'Vingt-Huit' },
{ number: 27, value: 'Vingt-Sept' },
{ number: 26, value: 'Vingt-Six' },
{ number: 25, value: 'Vingt-Cinq' },
{ number: 24, value: 'Vingt-Quatre' },
{ number: 23, value: 'Vingt-Trois' },
{ number: 22, value: 'Vingt-Deux' },
{ number: 21, value: 'Vingt Et Un' },
{ number: 20, value: 'Vingt' },
{ number: 19, value: 'Dix-Neuf' },
{ number: 18, value: 'Dix-Huit' },
{ number: 17, value: 'Dix-Sept' },
{ number: 16, value: 'Seize' },
{ number: 15, value: 'Quinze' },
{ number: 14, value: 'Quatorze' },
{ number: 13, value: 'Treize' },
{ number: 12, value: 'Douze' },
{ number: 11, value: 'Onze' },
{ number: 10, value: 'Dix' },
{ number: 9, value: 'Neuf' },
{ number: 8, value: 'Huit' },
{ number: 7, value: 'Sept' },
{ number: 6, value: 'Six' },
{ number: 5, value: 'Cinq' },
{ number: 4, value: 'Quatre' },
{ number: 3, value: 'Trois' },
{ number: 2, value: 'Deux' },
{ number: 1, value: 'Un' },
{ number: 0, value: 'Zéro' },
];
}
}
exports.Locale = Locale;
3 changes: 3 additions & 0 deletions dist/locales/locale.interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export interface LocaleInterface {
namedLessThan1000?: boolean;
splitWord?: string;
ignoreZeroInDecimals?: boolean;
pluralMark?: string;
pluralWords?: string[];
ignoreOneForWords?: string[];
};
texts: {
and: string;
Expand Down
Loading

0 comments on commit ae19f85

Please sign in to comment.