n2words 1.17.0 → 1.19.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.
@@ -76,10 +76,10 @@ export default class {
76
76
  const words = [];
77
77
  let allZero = true;
78
78
 
79
- // Split decimal portion into an array of characters in reverse
79
+ // Split decimal portion into an array of characters
80
80
  const chars = decimal.split('');
81
81
 
82
- // Loop through array (from the end) adding leading zeros to output array
82
+ // Loop through characters adding zeros to output array
83
83
  chars.forEach(char => {
84
84
  if (char === '0') {
85
85
  words.push(this.zero);
@@ -88,7 +88,6 @@ export default class {
88
88
  }
89
89
  });
90
90
 
91
- // Add decimal number to word array
92
91
  if (allZero) {
93
92
  return words;
94
93
  }
@@ -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.19.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",
@@ -65,7 +65,7 @@
65
65
  "scripts": {
66
66
  "bench": "node bench.js",
67
67
  "build": "webpack --config webpack.config.js --progress",
68
- "build:types": "npx -p typescript tsc lib/n2words.js dist/n2words.js --target es6 --module esnext --allowJs --declaration --emitDeclarationOnly",
68
+ "build:types": "npx -p typescript tsc lib/n2words.js dist/n2words.js --target es6 --module nodenext --allowJs --declaration --emitDeclarationOnly",
69
69
  "coverage": "c8 ava",
70
70
  "lint": "eslint --config .eslintrc.json lib/ test/ examples/ bench.js",
71
71
  "test": "ava --verbose",
@@ -91,26 +91,26 @@
91
91
  ]
92
92
  },
93
93
  "devDependencies": {
94
- "@babel/core": "^7.22.11",
95
- "@babel/preset-env": "^7.22.10",
96
- "ava": "^5.3.1",
94
+ "@babel/core": "^7.23.9",
95
+ "@babel/preset-env": "^7.23.9",
96
+ "ava": "^6.1.1",
97
97
  "babel-loader": "^9.1.3",
98
98
  "benchmark": "^2.1.4",
99
- "c8": "^8.0.1",
99
+ "c8": "^9.1.0",
100
100
  "chalk": "^5.3.0",
101
- "chromedriver": "^116.0.0",
102
- "core-js": "^3.32.1",
103
- "eslint": "^8.48.0",
101
+ "chromedriver": "^121.0.0",
102
+ "core-js": "^3.35.1",
103
+ "eslint": "^8.56.0",
104
104
  "eslint-plugin-ava": "^14.0.0",
105
- "eslint-plugin-import": "^2.28.1",
106
- "eslint-plugin-jsdoc": "^46.5.0",
105
+ "eslint-plugin-import": "^2.29.1",
106
+ "eslint-plugin-jsdoc": "^48.0.6",
107
107
  "eslint-plugin-node": "^11.1.0",
108
108
  "microtime": "^3.1.1",
109
- "selenium-webdriver": "^4.11.1",
110
- "webpack": "^5.88.2",
109
+ "selenium-webdriver": "^4.17.0",
110
+ "webpack": "^5.90.1",
111
111
  "webpack-cli": "^5.1.4"
112
112
  },
113
113
  "engines": {
114
- "node": "16 || 18 || >=20"
114
+ "node": "18 || >=20"
115
115
  }
116
116
  }