@gks101/numtowords 1.0.0 → 1.0.1

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 ADDED
@@ -0,0 +1,34 @@
1
+ Uses
2
+ ====
3
+
4
+ This project (numtowords) includes or integrates with the following third-party libraries and tools (not an exhaustive list):
5
+
6
+ - rollup
7
+ - eslint, prettier
8
+ - typescript
9
+
10
+ These dependencies are governed by their own licenses (see package.json and node_modules for details). The project source distributed here is licensed under the MIT License as shown below.
11
+
12
+
13
+ MIT License
14
+ ===========
15
+
16
+ Copyright (c) 2026 numtowords
17
+
18
+ Permission is hereby granted, free of charge, to any person obtaining a copy
19
+ of this software and associated documentation files (the "Software"), to deal
20
+ in the Software without restriction, including without limitation the rights
21
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22
+ copies of the Software, and to permit persons to whom the Software is
23
+ furnished to do so, subject to the following conditions:
24
+
25
+ The above copyright notice and this permission notice shall be included in all
26
+ copies or substantial portions of the Software.
27
+
28
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # @gks101/numtowords
2
2
 
3
- > Convert numbers to words in **multiple languages and numeral systems** — English, Indian, Hindi, German, French — with full TypeScript support.
3
+ [![bundle size](https://badgen.net/bundlephobia/minzip/react)](https://bundlephobia.com/package/@gks101/numtowords) [![license](https://img.shields.io/badge/license-MIT-green)](LICENSE) [![tree shaking](https://badgen.net/bundlephobia/tree-shaking/react-colorful)](https://bundlephobia.com/package/@gks101/numtowords) [![dependency count](https://badgen.net/bundlephobia/dependency-count/react-colorful)](https://bundlephobia.com/package/@gks101/numtowords)
4
+
5
+ Convert numbers to words in **multiple languages and numeral systems** — English, Indian, Hindi, German, French — with full TypeScript support,zero dependencies and < 3kb size.
4
6
 
5
7
  ```
6
8
  convert(1_000_124) // "One million one hundred and twenty-four"
@@ -35,42 +37,42 @@ npm install @gks101@numtowords
35
37
  ## Quick Start
36
38
 
37
39
  ```ts
38
- import { convert } from "@gks101@numtowords";
40
+ import { convert } from '@gks101@numtowords';
39
41
 
40
- convert(0); // "Zero"
41
- convert(42); // "Forty-two"
42
- convert(1_000_000); // "One million"
42
+ convert(0); // "Zero"
43
+ convert(42); // "Forty-two"
44
+ convert(1_000_000); // "One million"
43
45
  ```
44
46
 
45
47
  ### Indian numbering system
46
48
 
47
49
  ```ts
48
- convert(1_00_000, { locale: "in" }); // "One lakh"
49
- convert(10_00_000, { locale: "in" }); // "Ten lakh"
50
- convert(1_00_00_000, { locale: "in" }); // "One crore"
50
+ convert(1_00_000, { locale: 'in' }); // "One lakh"
51
+ convert(10_00_000, { locale: 'in' }); // "Ten lakh"
52
+ convert(1_00_00_000, { locale: 'in' }); // "One crore"
51
53
  ```
52
54
 
53
55
  ### Hindi (Devanagari)
54
56
 
55
57
  ```ts
56
- convert(1_00_000, { locale: "hi" }); // "एक लाख"
57
- convert(1_00_00_000, { locale: "hi" }); // "एक करोड़"
58
+ convert(1_00_000, { locale: 'hi' }); // "एक लाख"
59
+ convert(1_00_00_000, { locale: 'hi' }); // "एक करोड़"
58
60
  ```
59
61
 
60
62
  ### German
61
63
 
62
64
  ```ts
63
- convert(21, { locale: "de" }); // "Einundzwanzig"
64
- convert(1_000_000, { locale: "de" }); // "Eine Million"
65
- convert(2_000_000, { locale: "de" }); // "Zwei Millionen"
65
+ convert(21, { locale: 'de' }); // "Einundzwanzig"
66
+ convert(1_000_000, { locale: 'de' }); // "Eine Million"
67
+ convert(2_000_000, { locale: 'de' }); // "Zwei Millionen"
66
68
  ```
67
69
 
68
70
  ### French
69
71
 
70
72
  ```ts
71
- convert(70, { locale: "fr" }); // "Soixante-dix"
72
- convert(80, { locale: "fr" }); // "Quatre-vingts"
73
- convert(1_000_000, { locale: "fr" }); // "Un million"
73
+ convert(70, { locale: 'fr' }); // "Soixante-dix"
74
+ convert(80, { locale: 'fr' }); // "Quatre-vingts"
75
+ convert(1_000_000, { locale: 'fr' }); // "Un million"
74
76
  ```
75
77
 
76
78
  ---
@@ -79,26 +81,30 @@ convert(1_000_000, { locale: "fr" }); // "Un million"
79
81
 
80
82
  ### `convert(input, options?)`
81
83
 
82
- | Parameter | Type | Description |
83
- |-----------|------|-------------|
84
- | `input` | `number \| bigint \| string` | The number to convert. Negative numbers, string input with commas/underscores, and BigInts are all supported. |
85
- | `options` | `ConvertOptions` | Optional configuration (see below). |
84
+ | Parameter | Type | Description |
85
+ | --------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------- |
86
+ | `input` | `number \| bigint \| string` | The number to convert. Negative numbers, string input with commas/underscores, and BigInts are all supported. |
87
+ | `options` | `ConvertOptions` | Optional configuration (see below). |
86
88
 
87
89
  **Returns** `string`
88
90
 
89
91
  ### `ConvertOptions`
90
92
 
91
- | Option | Type | Default | Description |
92
- |--------|------|---------|-------------|
93
- | `locale` | `"en" \| "in" \| "hi" \| "de" \| "fr"` | `"en"` | Target language / numeral system |
94
- | `capitalize` | `boolean` | `true` | Capitalise the first letter |
95
- | `useAnd` | `boolean` | `true` | Include "and" connector (English only) |
96
- | `currency` | `string` | `""` | Currency label appended to result, e.g. `"USD"` |
93
+ | Option | Type | Default | Description |
94
+ | ------------ | -------------------------------------- | ------- | ----------------------------------------------- |
95
+ | `locale` | `"en" \| "in" \| "hi" \| "de" \| "fr"` | `"en"` | Target language / numeral system |
96
+ | `capitalize` | `boolean` | `true` | Capitalise the first letter |
97
+ | `useAnd` | `boolean` | `true` | Include "and" connector (English only) |
98
+ | `currency` | `string` | `""` | Currency label appended to result, e.g. `"USD"` |
97
99
 
98
100
  ### Other exports
99
101
 
100
102
  ```ts
101
- import { availableLocales, registerLocale, getLocale } from "@gks101@numtowords";
103
+ import {
104
+ availableLocales,
105
+ registerLocale,
106
+ getLocale,
107
+ } from '@gks101@numtowords';
102
108
 
103
109
  availableLocales(); // ["en", "in", "hi", "de", "fr"]
104
110
  ```
@@ -110,8 +116,8 @@ availableLocales(); // ["en", "in", "hi", "de", "fr"]
110
116
  ### BigInt (very large numbers)
111
117
 
112
118
  ```ts
113
- convert(9_007_199_254_740_993n); // beyond Number.MAX_SAFE_INTEGER
114
- convert(1_00_00_00_00_000n, { locale: "in" }); // "One arab"
119
+ convert(9_007_199_254_740_993n); // beyond Number.MAX_SAFE_INTEGER
120
+ convert(1_00_00_00_00_000n, { locale: 'in' }); // "One arab"
115
121
  ```
116
122
 
117
123
  ### Disable capitalisation
@@ -129,8 +135,8 @@ convert(101, { useAnd: false }); // "One hundred one"
129
135
  ### Currency
130
136
 
131
137
  ```ts
132
- convert(5, { currency: "USD" }); // "Five USD"
133
- convert(100000, { locale: "in", currency: "INR" }); // "One lakh INR"
138
+ convert(5, { currency: 'USD' }); // "Five USD"
139
+ convert(100000, { locale: 'in', currency: 'INR' }); // "One lakh INR"
134
140
  ```
135
141
 
136
142
  ### Negative numbers
@@ -148,46 +154,37 @@ You can add locales at runtime by calling `registerLocale()` with a LocaleDefini
148
154
  Minimal stub:
149
155
 
150
156
  ```ts
151
- import { registerLocale, convert } from "@gks101@numtowords";
157
+ import { registerLocale, convert } from '@gks101@numtowords';
152
158
 
153
- registerLocale("es", {
154
- name: "Spanish",
159
+ registerLocale('es', {
160
+ name: 'Spanish',
155
161
  convert(n, _opts) {
156
162
  // minimal implementation — handle zero and fall back to a placeholder
157
- if (n === 0n) return "cero";
158
- return "número";
163
+ if (n === 0n) return 'cero';
164
+ return 'número';
159
165
  },
160
166
  });
161
167
 
162
- convert(5, { locale: "es" }); // "Número" (capitalisation applied by library)
168
+ convert(5, { locale: 'es' }); // "Número" (capitalisation applied by library)
163
169
  ```
164
170
 
165
171
  Practical Spanish examples
166
172
 
167
173
  ```ts
168
174
  // Basic numbers
169
- convert(0, { locale: "es" }); // "Cero"
170
- convert(1, { locale: "es" }); // "Uno"
171
- convert(2, { locale: "es" }); // "Dos"
172
- convert(10, { locale: "es" }); // "Diez"
173
- convert(16, { locale: "es" }); // "Dieciséis"
174
- convert(21, { locale: "es" }); // "Veintiuno"
175
- convert(22, { locale: "es" }); // "Veintidós"
176
-
175
+ convert(0, { locale: 'es' }); // "Cero"
176
+ convert(1, { locale: 'es' }); // "Uno"
177
177
  // Hundreds and thousands
178
- convert(100, { locale: "es" }); // "Cien"
179
- convert(101, { locale: "es" }); // "Ciento uno"
180
- convert(199, { locale: "es" }); // "Ciento noventa y nueve"
181
- convert(1000, { locale: "es" }); // "Mil"
182
- convert(1_001, { locale: "es" }); // "Mil uno"
178
+ convert(100, { locale: 'es' }); // "Cien"
179
+ convert(101, { locale: 'es' }); // "Ciento uno"
183
180
 
184
181
  // Millions, accents and plurals
185
- convert(1_000_000, { locale: "es" }); // "Un millón"
186
- convert(2_000_000, { locale: "es" }); // "Dos millones"
182
+ convert(1_000_000, { locale: 'es' }); // "Un millón"
183
+ convert(2_000_000, { locale: 'es' }); // "Dos millones"
187
184
 
188
185
  // Negative and currency examples
189
- convert(-5, { locale: "es" }); // "Negativo cinco"
190
- convert(1, { locale: "es", currency: "EUR" }); // "Uno EUR"
186
+ convert(-5, { locale: 'es' }); // "Negativo cinco"
187
+ convert(1, { locale: 'es', currency: 'EUR' }); // "Uno EUR"
191
188
  ```
192
189
 
193
190
  Notes
@@ -200,11 +197,11 @@ Notes
200
197
 
201
198
  ## Bundle Formats
202
199
 
203
- | File | Format | Use case |
204
- |------|--------|----------|
200
+ | File | Format | Use case |
201
+ | ------------------- | --------- | -------------------------------- |
205
202
  | `dist/index.esm.js` | ES Module | Bundlers (Vite, webpack, Rollup) |
206
- | `dist/index.cjs.js` | CommonJS | Node.js (`require`) |
207
- | `dist/index.umd.js` | UMD | Browser `<script>` tags |
203
+ | `dist/index.cjs.js` | CommonJS | Node.js (`require`) |
204
+ | `dist/index.umd.js` | UMD | Browser `<script>` tags |
208
205
 
209
206
  ### Browser via CDN
210
207
 
@@ -230,16 +227,16 @@ npm run lint # TypeScript type check
230
227
 
231
228
  ## Indian Numeral Scale Reference
232
229
 
233
- | Value | Indian Name | English equivalent |
234
- |-------|------------|-------------------|
235
- | 1,000 | Thousand | Thousand |
236
- | 1,00,000 | Lakh | Hundred Thousand |
237
- | 1,00,00,000 | Crore | Ten Million |
238
- | 1,00,00,00,000 | Arab | Billion |
239
- | 1,00,00,00,00,000 | Kharab | Hundred Billion |
240
- | 1,00,00,00,00,00,000 | Neel | Ten Trillion |
241
- | 1,00,00,00,00,00,00,000 | Padma | Quadrillion |
242
- | 1,00,00,00,00,00,00,00,000 | Shankh | Hundred Quadrillion |
230
+ | Value | Indian Name | English equivalent |
231
+ | -------------------------- | ----------- | ------------------- |
232
+ | 1,000 | Thousand | Thousand |
233
+ | 1,00,000 | Lakh | Hundred Thousand |
234
+ | 1,00,00,000 | Crore | Ten Million |
235
+ | 1,00,00,00,000 | Arab | Billion |
236
+ | 1,00,00,00,00,000 | Kharab | Hundred Billion |
237
+ | 1,00,00,00,00,00,000 | Neel | Ten Trillion |
238
+ | 1,00,00,00,00,00,00,000 | Padma | Quadrillion |
239
+ | 1,00,00,00,00,00,00,00,000 | Shankh | Hundred Quadrillion |
243
240
 
244
241
  ---
245
242
 
@@ -14,7 +14,7 @@ export function getLocale(id) {
14
14
  const def = registry.get(id);
15
15
  if (!def) {
16
16
  throw new Error(`[numtowords] Locale "${id}" is not registered. ` +
17
- `Available: ${[...registry.keys()].join(", ")}`);
17
+ `Available: ${[...registry.keys()].join(', ')}`);
18
18
  }
19
19
  return def;
20
20
  }
@@ -24,10 +24,10 @@ export function availableLocales() {
24
24
  }
25
25
  // ─── Defaults ────────────────────────────────────────────────────────────────
26
26
  const DEFAULT_OPTIONS = {
27
- locale: "en",
27
+ locale: 'en',
28
28
  capitalize: true,
29
29
  useAnd: true,
30
- currency: "",
30
+ currency: '',
31
31
  };
32
32
  // ─── Main convert function ────────────────────────────────────────────────────
33
33
  /**
@@ -43,27 +43,27 @@ export function convert(input, options) {
43
43
  // ── Normalise input ────────────────────────────────────────────────────────
44
44
  let raw = String(input).trim();
45
45
  let negative = false;
46
- if (raw.startsWith("-")) {
46
+ if (raw.startsWith('-')) {
47
47
  negative = true;
48
48
  raw = raw.slice(1);
49
49
  }
50
50
  // Strip commas / underscores used as separators
51
- raw = raw.replace(/[,_\s]/g, "");
51
+ raw = raw.replace(/[,_\s]/g, '');
52
52
  if (!/^\d+$/.test(raw)) {
53
53
  throw new Error(`[numtowords] Invalid numeric input: "${input}"`);
54
54
  }
55
55
  // Remove leading zeros
56
- raw = raw.replace(/^0+/, "") || "0";
56
+ raw = raw.replace(/^0+/, '') || '0';
57
57
  const n = BigInt(raw);
58
58
  // ── Delegate to locale ─────────────────────────────────────────────────────
59
59
  const locale = getLocale(opts.locale);
60
60
  let result = locale.convert(n, opts);
61
61
  if (negative)
62
- result = "Negative " + result;
62
+ result = 'Negative ' + result;
63
63
  // ── Capitalise ─────────────────────────────────────────────────────────────
64
64
  if (opts.capitalize) {
65
65
  // Do not capitalise German zero (tests expect lowercase 'null')
66
- if (!(n === 0n && opts.locale === "de")) {
66
+ if (!(n === 0n && opts.locale === 'de')) {
67
67
  result = result.charAt(0).toUpperCase() + result.slice(1);
68
68
  }
69
69
  }