gemcap-be-common 1.1.2 → 1.1.3

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,67 @@
1
+ import dayjs, { OpUnitType, QUnitType } from 'dayjs';
2
+ import utc from 'dayjs/plugin/utc';
3
+ import timezone from 'dayjs/plugin/timezone';
4
+
5
+ dayjs.extend(utc);
6
+ dayjs.extend(timezone);
7
+
8
+ export const defaultDateFormat = 'MM-DD-YYYY';
9
+
10
+ export const normalizeDate = (date: Date, type: 'start' | 'end'): Date => {
11
+ return type === 'start'
12
+ ? dayjs(date).utc().startOf('day').toDate()
13
+ : dayjs(date).utc().endOf('day').toDate();
14
+ };
15
+
16
+ export const convertAnyToDate = (date: any): Date | null => {
17
+ let result = null;
18
+ if (!date) {
19
+ result = null;
20
+ }
21
+ if (date instanceof Date) {
22
+ result = date;
23
+ }
24
+ if (date.toString().match(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/)) {
25
+ result = new Date(date.slice(0, -1));
26
+ }
27
+ return result;
28
+ };
29
+
30
+ export const dateLessThan = (date: Date, compare: number, unit: QUnitType | OpUnitType) => {
31
+ const currentDate = dayjs();
32
+ const nextDate = dayjs(new Date(date));
33
+ const difference = currentDate.diff(nextDate, unit);
34
+ return difference < compare;
35
+ };
36
+
37
+ export const formatDate = (date: Date): string => {
38
+ return dayjs(date).format(defaultDateFormat);
39
+ };
40
+
41
+ export const calculateTimeZone = (date: Date): Date => {
42
+ if (date instanceof Date) {
43
+ return dayjs(date).subtract(date.getTimezoneOffset(), 'minute').toDate();
44
+ }
45
+ return date;
46
+ };
47
+
48
+ export const convertToDefaultFormat = (date: Date) => {
49
+ return dayjs(date).format(defaultDateFormat);
50
+ };
51
+
52
+ export const excelDateToJSDate = (serial: number) => {
53
+ const utcDays = Math.floor(serial - 25569);
54
+ const utcValue = utcDays * 86400;
55
+ const dateInfo = new Date(utcValue * 1000);
56
+ const fractionalDay = serial - Math.floor(serial) + 0.0000001;
57
+ let totalSeconds = Math.floor(86400 * fractionalDay);
58
+ const seconds = totalSeconds % 60;
59
+ totalSeconds -= seconds;
60
+ const hours = Math.floor(totalSeconds / (60 * 60));
61
+ const minutes = Math.floor(totalSeconds / 60) % 60;
62
+ return new Date(dateInfo.getFullYear(), dateInfo.getMonth(), dateInfo.getDate(), hours, minutes, seconds);
63
+ };
64
+
65
+ export const convertToFormat = (date: Date, format: string) => {
66
+ return dayjs(date).format(format);
67
+ };
@@ -0,0 +1,3 @@
1
+ export declare const getEnumKeyByEnumValue: <T extends {
2
+ [index: string]: string | number;
3
+ }>(myEnum: T, enumValue: string) => keyof T;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEnumKeyByEnumValue = void 0;
4
+ var getEnumKeyByEnumValue = function (myEnum, enumValue) {
5
+ var keys = Object.keys(myEnum).filter(function (x) { return myEnum[x] == enumValue; });
6
+ return keys.length > 0 ? keys[0] : null;
7
+ };
8
+ exports.getEnumKeyByEnumValue = getEnumKeyByEnumValue;
@@ -0,0 +1,4 @@
1
+ export const getEnumKeyByEnumValue = <T extends { [index: string]: string | number }>(myEnum: T, enumValue: string): keyof T | null => {
2
+ let keys = Object.keys(myEnum).filter((x) => myEnum[x] == enumValue);
3
+ return keys.length > 0 ? keys[0] : null;
4
+ };
@@ -0,0 +1 @@
1
+ export declare const asyncExec: (command: string) => Promise<string>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.asyncExec = void 0;
4
+ var child_process_1 = require("child_process");
5
+ var asyncExec = function (command) {
6
+ return new Promise(function (resolve, reject) {
7
+ (0, child_process_1.exec)(command, { maxBuffer: 2048 * 2048 }, function (error, stdout, stderr) {
8
+ if (error) {
9
+ console.error({ error: error });
10
+ reject({ error: error, message: 'Something went wrong' });
11
+ }
12
+ resolve(stdout || stderr);
13
+ });
14
+ });
15
+ };
16
+ exports.asyncExec = asyncExec;
@@ -0,0 +1,13 @@
1
+ import { exec } from 'child_process';
2
+
3
+ export const asyncExec = (command: string): Promise<string> => {
4
+ return new Promise((resolve, reject) => {
5
+ exec(command, { maxBuffer: 2048 * 2048 }, (error, stdout, stderr) => {
6
+ if (error) {
7
+ console.error({ error });
8
+ reject({ error, message: 'Something went wrong' });
9
+ }
10
+ resolve(stdout || stderr);
11
+ });
12
+ });
13
+ }
@@ -0,0 +1,7 @@
1
+ export * from './common.helper';
2
+ export * from './common.helper';
3
+ export * from './date.helper';
4
+ export * from './enums.helper';
5
+ export * from './exec';
6
+ export * from './mongo.helper';
7
+ export * from './paginator.helper';
@@ -0,0 +1,23 @@
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("./common.helper"), exports);
18
+ __exportStar(require("./common.helper"), exports);
19
+ __exportStar(require("./date.helper"), exports);
20
+ __exportStar(require("./enums.helper"), exports);
21
+ __exportStar(require("./exec"), exports);
22
+ __exportStar(require("./mongo.helper"), exports);
23
+ __exportStar(require("./paginator.helper"), exports);
@@ -0,0 +1,7 @@
1
+ export * from './common.helper';
2
+ export * from './common.helper';
3
+ export * from './date.helper';
4
+ export * from './enums.helper';
5
+ export * from './exec';
6
+ export * from './mongo.helper';
7
+ export * from './paginator.helper';
@@ -0,0 +1,4 @@
1
+ import mongoose from 'mongoose';
2
+ export declare const renameKeys: (obj: any, oldName: any, newName: any) => any;
3
+ export declare const normalizeObject: (obj: any, oldName?: string, newName?: string) => any;
4
+ export declare const isValidId: (id: string | mongoose.Types.ObjectId) => boolean;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.isValidId = exports.normalizeObject = exports.renameKeys = void 0;
16
+ var mongoose_1 = __importDefault(require("mongoose"));
17
+ var renameKeys = function (obj, oldName, newName) {
18
+ var keyValues = Object.keys(obj).map(function (key) {
19
+ var _a;
20
+ var newKey;
21
+ if (key === oldName) {
22
+ newKey = newName;
23
+ }
24
+ else {
25
+ newKey = key;
26
+ }
27
+ if (Array.isArray(obj[key])) {
28
+ obj[key] = obj[key].map(function (obj) { return (0, exports.renameKeys)(obj, oldName, newName); });
29
+ }
30
+ if ((obj[key] instanceof Object) && !(obj[key] instanceof Date) && !(obj[key] instanceof mongoose_1.default.Types.ObjectId)) {
31
+ obj[key] = (0, exports.renameKeys)(obj[key], oldName, newName);
32
+ }
33
+ return _a = {},
34
+ _a[newKey] = obj[key],
35
+ _a;
36
+ });
37
+ return Object.assign.apply(Object, __spreadArray([{}], keyValues, false));
38
+ };
39
+ exports.renameKeys = renameKeys;
40
+ var normalizeObject = function (obj, oldName, newName) {
41
+ if (oldName === void 0) { oldName = '_id'; }
42
+ if (newName === void 0) { newName = 'id'; }
43
+ var keyValues = Object.keys(obj).map(function (key) {
44
+ var _a, _b, _c;
45
+ if (key === '__v') {
46
+ delete obj[key];
47
+ return;
48
+ }
49
+ var newKey;
50
+ if (key === oldName) {
51
+ newKey = newName;
52
+ }
53
+ else {
54
+ newKey = key;
55
+ }
56
+ if (Array.isArray(obj[key])) {
57
+ obj[key] = obj[key].map(function (nestedObj) { return (0, exports.normalizeObject)(nestedObj, oldName, newName); });
58
+ return _a = {}, _a[newKey] = obj[key], _a;
59
+ }
60
+ if ((obj[key] instanceof Object) && !(obj[key] instanceof Date) && !(obj[key] instanceof mongoose_1.default.Types.ObjectId)) {
61
+ obj[key] = (0, exports.normalizeObject)(obj[key], oldName, newName);
62
+ return _b = {}, _b[newKey] = obj[key], _b;
63
+ }
64
+ return _c = {},
65
+ _c[newKey] = obj[key],
66
+ _c;
67
+ });
68
+ return Object.assign.apply(Object, __spreadArray([{}], keyValues, false));
69
+ };
70
+ exports.normalizeObject = normalizeObject;
71
+ var mongoValidator = function (id) {
72
+ if (mongoose_1.default.Types.ObjectId.isValid(id)) {
73
+ return (String)(new mongoose_1.default.Types.ObjectId(id)) === id;
74
+ }
75
+ return false;
76
+ };
77
+ var isValidId = function (id) {
78
+ if (!id) {
79
+ return false;
80
+ }
81
+ if (typeof id !== 'string') {
82
+ id = id.toString();
83
+ }
84
+ var regex = new RegExp(/^[a-f\d]{24}$/i);
85
+ var regexResult = regex.test(id);
86
+ return regexResult && mongoValidator(id);
87
+ };
88
+ exports.isValidId = isValidId;
@@ -0,0 +1,68 @@
1
+ import mongoose from 'mongoose';
2
+
3
+ export const renameKeys = (obj, oldName, newName) => {
4
+ const keyValues = Object.keys(obj).map((key) => {
5
+ let newKey;
6
+ if (key === oldName) {
7
+ newKey = newName;
8
+ } else {
9
+ newKey = key;
10
+ }
11
+ if (Array.isArray(obj[key])) {
12
+ obj[key] = obj[key].map(obj => renameKeys(obj, oldName, newName));
13
+ }
14
+ if ((obj[key] instanceof Object) && !(obj[key] instanceof Date) && !(obj[key] instanceof mongoose.Types.ObjectId)) {
15
+ obj[key] = renameKeys(obj[key], oldName, newName);
16
+ }
17
+ return {
18
+ [newKey]: obj[key]
19
+ };
20
+ });
21
+ return Object.assign({}, ...keyValues);
22
+ }
23
+
24
+ export const normalizeObject = (obj, oldName = '_id', newName = 'id') => {
25
+ const keyValues = Object.keys(obj).map((key) => {
26
+ if (key === '__v') {
27
+ delete obj[key];
28
+ return;
29
+ }
30
+ let newKey;
31
+ if (key === oldName) {
32
+ newKey = newName;
33
+ } else {
34
+ newKey = key;
35
+ }
36
+ if (Array.isArray(obj[key])) {
37
+ obj[key] = obj[key].map((nestedObj) => normalizeObject(nestedObj, oldName, newName));
38
+ return { [newKey]: obj[key] }
39
+ }
40
+ if ((obj[key] instanceof Object) && !(obj[key] instanceof Date) && !(obj[key] instanceof mongoose.Types.ObjectId)) {
41
+ obj[key] = normalizeObject(obj[key], oldName, newName);
42
+ return { [newKey]: obj[key] }
43
+ }
44
+ return {
45
+ [newKey]: obj[key]
46
+ };
47
+ });
48
+ return Object.assign({}, ...keyValues);
49
+ }
50
+
51
+ const mongoValidator = (id: string) => {
52
+ if (mongoose.Types.ObjectId.isValid(id)) {
53
+ return (String)(new mongoose.Types.ObjectId(id)) === id;
54
+ }
55
+ return false;
56
+ }
57
+
58
+ export const isValidId = (id: string | mongoose.Types.ObjectId) => {
59
+ if (!id) {
60
+ return false;
61
+ }
62
+ if (typeof id !== 'string') {
63
+ id = id.toString();
64
+ }
65
+ const regex = new RegExp(/^[a-f\d]{24}$/i);
66
+ const regexResult = regex.test(id);
67
+ return regexResult && mongoValidator(id);
68
+ }
@@ -0,0 +1,2 @@
1
+ import { IPaginatorOptions } from '../interfaces';
2
+ export declare const getPaginator: (request: any) => IPaginatorOptions;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.getPaginator = void 0;
15
+ var interfaces_1 = require("../interfaces");
16
+ var getPaginator = function (request) {
17
+ var paginator = __assign({}, interfaces_1.DEFAULT_PAGINATOR);
18
+ if (request.pageIndex) {
19
+ paginator.pageIndex = +request.pageIndex;
20
+ }
21
+ if (request.pageSize) {
22
+ paginator.pageSize = +request.pageSize;
23
+ }
24
+ return paginator;
25
+ };
26
+ exports.getPaginator = getPaginator;
@@ -0,0 +1,12 @@
1
+ import { DEFAULT_PAGINATOR, IPaginatorOptions } from '../interfaces';
2
+
3
+ export const getPaginator = (request): IPaginatorOptions => {
4
+ const paginator = { ...DEFAULT_PAGINATOR };
5
+ if (request.pageIndex) {
6
+ paginator.pageIndex = +request.pageIndex;
7
+ }
8
+ if (request.pageSize) {
9
+ paginator.pageSize = +request.pageSize;
10
+ }
11
+ return paginator;
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gemcap-be-common",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,11 +10,14 @@
10
10
  "author": "Nick Elovsky",
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
+ "dayjs": "^1.11.9",
13
14
  "express": "^4.18.2",
15
+ "lodash": "^4.17.21",
14
16
  "mongoose": "^6.10.5"
15
17
  },
16
18
  "devDependencies": {
17
19
  "@types/express": "^4.17.17",
20
+ "@types/lodash": "^4.14.196",
18
21
  "typescript": "^5.1.6"
19
22
  }
20
23
  }