@ryokaringumi/english-number-naming-js 1.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/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # english-number-naming
2
+
3
+ A JavaScript library to convert numbers to their English word representations.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install english-number-naming
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```javascript
14
+ const { numberToEnglish } = require('english-number-naming');
15
+
16
+ console.log(numberToEnglish(123)); // "one hundred twenty-three"
17
+ console.log(numberToEnglish(0)); // "zero"
18
+ console.log(numberToEnglish(-5)); // "minus five"
19
+ console.log(numberToEnglish(3.14)); // "three point one four"
20
+ ```
21
+
22
+ ## Development
23
+
24
+ ### Build
25
+
26
+ ```bash
27
+ npm run build
28
+ ```
29
+
30
+ ### Test
31
+
32
+ ```bash
33
+ npm run test
34
+ ```
35
+
36
+ ## License
37
+
38
+ ISC
@@ -0,0 +1 @@
1
+ export declare function numberToEnglish(n: number): string;
package/dist/index.js ADDED
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.numberToEnglish = numberToEnglish;
4
+ function zeroToTowelve(n) {
5
+ const ones = [
6
+ "zero",
7
+ "one",
8
+ "two",
9
+ "three",
10
+ "four",
11
+ "five",
12
+ "six",
13
+ "seven",
14
+ "eight",
15
+ "nine",
16
+ "ten",
17
+ "eleven",
18
+ "twelve",
19
+ ];
20
+ return ones[n];
21
+ }
22
+ function thirteenToNineteen(n) {
23
+ if (n === 13)
24
+ return "thirteen";
25
+ if (n === 15)
26
+ return "fifteen";
27
+ if (n === 18)
28
+ return "eighteen";
29
+ return numberToEnglish(n % 10) + "teen";
30
+ }
31
+ function twentyToNinetyNine(n) {
32
+ const tens = [
33
+ "",
34
+ "ten",
35
+ "twenty",
36
+ "thirty",
37
+ "forty",
38
+ "fifty",
39
+ "sixty",
40
+ "seventy",
41
+ "eighty",
42
+ "ninety",
43
+ ];
44
+ const tenPart = Math.floor(n / 10);
45
+ const onePart = n % 10;
46
+ if (onePart === 0) {
47
+ return tens[tenPart];
48
+ }
49
+ else {
50
+ return tens[tenPart] + "-" + numberToEnglish(onePart);
51
+ }
52
+ }
53
+ function oneHundredToNineHundredNinetyNine(n) {
54
+ const hundredPart = Math.floor(n / 100);
55
+ const restPart = n % 100;
56
+ if (restPart === 0) {
57
+ return numberToEnglish(hundredPart) + " hundred";
58
+ }
59
+ else {
60
+ return numberToEnglish(hundredPart) + " hundred " + numberToEnglish(restPart);
61
+ }
62
+ }
63
+ function largeNumber(number) {
64
+ const digits = Math.floor(Math.log10(number)) + 1;
65
+ const largeNumberUnits = {
66
+ 4: "thousand",
67
+ 7: "million",
68
+ 10: "billion",
69
+ 13: "trillion",
70
+ 16: "quadrillion",
71
+ 19: "quintillion",
72
+ };
73
+ let unitsDigits = digits;
74
+ while (!(unitsDigits in largeNumberUnits) && unitsDigits > 3) {
75
+ unitsDigits--;
76
+ }
77
+ const unitName = largeNumberUnits[unitsDigits];
78
+ const unitValue = Math.pow(10, unitsDigits - 1);
79
+ const leadingPart = Math.floor(number / unitValue);
80
+ const restPart = number % unitValue;
81
+ let result = numberToEnglish(leadingPart) + " " + unitName;
82
+ if (restPart > 0) {
83
+ result += " " + numberToEnglish(restPart);
84
+ }
85
+ return result;
86
+ }
87
+ function decimalPointToEnglish(n) {
88
+ const naturalPart = Math.floor(n);
89
+ let result = numberToEnglish(naturalPart) + " point";
90
+ const decimalString = n.toString().split(".")[1].split("").map(c => numberToEnglish(parseInt(c))).join(" ");
91
+ return result + " " + decimalString;
92
+ }
93
+ function numberToEnglish(n) {
94
+ if (Number.isNaN(n)) {
95
+ throw new Error("Input is not a number");
96
+ }
97
+ if (n < 0) {
98
+ return "minus " + numberToEnglish(-n);
99
+ }
100
+ if (n % 1 !== 0) {
101
+ return decimalPointToEnglish(n);
102
+ }
103
+ const digits = Math.floor(Math.log10(n)) + 1;
104
+ if (digits >= 4) {
105
+ return largeNumber(n);
106
+ }
107
+ if (n >= 0 && n <= 12) {
108
+ return zeroToTowelve(n);
109
+ }
110
+ if (n >= 13 && n <= 19) {
111
+ return thirteenToNineteen(n);
112
+ }
113
+ if (n >= 20 && n <= 99) {
114
+ return twentyToNinetyNine(n);
115
+ }
116
+ if (n >= 100 && n <= 999) {
117
+ return oneHundredToNineHundredNinetyNine(n);
118
+ }
119
+ throw new Error("Number out of range");
120
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@ryokaringumi/english-number-naming-js",
3
+ "version": "1.0.0",
4
+ "description": "A JavaScript library to convert numbers to their English word representations.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "rm -rf dist && tsc",
12
+ "test": "vitest",
13
+ "prepublishOnly": "npm run build"
14
+ },
15
+ "keywords": [
16
+ "number",
17
+ "english",
18
+ "conversion",
19
+ "words"
20
+ ],
21
+ "author": "RyoKaringumi",
22
+ "license": "ISC",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/ryokaringumi/english-number-naming-js.git"
26
+ },
27
+ "homepage": "https://github.com/ryokaringumi/english-number-naming-js#readme",
28
+ "bugs": {
29
+ "url": "https://github.com/ryokaringumi/english-number-naming-js/issues"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^25.0.3",
33
+ "typescript": "^5.9.3",
34
+ "vitest": "^4.0.16"
35
+ },
36
+ "vitest": {
37
+ "include": ["src/**/*.test.ts"],
38
+ "exclude": ["dist/**"]
39
+ }
40
+ }