diginet-core-ui 1.4.66-beta.4 → 1.4.66-beta.5

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.
@@ -38,13 +38,29 @@ const num2WordsVi = function () {
38
38
  return e > 0 && (o += n(e, r) + ' ngàn', r = !0), t > 0 && (o += n(t, r)), o;
39
39
  };
40
40
  return {
41
- convert: function (r) {
41
+ convert: function (input) {
42
+ if (!Number.isFinite(input)) return '';
43
+ let parts = String(input).split('.');
44
+ let intPart = Math.floor(Number(parts[0]));
45
+ let decimalPart = parts[1];
46
+ let temp = intPart;
47
+ let nStr = '';
48
+ let a = '';
42
49
  let ty;
43
- if (0 === r) return t[0];
44
- let n = '',
45
- a = '';
46
- do ty = r % 1e9, r = Math.floor(r / 1e9), n = r > 0 ? o(ty, !0) + a + n : o(ty, !1) + a + n, a = ' tỷ'; while (r > 0);
47
- return n.trim();
50
+ if (intPart === 0) return t[0];
51
+ do {
52
+ ty = temp % 1e9;
53
+ temp = Math.floor(temp / 1e9);
54
+ nStr = temp > 0 ? o(ty, true) + a + nStr : o(ty, false) + a + nStr;
55
+ a = ' tỷ';
56
+ } while (temp > 0);
57
+ let result = nStr.trim();
58
+
59
+ // xử lý thập phân
60
+ if (decimalPart) {
61
+ result += ' phẩy ' + decimalPart.split('').map(d => t[Number(d)]).join(' ');
62
+ }
63
+ return result;
48
64
  }
49
65
  };
50
66
  }();
@@ -56,22 +72,32 @@ const num2WordsEn = n => {
56
72
  const drop = n => xs => xs.slice(n);
57
73
  const reverse = xs => xs.slice(0).reverse();
58
74
  const comp = f => g => x => f(g(x));
59
- const not = x => !x;
60
75
  const chunk = n => xs => isEmpty(xs) ? [] : [take(n)(xs), ...chunk(n)(drop(n)(xs))];
61
76
  let a = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
62
77
  let b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
63
78
  let g = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion'];
64
- // this part is really nasty still
65
- // it might edit this again later to show how Monoids could fix this up
66
79
  let makeGroup = ([ones, tens, huns]) => {
67
80
  return [num(huns) === 0 ? '' : a[huns] + ' hundred ', num(ones) === 0 ? b[tens] : b[tens] && b[tens] + '-' || '', a[tens + ones] || a[ones]].join('');
68
81
  };
69
- // "thousands" constructor; no real good names for this, I guess
70
82
  let thousand = (group, i) => group === '' ? group : `${group} ${g[i]}`;
71
- // execute !
72
- if (typeof n === 'number') return num2WordsEn(String(n));
73
- if (n === '0') return 'zero';
74
- return comp(chunk(3))(reverse)(arr(n)).map(makeGroup).map(thousand).filter(comp(not)(isEmpty)).reverse().join(' ');
83
+ // convert integer part
84
+ const convertInt = str => {
85
+ if (str === '0') return 'zero';
86
+ return comp(chunk(3))(reverse)(arr(str)).map(makeGroup).map(thousand).filter(x => x.trim().length > 0).reverse().join(' ').trim();
87
+ };
88
+ // normalize input
89
+ if (typeof n === 'number') n = String(n);
90
+ const parts = n.split('.');
91
+ const intPart = parts[0];
92
+ const decimalPart = parts[1];
93
+ let result = convertInt(intPart);
94
+
95
+ // decimal handling
96
+ if (decimalPart) {
97
+ const decimalWords = decimalPart.split('').map(d => a[Number(d)] || '').join(' ').trim();
98
+ result += ' point ' + decimalWords;
99
+ }
100
+ return result;
75
101
  };
76
102
  const num2WordsZh = (() => {
77
103
  const t = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
@@ -79,47 +105,63 @@ const num2WordsZh = (() => {
79
105
  const bigUnits = ['', '万', '亿', '兆'];
80
106
  const readSection = num => {
81
107
  let str = '';
82
- let zero = true;
83
- for (let i = 0; i < 4 && num > 0; i++) {
108
+ let zero = false;
109
+ for (let i = 0; i < 4; i++) {
84
110
  const digit = num % 10;
85
111
  if (digit === 0) {
86
- if (!zero) {
87
- zero = true;
112
+ if (!zero && str !== '') {
88
113
  str = t[0] + str;
89
114
  }
115
+ zero = true;
90
116
  } else {
91
- zero = false;
92
117
  str = t[digit] + units[i] + str;
118
+ zero = false;
93
119
  }
94
120
  num = Math.floor(num / 10);
121
+ if (num === 0) break;
95
122
  }
96
123
  return str;
97
124
  };
98
- return {
99
- convert: num => {
100
- if (num === 0) return t[0];
101
- let str = '';
102
- let unitPos = 0;
103
- let needZero = false;
104
- while (num > 0) {
105
- const section = num % 10000;
106
- if (section === 0) {
107
- if (needZero) {
108
- str = t[0] + str;
109
- needZero = false;
110
- }
111
- } else {
112
- const sectionStr = readSection(section) + bigUnits[unitPos];
113
- str = sectionStr + str;
114
- needZero = true;
125
+ const readInteger = num => {
126
+ if (num === 0) return t[0];
127
+ let sections = [];
128
+ while (num > 0) {
129
+ sections.unshift(num % 10000);
130
+ num = Math.floor(num / 10000);
131
+ }
132
+ let result = '';
133
+ for (let i = 0; i < sections.length; i++) {
134
+ const section = sections[i];
135
+ const unit = bigUnits[sections.length - 1 - i];
136
+ if (section === 0) {
137
+ if (result && !result.endsWith(t[0]) && sections.slice(i + 1).some(v => v > 0)) {
138
+ result += t[0];
115
139
  }
116
- num = Math.floor(num / 10000);
117
- unitPos++;
140
+ continue;
118
141
  }
119
142
 
120
- // Special cases
121
- str = str.replace(/^一十/, '十'); // "一十X" => "十X"
122
- return str;
143
+ // Nếu nhóm hiện tại <1000 và phía trước đã có nội dung
144
+ // => cần thêm ""
145
+ if (result && section < 1000 && !result.endsWith(t[0])) {
146
+ result += t[0];
147
+ }
148
+ result += readSection(section) + unit;
149
+ }
150
+ return result.replace(/零+/g, '零').replace(/零$/g, '').replace(/^一十/, '十');
151
+ };
152
+ return {
153
+ convert: num => {
154
+ if (typeof num === 'string') {
155
+ num = num.trim();
156
+ }
157
+ const strNum = String(num);
158
+ if (!strNum.includes('.')) {
159
+ return readInteger(Number(strNum));
160
+ }
161
+ const [intPart, decPart] = strNum.split('.');
162
+ const intWords = readInteger(Number(intPart));
163
+ const decWords = decPart.split('').map(ch => t[Number(ch)]).join('');
164
+ return `${intWords}点${decWords}`;
123
165
  }
124
166
  };
125
167
  })();
@@ -403,7 +445,7 @@ const MoneyInput = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((inProps, referenc
403
445
  }
404
446
  }
405
447
  const v = getValueWithDecimal(number.toString().replace('.', decimalSymbol));
406
- if (convertToWords && !decimalDigit && (disabled || readOnly)) {
448
+ if (convertToWords && (disabled || readOnly)) {
407
449
  inputRef.current.value = convertNumToWords(value);
408
450
  } else {
409
451
  inputRef.current.value = parseValueWithFix(thousandSeparator ? getThousandSeparator(v) : v);
@@ -431,9 +473,9 @@ const MoneyInput = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((inProps, referenc
431
473
  });
432
474
  const convertNumToWords = number => {
433
475
  let valueConverted = getGlobal('helperInvalid');
434
- if (Number.isInteger(number)) {
476
+ if (Number.isFinite(number)) {
435
477
  const currentLocale = locale.get();
436
- const converter = converters[currentLocale] || num2WordsVi; // fallback VN
478
+ const converter = converters[currentLocale] || num2WordsEn; // fallback VN
437
479
  valueConverted = Object.hasOwn(converter, 'convert') ? converter.convert(number) : converter(number);
438
480
  }
439
481
  return valueConverted;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diginet-core-ui",
3
- "version": "1.4.66-beta.4",
3
+ "version": "1.4.66-beta.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "license": "UNLICENSED",