n2words 1.17.0 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Converts a value to cardinal (written) form.
3
+ * @param {number|string} value Number to be convert.
4
+ * @param {object} [options] Options for class.
5
+ * @throws {Error} Value cannot be invalid.
6
+ * @returns {string} Value in cardinal (written) format.
7
+ */
8
+ export default function _default(value: number | string, options?: object): string;
9
+ export class N2WordsFRBE extends N2WordsFR {
10
+ }
11
+ import { N2WordsFR } from './fr.js';
@@ -0,0 +1,20 @@
1
+ import { N2WordsFR } from './fr.js';
2
+
3
+ export class N2WordsFRBE extends N2WordsFR {
4
+ constructor(options) {
5
+ options = Object.assign({}, options, { _region: 'BE' });
6
+
7
+ super(options);
8
+ }
9
+ }
10
+
11
+ /**
12
+ * Converts a value to cardinal (written) form.
13
+ * @param {number|string} value Number to be convert.
14
+ * @param {object} [options] Options for class.
15
+ * @throws {Error} Value cannot be invalid.
16
+ * @returns {string} Value in cardinal (written) format.
17
+ */
18
+ export default function (value, options = {}) {
19
+ return new N2WordsFRBE(options).floatToCardinal(value);
20
+ }
package/lib/i18n/fr.js CHANGED
@@ -5,7 +5,8 @@ export class N2WordsFR extends BaseLanguage {
5
5
  options = Object.assign({
6
6
  negativeWord: 'moins',
7
7
  separatorWord: 'virgule',
8
- zero: 'zéro'
8
+ zero: 'zéro',
9
+ _region: 'FR'
9
10
  }, options);
10
11
 
11
12
  super(options, [
@@ -19,7 +20,9 @@ export class N2WordsFR extends BaseLanguage {
19
20
  [1000000n, 'million'],
20
21
  [1000n, 'mille'],
21
22
  [100n, 'cent'],
23
+ ...(['BE'].includes(options._region) ? [[90n, 'nonante']] : []),
22
24
  [80n, 'quatre-vingts'],
25
+ ...(['BE'].includes(options._region) ? [[70n, 'septante']] : []),
23
26
  [60n, 'soixante'],
24
27
  [50n, 'cinquante'],
25
28
  [40n, 'quarante'],
package/lib/n2words.js CHANGED
@@ -7,6 +7,7 @@ import n2wordsEN from './i18n/en.js';
7
7
  import n2wordsES from './i18n/es.js';
8
8
  import n2wordsFA from './i18n/fa.js';
9
9
  import n2wordsFR from './i18n/fr.js';
10
+ import n2wordsFRBE from './i18n/fr-BE.js';
10
11
  import n2wordsHE from './i18n/he.js';
11
12
  import n2wordsHR from './i18n/hr.js';
12
13
  import n2wordsHU from './i18n/hu.js';
@@ -26,6 +27,37 @@ import n2wordsUK from './i18n/uk.js';
26
27
  import n2wordsVI from './i18n/vi.js';
27
28
  import n2wordsZH from './i18n/zh.js';
28
29
 
30
+ const dict = {
31
+ 'ar': n2wordsAR,
32
+ 'az': n2wordsAZ,
33
+ 'cz': n2wordsCZ,
34
+ 'de': n2wordsDE,
35
+ 'dk': n2wordsDK,
36
+ 'en': n2wordsEN, // default
37
+ 'es': n2wordsES,
38
+ 'fa': n2wordsFA,
39
+ 'fr': n2wordsFR,
40
+ 'fr-BE': n2wordsFRBE,
41
+ 'he': n2wordsHE, // only for numbers <= 9999
42
+ 'hr': n2wordsHR,
43
+ 'hu': n2wordsHU,
44
+ 'id': n2wordsID,
45
+ 'it': n2wordsIT,
46
+ 'ko': n2wordsKO,
47
+ 'lt': n2wordsLT,
48
+ 'lv': n2wordsLV,
49
+ 'nl': n2wordsNL,
50
+ 'no': n2wordsNO,
51
+ 'pl': n2wordsPL,
52
+ 'pt': n2wordsPT,
53
+ 'ru': n2wordsRU,
54
+ 'sr': n2wordsSR,
55
+ 'tr': n2wordsTR,
56
+ 'uk': n2wordsUK,
57
+ 'vi': n2wordsVI,
58
+ 'zh': n2wordsZH,
59
+ };
60
+
29
61
  /**
30
62
  * Converts a number to written form.
31
63
  * @param {number|string} value The number to convert.
@@ -33,34 +65,13 @@ import n2wordsZH from './i18n/zh.js';
33
65
  * @returns {string} Value in written format.
34
66
  */
35
67
  export default function (value, options = { lang: 'en' }) {
36
- switch (options.lang) {
37
- case 'en': return n2wordsEN(value, options); // default
38
- case 'ar': return n2wordsAR(value, options);
39
- case 'az': return n2wordsAZ(value, options);
40
- case 'cz': return n2wordsCZ(value, options);
41
- case 'de': return n2wordsDE(value, options);
42
- case 'dk': return n2wordsDK(value, options);
43
- case 'es': return n2wordsES(value, options);
44
- case 'fa': return n2wordsFA(value, options);
45
- case 'fr': return n2wordsFR(value, options);
46
- case 'he': return n2wordsHE(value, options); // only for numbers <= 9999
47
- case 'hr': return n2wordsHR(value, options);
48
- case 'hu': return n2wordsHU(value, options);
49
- case 'id': return n2wordsID(value, options);
50
- case 'it': return n2wordsIT(value, options);
51
- case 'ko': return n2wordsKO(value, options);
52
- case 'lt': return n2wordsLT(value, options);
53
- case 'lv': return n2wordsLV(value, options);
54
- case 'nl': return n2wordsNL(value, options);
55
- case 'no': return n2wordsNO(value, options);
56
- case 'pl': return n2wordsPL(value, options);
57
- case 'pt': return n2wordsPT(value, options);
58
- case 'ru': return n2wordsRU(value, options);
59
- case 'sr': return n2wordsSR(value, options);
60
- case 'tr': return n2wordsTR(value, options);
61
- case 'uk': return n2wordsUK(value, options);
62
- case 'vi': return n2wordsVI(value, options);
63
- case 'zh': return n2wordsZH(value, options);
64
- default: throw Error('Unsupported language: ' + value + '.');
65
- }
68
+ const fn = dict[options.lang];
69
+ if (fn != null) return fn(value, options);
70
+
71
+ const fallbackLang = options.lang.split('-') // 'en-UK' -> ['en', 'UK']
72
+ .map((_, i, arr) => arr.slice(0, arr.length - i).join('-')) // ['en-UK', 'en']
73
+ .find(l => dict[l] != null);
74
+ if (fallbackLang != null) return dict[fallbackLang](value, options);
75
+
76
+ throw Error('Unsupported language: ' + value + '.');
66
77
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n2words",
3
- "version": "1.17.0",
3
+ "version": "1.18.0",
4
4
  "description": "n2words converts a numerical number into a written one, supports 27 languages and has zero dependencies.",
5
5
  "keywords": [
6
6
  "n2words",