@react-pakistan/util-functions 1.25.27 → 1.25.28
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/general/cnic-formatter.d.ts +7 -0
- package/general/cnic-formatter.js +53 -0
- package/general/index.d.ts +1 -0
- package/general/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
interface FormatCnicOptions {
|
|
2
|
+
separator?: string;
|
|
3
|
+
allowLoose?: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare const formatCnic: (value: string | number | null | undefined, opts?: FormatCnicOptions) => string | null;
|
|
6
|
+
export declare const isValidCnic: (value: string | number | null | undefined) => boolean;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidCnic = exports.formatCnic = void 0;
|
|
4
|
+
var unicodeDigitMap = (function () {
|
|
5
|
+
var map = {};
|
|
6
|
+
// Arabic-Indic digits \u0660 - \u0669
|
|
7
|
+
for (var i = 0; i <= 9; i++) {
|
|
8
|
+
map[String.fromCharCode(0x660 + i)] = String(i);
|
|
9
|
+
}
|
|
10
|
+
// Eastern Arabic-Indic digits \u06F0 - \u06F9
|
|
11
|
+
for (var i = 0; i <= 9; i++) {
|
|
12
|
+
map[String.fromCharCode(0x6f0 + i)] = String(i);
|
|
13
|
+
}
|
|
14
|
+
return map;
|
|
15
|
+
})();
|
|
16
|
+
var toAsciiDigits = function (input) {
|
|
17
|
+
return input.replace(/./g, function (ch) { var _a; return (_a = unicodeDigitMap[ch]) !== null && _a !== void 0 ? _a : ch; });
|
|
18
|
+
};
|
|
19
|
+
var formatCnic = function (value, opts) {
|
|
20
|
+
var _a;
|
|
21
|
+
if (opts === void 0) { opts = {}; }
|
|
22
|
+
if (value === null || value === undefined)
|
|
23
|
+
return null;
|
|
24
|
+
var separator = (_a = opts.separator) !== null && _a !== void 0 ? _a : '-';
|
|
25
|
+
var raw = typeof value === 'number' ? String(value) : String(value);
|
|
26
|
+
raw = raw.trim();
|
|
27
|
+
if (raw.length === 0)
|
|
28
|
+
return null;
|
|
29
|
+
// Normalize unicode digits to ASCII then strip all non-digit characters
|
|
30
|
+
var ascii = toAsciiDigits(raw);
|
|
31
|
+
var digits = ascii.replace(/[^0-9]/g, '');
|
|
32
|
+
if (digits.length === 0)
|
|
33
|
+
return null;
|
|
34
|
+
// For strict mode, only accept exactly 13 digits
|
|
35
|
+
if (!opts.allowLoose && digits.length !== 13)
|
|
36
|
+
return null;
|
|
37
|
+
// Build groups: 5, 7, 1. For allowLoose, allow missing trailing groups.
|
|
38
|
+
var g1 = digits.slice(0, 5);
|
|
39
|
+
var g2 = digits.slice(5, 12);
|
|
40
|
+
var g3 = digits.slice(12);
|
|
41
|
+
var parts = [g1, g2, g3].filter(function (p) { return p && p.length > 0; });
|
|
42
|
+
return parts.join(separator);
|
|
43
|
+
};
|
|
44
|
+
exports.formatCnic = formatCnic;
|
|
45
|
+
var isValidCnic = function (value) {
|
|
46
|
+
if (value === null || value === undefined)
|
|
47
|
+
return false;
|
|
48
|
+
var raw = typeof value === 'number' ? String(value) : String(value);
|
|
49
|
+
var ascii = toAsciiDigits(raw);
|
|
50
|
+
var digits = ascii.replace(/[^0-9]/g, '');
|
|
51
|
+
return digits.length === 13;
|
|
52
|
+
};
|
|
53
|
+
exports.isValidCnic = isValidCnic;
|
package/general/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export * from './fetch-supabase-private-assets';
|
|
|
15
15
|
export * from './format-date';
|
|
16
16
|
export * from './format-number';
|
|
17
17
|
export * from './format-phone';
|
|
18
|
+
export * from './cnic-formatter';
|
|
18
19
|
export * from './format-pricing';
|
|
19
20
|
export * from './format-secs';
|
|
20
21
|
export * from './format-time';
|
package/general/index.js
CHANGED
|
@@ -31,6 +31,7 @@ __exportStar(require("./fetch-supabase-private-assets"), exports);
|
|
|
31
31
|
__exportStar(require("./format-date"), exports);
|
|
32
32
|
__exportStar(require("./format-number"), exports);
|
|
33
33
|
__exportStar(require("./format-phone"), exports);
|
|
34
|
+
__exportStar(require("./cnic-formatter"), exports);
|
|
34
35
|
__exportStar(require("./format-pricing"), exports);
|
|
35
36
|
__exportStar(require("./format-secs"), exports);
|
|
36
37
|
__exportStar(require("./format-time"), exports);
|