@prachisekera/desi-utils 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/README.md CHANGED
@@ -1,10 +1,6 @@
1
1
  # @prachisekera/desi-utils
2
2
 
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.
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.
8
4
 
9
5
  ## Install
10
6
 
@@ -12,54 +8,91 @@ Every Indian sign-up form, payment page or dashboard needs the same small things
12
8
  npm install @prachisekera/desi-utils
13
9
  ```
14
10
 
15
- ## Usage
11
+ The package provides CommonJS and native ES Module builds, plus TypeScript declarations.
12
+
13
+ ## ES Module usage
16
14
 
17
15
  ```js
18
16
  import {
19
- formatINR, numberToWords,
20
- isValidMobile, isValidPincode, isValidPAN, isValidIFSC, isValidAadhaar,
17
+ formatINR,
18
+ numberToWords,
19
+ isValidMobile,
20
+ isValidPincode,
21
+ isValidPAN,
22
+ isValidIFSC,
23
+ isValidAadhaar,
21
24
  } from "@prachisekera/desi-utils";
22
25
 
23
- formatINR(1234567); // "₹12,34,567.00"
24
- formatINR(1500, { decimals: 0 }); // "₹1,500"
25
- formatINR(1500, { symbol: false }); // "1,500.00"
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
+ ```
26
34
 
27
- numberToWords(125000); // "One Lakh Twenty Five Thousand"
35
+ ## CommonJS usage
28
36
 
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
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"
34
49
  ```
35
50
 
36
- CommonJS is supported as well:
51
+ ## TypeScript usage
37
52
 
38
- ```js
39
- const { formatINR, numberToWords } = require("@prachisekera/desi-utils");
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");
40
65
  ```
41
66
 
42
67
  ## API
43
68
 
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. |
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.
53
80
 
54
- ## Important
81
+ ## Input handling and limitations
55
82
 
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.
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.
57
88
 
58
89
  ## Development
59
90
 
60
91
  ```bash
61
92
  npm install
62
- npm test # builds, then runs the tests with Node's built-in test runner
93
+ npm test
94
+ npm run build:cjs
95
+ npm run build:esm
63
96
  ```
64
97
 
65
98
  ## License
@@ -1,5 +1,14 @@
1
+ /** Options for formatting a number as Indian rupees. */
1
2
  export interface FormatINROptions {
3
+ /** Fraction digits to display, from 0 to 100. Defaults to 2. */
2
4
  decimals?: number;
5
+ /** Whether to include the rupee symbol. Defaults to true. */
3
6
  symbol?: boolean;
4
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
+ */
5
14
  export declare function formatINR(amount: number, options?: FormatINROptions): string;
@@ -1,13 +1,41 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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
+ */
4
10
  function formatINR(amount, options = {}) {
5
- const { decimals = 2, symbol = true } = options;
6
11
  if (typeof amount !== "number" || !Number.isFinite(amount)) {
7
12
  throw new TypeError("formatINR expects a finite number");
8
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
+ }
9
28
  const isNegative = amount < 0;
10
- const [whole, fraction] = Math.abs(amount).toFixed(decimals).split(".");
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(".");
11
39
  const lastThree = whole.slice(-3);
12
40
  const rest = whole.slice(0, -3);
13
41
  const grouped = rest ? rest.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + "," + lastThree : lastThree;
@@ -1,5 +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
+ */
1
5
  export declare function isValidMobile(value: string): boolean;
6
+ /** Checks for a six-digit Indian pincode that does not start with 0. */
2
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
+ */
3
12
  export declare function isValidPAN(value: string): boolean;
13
+ /** Checks the IFSC shape: four letters, a zero, and six letters or digits. */
4
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
+ */
5
19
  export declare function isValidAadhaar(value: string): boolean;
@@ -8,6 +8,10 @@ exports.isValidAadhaar = isValidAadhaar;
8
8
  function clean(value) {
9
9
  return value.replace(/[\s-]/g, "");
10
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
+ */
11
15
  function isValidMobile(value) {
12
16
  if (typeof value !== "string")
13
17
  return false;
@@ -20,16 +24,22 @@ function isValidMobile(value) {
20
24
  v = v.slice(1);
21
25
  return /^[6-9]\d{9}$/.test(v);
22
26
  }
27
+ /** Checks for a six-digit Indian pincode that does not start with 0. */
23
28
  function isValidPincode(value) {
24
29
  if (typeof value !== "string")
25
30
  return false;
26
31
  return /^[1-9]\d{5}$/.test(value.trim());
27
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
+ */
28
37
  function isValidPAN(value) {
29
38
  if (typeof value !== "string")
30
39
  return false;
31
- return /^[A-Z]{3}[PCHFATBLJG][A-Z]\d{4}[A-Z]$/.test(value.trim().toUpperCase());
40
+ return /^[A-Z]{5}\d{4}[A-Z]$/.test(value.trim().toUpperCase());
32
41
  }
42
+ /** Checks the IFSC shape: four letters, a zero, and six letters or digits. */
33
43
  function isValidIFSC(value) {
34
44
  if (typeof value !== "string")
35
45
  return false;
@@ -57,6 +67,7 @@ const P = [
57
67
  [2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
58
68
  [7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
59
69
  ];
70
+ /** Checks a digit string with the Verhoeff checksum algorithm. */
60
71
  function verhoeffIsValid(digits) {
61
72
  let c = 0;
62
73
  const reversed = digits.split("").reverse();
@@ -65,6 +76,10 @@ function verhoeffIsValid(digits) {
65
76
  }
66
77
  return c === 0;
67
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
+ */
68
83
  function isValidAadhaar(value) {
69
84
  if (typeof value !== "string")
70
85
  return false;
@@ -1 +1,6 @@
1
+ /**
2
+ * Converts a safe integer to words using the Indian numbering system.
3
+ *
4
+ * @throws {TypeError} If n is not a safe integer.
5
+ */
1
6
  export declare function numberToWords(n: number): string;
package/dist/cjs/words.js CHANGED
@@ -22,6 +22,11 @@ function threeDigits(n) {
22
22
  parts.push(twoDigits(rest));
23
23
  return parts.join(" ");
24
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
+ */
25
30
  function numberToWords(n) {
26
31
  if (!Number.isSafeInteger(n)) {
27
32
  throw new TypeError("numberToWords expects a safe integer");
@@ -1,10 +1,38 @@
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
+ */
1
7
  export function formatINR(amount, options = {}) {
2
- const { decimals = 2, symbol = true } = options;
3
8
  if (typeof amount !== "number" || !Number.isFinite(amount)) {
4
9
  throw new TypeError("formatINR expects a finite number");
5
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
+ }
6
25
  const isNegative = amount < 0;
7
- const [whole, fraction] = Math.abs(amount).toFixed(decimals).split(".");
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(".");
8
36
  const lastThree = whole.slice(-3);
9
37
  const rest = whole.slice(0, -3);
10
38
  const grouped = rest ? rest.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + "," + lastThree : lastThree;
@@ -1,6 +1,10 @@
1
1
  function clean(value) {
2
2
  return value.replace(/[\s-]/g, "");
3
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
+ */
4
8
  export function isValidMobile(value) {
5
9
  if (typeof value !== "string")
6
10
  return false;
@@ -13,16 +17,22 @@ export function isValidMobile(value) {
13
17
  v = v.slice(1);
14
18
  return /^[6-9]\d{9}$/.test(v);
15
19
  }
20
+ /** Checks for a six-digit Indian pincode that does not start with 0. */
16
21
  export function isValidPincode(value) {
17
22
  if (typeof value !== "string")
18
23
  return false;
19
24
  return /^[1-9]\d{5}$/.test(value.trim());
20
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
+ */
21
30
  export function isValidPAN(value) {
22
31
  if (typeof value !== "string")
23
32
  return false;
24
- return /^[A-Z]{3}[PCHFATBLJG][A-Z]\d{4}[A-Z]$/.test(value.trim().toUpperCase());
33
+ return /^[A-Z]{5}\d{4}[A-Z]$/.test(value.trim().toUpperCase());
25
34
  }
35
+ /** Checks the IFSC shape: four letters, a zero, and six letters or digits. */
26
36
  export function isValidIFSC(value) {
27
37
  if (typeof value !== "string")
28
38
  return false;
@@ -50,6 +60,7 @@ const P = [
50
60
  [2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
51
61
  [7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
52
62
  ];
63
+ /** Checks a digit string with the Verhoeff checksum algorithm. */
53
64
  function verhoeffIsValid(digits) {
54
65
  let c = 0;
55
66
  const reversed = digits.split("").reverse();
@@ -58,6 +69,10 @@ function verhoeffIsValid(digits) {
58
69
  }
59
70
  return c === 0;
60
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
+ */
61
76
  export function isValidAadhaar(value) {
62
77
  if (typeof value !== "string")
63
78
  return false;
package/dist/esm/words.js CHANGED
@@ -19,6 +19,11 @@ function threeDigits(n) {
19
19
  parts.push(twoDigits(rest));
20
20
  return parts.join(" ");
21
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
+ */
22
27
  export function numberToWords(n) {
23
28
  if (!Number.isSafeInteger(n)) {
24
29
  throw new TypeError("numberToWords expects a safe integer");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prachisekera/desi-utils",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Tiny zero-dependency TypeScript helpers for Indian apps: INR formatting, number to words, and PAN / IFSC / Aadhaar / mobile / pincode validation.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -22,7 +22,8 @@
22
22
  "build:cjs": "tsc -p tsconfig.json",
23
23
  "build:esm": "tsc -p tsconfig.esm.json && node scripts/write-esm-package.js",
24
24
  "build": "npm run build:cjs && npm run build:esm",
25
- "test": "npm run build && node --test",
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",
26
27
  "prepublishOnly": "npm test"
27
28
  },
28
29
  "keywords": [
@@ -42,8 +43,8 @@
42
43
  "license": "MIT",
43
44
  "author": "Hachi Sekera",
44
45
  "devDependencies": {
45
- "typescript": "^5.4.0",
46
- "@types/node": "^22.0.0"
46
+ "@types/node": "^22.0.0",
47
+ "typescript": "^5.4.0"
47
48
  },
48
49
  "directories": {
49
50
  "test": "test"