@prachisekera/desi-utils 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +66 -2
- package/dist/cjs/format.d.ts +5 -0
- package/dist/cjs/format.js +16 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/validators.d.ts +5 -0
- package/dist/cjs/validators.js +73 -0
- package/dist/cjs/words.d.ts +1 -0
- package/dist/cjs/words.js +53 -0
- package/dist/esm/format.js +13 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/validators.js +66 -0
- package/dist/esm/words.js +50 -0
- package/package.json +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hachi Sekera
|
|
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
CHANGED
|
@@ -1,3 +1,67 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @prachisekera/desi-utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Tiny, zero-dependency TypeScript helpers for apps built for India: format money in lakh/crore style, convert numbers to words, and validate PAN, IFSC, Aadhaar, mobile numbers and pincodes.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
Every Indian sign-up form, payment page or dashboard needs the same small things, and `toLocaleString` can't do all of them. This package puts them in one small, tested place. It has no dependencies and ships its own TypeScript types.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @prachisekera/desi-utils
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import {
|
|
19
|
+
formatINR, numberToWords,
|
|
20
|
+
isValidMobile, isValidPincode, isValidPAN, isValidIFSC, isValidAadhaar,
|
|
21
|
+
} from "@prachisekera/desi-utils";
|
|
22
|
+
|
|
23
|
+
formatINR(1234567); // "₹12,34,567.00"
|
|
24
|
+
formatINR(1500, { decimals: 0 }); // "₹1,500"
|
|
25
|
+
formatINR(1500, { symbol: false }); // "1,500.00"
|
|
26
|
+
|
|
27
|
+
numberToWords(125000); // "One Lakh Twenty Five Thousand"
|
|
28
|
+
|
|
29
|
+
isValidMobile("+91 98765 43210"); // true
|
|
30
|
+
isValidPincode("131001"); // true
|
|
31
|
+
isValidPAN("ABCPE1234F"); // true
|
|
32
|
+
isValidIFSC("SBIN0001234"); // true
|
|
33
|
+
isValidAadhaar("2345 6789 0123"); // true only if the checksum is correct
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
CommonJS is supported as well:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
const { formatINR, numberToWords } = require("@prachisekera/desi-utils");
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
| Function | Returns | Notes |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| `formatINR(amount, { decimals = 2, symbol = true })` | `string` | Indian grouping (12,34,567). Throws on NaN/Infinity. |
|
|
47
|
+
| `numberToWords(n)` | `string` | Whole numbers only. Uses Lakh/Crore. Negative gives "Minus ...". |
|
|
48
|
+
| `isValidMobile(value)` | `boolean` | 10 digits starting 6-9. Accepts `+91`, `91`, `0` prefix, spaces, dashes. |
|
|
49
|
+
| `isValidPincode(value)` | `boolean` | 6 digits, cannot start with 0. |
|
|
50
|
+
| `isValidPAN(value)` | `boolean` | `AAAPA1234A` format, 4th letter must be a valid holder type. Case-insensitive. |
|
|
51
|
+
| `isValidIFSC(value)` | `boolean` | 4 letters + `0` + 6 letters/digits. Case-insensitive. |
|
|
52
|
+
| `isValidAadhaar(value)` | `boolean` | 12 digits, first digit 2-9, Verhoeff checksum must pass. |
|
|
53
|
+
|
|
54
|
+
## Important
|
|
55
|
+
|
|
56
|
+
These validators only check **format** (and the checksum for Aadhaar). They do not confirm that a PAN, IFSC or Aadhaar actually exists. Use official services for that.
|
|
57
|
+
|
|
58
|
+
## Development
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npm install
|
|
62
|
+
npm test # builds, then runs the tests with Node's built-in test runner
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
MIT
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatINR = formatINR;
|
|
4
|
+
function formatINR(amount, options = {}) {
|
|
5
|
+
const { decimals = 2, symbol = true } = options;
|
|
6
|
+
if (typeof amount !== "number" || !Number.isFinite(amount)) {
|
|
7
|
+
throw new TypeError("formatINR expects a finite number");
|
|
8
|
+
}
|
|
9
|
+
const isNegative = amount < 0;
|
|
10
|
+
const [whole, fraction] = Math.abs(amount).toFixed(decimals).split(".");
|
|
11
|
+
const lastThree = whole.slice(-3);
|
|
12
|
+
const rest = whole.slice(0, -3);
|
|
13
|
+
const grouped = rest ? rest.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + "," + lastThree : lastThree;
|
|
14
|
+
const number = fraction ? `${grouped}.${fraction}` : grouped;
|
|
15
|
+
return `${isNegative ? "-" : ""}${symbol ? "₹" : ""}${number}`;
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidAadhaar = exports.isValidIFSC = exports.isValidPAN = exports.isValidPincode = exports.isValidMobile = exports.numberToWords = exports.formatINR = void 0;
|
|
4
|
+
var format_js_1 = require("./format.js");
|
|
5
|
+
Object.defineProperty(exports, "formatINR", { enumerable: true, get: function () { return format_js_1.formatINR; } });
|
|
6
|
+
var words_js_1 = require("./words.js");
|
|
7
|
+
Object.defineProperty(exports, "numberToWords", { enumerable: true, get: function () { return words_js_1.numberToWords; } });
|
|
8
|
+
var validators_js_1 = require("./validators.js");
|
|
9
|
+
Object.defineProperty(exports, "isValidMobile", { enumerable: true, get: function () { return validators_js_1.isValidMobile; } });
|
|
10
|
+
Object.defineProperty(exports, "isValidPincode", { enumerable: true, get: function () { return validators_js_1.isValidPincode; } });
|
|
11
|
+
Object.defineProperty(exports, "isValidPAN", { enumerable: true, get: function () { return validators_js_1.isValidPAN; } });
|
|
12
|
+
Object.defineProperty(exports, "isValidIFSC", { enumerable: true, get: function () { return validators_js_1.isValidIFSC; } });
|
|
13
|
+
Object.defineProperty(exports, "isValidAadhaar", { enumerable: true, get: function () { return validators_js_1.isValidAadhaar; } });
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function isValidMobile(value: string): boolean;
|
|
2
|
+
export declare function isValidPincode(value: string): boolean;
|
|
3
|
+
export declare function isValidPAN(value: string): boolean;
|
|
4
|
+
export declare function isValidIFSC(value: string): boolean;
|
|
5
|
+
export declare function isValidAadhaar(value: string): boolean;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidMobile = isValidMobile;
|
|
4
|
+
exports.isValidPincode = isValidPincode;
|
|
5
|
+
exports.isValidPAN = isValidPAN;
|
|
6
|
+
exports.isValidIFSC = isValidIFSC;
|
|
7
|
+
exports.isValidAadhaar = isValidAadhaar;
|
|
8
|
+
function clean(value) {
|
|
9
|
+
return value.replace(/[\s-]/g, "");
|
|
10
|
+
}
|
|
11
|
+
function isValidMobile(value) {
|
|
12
|
+
if (typeof value !== "string")
|
|
13
|
+
return false;
|
|
14
|
+
let v = clean(value);
|
|
15
|
+
if (v.startsWith("+91"))
|
|
16
|
+
v = v.slice(3);
|
|
17
|
+
else if (v.length === 12 && v.startsWith("91"))
|
|
18
|
+
v = v.slice(2);
|
|
19
|
+
else if (v.length === 11 && v.startsWith("0"))
|
|
20
|
+
v = v.slice(1);
|
|
21
|
+
return /^[6-9]\d{9}$/.test(v);
|
|
22
|
+
}
|
|
23
|
+
function isValidPincode(value) {
|
|
24
|
+
if (typeof value !== "string")
|
|
25
|
+
return false;
|
|
26
|
+
return /^[1-9]\d{5}$/.test(value.trim());
|
|
27
|
+
}
|
|
28
|
+
function isValidPAN(value) {
|
|
29
|
+
if (typeof value !== "string")
|
|
30
|
+
return false;
|
|
31
|
+
return /^[A-Z]{3}[PCHFATBLJG][A-Z]\d{4}[A-Z]$/.test(value.trim().toUpperCase());
|
|
32
|
+
}
|
|
33
|
+
function isValidIFSC(value) {
|
|
34
|
+
if (typeof value !== "string")
|
|
35
|
+
return false;
|
|
36
|
+
return /^[A-Z]{4}0[A-Z0-9]{6}$/.test(value.trim().toUpperCase());
|
|
37
|
+
}
|
|
38
|
+
const D = [
|
|
39
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
40
|
+
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
|
41
|
+
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
|
42
|
+
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
|
43
|
+
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
|
|
44
|
+
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
|
45
|
+
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
|
|
46
|
+
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
|
47
|
+
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
|
48
|
+
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
|
|
49
|
+
];
|
|
50
|
+
const P = [
|
|
51
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
52
|
+
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
|
53
|
+
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
|
54
|
+
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
|
55
|
+
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
|
|
56
|
+
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
|
57
|
+
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
|
|
58
|
+
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
|
|
59
|
+
];
|
|
60
|
+
function verhoeffIsValid(digits) {
|
|
61
|
+
let c = 0;
|
|
62
|
+
const reversed = digits.split("").reverse();
|
|
63
|
+
for (let i = 0; i < reversed.length; i++) {
|
|
64
|
+
c = D[c][P[i % 8][Number(reversed[i])]];
|
|
65
|
+
}
|
|
66
|
+
return c === 0;
|
|
67
|
+
}
|
|
68
|
+
function isValidAadhaar(value) {
|
|
69
|
+
if (typeof value !== "string")
|
|
70
|
+
return false;
|
|
71
|
+
const v = clean(value);
|
|
72
|
+
return /^[2-9]\d{11}$/.test(v) && verhoeffIsValid(v);
|
|
73
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function numberToWords(n: number): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.numberToWords = numberToWords;
|
|
4
|
+
const ONES = [
|
|
5
|
+
"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
|
|
6
|
+
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
|
|
7
|
+
"Eighteen", "Nineteen",
|
|
8
|
+
];
|
|
9
|
+
const TENS = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
|
|
10
|
+
function twoDigits(n) {
|
|
11
|
+
if (n < 20)
|
|
12
|
+
return ONES[n];
|
|
13
|
+
return TENS[Math.floor(n / 10)] + (n % 10 ? " " + ONES[n % 10] : "");
|
|
14
|
+
}
|
|
15
|
+
function threeDigits(n) {
|
|
16
|
+
const hundreds = Math.floor(n / 100);
|
|
17
|
+
const rest = n % 100;
|
|
18
|
+
const parts = [];
|
|
19
|
+
if (hundreds)
|
|
20
|
+
parts.push(ONES[hundreds] + " Hundred");
|
|
21
|
+
if (rest)
|
|
22
|
+
parts.push(twoDigits(rest));
|
|
23
|
+
return parts.join(" ");
|
|
24
|
+
}
|
|
25
|
+
function numberToWords(n) {
|
|
26
|
+
if (!Number.isSafeInteger(n)) {
|
|
27
|
+
throw new TypeError("numberToWords expects a safe integer");
|
|
28
|
+
}
|
|
29
|
+
if (n === 0)
|
|
30
|
+
return "Zero";
|
|
31
|
+
if (n < 0)
|
|
32
|
+
return "Minus " + numberToWords(-n);
|
|
33
|
+
const parts = [];
|
|
34
|
+
let remaining = n;
|
|
35
|
+
const crore = Math.floor(remaining / 10000000);
|
|
36
|
+
if (crore) {
|
|
37
|
+
parts.push(numberToWords(crore) + " Crore");
|
|
38
|
+
remaining %= 10000000;
|
|
39
|
+
}
|
|
40
|
+
const lakh = Math.floor(remaining / 100000);
|
|
41
|
+
if (lakh) {
|
|
42
|
+
parts.push(twoDigits(lakh) + " Lakh");
|
|
43
|
+
remaining %= 100000;
|
|
44
|
+
}
|
|
45
|
+
const thousand = Math.floor(remaining / 1000);
|
|
46
|
+
if (thousand) {
|
|
47
|
+
parts.push(twoDigits(thousand) + " Thousand");
|
|
48
|
+
remaining %= 1000;
|
|
49
|
+
}
|
|
50
|
+
if (remaining)
|
|
51
|
+
parts.push(threeDigits(remaining));
|
|
52
|
+
return parts.join(" ");
|
|
53
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function formatINR(amount, options = {}) {
|
|
2
|
+
const { decimals = 2, symbol = true } = options;
|
|
3
|
+
if (typeof amount !== "number" || !Number.isFinite(amount)) {
|
|
4
|
+
throw new TypeError("formatINR expects a finite number");
|
|
5
|
+
}
|
|
6
|
+
const isNegative = amount < 0;
|
|
7
|
+
const [whole, fraction] = Math.abs(amount).toFixed(decimals).split(".");
|
|
8
|
+
const lastThree = whole.slice(-3);
|
|
9
|
+
const rest = whole.slice(0, -3);
|
|
10
|
+
const grouped = rest ? rest.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + "," + lastThree : lastThree;
|
|
11
|
+
const number = fraction ? `${grouped}.${fraction}` : grouped;
|
|
12
|
+
return `${isNegative ? "-" : ""}${symbol ? "₹" : ""}${number}`;
|
|
13
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
function clean(value) {
|
|
2
|
+
return value.replace(/[\s-]/g, "");
|
|
3
|
+
}
|
|
4
|
+
export function isValidMobile(value) {
|
|
5
|
+
if (typeof value !== "string")
|
|
6
|
+
return false;
|
|
7
|
+
let v = clean(value);
|
|
8
|
+
if (v.startsWith("+91"))
|
|
9
|
+
v = v.slice(3);
|
|
10
|
+
else if (v.length === 12 && v.startsWith("91"))
|
|
11
|
+
v = v.slice(2);
|
|
12
|
+
else if (v.length === 11 && v.startsWith("0"))
|
|
13
|
+
v = v.slice(1);
|
|
14
|
+
return /^[6-9]\d{9}$/.test(v);
|
|
15
|
+
}
|
|
16
|
+
export function isValidPincode(value) {
|
|
17
|
+
if (typeof value !== "string")
|
|
18
|
+
return false;
|
|
19
|
+
return /^[1-9]\d{5}$/.test(value.trim());
|
|
20
|
+
}
|
|
21
|
+
export function isValidPAN(value) {
|
|
22
|
+
if (typeof value !== "string")
|
|
23
|
+
return false;
|
|
24
|
+
return /^[A-Z]{3}[PCHFATBLJG][A-Z]\d{4}[A-Z]$/.test(value.trim().toUpperCase());
|
|
25
|
+
}
|
|
26
|
+
export function isValidIFSC(value) {
|
|
27
|
+
if (typeof value !== "string")
|
|
28
|
+
return false;
|
|
29
|
+
return /^[A-Z]{4}0[A-Z0-9]{6}$/.test(value.trim().toUpperCase());
|
|
30
|
+
}
|
|
31
|
+
const D = [
|
|
32
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
33
|
+
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
|
34
|
+
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
|
35
|
+
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
|
36
|
+
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
|
|
37
|
+
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
|
38
|
+
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
|
|
39
|
+
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
|
40
|
+
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
|
41
|
+
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
|
|
42
|
+
];
|
|
43
|
+
const P = [
|
|
44
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
45
|
+
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
|
46
|
+
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
|
47
|
+
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
|
48
|
+
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
|
|
49
|
+
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
|
50
|
+
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
|
|
51
|
+
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
|
|
52
|
+
];
|
|
53
|
+
function verhoeffIsValid(digits) {
|
|
54
|
+
let c = 0;
|
|
55
|
+
const reversed = digits.split("").reverse();
|
|
56
|
+
for (let i = 0; i < reversed.length; i++) {
|
|
57
|
+
c = D[c][P[i % 8][Number(reversed[i])]];
|
|
58
|
+
}
|
|
59
|
+
return c === 0;
|
|
60
|
+
}
|
|
61
|
+
export function isValidAadhaar(value) {
|
|
62
|
+
if (typeof value !== "string")
|
|
63
|
+
return false;
|
|
64
|
+
const v = clean(value);
|
|
65
|
+
return /^[2-9]\d{11}$/.test(v) && verhoeffIsValid(v);
|
|
66
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const ONES = [
|
|
2
|
+
"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
|
|
3
|
+
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
|
|
4
|
+
"Eighteen", "Nineteen",
|
|
5
|
+
];
|
|
6
|
+
const TENS = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
|
|
7
|
+
function twoDigits(n) {
|
|
8
|
+
if (n < 20)
|
|
9
|
+
return ONES[n];
|
|
10
|
+
return TENS[Math.floor(n / 10)] + (n % 10 ? " " + ONES[n % 10] : "");
|
|
11
|
+
}
|
|
12
|
+
function threeDigits(n) {
|
|
13
|
+
const hundreds = Math.floor(n / 100);
|
|
14
|
+
const rest = n % 100;
|
|
15
|
+
const parts = [];
|
|
16
|
+
if (hundreds)
|
|
17
|
+
parts.push(ONES[hundreds] + " Hundred");
|
|
18
|
+
if (rest)
|
|
19
|
+
parts.push(twoDigits(rest));
|
|
20
|
+
return parts.join(" ");
|
|
21
|
+
}
|
|
22
|
+
export function numberToWords(n) {
|
|
23
|
+
if (!Number.isSafeInteger(n)) {
|
|
24
|
+
throw new TypeError("numberToWords expects a safe integer");
|
|
25
|
+
}
|
|
26
|
+
if (n === 0)
|
|
27
|
+
return "Zero";
|
|
28
|
+
if (n < 0)
|
|
29
|
+
return "Minus " + numberToWords(-n);
|
|
30
|
+
const parts = [];
|
|
31
|
+
let remaining = n;
|
|
32
|
+
const crore = Math.floor(remaining / 10000000);
|
|
33
|
+
if (crore) {
|
|
34
|
+
parts.push(numberToWords(crore) + " Crore");
|
|
35
|
+
remaining %= 10000000;
|
|
36
|
+
}
|
|
37
|
+
const lakh = Math.floor(remaining / 100000);
|
|
38
|
+
if (lakh) {
|
|
39
|
+
parts.push(twoDigits(lakh) + " Lakh");
|
|
40
|
+
remaining %= 100000;
|
|
41
|
+
}
|
|
42
|
+
const thousand = Math.floor(remaining / 1000);
|
|
43
|
+
if (thousand) {
|
|
44
|
+
parts.push(twoDigits(thousand) + " Thousand");
|
|
45
|
+
remaining %= 1000;
|
|
46
|
+
}
|
|
47
|
+
if (remaining)
|
|
48
|
+
parts.push(threeDigits(remaining));
|
|
49
|
+
return parts.join(" ");
|
|
50
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prachisekera/desi-utils",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Tiny zero-dependency TypeScript helpers for Indian apps: INR formatting, number to words, and PAN / IFSC / Aadhaar / mobile / pincode validation.",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"module": "dist/esm/index.js",
|
|
7
|
+
"types": "dist/cjs/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/cjs/index.d.ts",
|
|
11
|
+
"import": "./dist/esm/index.js",
|
|
12
|
+
"require": "./dist/cjs/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/cjs",
|
|
17
|
+
"dist/esm",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build:cjs": "tsc -p tsconfig.json",
|
|
23
|
+
"build:esm": "tsc -p tsconfig.esm.json && node scripts/write-esm-package.js",
|
|
24
|
+
"build": "npm run build:cjs && npm run build:esm",
|
|
25
|
+
"test": "npm run build && node --test",
|
|
26
|
+
"prepublishOnly": "npm test"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"india",
|
|
30
|
+
"indian",
|
|
31
|
+
"inr",
|
|
32
|
+
"rupee",
|
|
33
|
+
"lakh",
|
|
34
|
+
"crore",
|
|
35
|
+
"pan",
|
|
36
|
+
"ifsc",
|
|
37
|
+
"aadhaar",
|
|
38
|
+
"pincode",
|
|
39
|
+
"validation",
|
|
40
|
+
"formatter"
|
|
41
|
+
],
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"author": "Hachi Sekera",
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"typescript": "^5.4.0",
|
|
46
|
+
"@types/node": "^22.0.0"
|
|
47
|
+
},
|
|
48
|
+
"directories": {
|
|
49
|
+
"test": "test"
|
|
50
|
+
},
|
|
51
|
+
"type": "commonjs"
|
|
52
|
+
}
|