@mysetup/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.
Files changed (49) hide show
  1. package/dist/index.d.ts +2 -0
  2. package/dist/index.d.ts.map +1 -0
  3. package/dist/index.js +1 -0
  4. package/dist/src/arrayMethods/arrayMethods.d.ts +9 -0
  5. package/dist/src/arrayMethods/arrayMethods.d.ts.map +1 -0
  6. package/dist/src/arrayMethods/arrayMethods.js +21 -0
  7. package/dist/src/arrayMethods/index.d.ts +2 -0
  8. package/dist/src/arrayMethods/index.d.ts.map +1 -0
  9. package/dist/src/arrayMethods/index.js +1 -0
  10. package/dist/src/formatPhoneNumber/formatPhoneNumber.d.ts +3 -0
  11. package/dist/src/formatPhoneNumber/formatPhoneNumber.d.ts.map +1 -0
  12. package/dist/src/formatPhoneNumber/formatPhoneNumber.js +14 -0
  13. package/dist/src/formatPhoneNumber/index.d.ts +2 -0
  14. package/dist/src/formatPhoneNumber/index.d.ts.map +1 -0
  15. package/dist/src/formatPhoneNumber/index.js +1 -0
  16. package/dist/src/generate-cache-key/generate-cache-key.d.ts +2 -0
  17. package/dist/src/generate-cache-key/generate-cache-key.d.ts.map +1 -0
  18. package/dist/src/generate-cache-key/generate-cache-key.js +3 -0
  19. package/dist/src/generate-cache-key/index.d.ts +2 -0
  20. package/dist/src/generate-cache-key/index.d.ts.map +1 -0
  21. package/dist/src/generate-cache-key/index.js +1 -0
  22. package/dist/src/getCrypto/getCrypto.d.ts +3 -0
  23. package/dist/src/getCrypto/getCrypto.d.ts.map +1 -0
  24. package/dist/src/getCrypto/getCrypto.js +27 -0
  25. package/dist/src/getCrypto/index.d.ts +2 -0
  26. package/dist/src/getCrypto/index.d.ts.map +1 -0
  27. package/dist/src/getCrypto/index.js +1 -0
  28. package/dist/src/getDateTimeDiff/getDateTimeDiff.d.ts +7 -0
  29. package/dist/src/getDateTimeDiff/getDateTimeDiff.d.ts.map +1 -0
  30. package/dist/src/getDateTimeDiff/getDateTimeDiff.js +32 -0
  31. package/dist/src/getDateTimeDiff/index.d.ts +2 -0
  32. package/dist/src/getDateTimeDiff/index.d.ts.map +1 -0
  33. package/dist/src/getDateTimeDiff/index.js +1 -0
  34. package/dist/src/getTextWidth/getTextWidth.d.ts +8 -0
  35. package/dist/src/getTextWidth/getTextWidth.d.ts.map +1 -0
  36. package/dist/src/getTextWidth/getTextWidth.js +15 -0
  37. package/dist/src/getTextWidth/index.d.ts +2 -0
  38. package/dist/src/getTextWidth/index.d.ts.map +1 -0
  39. package/dist/src/getTextWidth/index.js +1 -0
  40. package/dist/src/index.d.ts +8 -0
  41. package/dist/src/index.d.ts.map +1 -0
  42. package/dist/src/index.js +7 -0
  43. package/dist/src/setCookie/index.d.ts +2 -0
  44. package/dist/src/setCookie/index.d.ts.map +1 -0
  45. package/dist/src/setCookie/index.js +1 -0
  46. package/dist/src/setCookie/setCookie.d.ts +2 -0
  47. package/dist/src/setCookie/setCookie.d.ts.map +1 -0
  48. package/dist/src/setCookie/setCookie.js +6 -0
  49. package/package.json +54 -0
@@ -0,0 +1,2 @@
1
+ export * from "./src";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./src";
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Returns longest string from a array
3
+ *
4
+ * @param array - array of object
5
+ * @param key - string
6
+ * @returns lonest string in array
7
+ */
8
+ export declare function longestStrInArray<T extends object>(array: T[], key: keyof T): string;
9
+ //# sourceMappingURL=arrayMethods.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arrayMethods.d.ts","sourceRoot":"","sources":["../../../src/arrayMethods/arrayMethods.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,MAAM,CAAC,UAc3E"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Returns longest string from a array
3
+ *
4
+ * @param array - array of object
5
+ * @param key - string
6
+ * @returns lonest string in array
7
+ */
8
+ export function longestStrInArray(array, key) {
9
+ let ans = "";
10
+ let maxStr = 0;
11
+ for (const item of array) {
12
+ if (typeof item[key] === "string") {
13
+ const maxi = String(item[key]).length;
14
+ if (maxi > maxStr) {
15
+ ans = String(item[key]);
16
+ maxStr = maxi;
17
+ }
18
+ }
19
+ }
20
+ return ans;
21
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./arrayMethods";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/arrayMethods/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./arrayMethods";
@@ -0,0 +1,3 @@
1
+ export declare function formatPhoneNumber(phoneNumber: string): string | null;
2
+ export declare const phoneNumberAutoFormat: (phoneNumber: string) => string;
3
+ //# sourceMappingURL=formatPhoneNumber.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatPhoneNumber.d.ts","sourceRoot":"","sources":["../../../src/formatPhoneNumber/formatPhoneNumber.ts"],"names":[],"mappings":"AAEA,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAUpE;AAED,eAAO,MAAM,qBAAqB,gBAAiB,MAAM,KAAG,MAM3D,CAAC"}
@@ -0,0 +1,14 @@
1
+ const countryCodeUS = "+91";
2
+ export function formatPhoneNumber(phoneNumber) {
3
+ const sanitized = phoneNumber.replace(/[`() +\\-]/gi, "").trim();
4
+ if (sanitized === "")
5
+ return null;
6
+ if (sanitized.length !== 10 || /\D/.test(sanitized)) {
7
+ return null;
8
+ }
9
+ return `${countryCodeUS}${sanitized}`;
10
+ }
11
+ export const phoneNumberAutoFormat = (phoneNumber) => {
12
+ const number = phoneNumber.trim().slice(-10);
13
+ return number.replace(/(?<start>\d{3})(?<middle>\d{3})(?<end>\d{4})/, "$<start>-$<middle>-$<end>");
14
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./formatPhoneNumber";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/formatPhoneNumber/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./formatPhoneNumber";
@@ -0,0 +1,2 @@
1
+ export declare const generateCacheKey: (brand: string, environment: string, key: string) => string;
2
+ //# sourceMappingURL=generate-cache-key.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate-cache-key.d.ts","sourceRoot":"","sources":["../../../src/generate-cache-key/generate-cache-key.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,UAClB,MAAM,eACA,MAAM,OACd,MAAM,WAGd,CAAC"}
@@ -0,0 +1,3 @@
1
+ export const generateCacheKey = (brand, environment, key) => {
2
+ return `${brand}-${environment}-${key}`;
3
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./generate-cache-key";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/generate-cache-key/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./generate-cache-key";
@@ -0,0 +1,3 @@
1
+ export declare const encodeData: (data: string, dataKey: string) => string;
2
+ export declare const decodeData: (encryptedData: string, dataKey: string) => string;
3
+ //# sourceMappingURL=getCrypto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getCrypto.d.ts","sourceRoot":"","sources":["../../../src/getCrypto/getCrypto.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,UAAU,SAAU,MAAM,WAAW,MAAM,KAAG,MAgB1D,CAAC;AAEF,eAAO,MAAM,UAAU,kBAAmB,MAAM,WAAW,MAAM,KAAG,MAoBnE,CAAC"}
@@ -0,0 +1,27 @@
1
+ import * as crypto from "crypto";
2
+ export const encodeData = (data, dataKey) => {
3
+ const option = dataKey.split("/");
4
+ if (!data) {
5
+ return data;
6
+ }
7
+ const keyHash = crypto.createHash("sha256");
8
+ keyHash.update(option[2]);
9
+ const key = keyHash.digest();
10
+ const cipher = crypto.createCipheriv(option[0], key, Buffer.from(option[1], option[3]));
11
+ let encryptedData = cipher.update(data, "utf8", option[3]);
12
+ encryptedData += cipher.final(option[3]);
13
+ return encryptedData;
14
+ };
15
+ export const decodeData = (encryptedData, dataKey) => {
16
+ const option = dataKey.split("/");
17
+ if (!encryptedData) {
18
+ return encryptedData;
19
+ }
20
+ const keyHash = crypto.createHash("sha256");
21
+ keyHash.update(option[2]);
22
+ const key = keyHash.digest();
23
+ const decipher = crypto.createDecipheriv(option[0], key, Buffer.from(option[1], option[3]));
24
+ let decryptedData = decipher.update(encryptedData, option[3], "utf8");
25
+ decryptedData += decipher.final("utf8");
26
+ return decryptedData;
27
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./getCrypto";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/getCrypto/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./getCrypto";
@@ -0,0 +1,7 @@
1
+ export declare const getDateTimeDiff: (fromDate: string, toDate?: string) => {
2
+ days: () => number;
3
+ hours: () => number;
4
+ minutes: () => number;
5
+ asMilliseconds: () => number;
6
+ };
7
+ //# sourceMappingURL=getDateTimeDiff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getDateTimeDiff.d.ts","sourceRoot":"","sources":["../../../src/getDateTimeDiff/getDateTimeDiff.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,eAAe,aAAc,MAAM,WAAW,MAAM;;;;;CAiChE,CAAC"}
@@ -0,0 +1,32 @@
1
+ import dayjs from "dayjs";
2
+ import durationPlugin from "dayjs/plugin/duration";
3
+ import utc from "dayjs/plugin/utc";
4
+ dayjs.extend(durationPlugin);
5
+ dayjs.extend(utc);
6
+ export const getDateTimeDiff = (fromDate, toDate) => {
7
+ if (!fromDate || fromDate.trim() === "") {
8
+ throw new Error("Invalid date format");
9
+ }
10
+ if (toDate !== undefined && toDate.trim() === "") {
11
+ throw new Error("Invalid date format");
12
+ }
13
+ const start = dayjs.utc(fromDate);
14
+ const end = toDate ? dayjs.utc(toDate) : start.endOf("month").endOf("day");
15
+ if (!start.isValid() || !end.isValid()) {
16
+ throw new Error("Invalid date format");
17
+ }
18
+ const diffInMilliseconds = end.diff(start);
19
+ if (diffInMilliseconds < 0) {
20
+ throw new Error("toDate must be greater than or equal to fromDate");
21
+ }
22
+ const duration = dayjs.duration(diffInMilliseconds);
23
+ const totalDays = Math.floor(duration.asDays());
24
+ const remainingHours = duration.subtract(totalDays, "days").hours();
25
+ const remainingMinutes = duration.subtract(totalDays, "days").minutes();
26
+ return {
27
+ days: () => totalDays,
28
+ hours: () => remainingHours,
29
+ minutes: () => remainingMinutes,
30
+ asMilliseconds: () => diffInMilliseconds,
31
+ };
32
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./getDateTimeDiff";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/getDateTimeDiff/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./getDateTimeDiff";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Returns width in px from given string
3
+ *
4
+ * @param text - string
5
+ * @returns width in px of the given text
6
+ */
7
+ export declare const getTextWidth: (text: string, font?: string) => number | undefined;
8
+ //# sourceMappingURL=getTextWidth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getTextWidth.d.ts","sourceRoot":"","sources":["../../../src/getTextWidth/getTextWidth.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,YAAY,SAAU,MAAM,SAAS,MAAM,uBAWvD,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Returns width in px from given string
3
+ *
4
+ * @param text - string
5
+ * @returns width in px of the given text
6
+ */
7
+ export const getTextWidth = (text, font) => {
8
+ const canvas = document.createElement("canvas");
9
+ const context = canvas.getContext("2d");
10
+ if (context && font) {
11
+ context.font = font;
12
+ }
13
+ const metrics = context === null || context === void 0 ? void 0 : context.measureText(text);
14
+ return metrics === null || metrics === void 0 ? void 0 : metrics.width;
15
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./getTextWidth";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/getTextWidth/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./getTextWidth";
@@ -0,0 +1,8 @@
1
+ export * from "./arrayMethods";
2
+ export * from "./formatPhoneNumber";
3
+ export * from "./generate-cache-key";
4
+ export * from "./getCrypto";
5
+ export * from "./getDateTimeDiff";
6
+ export * from "./getTextWidth";
7
+ export * from "./setCookie";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,7 @@
1
+ export * from "./arrayMethods";
2
+ export * from "./formatPhoneNumber";
3
+ export * from "./generate-cache-key";
4
+ export * from "./getCrypto";
5
+ export * from "./getDateTimeDiff";
6
+ export * from "./getTextWidth";
7
+ export * from "./setCookie";
@@ -0,0 +1,2 @@
1
+ export * from "./setCookie";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/setCookie/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./setCookie";
@@ -0,0 +1,2 @@
1
+ export declare const setCookie: (cname: string, cvalue: string, exdays: number) => void;
2
+ //# sourceMappingURL=setCookie.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setCookie.d.ts","sourceRoot":"","sources":["../../../src/setCookie/setCookie.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,UAAW,MAAM,UAAU,MAAM,UAAU,MAAM,SAKtE,CAAC"}
@@ -0,0 +1,6 @@
1
+ export const setCookie = (cname, cvalue, exdays) => {
2
+ const d = new Date();
3
+ d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
4
+ const expires = `expires=${d.toUTCString()}`;
5
+ document.cookie = `${cname}=${cvalue};${expires};path=${location.origin}`;
6
+ };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@mysetup/helpers",
3
+ "version": "1.0.0",
4
+ "license": "MIT",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.js"
14
+ },
15
+ "./package.json": "./package.json"
16
+ },
17
+ "scripts": {
18
+ "build": "rm -rf ./dist && tsc -p tsconfig.build.json",
19
+ "lint": "eslint .",
20
+ "test": "jest",
21
+ "test:update": "pnpm test -- -u",
22
+ "test:coverage": "jest --coverage",
23
+ "typecheck": "tsc --noEmit",
24
+ "format": "prettier --write \"**/*.{ts,tsx,md,js,mjs,json}\"",
25
+ "checks": "pnpm typecheck && pnpm lint && pnpm test",
26
+ "clean": "rm -rf node_modules .swc dist pnpm-lock.yaml && echo \"✅ Successfully removed \""
27
+ },
28
+ "dependencies": {
29
+ "@mysetup/logger": "latest",
30
+ "dayjs": "^1.11.13"
31
+ },
32
+ "devDependencies": {
33
+ "@mysetup/eslint-config": "latest",
34
+ "@mysetup/jest-config": "latest",
35
+ "@mysetup/prettier-config": "latest",
36
+ "@mysetup/tsconfig": "latest",
37
+ "@mysetup/types": "latest",
38
+ "@types/jest": "^29.5.14",
39
+ "@types/node": "^22.8.6",
40
+ "@types/react": "^18.3.12",
41
+ "@types/react-dom": "^18.3.1",
42
+ "eslint": "8.57.0",
43
+ "jest": "^29.7.0",
44
+ "next": "^15.0.2",
45
+ "react": "^18.3.1",
46
+ "react-dom": "^18.3.1",
47
+ "typescript": "^5.6.3"
48
+ },
49
+ "prettier": "@mysetup/prettier-config",
50
+ "engines": {
51
+ "node": ">=18.0.0"
52
+ },
53
+ "packageManager": "pnpm@9.9.0"
54
+ }