n2words 1.12.2 → 1.14.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.
package/lib/i18n/TR.mjs CHANGED
@@ -1,372 +1,64 @@
1
- import N2WordsBase from '../classes/N2WordsBase.mjs';
1
+ import BaseLanguage from '../classes/BaseLanguage.mjs';
2
2
 
3
- export class N2WordsTR extends N2WordsBase {
3
+ export class N2WordsTR extends BaseLanguage {
4
4
  constructor(options) {
5
- super();
6
-
7
- this.options = Object.assign({
8
- dropSpaces: false,
9
- }, options);
10
-
11
- this.negativeWord = 'eksi';
12
- this.separatorWord = 'virgül';
13
- this.zero = 'sıfır';
14
- this.spaceSeparator = (this.options.dropSpaces) ? '' : ' ';
15
- this.precision = 2;
16
-
17
- this.cardinalOnes = {
18
- 1: 'bir',
19
- 2: 'iki',
20
- 3: 'üç',
21
- 4: 'dört',
22
- 5: 'beş',
23
- 6: 'altı',
24
- 7: 'yedi',
25
- 8: 'sekiz',
26
- 9: 'dokuz',
27
- };
28
- this.cardinalTens = {
29
- 1: 'on',
30
- 2: 'yirmi',
31
- 3: 'otuz',
32
- 4: 'kırk',
33
- 5: 'elli',
34
- 6: 'altmış',
35
- 7: 'yetmiş',
36
- 8: 'seksen',
37
- 9: 'doksan',
38
- };
39
- this.hundreds = {
40
- 2: 'iki',
41
- 3: 'üç',
42
- 4: 'dört',
43
- 5: 'beş',
44
- 6: 'altı',
45
- 7: 'yedi',
46
- 8: 'sekiz',
47
- 9: 'dokuz',
48
- };
49
- this.cardinalHundred = ['yüz', ''];
50
- this.cardinalTriplets = {
51
- 1: 'bin',
52
- 2: 'milyon',
53
- 3: 'milyar',
54
- 4: 'trilyon',
55
- 5: 'katrilyon',
56
- 6: 'kentilyon',
57
- };
58
- this.integersToRead = [];
59
- this.totalTripletsToRead = 0;
60
- this.totalDigitsOutsideTriplets = 0;
61
- this.lastZeroDigitOrder = 0;
5
+ super({
6
+ negativeWord: 'eksi',
7
+ separatorWord: 'virgül',
8
+ zero: 'sıfır',
9
+ spaceSeparator: (options.dropSpaces === true ? '' : ' '),
10
+ },[
11
+ [1000000000000000000n, 'kentilyon'],
12
+ [1000000000000000n, 'katrilyon'],
13
+ [1000000000000n, 'trilyon'],
14
+ [1000000000n, 'milyar'],
15
+ [1000000n, 'milyon'],
16
+ [1000n, 'bin'],
17
+ [100n, 'yüz'],
18
+ [90n, 'doksan'],
19
+ [80n, 'seksen'],
20
+ [70n, 'yetmiş'],
21
+ [60n, 'altmış'],
22
+ [50n, 'elli'],
23
+ [40n, 'kırk'],
24
+ [30n, 'otuz'],
25
+ [20n, 'yirmi'],
26
+ [10n, 'on'],
27
+ [9n, 'dokuz'],
28
+ [8n, 'sekiz'],
29
+ [7n, 'yedi'],
30
+ [6n, 'altı'],
31
+ [5n, 'beş'],
32
+ [4n, 'dört'],
33
+ [3n, 'üç'],
34
+ [2n, 'iki'],
35
+ [1n, 'bir'],
36
+ [0n, 'sıfır']
37
+ ]);
62
38
  }
63
39
 
64
- splitNum(value) {
65
- const floatDigits = JSON.stringify(value * 10 ** this.precision);
66
- if (parseInt(value) != 0) {
67
- this.integersToRead = [
68
- JSON.stringify(parseInt(value)),
69
- floatDigits.slice(floatDigits.length - this.precision, floatDigits.length),
70
- ];
40
+ merge(lPair, rPair) {
41
+ const lText = Object.keys(lPair)[0];
42
+ const rText = Object.keys(rPair)[0];
43
+ const lNum = parseInt(Object.values(lPair)[0]);
44
+ const rNum = parseInt(Object.values(rPair)[0]);
45
+ if (lNum == 1 && (rNum <= 100 || rNum == 1000)) {
46
+ return { [rText]: rNum };
47
+ } else if (rNum > lNum) {
48
+ return { [`${lText}${this.spaceSeparator}${rText}`]: lNum * rNum };
71
49
  } else {
72
- this.integersToRead = [
73
- '0',
74
- '0'.repeat(this.precision - floatDigits.length) +
75
- floatDigits.slice(floatDigits.length - this.precision, floatDigits.length),
76
- ];
77
- }
78
- if (this.integersToRead[0].length % 3 > 0) {
79
- this.totalTripletsToRead = Math.floor(this.integersToRead[0].length / 3) + 1;
80
- } else if (this.integersToRead[0].length % 3 == 0) {
81
- this.totalTripletsToRead = Math.floor(this.integersToRead[0].length / 3);
82
- }
83
- this.totalDigitsOutsideTriplets = this.integersToRead[0].length % 3;
84
-
85
- const okunacak = this.integersToRead[0].split('').reverse();
86
- this.lastZeroDigitOrder = 0;
87
- let found = 0;
88
- for (let i = 0; i < okunacak.length; i++) {
89
- if (parseInt(okunacak[i]) == 0 && found == 0) {
90
- this.lastZeroDigitOrder = i + 1;
91
- } else {
92
- found = 1;
93
- }
94
- }
95
- }
96
-
97
- joinWords(wordArray) {
98
- return wordArray.join(this.spaceSeparator).trim();
99
- }
100
-
101
- toCardinal(value) {
102
- if (parseInt(value) == 0) {
103
- return 'sıfır';
104
- }
105
- this.splitNum(value);
106
- let wordArray = [];
107
- if (this.lastZeroDigitOrder >= this.integersToRead[0].length) {
108
- return this.joinWords(wordArray);
109
- }
110
- if (this.totalTripletsToRead == 1) {
111
- if (this.totalDigitsOutsideTriplets == 2) {
112
- if (this.lastZeroDigitOrder == 1) {
113
- if(this.cardinalTens[this.integersToRead[0][0]]) {
114
- wordArray.push(this.cardinalTens[this.integersToRead[0][0]]);
115
- }
116
- return this.joinWords(wordArray);
117
- }
118
- if (this.lastZeroDigitOrder == 0) {
119
- if(this.cardinalTens[this.integersToRead[0][0]]) {
120
- wordArray.push(this.cardinalTens[this.integersToRead[0][0]]);
121
- }
122
- if(this.cardinalOnes[this.integersToRead[0][1]]) {
123
- wordArray.push(this.cardinalOnes[this.integersToRead[0][1]]);
124
- }
125
- }
126
- return this.joinWords(wordArray);
127
- }
128
- if (this.totalDigitsOutsideTriplets == 1) {
129
- if (this.lastZeroDigitOrder == 0) {
130
- if(this.cardinalOnes[this.integersToRead[0][0]]) {
131
- wordArray.push(this.cardinalOnes[this.integersToRead[0][0]]);
132
- }
133
- return this.joinWords(wordArray);
134
- }
135
- }
136
- if (this.totalDigitsOutsideTriplets == 0) {
137
- if (this.lastZeroDigitOrder == 2) {
138
- if(this.hundreds[this.integersToRead[0][0]]) {
139
- wordArray.push(this.hundreds[this.integersToRead[0][0]]);
140
- }
141
- wordArray.push(this.cardinalHundred[0]);
142
- return this.joinWords(wordArray);
143
- }
144
- if (this.lastZeroDigitOrder == 1) {
145
- if(this.hundreds[this.integersToRead[0][0]]) {
146
- wordArray.push(this.hundreds[this.integersToRead[0][0]]);
147
- }
148
- wordArray.push(this.cardinalHundred[0]);
149
- if(this.cardinalTens[this.integersToRead[0][1]]) {
150
- wordArray.push(this.cardinalTens[this.integersToRead[0][1]]);
151
- }
152
- return this.joinWords(wordArray);
153
- }
154
- if (this.lastZeroDigitOrder == 0) {
155
- if(this.hundreds[this.integersToRead[0][0]]) {
156
- wordArray.push(this.hundreds[this.integersToRead[0][0]]);
157
- }
158
- wordArray.push(this.cardinalHundred[0]);
159
- if(this.cardinalTens[this.integersToRead[0][1]]) {
160
- wordArray.push(this.cardinalTens[this.integersToRead[0][1]]);
161
- }
162
- if(this.cardinalOnes[this.integersToRead[0][2]]) {
163
- wordArray.push(this.cardinalOnes[this.integersToRead[0][2]]);
164
- }
165
- return this.joinWords(wordArray);
166
- }
167
- }
168
- }
169
- if (this.totalTripletsToRead >= 2) {
170
- if (this.totalDigitsOutsideTriplets == 2) {
171
- if (this.lastZeroDigitOrder == this.integersToRead[0].length - 1) {
172
- if(this.cardinalTens[this.integersToRead[0][0]]) {
173
- wordArray.push(this.cardinalTens[this.integersToRead[0][0]]);
174
- }
175
- wordArray.push(this.cardinalTriplets[this.totalTripletsToRead - 1]);
176
- return this.joinWords(wordArray);
177
- }
178
- if (this.lastZeroDigitOrder == this.integersToRead[0].length - 2) {
179
- if(this.cardinalTens[this.integersToRead[0][0]]) {
180
- wordArray.push(this.cardinalTens[this.integersToRead[0][0]]);
181
- }
182
- if(this.cardinalOnes[this.integersToRead[0][1]]) {
183
- wordArray.push(this.cardinalOnes[this.integersToRead[0][1]]);
184
- }
185
- wordArray.push(this.cardinalTriplets[this.totalTripletsToRead - 1]);
186
- return this.joinWords(wordArray);
187
- }
188
- if (this.lastZeroDigitOrder < this.integersToRead[0].length - 2) {
189
- if(this.cardinalTens[this.integersToRead[0][0]]) {
190
- wordArray.push(this.cardinalTens[this.integersToRead[0][0]]);
191
- }
192
- if(this.cardinalOnes[this.integersToRead[0][1]]) {
193
- wordArray.push(this.cardinalOnes[this.integersToRead[0][1]]);
194
- }
195
- wordArray.push(this.cardinalTriplets[this.totalTripletsToRead - 1]);
196
- }
197
- }
198
- if (this.totalDigitsOutsideTriplets == 1) {
199
- if (this.lastZeroDigitOrder == this.integersToRead[0].length - 1) {
200
- if (!(this.totalTripletsToRead == 2 && this.integersToRead[0][0] == '1')) {
201
- if(this.cardinalOnes[this.integersToRead[0][0]]) {
202
- wordArray.push(this.cardinalOnes[this.integersToRead[0][0]]);
203
- }
204
- }
205
- wordArray.push(this.cardinalTriplets[this.totalTripletsToRead - 1]);
206
- return this.joinWords(wordArray);
207
- }
208
- if (this.lastZeroDigitOrder < this.integersToRead[0].length - 1) {
209
- if (!(this.totalTripletsToRead == 2 && this.integersToRead[0][0] == '1')) {
210
- if(this.cardinalOnes[this.integersToRead[0][0]]) {
211
- wordArray.push(this.cardinalOnes[this.integersToRead[0][0]]);
212
- }
213
- }
214
- wordArray.push(this.cardinalTriplets[this.totalTripletsToRead - 1]);
215
- }
216
- }
217
- if (this.totalDigitsOutsideTriplets == 0) {
218
- if (this.lastZeroDigitOrder == this.integersToRead[0].length - 1) {
219
- if(this.hundreds[this.integersToRead[0][0]]) {
220
- wordArray.push(this.hundreds[this.integersToRead[0][0]]);
221
- }
222
- wordArray.push(this.cardinalHundred[0]);
223
- wordArray.push(this.cardinalTriplets[this.totalTripletsToRead - 1]);
224
- return this.joinWords(wordArray);
225
- }
226
- if (this.lastZeroDigitOrder == this.integersToRead[0].length - 2) {
227
- if(this.hundreds[this.integersToRead[0][0]]) {
228
- wordArray.push(this.hundreds[this.integersToRead[0][0]]);
229
- }
230
- wordArray.push(this.cardinalHundred[0]);
231
- if(this.cardinalTens[this.integersToRead[0][1]]) {
232
- wordArray.push(this.cardinalTens[this.integersToRead[0][1]]);
233
- }
234
- wordArray.push(this.cardinalTriplets[this.totalTripletsToRead - 1]);
235
- return this.joinWords(wordArray);
236
- }
237
- if (this.lastZeroDigitOrder == this.integersToRead[0].length - 3) {
238
- if(this.hundreds[this.integersToRead[0][0]]) {
239
- wordArray.push(this.hundreds[this.integersToRead[0][0]]);
240
- }
241
- wordArray.push(this.cardinalHundred[0]);
242
- if(this.cardinalTens[this.integersToRead[0][1]]) {
243
- wordArray.push(this.cardinalTens[this.integersToRead[0][1]]);
244
- }
245
- if(this.cardinalOnes[this.integersToRead[0][2]]) {
246
- wordArray.push(this.cardinalOnes[this.integersToRead[0][2]]);
247
- }
248
- wordArray.push(this.cardinalTriplets[this.totalTripletsToRead - 1]);
249
- return this.joinWords(wordArray);
250
- }
251
- if (this.lastZeroDigitOrder < this.integersToRead[0].length - 3) {
252
- if(this.hundreds[this.integersToRead[0][0]]) {
253
- wordArray.push(this.hundreds[this.integersToRead[0][0]]);
254
- }
255
- wordArray.push(this.cardinalHundred[0]);
256
- if(this.cardinalTens[this.integersToRead[0][1]]) {
257
- wordArray.push(this.cardinalTens[this.integersToRead[0][1]]);
258
- }
259
- if (!(this.totalTripletsToRead == 2 && this.integersToRead[0][2] == '1')) {
260
- if(this.cardinalOnes[this.integersToRead[0][2]]) {
261
- wordArray.push(this.cardinalOnes[this.integersToRead[0][2]]);
262
- }
263
- }
264
- wordArray.push(this.cardinalTriplets[this.totalTripletsToRead - 1]);
265
- }
266
- }
267
- for (let i = this.totalTripletsToRead - 1; i > 0; i--) {
268
- const readingTripletOrder = this.totalTripletsToRead - i;
269
- let lastReadDigitOrder;
270
- if (this.totalDigitsOutsideTriplets == 0) {
271
- lastReadDigitOrder = readingTripletOrder * 3;
272
- } else {
273
- lastReadDigitOrder = (readingTripletOrder - 1) * 3 + this.totalDigitsOutsideTriplets;
274
- }
275
- if (this.integersToRead[0].slice(lastReadDigitOrder, lastReadDigitOrder + 3) != '000') {
276
- if (this.integersToRead[0][lastReadDigitOrder] != '0') {
277
- if(this.hundreds[this.integersToRead[0][lastReadDigitOrder]]) {
278
- wordArray.push(this.hundreds[this.integersToRead[0][lastReadDigitOrder]]);
279
- }
280
- if (this.lastZeroDigitOrder == this.integersToRead[0].length - lastReadDigitOrder - 1) {
281
- if (i == 1) {
282
- wordArray.push(this.cardinalHundred[0]);
283
- return this.joinWords(wordArray);
284
- } else if (i > 1) {
285
- wordArray.push(this.cardinalHundred[0]);
286
- wordArray.push(this.cardinalTriplets[i - 1]);
287
- return this.joinWords(wordArray);
288
- }
289
- } else {
290
- wordArray.push(this.cardinalHundred[0]);
291
- }
292
- }
293
- if (this.integersToRead[0][lastReadDigitOrder + 1] != '0') {
294
- if (this.lastZeroDigitOrder == this.integersToRead[0].length - lastReadDigitOrder - 2) {
295
- if (i == 1) {
296
- if(this.cardinalTens[this.integersToRead[0][lastReadDigitOrder + 1]]) {
297
- wordArray.push(this.cardinalTens[this.integersToRead[0][lastReadDigitOrder + 1]]);
298
- }
299
- return this.joinWords(wordArray);
300
- } else if (i > 1) {
301
- if(this.cardinalTens[this.integersToRead[0][lastReadDigitOrder + 1]]) {
302
- wordArray.push(this.cardinalTens[this.integersToRead[0][lastReadDigitOrder + 1]]);
303
- }
304
- wordArray.push(this.cardinalTriplets[i - 1]);
305
- return this.joinWords(wordArray);
306
- }
307
- } else {
308
- if(this.cardinalTens[this.integersToRead[0][lastReadDigitOrder + 1]]) {
309
- wordArray.push(this.cardinalTens[this.integersToRead[0][lastReadDigitOrder + 1]]);
310
- }
311
- }
312
- }
313
- if (this.integersToRead[0][lastReadDigitOrder + 2] != '0') {
314
- if (this.lastZeroDigitOrder == this.integersToRead[0].length - lastReadDigitOrder - 3) {
315
- if (i == 1) {
316
- if(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]) {
317
- wordArray.push(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]);
318
- }
319
- return this.joinWords(wordArray);
320
- }
321
- if (i == 2) {
322
- if (this.integersToRead[0].slice(lastReadDigitOrder, lastReadDigitOrder + 2) != '00') {
323
- if(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]) {
324
- wordArray.push(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]);
325
- }
326
- } else if (this.integersToRead[0][lastReadDigitOrder + 2] != '1') {
327
- if(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]) {
328
- wordArray.push(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]);
329
- }
330
- }
331
- wordArray.push(this.cardinalTriplets[i - 1]);
332
- return this.joinWords(wordArray);
333
- }
334
- if (i > 2) {
335
- if(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]) {
336
- wordArray.push(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]);
337
- }
338
- wordArray.push(this.cardinalTriplets[i - 1]);
339
- return this.joinWords(wordArray);
340
- }
341
- } else {
342
- if (this.integersToRead[0].slice(lastReadDigitOrder, lastReadDigitOrder + 2) != '00') {
343
- if(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]) {
344
- wordArray.push(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]);
345
- }
346
- } else {
347
- if (i == 2) {
348
- if (this.integersToRead[0].slice(lastReadDigitOrder, lastReadDigitOrder + 2) != '00') {
349
- if(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]) {
350
- wordArray.push(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]);
351
- }
352
- } else if (this.integersToRead[0][lastReadDigitOrder + 2] != '1') {
353
- if(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]) {
354
- wordArray.push(this.cardinalOnes[this.integersToRead[0][lastReadDigitOrder + 2]]);
355
- }
356
- }
357
- }
358
- }
359
- }
360
- }
361
-
362
- wordArray.push(this.cardinalTriplets[i - 1]);
363
- }
364
- }
50
+ return { [`${lText}${this.spaceSeparator}${rText}`]: lNum + rNum };
365
51
  }
366
- return this.joinWords(wordArray);
367
52
  }
368
53
  }
369
54
 
370
- export default function(n, options = {}) {
371
- return new N2WordsTR(options).floatToCardinal(n);
55
+ /**
56
+ * Converts a value to cardinal (written) form.
57
+ * @param {number|string} value Number to be convert.
58
+ * @param {object} options Options for class.
59
+ * @throws {Error} Value cannot be invalid.
60
+ * @returns {string} Value in cardinal (written) format.
61
+ */
62
+ export default function(value, options = {}) {
63
+ return new N2WordsTR(options).floatToCardinal(value);
372
64
  }
package/lib/i18n/UK.mjs CHANGED
@@ -2,11 +2,12 @@ import {N2WordsRU} from './RU.mjs';
2
2
 
3
3
  export class N2WordsUK extends N2WordsRU {
4
4
  constructor() {
5
- super();
5
+ super({
6
+ negativeWord: 'мiнус',
7
+ separatorWord: 'кома',
8
+ zero: 'нуль'
9
+ });
6
10
 
7
- this.negativeWord = 'мiнус';
8
- this.separatorWord = 'кома';
9
- this.zero = 'нуль';
10
11
  this.feminine = false;
11
12
  this.ones = {
12
13
  1: 'один',
@@ -78,6 +79,12 @@ export class N2WordsUK extends N2WordsRU {
78
79
  }
79
80
  }
80
81
 
81
- export default function(n) {
82
- return new N2WordsUK().floatToCardinal(n);
82
+ /**
83
+ * Converts a value to cardinal (written) form.
84
+ * @param {number|string} value Number to be convert.
85
+ * @throws {Error} Value cannot be invalid.
86
+ * @returns {string} Value in cardinal (written) format.
87
+ */
88
+ export default function(value) {
89
+ return new N2WordsUK().floatToCardinal(value);
83
90
  }
package/lib/i18n/VI.mjs CHANGED
@@ -1,12 +1,13 @@
1
- import N2WordsAbs from '../classes/N2WordsAbs.mjs';
1
+ import AbstractLanguage from '../classes/AbstractLanguage.mjs';
2
2
 
3
- export class N2WordsID extends N2WordsAbs {
3
+ export class N2WordsID extends AbstractLanguage {
4
4
  constructor() {
5
- super();
5
+ super({
6
+ negativeWord: 'âm',
7
+ separatorWord: 'phẩy',
8
+ zero: 'không'
9
+ });
6
10
 
7
- this.negativeWord = 'âm';
8
- this.separatorWord = 'phẩy';
9
- this.zero = 'không';
10
11
  this.base = {
11
12
  0: 'không',
12
13
  1: 'một',
@@ -67,15 +68,15 @@ export class N2WordsID extends N2WordsAbs {
67
68
  let unitsPart = number % 10;
68
69
  let tensPart = number - unitsPart;
69
70
  let tensPartText = this.tens[tensPart];
70
- if (unitsPart === 0) {
71
+ if (unitsPart == 0) {
71
72
  return tensPartText;
72
73
  }
73
74
  let unitsPartText = this.base[unitsPart];
74
75
  let suffix = unitsPartText;
75
- if (unitsPart === 1) {
76
+ if (unitsPart == 1) {
76
77
  suffix = 'mốt';
77
78
  }
78
- if (unitsPart === 5) {
79
+ if (unitsPart == 5) {
79
80
  suffix = 'lăm';
80
81
  }
81
82
  return tensPartText + ' ' + suffix;
@@ -92,7 +93,7 @@ export class N2WordsID extends N2WordsAbs {
92
93
  if (words.length > 0) {
93
94
  words.push('lẻ');
94
95
  }
95
- if (tensUnitsPart === 5) {
96
+ if (tensUnitsPart == 5) {
96
97
  words.push('năm');
97
98
  } else {
98
99
  words.push(this.base[tensUnitsPart]);
@@ -106,16 +107,16 @@ export class N2WordsID extends N2WordsAbs {
106
107
 
107
108
  convertMore1000(number) {
108
109
  let words = [];
109
- let division = Math.floor(number / 1000);
110
+ let division = number / 1000n;
110
111
  let power = 1;
111
- while (division >= 1000) {
112
- division = Math.floor(division / 1000);
112
+ while (division >= 1000n) {
113
+ division = division / 1000n;
113
114
  power = power + 1;
114
115
  }
115
- let r = number - (division * Math.pow(1000, power));
116
+ let r = number - (division * BigInt(Math.pow(1000, power)));
116
117
  words.push(this.toCardinal(division), this.thousands[power]);
117
- if (r > 0) {
118
- if (r <= 99) {
118
+ if (r > 0n) {
119
+ if (r <= 99n) {
119
120
  words.push('lẻ');
120
121
  }
121
122
  words.push(this.toCardinal(r));
@@ -124,14 +125,14 @@ export class N2WordsID extends N2WordsAbs {
124
125
  }
125
126
 
126
127
  toCardinal(number) {
127
- if (number < 20) {
128
- return this.base[number];
128
+ if (number < 20n) {
129
+ return this.base[Number(number)];
129
130
  } else {
130
- if (number < 100) {
131
- return this.convertLess100(number);
131
+ if (number < 100n) {
132
+ return this.convertLess100(Number(number));
132
133
  } else {
133
- if (number < 1000) {
134
- return this.convertLess1000(number);
134
+ if (number < 1000n) {
135
+ return this.convertLess1000(Number(number));
135
136
  } else { // number >= 1000
136
137
  return this.convertMore1000(number);
137
138
  }
@@ -140,6 +141,12 @@ export class N2WordsID extends N2WordsAbs {
140
141
  }
141
142
  }
142
143
 
143
- export default function (n) {
144
- return new N2WordsID().floatToCardinal(n);
144
+ /**
145
+ * Converts a value to cardinal (written) form.
146
+ * @param {number|string} value Number to be convert.
147
+ * @throws {Error} Value cannot be invalid.
148
+ * @returns {string} Value in cardinal (written) format.
149
+ */
150
+ export default function (value) {
151
+ return new N2WordsID().floatToCardinal(value);
145
152
  }
package/lib/i18n/ZH.mjs CHANGED
@@ -1,31 +1,30 @@
1
- import N2WordsBase from '../classes/N2WordsBase.mjs';
1
+ import BaseLanguage from '../classes/BaseLanguage.mjs';
2
2
 
3
- export class N2WordsZH extends N2WordsBase {
3
+ export class N2WordsZH extends BaseLanguage {
4
4
  constructor() {
5
- super();
6
-
7
- this.negativeWord = '';
8
- this.separatorWord = '';
9
- this.zero = '';
10
- this.spaceSeparator = ''; // no
11
- this.cards = [
12
- { 1000000000000: '' },
13
- { 100000000: '亿' },
14
- { 10000: '' },
15
- { 1000: '' },
16
- { 100: '' },
17
- { 10: '' },
18
- { 9: '' },
19
- { 8: '' },
20
- { 7: '' },
21
- { 6: '' },
22
- { 5: '' },
23
- { 4: '' },
24
- { 3: '' },
25
- { 2: '' },
26
- { 1: '' },
27
- { 0: '零' },
28
- ];
5
+ super({
6
+ negativeWord: '负',
7
+ separatorWord: '',
8
+ zero: '',
9
+ spaceSeparator: ''
10
+ },[
11
+ [1000000000000n, '万'],
12
+ [100000000n, '亿'],
13
+ [10000n, ''],
14
+ [1000n, ''],
15
+ [100n, ''],
16
+ [10n, ''],
17
+ [9n, ''],
18
+ [8n, ''],
19
+ [7n, ''],
20
+ [6n, ''],
21
+ [5n, ''],
22
+ [4n, ''],
23
+ [3n, ''],
24
+ [2n, ''],
25
+ [1n, ''],
26
+ [0n, '']
27
+ ]);
29
28
  }
30
29
 
31
30
  merge(lPair, rPair) {
@@ -48,10 +47,11 @@ export class N2WordsZH extends N2WordsBase {
48
47
  return result;
49
48
  }
50
49
 
51
- toDecimal(decimalPart) {
52
- const decimalPartArray = Array.from(decimalPart);
50
+ decimalToCardinal(decimal) {
51
+ const decimalPartArray = decimal.split('');
52
+
53
53
  const decimalPartWordsArray = decimalPartArray.map(decimal =>
54
- this.getValueFromCards(decimal)
54
+ this.getCardWord(decimal)
55
55
  );
56
56
 
57
57
  return decimalPartWordsArray.join(this.spaceSeparator);
@@ -62,10 +62,16 @@ export class N2WordsZH extends N2WordsBase {
62
62
  }
63
63
 
64
64
  zeroDigit(num) {
65
- return Array.from(num.toString()).filter(c => c === '0').length;
65
+ return Array.from(num.toString()).filter(c => c == '0').length;
66
66
  }
67
67
  }
68
68
 
69
- export default function(n) {
70
- return new N2WordsZH().floatToCardinal(n);
69
+ /**
70
+ * Converts a value to cardinal (written) form.
71
+ * @param {number|string} value Number to be convert.
72
+ * @throws {Error} Value cannot be invalid.
73
+ * @returns {string} Value in cardinal (written) format.
74
+ */
75
+ export default function(value) {
76
+ return new N2WordsZH().floatToCardinal(value);
71
77
  }