react-restyle-components 0.2.76 → 0.2.77
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/src/core-utils/convert/numberToWords/numToWords.util.d.ts +5 -0
- package/lib/src/core-utils/convert/numberToWords/numToWords.util.d.ts.map +1 -0
- package/lib/src/core-utils/convert/numberToWords/numToWords.util.js +145 -0
- package/lib/src/core-utils/index.d.ts +1 -0
- package/lib/src/core-utils/index.d.ts.map +1 -1
- package/lib/src/core-utils/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numToWords.util.d.ts","sourceRoot":"","sources":["../../../../../src/core-utils/convert/numberToWords/numToWords.util.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,eAAO,MAAM,UAAU,MAAO,MAAM,GAAG,MAAM,KAAG,MAgI/C,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
const isEmpty = (xs) => xs.length === 0;
|
|
2
|
+
const take = (n) => (xs) => xs.slice(0, n);
|
|
3
|
+
const drop = (n) => (xs) => xs.slice(n);
|
|
4
|
+
const chunk = (n) => (xs) => isEmpty(xs) ? [] : [take(n)(xs), ...chunk(n)(drop(n)(xs))];
|
|
5
|
+
/**
|
|
6
|
+
* Converts a number to words using the Indian numbering system (supports lakh, crore, arab, etc.).
|
|
7
|
+
*/
|
|
8
|
+
export const numToWords = (n) => {
|
|
9
|
+
const a = [
|
|
10
|
+
'',
|
|
11
|
+
'one',
|
|
12
|
+
'two',
|
|
13
|
+
'three',
|
|
14
|
+
'four',
|
|
15
|
+
'five',
|
|
16
|
+
'six',
|
|
17
|
+
'seven',
|
|
18
|
+
'eight',
|
|
19
|
+
'nine',
|
|
20
|
+
'ten',
|
|
21
|
+
'eleven',
|
|
22
|
+
'twelve',
|
|
23
|
+
'thirteen',
|
|
24
|
+
'fourteen',
|
|
25
|
+
'fifteen',
|
|
26
|
+
'sixteen',
|
|
27
|
+
'seventeen',
|
|
28
|
+
'eighteen',
|
|
29
|
+
'nineteen',
|
|
30
|
+
];
|
|
31
|
+
const b = [
|
|
32
|
+
'',
|
|
33
|
+
'',
|
|
34
|
+
'twenty',
|
|
35
|
+
'thirty',
|
|
36
|
+
'forty',
|
|
37
|
+
'fifty',
|
|
38
|
+
'sixty',
|
|
39
|
+
'seventy',
|
|
40
|
+
'eighty',
|
|
41
|
+
'ninety',
|
|
42
|
+
];
|
|
43
|
+
const g = [
|
|
44
|
+
'',
|
|
45
|
+
'thousand',
|
|
46
|
+
'lakh',
|
|
47
|
+
'crore',
|
|
48
|
+
'arab',
|
|
49
|
+
'kharab',
|
|
50
|
+
'neel',
|
|
51
|
+
'padma',
|
|
52
|
+
'shankh',
|
|
53
|
+
];
|
|
54
|
+
// Plural forms for units
|
|
55
|
+
const gPlural = [
|
|
56
|
+
'',
|
|
57
|
+
'thousand',
|
|
58
|
+
'lakhs',
|
|
59
|
+
'crores',
|
|
60
|
+
'arabs',
|
|
61
|
+
'kharabs',
|
|
62
|
+
'neels',
|
|
63
|
+
'padmas',
|
|
64
|
+
'shankhs',
|
|
65
|
+
];
|
|
66
|
+
if (typeof n === 'number')
|
|
67
|
+
n = String(n);
|
|
68
|
+
if (n === '0')
|
|
69
|
+
return 'zero';
|
|
70
|
+
// Indian chunking: last 3 digits, then every 2 digits
|
|
71
|
+
const chunkIndian = (xs) => {
|
|
72
|
+
if (xs.length <= 3)
|
|
73
|
+
return [xs];
|
|
74
|
+
const last3 = xs.slice(xs.length - 3);
|
|
75
|
+
const rest = xs.slice(0, xs.length - 3);
|
|
76
|
+
const pairs = [];
|
|
77
|
+
while (rest.length > 0) {
|
|
78
|
+
pairs.unshift(rest.splice(-2));
|
|
79
|
+
}
|
|
80
|
+
return [...pairs, last3];
|
|
81
|
+
};
|
|
82
|
+
// Convert a group (1 or 2 or 3 digits) to words
|
|
83
|
+
const groupToWords = (group) => {
|
|
84
|
+
let numVal = Number(group.join(''));
|
|
85
|
+
if (numVal === 0)
|
|
86
|
+
return '';
|
|
87
|
+
if (group.length === 3) {
|
|
88
|
+
// 3 digits: hundreds
|
|
89
|
+
let [h, t, o] = group.map(Number);
|
|
90
|
+
let str = '';
|
|
91
|
+
if (h)
|
|
92
|
+
str += a[h] + ' hundred ';
|
|
93
|
+
let lastTwo = t * 10 + o;
|
|
94
|
+
if (lastTwo) {
|
|
95
|
+
if (str)
|
|
96
|
+
str += 'and ';
|
|
97
|
+
if (lastTwo < 20)
|
|
98
|
+
str += a[lastTwo];
|
|
99
|
+
else
|
|
100
|
+
str += b[t] + (o ? '-' + a[o] : '');
|
|
101
|
+
}
|
|
102
|
+
return str.trim();
|
|
103
|
+
}
|
|
104
|
+
else if (group.length === 2) {
|
|
105
|
+
// 2 digits
|
|
106
|
+
let [t, o] = group.map(Number);
|
|
107
|
+
if (t === 0)
|
|
108
|
+
return a[o];
|
|
109
|
+
if (t * 10 + o < 20)
|
|
110
|
+
return a[t * 10 + o];
|
|
111
|
+
return b[t] + (o ? '-' + a[o] : '');
|
|
112
|
+
}
|
|
113
|
+
else if (group.length === 1) {
|
|
114
|
+
return a[Number(group[0])];
|
|
115
|
+
}
|
|
116
|
+
return '';
|
|
117
|
+
};
|
|
118
|
+
const digits = Array.from(n);
|
|
119
|
+
const groups = chunkIndian(digits);
|
|
120
|
+
const words = groups
|
|
121
|
+
.map((group, i, arr) => {
|
|
122
|
+
// Only pad the last group (units) to 3 digits, others to 2
|
|
123
|
+
if (i === arr.length - 1) {
|
|
124
|
+
while (group.length < 3)
|
|
125
|
+
group.unshift('0');
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
while (group.length < 2)
|
|
129
|
+
group.unshift('0');
|
|
130
|
+
}
|
|
131
|
+
return groupToWords(group);
|
|
132
|
+
})
|
|
133
|
+
.map((word, i, arr) => {
|
|
134
|
+
const unitIdx = arr.length - 1 - i;
|
|
135
|
+
// Use plural for units if value > 1 and unit is not ''
|
|
136
|
+
const groupValue = Number(groups[i].join(''));
|
|
137
|
+
const unit = groupValue > 1 && g[unitIdx] ? gPlural[unitIdx] : g[unitIdx];
|
|
138
|
+
return word ? `${word} ${unit}`.trim() : '';
|
|
139
|
+
})
|
|
140
|
+
.filter(Boolean)
|
|
141
|
+
.join(' ')
|
|
142
|
+
.replace(/\s+/g, ' ')
|
|
143
|
+
.trim();
|
|
144
|
+
return words;
|
|
145
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core-utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gCAAgC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core-utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yCAAyC,CAAC"}
|