ia-common 1.1.1-beta.21 → 1.1.1-beta.23
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.
|
@@ -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();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ia-common",
|
|
3
|
-
"version": "1.1.1-beta.
|
|
3
|
+
"version": "1.1.1-beta.23",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
17
17
|
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" 2>&1| tee eslint.log",
|
|
18
18
|
"lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
|
19
|
-
"link": "npm run build && npm link"
|
|
19
|
+
"link": "npm run build && npm link",
|
|
20
|
+
"pull:link": "git pull && npm run link"
|
|
20
21
|
},
|
|
21
22
|
"repository": {
|
|
22
23
|
"type": "git",
|