@ahlt/helpers 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.
@@ -0,0 +1,3 @@
1
+ export declare function encryptData(data: string | object, encryptKeyHex: string | undefined, encryptIvHex: string | undefined): string;
2
+ export declare function decryptData(encryptedText: string, encryptKeyHex: string | undefined, encryptIvHex: string | undefined): any;
3
+ //# sourceMappingURL=data-encryption.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data-encryption.d.ts","sourceRoot":"","sources":["../src/data-encryption.ts"],"names":[],"mappings":"AAUA,wBAAgB,WAAW,CACvB,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,aAAa,EAAE,MAAM,GAAG,SAAS,EACjC,YAAY,EAAE,MAAM,GAAG,SAAS,GACjC,MAAM,CAkBR;AAED,wBAAgB,WAAW,CACvB,aAAa,EAAE,MAAM,EACrB,aAAa,EAAE,MAAM,GAAG,SAAS,EACjC,YAAY,EAAE,MAAM,GAAG,SAAS,OAuBnC"}
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.encryptData = encryptData;
7
+ exports.decryptData = decryptData;
8
+ const crypto_js_1 = __importDefault(require("crypto-js"));
9
+ function parseHex(name, hex, expectedLen) {
10
+ if (!hex)
11
+ throw new Error(`${name} is required.`);
12
+ if (hex.length !== expectedLen) {
13
+ throw new Error(`${name} must be ${expectedLen} hex characters.`);
14
+ }
15
+ return crypto_js_1.default.enc.Hex.parse(hex);
16
+ }
17
+ function encryptData(data, encryptKeyHex, encryptIvHex) {
18
+ try {
19
+ const secretKey = parseHex('encryptKey', encryptKeyHex, 64); // 32 bytes
20
+ const iv = parseHex('encryptIv', encryptIvHex, 32); // 16 bytes
21
+ const plainText = typeof data === 'string' ? data : JSON.stringify(data);
22
+ const encrypted = crypto_js_1.default.AES.encrypt(plainText, secretKey, {
23
+ iv,
24
+ mode: crypto_js_1.default.mode.CBC,
25
+ padding: crypto_js_1.default.pad.Pkcs7,
26
+ });
27
+ return encrypted.toString();
28
+ }
29
+ catch (error) {
30
+ console.error('Encryption failed:', error);
31
+ return '';
32
+ }
33
+ }
34
+ function decryptData(encryptedText, encryptKeyHex, encryptIvHex) {
35
+ try {
36
+ const secretKey = parseHex('encryptKey', encryptKeyHex, 64);
37
+ const iv = parseHex('encryptIv', encryptIvHex, 32);
38
+ const decrypted = crypto_js_1.default.AES.decrypt(encryptedText, secretKey, {
39
+ iv,
40
+ mode: crypto_js_1.default.mode.CBC,
41
+ padding: crypto_js_1.default.pad.Pkcs7,
42
+ });
43
+ const decryptedText = decrypted.toString(crypto_js_1.default.enc.Utf8);
44
+ try {
45
+ return JSON.parse(decryptedText);
46
+ }
47
+ catch {
48
+ return decryptedText;
49
+ }
50
+ }
51
+ catch (error) {
52
+ console.error('Decryption failed:', error);
53
+ return null;
54
+ }
55
+ }
@@ -0,0 +1,2 @@
1
+ export declare const formatDate: (date: Date | string | number | null | undefined, pattern?: string) => string;
2
+ //# sourceMappingURL=formatDate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatDate.d.ts","sourceRoot":"","sources":["../src/formatDate.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,UAAU,GACnB,MAAM,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,EAC/C,gBAAsB,WAYzB,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatDate = void 0;
4
+ const date_fns_1 = require("date-fns");
5
+ const formatDate = (date, pattern = 'dd/MM/yyyy') => {
6
+ if (!date)
7
+ return '';
8
+ const dateObj = new Date(date);
9
+ if (Number.isNaN(dateObj.getTime()))
10
+ return '';
11
+ try {
12
+ return (0, date_fns_1.format)(dateObj, pattern);
13
+ }
14
+ catch {
15
+ return '';
16
+ }
17
+ };
18
+ exports.formatDate = formatDate;
@@ -0,0 +1,6 @@
1
+ export declare function generateStrongPassword(length?: number): {
2
+ newStrength: "" | "Very Weak" | "Weak" | "Moderate" | "Strong" | "Very Strong";
3
+ password: string;
4
+ newMissingCriteria: string[];
5
+ };
6
+ //# sourceMappingURL=generateStrongPassword.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateStrongPassword.d.ts","sourceRoot":"","sources":["../src/generateStrongPassword.ts"],"names":[],"mappings":"AAKA,wBAAgB,sBAAsB,CAAC,MAAM,GAAE,MAAU;;;;EA+CxD"}
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateStrongPassword = generateStrongPassword;
4
+ function getRandomChar(charSet) {
5
+ const randomIndex = Math.floor(Math.random() * charSet.length);
6
+ return charSet.charAt(randomIndex);
7
+ }
8
+ function generateStrongPassword(length = 8) {
9
+ const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
10
+ const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
11
+ const numberChars = '0123456789';
12
+ const specialChars = "!@#$%^&*()_-+=~`|:;'<>,.?/";
13
+ const targetLength = Math.max(4, Math.floor(length)); // minimum 4 to include all groups
14
+ let password = '';
15
+ // Ensure at least one character from each character set
16
+ password += getRandomChar(lowercaseChars);
17
+ password += getRandomChar(uppercaseChars);
18
+ password += getRandomChar(numberChars);
19
+ password += getRandomChar(specialChars);
20
+ // Generate the rest of the password to reach targetLength
21
+ const allChars = lowercaseChars + uppercaseChars + numberChars + specialChars;
22
+ for (let i = 4; i < targetLength; i++) {
23
+ password += getRandomChar(allChars);
24
+ }
25
+ // Shuffle the password characters
26
+ password = password
27
+ .split('')
28
+ .sort(() => Math.random() - 0.5)
29
+ .join('');
30
+ const criteria = {
31
+ length: password.length >= targetLength,
32
+ lowercase: /[a-z]/.test(password),
33
+ uppercase: /[A-Z]/.test(password),
34
+ number: /\d/.test(password),
35
+ special: /[\W_]/.test(password),
36
+ };
37
+ const strength = Object.values(criteria).filter(Boolean).length;
38
+ const newMissingCriteria = Object.keys(criteria).filter((k) => !criteria[k]);
39
+ let newStrength = '';
40
+ if (strength === 1)
41
+ newStrength = 'Very Weak';
42
+ else if (strength === 2)
43
+ newStrength = 'Weak';
44
+ else if (strength === 3)
45
+ newStrength = 'Moderate';
46
+ else if (strength === 4)
47
+ newStrength = 'Strong';
48
+ else if (strength === 5)
49
+ newStrength = 'Very Strong';
50
+ return { newStrength, password, newMissingCriteria };
51
+ }
@@ -0,0 +1,4 @@
1
+ export * from './data-encryption';
2
+ export * from './formatDate';
3
+ export * from './generateStrongPassword';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,cAAc,CAAC;AAC7B,cAAc,0BAA0B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./data-encryption"), exports);
18
+ __exportStar(require("./formatDate"), exports);
19
+ __exportStar(require("./generateStrongPassword"), exports);
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@ahlt/helpers",
3
+ "version": "1.0.0",
4
+ "keywords": [],
5
+ "author": "Ahmed Khaled",
6
+ "license": "ISC",
7
+ "description": "",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "exports": {
14
+ ".": {
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.js",
17
+ "types": "./dist/index.d.ts"
18
+ }
19
+ },
20
+ "scripts": {
21
+ "dev": "pnpm build --watch",
22
+ "build": "tsc -b -v",
23
+ "clean": "rimraf dist"
24
+ },
25
+ "dependencies": {
26
+ "date-fns": "^2.30.0",
27
+ "crypto-js": "^4.2.0"
28
+ },
29
+ "devDependencies": {
30
+ "@types/crypto-js": "^4.2.2",
31
+ "@ahlt/eslint-config": "workspace:*",
32
+ "@ahlt/typescript-config": "workspace:*"
33
+ }
34
+ }