@prachisekera/desi-utils 0.0.0-stage → 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 +21 -0
- package/README.md +99 -2
- package/dist/cjs/format.d.ts +14 -0
- package/dist/cjs/format.js +44 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/validators.d.ts +19 -0
- package/dist/cjs/validators.js +88 -0
- package/dist/cjs/words.d.ts +6 -0
- package/dist/cjs/words.js +58 -0
- package/dist/esm/format.js +41 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/validators.js +81 -0
- package/dist/esm/words.js +55 -0
- package/package.json +51 -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,100 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @prachisekera/desi-utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-dependency TypeScript helpers for Indian-number formatting and common Indian identifier format checks. Includes INR formatting, number-to-words conversion, and mobile, pincode, PAN, IFSC, and Aadhaar-shaped value validators.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @prachisekera/desi-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The package provides CommonJS and native ES Module builds, plus TypeScript declarations.
|
|
12
|
+
|
|
13
|
+
## ES Module usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import {
|
|
17
|
+
formatINR,
|
|
18
|
+
numberToWords,
|
|
19
|
+
isValidMobile,
|
|
20
|
+
isValidPincode,
|
|
21
|
+
isValidPAN,
|
|
22
|
+
isValidIFSC,
|
|
23
|
+
isValidAadhaar,
|
|
24
|
+
} from "@prachisekera/desi-utils";
|
|
25
|
+
|
|
26
|
+
formatINR(1234567); // "₹12,34,567.00"
|
|
27
|
+
numberToWords(125000); // "One Lakh Twenty Five Thousand"
|
|
28
|
+
isValidMobile("+91 98765 43210"); // true
|
|
29
|
+
isValidPincode("131001"); // true
|
|
30
|
+
isValidPAN("ABCDE1234F"); // true (format only)
|
|
31
|
+
isValidIFSC("SBIN0001234"); // true (format only)
|
|
32
|
+
isValidAadhaar("234567890124"); // true when the Verhoeff checksum passes
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## CommonJS usage
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const {
|
|
39
|
+
formatINR,
|
|
40
|
+
numberToWords,
|
|
41
|
+
isValidMobile,
|
|
42
|
+
isValidPincode,
|
|
43
|
+
isValidPAN,
|
|
44
|
+
isValidIFSC,
|
|
45
|
+
isValidAadhaar,
|
|
46
|
+
} = require("@prachisekera/desi-utils");
|
|
47
|
+
|
|
48
|
+
console.log(formatINR(1500, { decimals: 0 })); // "₹1,500"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## TypeScript usage
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import {
|
|
55
|
+
formatINR,
|
|
56
|
+
numberToWords,
|
|
57
|
+
isValidPAN,
|
|
58
|
+
type FormatINROptions,
|
|
59
|
+
} from "@prachisekera/desi-utils";
|
|
60
|
+
|
|
61
|
+
const options: FormatINROptions = { decimals: 0, symbol: false };
|
|
62
|
+
const amount: string = formatINR(1500, options);
|
|
63
|
+
const words: string = numberToWords(125000);
|
|
64
|
+
const panHasValidFormat: boolean = isValidPAN("ABCDE1234F");
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## API
|
|
68
|
+
|
|
69
|
+
| Function | Parameters | Return value | Behavior |
|
|
70
|
+
|---|---|---|---|
|
|
71
|
+
| `formatINR` | `amount: number`, `options?: { decimals?: number; symbol?: boolean }` | `string` | Indian grouping; decimals defaults to 2 and must be an integer from 0 to 100. Symbol defaults to `true`. |
|
|
72
|
+
| `numberToWords` | `n: number` | `string` | Converts safe integers to words using Lakh and Crore; negative numbers are prefixed with “Minus”. |
|
|
73
|
+
| `isValidMobile` | `value: string` | `boolean` | 10 digits starting 6-9; accepts optional `+91`, `91`, or `0` prefix. Spaces and hyphens are ignored. |
|
|
74
|
+
| `isValidPincode` | `value: string` | `boolean` | Six digits; the first digit cannot be 0. |
|
|
75
|
+
| `isValidPAN` | `value: string` | `boolean` | Basic shape of five letters, four digits, and a final letter. Case-insensitive. |
|
|
76
|
+
| `isValidIFSC` | `value: string` | `boolean` | Four letters, a `0`, then six letters or digits. Case-insensitive. |
|
|
77
|
+
| `isValidAadhaar` | `value: string` | `boolean` | 12 digits, first digit 2-9, with a valid Verhoeff checksum. Spaces and hyphens are ignored. |
|
|
78
|
+
|
|
79
|
+
`FormatINROptions` is exported for TypeScript consumers.
|
|
80
|
+
|
|
81
|
+
## Input handling and limitations
|
|
82
|
+
|
|
83
|
+
- `formatINR` throws `TypeError` for non-finite amounts or invalid option types. It throws `RangeError` when `decimals` is not an integer from 0 through 100.
|
|
84
|
+
- `numberToWords` throws `TypeError` unless its input is a safe integer.
|
|
85
|
+
- The five validators return `false` for non-string inputs and malformed values. PAN and IFSC are trimmed and case-insensitive; pincode is trimmed. Mobile and Aadhaar ignore spaces and hyphens.
|
|
86
|
+
- PAN and IFSC checks validate format only. The PAN check does not validate the holder-type position or issuance. The Aadhaar check validates format and checksum only; it does not verify identity or official issuance.
|
|
87
|
+
- None of these validators confirms that an identifier or number exists in an official database.
|
|
88
|
+
|
|
89
|
+
## Development
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
npm install
|
|
93
|
+
npm test
|
|
94
|
+
npm run build:cjs
|
|
95
|
+
npm run build:esm
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
MIT
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Options for formatting a number as Indian rupees. */
|
|
2
|
+
export interface FormatINROptions {
|
|
3
|
+
/** Fraction digits to display, from 0 to 100. Defaults to 2. */
|
|
4
|
+
decimals?: number;
|
|
5
|
+
/** Whether to include the rupee symbol. Defaults to true. */
|
|
6
|
+
symbol?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Formats a finite number with Indian digit grouping.
|
|
10
|
+
*
|
|
11
|
+
* @throws {TypeError} If the amount or options have an invalid type.
|
|
12
|
+
* @throws {RangeError} If decimals is not an integer from 0 through 100.
|
|
13
|
+
*/
|
|
14
|
+
export declare function formatINR(amount: number, options?: FormatINROptions): string;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatINR = formatINR;
|
|
4
|
+
/**
|
|
5
|
+
* Formats a finite number with Indian digit grouping.
|
|
6
|
+
*
|
|
7
|
+
* @throws {TypeError} If the amount or options have an invalid type.
|
|
8
|
+
* @throws {RangeError} If decimals is not an integer from 0 through 100.
|
|
9
|
+
*/
|
|
10
|
+
function formatINR(amount, options = {}) {
|
|
11
|
+
if (typeof amount !== "number" || !Number.isFinite(amount)) {
|
|
12
|
+
throw new TypeError("formatINR expects a finite number");
|
|
13
|
+
}
|
|
14
|
+
if (options === null || typeof options !== "object" || Array.isArray(options)) {
|
|
15
|
+
throw new TypeError("formatINR options must be an object");
|
|
16
|
+
}
|
|
17
|
+
if (options.decimals !== undefined && typeof options.decimals !== "number") {
|
|
18
|
+
throw new TypeError("formatINR decimals must be a number");
|
|
19
|
+
}
|
|
20
|
+
if (options.symbol !== undefined && typeof options.symbol !== "boolean") {
|
|
21
|
+
throw new TypeError("formatINR symbol must be a boolean");
|
|
22
|
+
}
|
|
23
|
+
const decimals = options.decimals ?? 2;
|
|
24
|
+
const symbol = options.symbol ?? true;
|
|
25
|
+
if (!Number.isInteger(decimals) || decimals < 0 || decimals > 100) {
|
|
26
|
+
throw new RangeError("formatINR decimals must be an integer from 0 to 100");
|
|
27
|
+
}
|
|
28
|
+
const isNegative = amount < 0;
|
|
29
|
+
const absoluteAmount = Math.abs(amount);
|
|
30
|
+
const fixed = absoluteAmount.toFixed(decimals);
|
|
31
|
+
if (/[eE]/.test(fixed)) {
|
|
32
|
+
const formatted = new Intl.NumberFormat("en-IN", {
|
|
33
|
+
minimumFractionDigits: decimals,
|
|
34
|
+
maximumFractionDigits: decimals,
|
|
35
|
+
}).format(absoluteAmount);
|
|
36
|
+
return `${isNegative ? "-" : ""}${symbol ? "₹" : ""}${formatted}`;
|
|
37
|
+
}
|
|
38
|
+
const [whole, fraction] = fixed.split(".");
|
|
39
|
+
const lastThree = whole.slice(-3);
|
|
40
|
+
const rest = whole.slice(0, -3);
|
|
41
|
+
const grouped = rest ? rest.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + "," + lastThree : lastThree;
|
|
42
|
+
const number = fraction ? `${grouped}.${fraction}` : grouped;
|
|
43
|
+
return `${isNegative ? "-" : ""}${symbol ? "₹" : ""}${number}`;
|
|
44
|
+
}
|
|
@@ -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,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks for a 10-digit Indian mobile number, optionally prefixed by +91, 91, or 0.
|
|
3
|
+
* Spaces and hyphens are ignored; the local number must start with 6-9.
|
|
4
|
+
*/
|
|
5
|
+
export declare function isValidMobile(value: string): boolean;
|
|
6
|
+
/** Checks for a six-digit Indian pincode that does not start with 0. */
|
|
7
|
+
export declare function isValidPincode(value: string): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Checks the basic PAN shape: five letters, four digits, and a final letter.
|
|
10
|
+
* This does not verify the holder-type code or whether the PAN was issued.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isValidPAN(value: string): boolean;
|
|
13
|
+
/** Checks the IFSC shape: four letters, a zero, and six letters or digits. */
|
|
14
|
+
export declare function isValidIFSC(value: string): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Checks for a 12-digit Aadhaar-shaped value with a valid Verhoeff checksum.
|
|
17
|
+
* This does not verify identity or official issuance.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isValidAadhaar(value: string): boolean;
|
|
@@ -0,0 +1,88 @@
|
|
|
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
|
+
/**
|
|
12
|
+
* Checks for a 10-digit Indian mobile number, optionally prefixed by +91, 91, or 0.
|
|
13
|
+
* Spaces and hyphens are ignored; the local number must start with 6-9.
|
|
14
|
+
*/
|
|
15
|
+
function isValidMobile(value) {
|
|
16
|
+
if (typeof value !== "string")
|
|
17
|
+
return false;
|
|
18
|
+
let v = clean(value);
|
|
19
|
+
if (v.startsWith("+91"))
|
|
20
|
+
v = v.slice(3);
|
|
21
|
+
else if (v.length === 12 && v.startsWith("91"))
|
|
22
|
+
v = v.slice(2);
|
|
23
|
+
else if (v.length === 11 && v.startsWith("0"))
|
|
24
|
+
v = v.slice(1);
|
|
25
|
+
return /^[6-9]\d{9}$/.test(v);
|
|
26
|
+
}
|
|
27
|
+
/** Checks for a six-digit Indian pincode that does not start with 0. */
|
|
28
|
+
function isValidPincode(value) {
|
|
29
|
+
if (typeof value !== "string")
|
|
30
|
+
return false;
|
|
31
|
+
return /^[1-9]\d{5}$/.test(value.trim());
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Checks the basic PAN shape: five letters, four digits, and a final letter.
|
|
35
|
+
* This does not verify the holder-type code or whether the PAN was issued.
|
|
36
|
+
*/
|
|
37
|
+
function isValidPAN(value) {
|
|
38
|
+
if (typeof value !== "string")
|
|
39
|
+
return false;
|
|
40
|
+
return /^[A-Z]{5}\d{4}[A-Z]$/.test(value.trim().toUpperCase());
|
|
41
|
+
}
|
|
42
|
+
/** Checks the IFSC shape: four letters, a zero, and six letters or digits. */
|
|
43
|
+
function isValidIFSC(value) {
|
|
44
|
+
if (typeof value !== "string")
|
|
45
|
+
return false;
|
|
46
|
+
return /^[A-Z]{4}0[A-Z0-9]{6}$/.test(value.trim().toUpperCase());
|
|
47
|
+
}
|
|
48
|
+
const D = [
|
|
49
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
50
|
+
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
|
51
|
+
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
|
52
|
+
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
|
53
|
+
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
|
|
54
|
+
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
|
55
|
+
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
|
|
56
|
+
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
|
57
|
+
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
|
58
|
+
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
|
|
59
|
+
];
|
|
60
|
+
const P = [
|
|
61
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
62
|
+
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
|
63
|
+
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
|
64
|
+
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
|
65
|
+
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
|
|
66
|
+
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
|
67
|
+
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
|
|
68
|
+
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
|
|
69
|
+
];
|
|
70
|
+
/** Checks a digit string with the Verhoeff checksum algorithm. */
|
|
71
|
+
function verhoeffIsValid(digits) {
|
|
72
|
+
let c = 0;
|
|
73
|
+
const reversed = digits.split("").reverse();
|
|
74
|
+
for (let i = 0; i < reversed.length; i++) {
|
|
75
|
+
c = D[c][P[i % 8][Number(reversed[i])]];
|
|
76
|
+
}
|
|
77
|
+
return c === 0;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Checks for a 12-digit Aadhaar-shaped value with a valid Verhoeff checksum.
|
|
81
|
+
* This does not verify identity or official issuance.
|
|
82
|
+
*/
|
|
83
|
+
function isValidAadhaar(value) {
|
|
84
|
+
if (typeof value !== "string")
|
|
85
|
+
return false;
|
|
86
|
+
const v = clean(value);
|
|
87
|
+
return /^[2-9]\d{11}$/.test(v) && verhoeffIsValid(v);
|
|
88
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
/**
|
|
26
|
+
* Converts a safe integer to words using the Indian numbering system.
|
|
27
|
+
*
|
|
28
|
+
* @throws {TypeError} If n is not a safe integer.
|
|
29
|
+
*/
|
|
30
|
+
function numberToWords(n) {
|
|
31
|
+
if (!Number.isSafeInteger(n)) {
|
|
32
|
+
throw new TypeError("numberToWords expects a safe integer");
|
|
33
|
+
}
|
|
34
|
+
if (n === 0)
|
|
35
|
+
return "Zero";
|
|
36
|
+
if (n < 0)
|
|
37
|
+
return "Minus " + numberToWords(-n);
|
|
38
|
+
const parts = [];
|
|
39
|
+
let remaining = n;
|
|
40
|
+
const crore = Math.floor(remaining / 10000000);
|
|
41
|
+
if (crore) {
|
|
42
|
+
parts.push(numberToWords(crore) + " Crore");
|
|
43
|
+
remaining %= 10000000;
|
|
44
|
+
}
|
|
45
|
+
const lakh = Math.floor(remaining / 100000);
|
|
46
|
+
if (lakh) {
|
|
47
|
+
parts.push(twoDigits(lakh) + " Lakh");
|
|
48
|
+
remaining %= 100000;
|
|
49
|
+
}
|
|
50
|
+
const thousand = Math.floor(remaining / 1000);
|
|
51
|
+
if (thousand) {
|
|
52
|
+
parts.push(twoDigits(thousand) + " Thousand");
|
|
53
|
+
remaining %= 1000;
|
|
54
|
+
}
|
|
55
|
+
if (remaining)
|
|
56
|
+
parts.push(threeDigits(remaining));
|
|
57
|
+
return parts.join(" ");
|
|
58
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a finite number with Indian digit grouping.
|
|
3
|
+
*
|
|
4
|
+
* @throws {TypeError} If the amount or options have an invalid type.
|
|
5
|
+
* @throws {RangeError} If decimals is not an integer from 0 through 100.
|
|
6
|
+
*/
|
|
7
|
+
export function formatINR(amount, options = {}) {
|
|
8
|
+
if (typeof amount !== "number" || !Number.isFinite(amount)) {
|
|
9
|
+
throw new TypeError("formatINR expects a finite number");
|
|
10
|
+
}
|
|
11
|
+
if (options === null || typeof options !== "object" || Array.isArray(options)) {
|
|
12
|
+
throw new TypeError("formatINR options must be an object");
|
|
13
|
+
}
|
|
14
|
+
if (options.decimals !== undefined && typeof options.decimals !== "number") {
|
|
15
|
+
throw new TypeError("formatINR decimals must be a number");
|
|
16
|
+
}
|
|
17
|
+
if (options.symbol !== undefined && typeof options.symbol !== "boolean") {
|
|
18
|
+
throw new TypeError("formatINR symbol must be a boolean");
|
|
19
|
+
}
|
|
20
|
+
const decimals = options.decimals ?? 2;
|
|
21
|
+
const symbol = options.symbol ?? true;
|
|
22
|
+
if (!Number.isInteger(decimals) || decimals < 0 || decimals > 100) {
|
|
23
|
+
throw new RangeError("formatINR decimals must be an integer from 0 to 100");
|
|
24
|
+
}
|
|
25
|
+
const isNegative = amount < 0;
|
|
26
|
+
const absoluteAmount = Math.abs(amount);
|
|
27
|
+
const fixed = absoluteAmount.toFixed(decimals);
|
|
28
|
+
if (/[eE]/.test(fixed)) {
|
|
29
|
+
const formatted = new Intl.NumberFormat("en-IN", {
|
|
30
|
+
minimumFractionDigits: decimals,
|
|
31
|
+
maximumFractionDigits: decimals,
|
|
32
|
+
}).format(absoluteAmount);
|
|
33
|
+
return `${isNegative ? "-" : ""}${symbol ? "₹" : ""}${formatted}`;
|
|
34
|
+
}
|
|
35
|
+
const [whole, fraction] = fixed.split(".");
|
|
36
|
+
const lastThree = whole.slice(-3);
|
|
37
|
+
const rest = whole.slice(0, -3);
|
|
38
|
+
const grouped = rest ? rest.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + "," + lastThree : lastThree;
|
|
39
|
+
const number = fraction ? `${grouped}.${fraction}` : grouped;
|
|
40
|
+
return `${isNegative ? "-" : ""}${symbol ? "₹" : ""}${number}`;
|
|
41
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
function clean(value) {
|
|
2
|
+
return value.replace(/[\s-]/g, "");
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Checks for a 10-digit Indian mobile number, optionally prefixed by +91, 91, or 0.
|
|
6
|
+
* Spaces and hyphens are ignored; the local number must start with 6-9.
|
|
7
|
+
*/
|
|
8
|
+
export function isValidMobile(value) {
|
|
9
|
+
if (typeof value !== "string")
|
|
10
|
+
return false;
|
|
11
|
+
let v = clean(value);
|
|
12
|
+
if (v.startsWith("+91"))
|
|
13
|
+
v = v.slice(3);
|
|
14
|
+
else if (v.length === 12 && v.startsWith("91"))
|
|
15
|
+
v = v.slice(2);
|
|
16
|
+
else if (v.length === 11 && v.startsWith("0"))
|
|
17
|
+
v = v.slice(1);
|
|
18
|
+
return /^[6-9]\d{9}$/.test(v);
|
|
19
|
+
}
|
|
20
|
+
/** Checks for a six-digit Indian pincode that does not start with 0. */
|
|
21
|
+
export function isValidPincode(value) {
|
|
22
|
+
if (typeof value !== "string")
|
|
23
|
+
return false;
|
|
24
|
+
return /^[1-9]\d{5}$/.test(value.trim());
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Checks the basic PAN shape: five letters, four digits, and a final letter.
|
|
28
|
+
* This does not verify the holder-type code or whether the PAN was issued.
|
|
29
|
+
*/
|
|
30
|
+
export function isValidPAN(value) {
|
|
31
|
+
if (typeof value !== "string")
|
|
32
|
+
return false;
|
|
33
|
+
return /^[A-Z]{5}\d{4}[A-Z]$/.test(value.trim().toUpperCase());
|
|
34
|
+
}
|
|
35
|
+
/** Checks the IFSC shape: four letters, a zero, and six letters or digits. */
|
|
36
|
+
export function isValidIFSC(value) {
|
|
37
|
+
if (typeof value !== "string")
|
|
38
|
+
return false;
|
|
39
|
+
return /^[A-Z]{4}0[A-Z0-9]{6}$/.test(value.trim().toUpperCase());
|
|
40
|
+
}
|
|
41
|
+
const D = [
|
|
42
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
43
|
+
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
|
44
|
+
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
|
45
|
+
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
|
46
|
+
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
|
|
47
|
+
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
|
48
|
+
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
|
|
49
|
+
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
|
50
|
+
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
|
51
|
+
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
|
|
52
|
+
];
|
|
53
|
+
const P = [
|
|
54
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
55
|
+
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
|
56
|
+
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
|
57
|
+
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
|
58
|
+
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
|
|
59
|
+
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
|
60
|
+
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
|
|
61
|
+
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
|
|
62
|
+
];
|
|
63
|
+
/** Checks a digit string with the Verhoeff checksum algorithm. */
|
|
64
|
+
function verhoeffIsValid(digits) {
|
|
65
|
+
let c = 0;
|
|
66
|
+
const reversed = digits.split("").reverse();
|
|
67
|
+
for (let i = 0; i < reversed.length; i++) {
|
|
68
|
+
c = D[c][P[i % 8][Number(reversed[i])]];
|
|
69
|
+
}
|
|
70
|
+
return c === 0;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Checks for a 12-digit Aadhaar-shaped value with a valid Verhoeff checksum.
|
|
74
|
+
* This does not verify identity or official issuance.
|
|
75
|
+
*/
|
|
76
|
+
export function isValidAadhaar(value) {
|
|
77
|
+
if (typeof value !== "string")
|
|
78
|
+
return false;
|
|
79
|
+
const v = clean(value);
|
|
80
|
+
return /^[2-9]\d{11}$/.test(v) && verhoeffIsValid(v);
|
|
81
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
/**
|
|
23
|
+
* Converts a safe integer to words using the Indian numbering system.
|
|
24
|
+
*
|
|
25
|
+
* @throws {TypeError} If n is not a safe integer.
|
|
26
|
+
*/
|
|
27
|
+
export function numberToWords(n) {
|
|
28
|
+
if (!Number.isSafeInteger(n)) {
|
|
29
|
+
throw new TypeError("numberToWords expects a safe integer");
|
|
30
|
+
}
|
|
31
|
+
if (n === 0)
|
|
32
|
+
return "Zero";
|
|
33
|
+
if (n < 0)
|
|
34
|
+
return "Minus " + numberToWords(-n);
|
|
35
|
+
const parts = [];
|
|
36
|
+
let remaining = n;
|
|
37
|
+
const crore = Math.floor(remaining / 10000000);
|
|
38
|
+
if (crore) {
|
|
39
|
+
parts.push(numberToWords(crore) + " Crore");
|
|
40
|
+
remaining %= 10000000;
|
|
41
|
+
}
|
|
42
|
+
const lakh = Math.floor(remaining / 100000);
|
|
43
|
+
if (lakh) {
|
|
44
|
+
parts.push(twoDigits(lakh) + " Lakh");
|
|
45
|
+
remaining %= 100000;
|
|
46
|
+
}
|
|
47
|
+
const thousand = Math.floor(remaining / 1000);
|
|
48
|
+
if (thousand) {
|
|
49
|
+
parts.push(twoDigits(thousand) + " Thousand");
|
|
50
|
+
remaining %= 1000;
|
|
51
|
+
}
|
|
52
|
+
if (remaining)
|
|
53
|
+
parts.push(threeDigits(remaining));
|
|
54
|
+
return parts.join(" ");
|
|
55
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prachisekera/desi-utils",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
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 && npm run test:types",
|
|
26
|
+
"test:types": "tsc --noEmit --strict --module Node16 --moduleResolution Node16 type-tests/exports.ts",
|
|
27
|
+
"prepublishOnly": "npm test"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"india",
|
|
31
|
+
"indian",
|
|
32
|
+
"inr",
|
|
33
|
+
"rupee",
|
|
34
|
+
"lakh",
|
|
35
|
+
"crore",
|
|
36
|
+
"pan",
|
|
37
|
+
"ifsc",
|
|
38
|
+
"aadhaar",
|
|
39
|
+
"pincode",
|
|
40
|
+
"validation",
|
|
41
|
+
"formatter"
|
|
42
|
+
],
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"author": "Hachi Sekera",
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^22.0.0",
|
|
47
|
+
"typescript": "^5.4.0"
|
|
48
|
+
},
|
|
49
|
+
"directories": {
|
|
50
|
+
"test": "test"
|
|
51
|
+
},
|
|
52
|
+
"type": "commonjs"
|
|
53
|
+
}
|