italian-numbers 0.0.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/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/constants.js +40 -0
- package/dist/index.js +109 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Nicolò Zandarin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.and = exports.billions = exports.millions = exports.thousands = exports.hundred = exports.tens = exports.zeroNineteen = void 0;
|
|
4
|
+
exports.zeroNineteen = [
|
|
5
|
+
'zero',
|
|
6
|
+
'uno',
|
|
7
|
+
'due',
|
|
8
|
+
'tre',
|
|
9
|
+
'quattro',
|
|
10
|
+
'cinque',
|
|
11
|
+
'sei',
|
|
12
|
+
'sette',
|
|
13
|
+
'otto',
|
|
14
|
+
'nove',
|
|
15
|
+
'dieci',
|
|
16
|
+
'undici',
|
|
17
|
+
'dodici',
|
|
18
|
+
'tredici',
|
|
19
|
+
'quattordici',
|
|
20
|
+
'quindici',
|
|
21
|
+
'sedici',
|
|
22
|
+
'diciassette',
|
|
23
|
+
'diciotto',
|
|
24
|
+
'diciannove',
|
|
25
|
+
];
|
|
26
|
+
exports.tens = [
|
|
27
|
+
'venti',
|
|
28
|
+
'trenta',
|
|
29
|
+
'quaranta',
|
|
30
|
+
'cinquanta',
|
|
31
|
+
'sessanta',
|
|
32
|
+
'settanta',
|
|
33
|
+
'ottanta',
|
|
34
|
+
'novanta',
|
|
35
|
+
];
|
|
36
|
+
exports.hundred = 'cento';
|
|
37
|
+
exports.thousands = ['mille', 'mila'];
|
|
38
|
+
exports.millions = ['un milione', ' milioni'];
|
|
39
|
+
exports.billions = ['un miliardo', ' miliardi'];
|
|
40
|
+
exports.and = ' e ';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("./constants");
|
|
4
|
+
const tensConverter = (n) => {
|
|
5
|
+
if (n < 20) {
|
|
6
|
+
return constants_1.zeroNineteen[n];
|
|
7
|
+
}
|
|
8
|
+
const firstDigit = Math.floor(n / 10) - 2;
|
|
9
|
+
const secondDigit = n % 10;
|
|
10
|
+
const word = constants_1.tens[firstDigit];
|
|
11
|
+
if (secondDigit === 0) {
|
|
12
|
+
return word;
|
|
13
|
+
}
|
|
14
|
+
if (secondDigit === 1 || secondDigit === 8) {
|
|
15
|
+
return word.slice(0, word.length - 1) + constants_1.zeroNineteen[secondDigit];
|
|
16
|
+
}
|
|
17
|
+
return word + constants_1.zeroNineteen[secondDigit];
|
|
18
|
+
};
|
|
19
|
+
const hundredsConverter = (n) => {
|
|
20
|
+
const firstDigit = Math.floor(n / 100);
|
|
21
|
+
if (firstDigit === 1) {
|
|
22
|
+
return constants_1.hundred;
|
|
23
|
+
}
|
|
24
|
+
return constants_1.zeroNineteen[firstDigit] + constants_1.hundred;
|
|
25
|
+
};
|
|
26
|
+
const wordCalculator = (n) => {
|
|
27
|
+
// eslint-disable-next-line no-bitwise
|
|
28
|
+
const digitsNumber = Math.log(n) * Math.LOG10E + 1 | 0;
|
|
29
|
+
if (digitsNumber < 3) {
|
|
30
|
+
return tensConverter(n);
|
|
31
|
+
}
|
|
32
|
+
if (digitsNumber < 4) {
|
|
33
|
+
if (n % 100 === 0) {
|
|
34
|
+
return hundredsConverter(n);
|
|
35
|
+
}
|
|
36
|
+
if (n % 100 > 79 && n % 100 < 90) {
|
|
37
|
+
const word = hundredsConverter(n);
|
|
38
|
+
return word.slice(0, word.length - 1) + wordCalculator(n % 100);
|
|
39
|
+
}
|
|
40
|
+
return hundredsConverter(n) + wordCalculator(n % 100);
|
|
41
|
+
}
|
|
42
|
+
// thousands
|
|
43
|
+
if (digitsNumber < 7) {
|
|
44
|
+
const firstDigits = Math.floor(n / 1000);
|
|
45
|
+
if (firstDigits === 1) {
|
|
46
|
+
if (n % 1000 === 0) {
|
|
47
|
+
return constants_1.thousands[0];
|
|
48
|
+
}
|
|
49
|
+
return constants_1.thousands[0] + wordCalculator(n % 1000);
|
|
50
|
+
}
|
|
51
|
+
if (n % 1000 === 0) {
|
|
52
|
+
return wordCalculator(firstDigits) + constants_1.thousands[1];
|
|
53
|
+
}
|
|
54
|
+
return (wordCalculator(firstDigits) + constants_1.thousands[1] + wordCalculator(n % 1000));
|
|
55
|
+
}
|
|
56
|
+
// millions
|
|
57
|
+
if (digitsNumber < 10) {
|
|
58
|
+
const firstDigits = Math.floor(n / 1000000);
|
|
59
|
+
if (firstDigits === 1) {
|
|
60
|
+
if (n % 1000000 === 0) {
|
|
61
|
+
return constants_1.millions[0];
|
|
62
|
+
}
|
|
63
|
+
return constants_1.millions[0] + constants_1.and + wordCalculator(n % 1000000);
|
|
64
|
+
}
|
|
65
|
+
if (n % 1000000 === 0) {
|
|
66
|
+
return wordCalculator(firstDigits) + constants_1.millions[1];
|
|
67
|
+
}
|
|
68
|
+
return (wordCalculator(firstDigits) + constants_1.millions[1] + constants_1.and + wordCalculator(n % 1000000));
|
|
69
|
+
}
|
|
70
|
+
// billions
|
|
71
|
+
if (digitsNumber < 13) {
|
|
72
|
+
const firstDigits = Math.floor(n / 1000000000);
|
|
73
|
+
if (firstDigits === 1) {
|
|
74
|
+
if (n % 1000000000 === 0) {
|
|
75
|
+
return constants_1.billions[0];
|
|
76
|
+
}
|
|
77
|
+
return constants_1.billions[0] + constants_1.and + wordCalculator(n % 1000000000);
|
|
78
|
+
}
|
|
79
|
+
if (n % 1000000000 === 0) {
|
|
80
|
+
return wordCalculator(firstDigits) + constants_1.billions[1];
|
|
81
|
+
}
|
|
82
|
+
return (wordCalculator(firstDigits) + constants_1.billions[1] + constants_1.and + wordCalculator(n % 1000000000));
|
|
83
|
+
}
|
|
84
|
+
return 'oh my zsh';
|
|
85
|
+
};
|
|
86
|
+
const converter = (number) => {
|
|
87
|
+
if (Number.isNaN(number)) {
|
|
88
|
+
throw new Error('not a number');
|
|
89
|
+
}
|
|
90
|
+
if (number === Infinity) {
|
|
91
|
+
return 'infinito';
|
|
92
|
+
}
|
|
93
|
+
if (number > 999999999999) {
|
|
94
|
+
throw new Error('greater than 999999999999');
|
|
95
|
+
}
|
|
96
|
+
let prepend = '';
|
|
97
|
+
if (number < 0) {
|
|
98
|
+
prepend = 'meno ';
|
|
99
|
+
}
|
|
100
|
+
const n = Math.abs(number);
|
|
101
|
+
let result = prepend + wordCalculator(n);
|
|
102
|
+
if (result.endsWith('tre') && result !== 'tre') {
|
|
103
|
+
result = result.replace(/.$/, 'é');
|
|
104
|
+
}
|
|
105
|
+
// const regex = /(?<=\w+)tre/g;
|
|
106
|
+
result = result.replaceAll(/(?<=\w+)tre\s/g, 'tré ');
|
|
107
|
+
return result;
|
|
108
|
+
};
|
|
109
|
+
exports.default = converter;
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "italian-numbers",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Converts a number to an italian word representation",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"node",
|
|
9
|
+
"javascript",
|
|
10
|
+
"typescript",
|
|
11
|
+
"italian",
|
|
12
|
+
"numbers",
|
|
13
|
+
"converter"
|
|
14
|
+
],
|
|
15
|
+
"repository": "git@github.com:nikzanda/italian-numbers.git",
|
|
16
|
+
"author": "Nicolò Zandarin <nikzanda@hotmail.it>",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"watch": "nodemon ./src/index.ts",
|
|
23
|
+
"lint": "eslint . --ext .js,.ts",
|
|
24
|
+
"test": "jest",
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"clean": "rm -rf dist"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@jest/globals": "^29.7.0",
|
|
30
|
+
"@types/jest": "^29.5.5",
|
|
31
|
+
"@typescript-eslint/eslint-plugin": "^6.7.5",
|
|
32
|
+
"@typescript-eslint/parser": "^6.7.5",
|
|
33
|
+
"eslint": "^8.51.0",
|
|
34
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
35
|
+
"eslint-import-resolver-typescript": "^3.6.1",
|
|
36
|
+
"eslint-plugin-import": "^2.28.1",
|
|
37
|
+
"jest": "^29.7.0",
|
|
38
|
+
"nodemon": "^3.0.1",
|
|
39
|
+
"ts-jest": "^29.1.1",
|
|
40
|
+
"ts-node": "^10.9.1",
|
|
41
|
+
"typescript": "^5.2.2"
|
|
42
|
+
}
|
|
43
|
+
}
|