ia-common 1.1.1-beta.20 → 1.1.1-beta.22
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/build/src/enum/error.key.enum.d.ts +2 -1
- package/build/src/enum/error.key.enum.js +1 -0
- package/build/src/utils/date-util.d.ts +1 -0
- package/build/src/utils/date-util.js +44 -0
- package/build/src/utils/helper.fn.utils.d.ts +1 -0
- package/build/src/utils/helper.fn.utils.js +5 -1
- package/package.json +1 -1
|
@@ -19,5 +19,6 @@ export declare class DateUtil {
|
|
|
19
19
|
};
|
|
20
20
|
dateCodeFormatter(dateCode: string, formatter: string): string;
|
|
21
21
|
epochToDate(timestamp: number): Date;
|
|
22
|
+
getClosestEntityByDateCode<T extends Record<string, any>>(entities: T[], dateProperty: keyof T, targetDateCode: string): T | null;
|
|
22
23
|
}
|
|
23
24
|
export declare const dateUtil: DateUtil;
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.dateUtil = exports.DateUtil = void 0;
|
|
4
4
|
const date_fns_1 = require("date-fns");
|
|
5
|
+
const _enum_1 = require("../@enum");
|
|
6
|
+
const model_1 = require("../model");
|
|
5
7
|
class DateUtil {
|
|
6
8
|
constructor() {
|
|
7
9
|
this.convertMonthsToNumber = (months) => {
|
|
@@ -89,6 +91,48 @@ class DateUtil {
|
|
|
89
91
|
epochToDate(timestamp) {
|
|
90
92
|
return new Date(timestamp);
|
|
91
93
|
}
|
|
94
|
+
getClosestEntityByDateCode(entities, dateProperty, targetDateCode) {
|
|
95
|
+
// validate target date first
|
|
96
|
+
if (!DateUtil.isValidDate(targetDateCode)) {
|
|
97
|
+
throw new model_1.AppBadRequestException({
|
|
98
|
+
key: _enum_1.ErrorKeyEnum.DATE_CODE,
|
|
99
|
+
message: [`Invalid targetDateCode: ${targetDateCode}`],
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (!(entities === null || entities === void 0 ? void 0 : entities.length))
|
|
103
|
+
return null;
|
|
104
|
+
let closestPast = null;
|
|
105
|
+
let closestFuture = null;
|
|
106
|
+
for (const entity of entities) {
|
|
107
|
+
const rawDateCode = entity[dateProperty];
|
|
108
|
+
if (rawDateCode == null)
|
|
109
|
+
continue;
|
|
110
|
+
const entityDateCode = String(rawDateCode);
|
|
111
|
+
// validate entity date code
|
|
112
|
+
if (!DateUtil.isValidDate(entityDateCode)) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
// exact match
|
|
116
|
+
if (entityDateCode === targetDateCode) {
|
|
117
|
+
return entity;
|
|
118
|
+
}
|
|
119
|
+
// past date
|
|
120
|
+
if (entityDateCode < targetDateCode) {
|
|
121
|
+
if (!closestPast ||
|
|
122
|
+
entityDateCode > String(closestPast[dateProperty])) {
|
|
123
|
+
closestPast = entity;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// future date
|
|
127
|
+
if (entityDateCode > targetDateCode) {
|
|
128
|
+
if (!closestFuture ||
|
|
129
|
+
entityDateCode < String(closestFuture[dateProperty])) {
|
|
130
|
+
closestFuture = entity;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return closestPast !== null && closestPast !== void 0 ? closestPast : closestFuture;
|
|
135
|
+
}
|
|
92
136
|
}
|
|
93
137
|
exports.DateUtil = DateUtil;
|
|
94
138
|
exports.dateUtil = new DateUtil();
|
|
@@ -113,3 +113,4 @@ export declare function getPropertyFilterByPermissionFn<T extends EntityEnum>(pe
|
|
|
113
113
|
* - For objects, duplicates are based on **reference equality**, not deep equality.
|
|
114
114
|
*/
|
|
115
115
|
export declare function getDuplicates<T>(arr: T[]): T[];
|
|
116
|
+
export declare function getConsoleLog<T extends Record<string, any>>(entity: T, propertyName: keyof T): void;
|
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.getDuplicates = exports.getPropertyFilterByPermissionFn = exports.groupByOneToOneFunction = exports.deepMerge = exports.toDateFromEpoch = exports.groupByFunction = exports.transformDate = exports.getStringValues = exports.getTodayISTEpoch = void 0;
|
|
12
|
+
exports.getConsoleLog = exports.getDuplicates = exports.getPropertyFilterByPermissionFn = exports.groupByOneToOneFunction = exports.deepMerge = exports.toDateFromEpoch = exports.groupByFunction = exports.transformDate = exports.getStringValues = exports.getTodayISTEpoch = void 0;
|
|
13
13
|
const _type_1 = require("../@type");
|
|
14
14
|
/**
|
|
15
15
|
* Returns epoch time (ms) for today's date at the given local time (IST).
|
|
@@ -222,3 +222,7 @@ function getDuplicates(arr) {
|
|
|
222
222
|
return duplicates;
|
|
223
223
|
}
|
|
224
224
|
exports.getDuplicates = getDuplicates;
|
|
225
|
+
function getConsoleLog(entity, propertyName) {
|
|
226
|
+
console.log(propertyName, entity[propertyName]);
|
|
227
|
+
}
|
|
228
|
+
exports.getConsoleLog = getConsoleLog;
|