File size: 16,174 Bytes
cb1ad55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
numToLez = '''(input_text) => {
    const million = 1e6; // 10^6;
    const billion = 1e9; // 10^9;
    const trillion = 1e12; // 10^12;
    const quadrillion = 1e15; // 10^15;
    const quintillion = 1e18; // 10^18;
    const sextillion = 1e21; // 10^21;
    const septillion = 1e24; // 10^24;
    const octillion = 1e27; // 10^27;
    const nonillion = 1e30; // 10^30;
    const atomic = {
        0: 'нул',
        1: 'сад',
        2: 'кьвед',
        3: 'пуд',
        4: 'кьуд',
        5: 'вад',
        6: 'ругуд',
        7: 'ирид',
        8: 'муьжуьд',
        9: 'кIуьд',
        10: 'цIуд',
        20: 'къад',
        40: 'яхцIур',
        // 10^2
        100: 'виш',
        // 10^3
        1000: 'агъзур',
        // 10^6
        [million]: 'миллион',
        // 10^9
        [billion]: 'миллиард',
        // 10^12
        [trillion]: 'триллион',
        // 10^15
        [quadrillion]: 'квадриллион',
        // 10^18
        [quintillion]: 'квинтиллион',
        // 10^21
        [sextillion]: 'секстиллион',
        // 10^24
        [septillion]: 'септиллион',
        // 10^27
        [octillion]: 'октиллион',
        // 10^30
        [nonillion]: 'нониллион',
    };
    const MINUS = 'минус';
    function separateNumberIntoUnits(n) {
        if (n == 0)
            return [0];
        const arr = [];
        let i = 1;
        while (n > 0) {
            arr.unshift((n % 10) * i);
            n = Math.floor(n / 10);
            i *= 10;
        }
        const result = groupNumberUnitsToLezgiRange(arr);
        return result;
    }
    const ranges = [
        { start: nonillion, end: octillion }, // nonillion to octillion
        { start: octillion, end: septillion }, // octillion to septillion
        { start: septillion, end: sextillion }, // septillion to sextillion
        { start: sextillion, end: quintillion }, // sextillion to quintillion
        { start: quadrillion, end: quintillion }, // quadrillion to quintillion
        { start: trillion, end: quadrillion }, // trillion to quadrillion
        { start: billion, end: trillion }, // billion to trillion
        { start: million, end: billion }, // million to billion
        { start: 1000, end: million }, // thousand to million
    ];
    function groupNumberUnitsToLezgiRange(arr) {
        let result = [];
        for (let range of ranges) {
            let sum = arr.reduce((acc, num) => {
                if (num >= range.start && num < range.end) {
                    return acc + num;
                }
                return acc;
            }, 0);
            if (sum !== 0) {
                result.push(sum);
            }
            // Filter out the numbers that were added to sum
            arr = arr.filter((num) => num < range.start || num >= range.end);
        }
        // Concatenate the remaining numbers to the result
        result = result.concat(arr);
        return result;
    }
    function getTenPlusBase(num) {
        if (num < 10 || num >= 20) {
            throw new Error('Invalid number');
        }
        if (num === 10) {
            return [atomic[10]];
        }
        const base10 = atomic[10].slice(0, -2);
        if (num === 11 || num === 15 || num === 16) {
            return [base10 + 'у'];
        }
        else if (num < 15) {
            return [base10 + 'и'];
        }
        return [base10 + 'е'];
    }
    function getTwentyPlusBase(num) {
        return num === 20 ? [atomic[20]] : ['къанни'];
    }
    function getThirtyPlusBase(num) {
        return [...getTwentyPlusBase(num), ...getTenPlusBase(num - 20)];
    }
    function getFourtyPlusBase(num) {
        return num === 40 ? [atomic[40]] : [atomic[40], 'ни'];
    }
    function getFiftyPlusBase(num) {
        return [...getFourtyPlusBase(num), ...getTenPlusBase(num - 40)];
    }
    function getSixtyPlusBase(num) {
        return num === 60 ? [atomic[3], atomic[20]] : [atomic[3], ...getTwentyPlusBase(num)];
    }
    function getSeventyPlusBase(num) {
        return [...getSixtyPlusBase(61), ...getTenPlusBase(num - 60)];
    }
    function getEightyPlusBase(num) {
        return num === 80 ? [atomic[4], atomic[20]] : [atomic[4], ...getTwentyPlusBase(num)];
    }
    function getNinetyPlusBase(num) {
        return [...getEightyPlusBase(81), ...getTenPlusBase(num - 80)];
    }
    function getHundredPlusBase(num) {
        return num % 100 === 0 ? [atomic[100]] : [atomic[100], 'ни'];
    }
    function getHundredPlusNumCount(numCount) {
        if (atomic[numCount] !== undefined) {
            return numCount === 2 ? [atomic[numCount].slice(0, -1)] : [atomic[numCount]];
        }
        return undefined;
    }
    function getBetweenHundredAndThousand(num, followUpNumber) {
        const hundredsCount = num % 100 != 0 ? num - (num % 100) : num / 100;
        const hundredsCountInLezgi = getHundredPlusNumCount(hundredsCount);
        return [...hundredsCountInLezgi, ' ', ...getHundredPlusBase(num + followUpNumber)];
    }
    function getThousandPlusBase(num) {
        return num % 1000 === 0 ? [atomic[1000]] : [atomic[1000], 'ни'];
    }
    function getBetweenThousandAndMillion(num, followUpNumber) {
        var _a;
        const thousandsCount = num % 1000 != 0 ? num - (num % 1000) : num / 1000;
        const thousandsCountInLezgi = (_a = getHundredPlusNumCount(thousandsCount)) !== null && _a !== void 0 ? _a : getCompound(thousandsCount);
        return [...thousandsCountInLezgi, ' ', ...getThousandPlusBase(num + followUpNumber)];
    }
    function getMillionPlusBase(num) {
        return num % million === 0 ? [atomic[million]] : [atomic[million], 'ни'];
    }
    function getBetweenMillionAndBillion(num, followUpNumber) {
        var _a;
        const millionsCount = num % million != 0 ? num - (num % million) : num / million;
        const millionsCountInLezgi = (_a = getHundredPlusNumCount(millionsCount)) !== null && _a !== void 0 ? _a : getCompound(millionsCount);
        return [...millionsCountInLezgi, ' ', ...getMillionPlusBase(num + followUpNumber)];
    }
    function getBillionPlusBase(num) {
        return num % billion === 0 ? [atomic[billion]] : [atomic[billion], 'ни'];
    }
    function getBetweenBillionAndTrillion(num, followUpNumber) {
        var _a;
        const billionsCount = num % billion != 0 ? num - (num % billion) : num / billion;
        const billionsCountInLezgi = (_a = getHundredPlusNumCount(billionsCount)) !== null && _a !== void 0 ? _a : getCompound(billionsCount);
        return [...billionsCountInLezgi, ' ', ...getBillionPlusBase(num + followUpNumber)];
    }
    function getTrillionPlusBase(num) {
        return num % trillion === 0 ? [atomic[trillion]] : [atomic[trillion], 'ни'];
    }
    function getBetweenTrillionAndQuadrillion(num, followUpNumber) {
        var _a;
        const trillionsCount = num % trillion != 0 ? num - (num % trillion) : num / trillion;
        const trillionsCountInLezgi = (_a = getHundredPlusNumCount(trillionsCount)) !== null && _a !== void 0 ? _a : getCompound(trillionsCount);
        return [...trillionsCountInLezgi, ' ', ...getTrillionPlusBase(num + followUpNumber)];
    }
    function getQuadrillionPlusBase(num) {
        return num % quadrillion === 0 ? [atomic[quadrillion]] : [atomic[quadrillion], 'ни'];
    }
    function getBetweenQuadrillionAndQuintillion(num, followUpNumber) {
        var _a;
        const quadrillionsCount = num % quadrillion != 0 ? num - (num % quadrillion) : num / quadrillion;
        const quadrillionsCountInLezgi = (_a = getHundredPlusNumCount(quadrillionsCount)) !== null && _a !== void 0 ? _a : getCompound(quadrillionsCount);
        return [...quadrillionsCountInLezgi, ' ', ...getQuadrillionPlusBase(num + followUpNumber)];
    }
    function getQuintillionPlusBase(num) {
        return num % quintillion === 0 ? [atomic[quintillion]] : [atomic[quintillion], 'ни'];
    }
    function getBetweenQuintillionAndSextillion(num, followUpNumber) {
        var _a;
        const quintillionsCount = num % quintillion != 0 ? num - (num % quintillion) : num / quintillion;
        const quintillionsCountInLezgi = (_a = getHundredPlusNumCount(quintillionsCount)) !== null && _a !== void 0 ? _a : getCompound(quintillionsCount);
        return [...quintillionsCountInLezgi, ' ', ...getQuintillionPlusBase(num + followUpNumber)];
    }
    function getSextillionPlusBase(num) {
        return num % sextillion === 0 ? [atomic[sextillion]] : [atomic[sextillion], 'ни'];
    }
    function getBetweenSextillionAndSeptillion(num, followUpNumber) {
        var _a;
        const sextillionsCount = num % sextillion != 0 ? num - (num % sextillion) : num / sextillion;
        const sextillionsCountInLezgi = (_a = getHundredPlusNumCount(sextillionsCount)) !== null && _a !== void 0 ? _a : getCompound(sextillionsCount);
        return [...sextillionsCountInLezgi, ' ', ...getSextillionPlusBase(num + followUpNumber)];
    }
    function getSeptillionPlusBase(num) {
        return num % septillion === 0 ? [atomic[septillion]] : [atomic[septillion], 'ни'];
    }
    function getBetweenSeptillionAndOctillion(num, followUpNumber) {
        var _a;
        const septillionsCount = num % septillion != 0 ? num - (num % septillion) : num / septillion;
        const septillionsCountInLezgi = (_a = getHundredPlusNumCount(septillionsCount)) !== null && _a !== void 0 ? _a : getCompound(septillionsCount);
        return [...septillionsCountInLezgi, ' ', ...getSeptillionPlusBase(num + followUpNumber)];
    }
    function getOctillionPlusBase(num) {
        return num % octillion === 0 ? [atomic[octillion]] : [atomic[octillion], 'ни'];
    }
    function getBetweenOctillionAndNonillion(num, followUpNumber) {
        var _a;
        const octillionsCount = num % octillion != 0 ? num - (num % octillion) : num / octillion;
        const octillionsCountInLezgi = (_a = getHundredPlusNumCount(octillionsCount)) !== null && _a !== void 0 ? _a : getCompound(octillionsCount);
        return [...octillionsCountInLezgi, ' ', ...getOctillionPlusBase(num + followUpNumber)];
    }
    function getNonillionPlusBase(num) {
        return num % nonillion === 0 ? [atomic[nonillion]] : [atomic[nonillion], 'ни'];
    }
    function getCompound(num) {
        const units = separateNumberIntoUnits(num);
        const result = units.map((unit, i) => {
            if (i > 0 &&
                unit === 7 &&
                (units[i - 1] === 10 ||
                    units[i - 1] === 30 ||
                    units[i - 1] === 50 ||
                    units[i - 1] === 70 ||
                    units[i - 1] === 90)) {
                return [atomic[7].slice(1)];
            }
            const followUpNumber = units.slice(i + 1).reduce((acc, num) => acc + num, 0);
            if (unit === 10) {
                return getTenPlusBase(unit + followUpNumber);
            }
            if (unit === 20) {
                return getTwentyPlusBase(unit + followUpNumber);
            }
            if (unit === 30) {
                return getThirtyPlusBase(unit + followUpNumber);
            }
            if (unit === 40) {
                return getFourtyPlusBase(unit + followUpNumber);
            }
            if (unit === 50) {
                return getFiftyPlusBase(unit + followUpNumber);
            }
            if (unit === 60) {
                return getSixtyPlusBase(unit + followUpNumber);
            }
            if (unit === 70) {
                return getSeventyPlusBase(unit + followUpNumber);
            }
            if (unit === 80) {
                return getEightyPlusBase(unit + followUpNumber);
            }
            if (unit === 90) {
                return getNinetyPlusBase(unit + followUpNumber);
            }
            if (unit === 100) {
                return getHundredPlusBase(unit + followUpNumber);
            }
            if (unit > 100 && unit < 1000) {
                return getBetweenHundredAndThousand(unit, followUpNumber);
            }
            if (unit === 1000) {
                return getThousandPlusBase(unit + followUpNumber);
            }
            if (unit > 1000 && unit < million) {
                return getBetweenThousandAndMillion(unit, followUpNumber);
            }
            if (unit === million) {
                return getMillionPlusBase(unit + followUpNumber);
            }
            if (unit > million && unit < billion) {
                return getBetweenMillionAndBillion(unit, followUpNumber);
            }
            if (unit === billion) {
                return getBillionPlusBase(unit + followUpNumber);
            }
            if (unit > billion && unit < trillion) {
                return getBetweenBillionAndTrillion(unit, followUpNumber);
            }
            if (unit === trillion) {
                return getTrillionPlusBase(unit + followUpNumber);
            }
            if (unit > trillion && unit < quadrillion) {
                return getBetweenTrillionAndQuadrillion(unit, followUpNumber);
            }
            if (unit === quadrillion) {
                return getQuadrillionPlusBase(unit + followUpNumber);
            }
            if (unit > quadrillion && unit < quintillion) {
                return getBetweenQuadrillionAndQuintillion(unit, followUpNumber);
            }
            if (unit === quintillion) {
                return getQuintillionPlusBase(unit + followUpNumber);
            }
            if (unit > quintillion && unit < sextillion) {
                return getBetweenQuintillionAndSextillion(unit, followUpNumber);
            }
            if (unit === sextillion) {
                return getSextillionPlusBase(unit + followUpNumber);
            }
            if (unit > sextillion && unit < septillion) {
                return getBetweenSextillionAndSeptillion(unit, followUpNumber);
            }
            if (unit === septillion) {
                return getSeptillionPlusBase(unit + followUpNumber);
            }
            if (unit > septillion && unit < octillion) {
                return getBetweenSeptillionAndOctillion(unit, followUpNumber);
            }
            if (unit === octillion) {
                return getOctillionPlusBase(unit + followUpNumber);
            }
            if (unit > octillion && unit < nonillion) {
                return getBetweenOctillionAndNonillion(unit, followUpNumber);
            }
            if (unit === nonillion) {
                return getNonillionPlusBase(unit + followUpNumber);
            }
            return units.length > 1 && unit === 0 ? [''] : [atomic[unit] || unit.toString()];
        });
        return result.flat(); //.join('').replaceAll('  ', ' ').trim();
    }
    function getAtomicOrCompound(num) {
        if (atomic[num]) {
            return [atomic[num]];
        }
        else {
            return getCompound(num);
        }
    }
    function numToLezgiArray(num) {
        if (isNaN(num)) {
            throw new Error('Provided value is not a number');
        }
        if (!Number.isInteger(num)) {
            throw new Error('Provided number is not an integer. Currently only integers are supported!');
        }
        const isNegative = num < 0;
        num = Math.abs(num);
        const result = getAtomicOrCompound(num)
            .filter((word) => word !== '')
            .map((word) => (word.endsWith('ни') ? [word, ' '] : word))
            .flat();
        return isNegative ? [MINUS, ' ', ...result] : result;
    }
    function numToLezgi(num) {
        const resultArray = numToLezgiArray(num);
        return (resultArray
            // .map((word) => (word.endsWith('ни') || word === MINUS ? word + ' ' : word))
            .join('')
            .replaceAll('  ', ' ')
            .trim());
    }

function replaceNumbersWithWords(inputString) {
    return inputString.replaceAll(/\d+/g, (num) => numToLezgi(parseInt(num, 10)));
}
console.log(replaceNumbersWithWords(input_text))
return replaceNumbersWithWords(input_text)
}'''