@thejob/util 1.0.16 → 1.0.18
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/dist/cjs/date/utils.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import { DateRangeOption } from "@thejob/schema";
|
|
2
2
|
import dayjs, { Dayjs } from "dayjs";
|
|
3
|
+
export type DateAgoUnit = "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years";
|
|
4
|
+
export interface DateAgoResult {
|
|
5
|
+
value: number;
|
|
6
|
+
unit: DateAgoUnit;
|
|
7
|
+
isPast: boolean;
|
|
8
|
+
}
|
|
3
9
|
export declare const toDayJS: ({ date, dateFormat, strict, utc, }?: {
|
|
4
10
|
date?: string | number | Dayjs;
|
|
5
11
|
dateFormat?: string;
|
|
6
12
|
strict?: boolean;
|
|
7
13
|
utc?: boolean;
|
|
8
14
|
}) => dayjs.Dayjs;
|
|
9
|
-
export declare const dateAgo: (value: number | string | Date) =>
|
|
15
|
+
export declare const dateAgo: (value: number | string | Date) => DateAgoResult;
|
|
10
16
|
export declare const dateRangeOptionToDateRange: (option: DateRangeOption) => {
|
|
11
17
|
startDate: string | undefined;
|
|
12
18
|
endDate: string | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/date/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAoB,MAAM,gBAAgB,CAAC;AACnE,OAAO,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAQrC,eAAO,MAAM,OAAO,GAAI,qCAKrB;IACD,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,GAAG,CAAC,EAAE,OAAO,CAAC;CACV,gBASL,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,OAAO,MAAM,GAAG,MAAM,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/date/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAoB,MAAM,gBAAgB,CAAC;AACnE,OAAO,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAQrC,MAAM,MAAM,WAAW,GACnB,SAAS,GACT,SAAS,GACT,OAAO,GACP,MAAM,GACN,OAAO,GACP,QAAQ,GACR,OAAO,CAAC;AAEZ,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,WAAW,CAAC;IAClB,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,eAAO,MAAM,OAAO,GAAI,qCAKrB;IACD,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,GAAG,CAAC,EAAE,OAAO,CAAC;CACV,gBASL,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,OAAO,MAAM,GAAG,MAAM,GAAG,IAAI,KAAG,aA6BvD,CAAC;AAEF,eAAO,MAAM,0BAA0B,GAAI,QAAQ,eAAe;;;CA4CjE,CAAC"}
|
package/dist/cjs/date/utils.js
CHANGED
|
@@ -20,7 +20,38 @@ const toDayJS = ({ date, dateFormat = schema_1.SystemDateFormat, strict = false,
|
|
|
20
20
|
};
|
|
21
21
|
exports.toDayJS = toDayJS;
|
|
22
22
|
const dateAgo = (value) => {
|
|
23
|
-
|
|
23
|
+
const now = (0, dayjs_1.default)();
|
|
24
|
+
const date = (0, dayjs_1.default)(value);
|
|
25
|
+
const isPast = date.isBefore(now);
|
|
26
|
+
const diff = isPast ? now.diff(date) : date.diff(now);
|
|
27
|
+
const seconds = Math.floor(diff / 1000);
|
|
28
|
+
const minutes = Math.floor(seconds / 60);
|
|
29
|
+
const hours = Math.floor(minutes / 60);
|
|
30
|
+
const days = Math.floor(hours / 24);
|
|
31
|
+
const weeks = Math.floor(days / 7);
|
|
32
|
+
const months = Math.floor(days / 30);
|
|
33
|
+
const years = Math.floor(days / 365);
|
|
34
|
+
if (years > 0) {
|
|
35
|
+
return { value: years, unit: "years", isPast };
|
|
36
|
+
}
|
|
37
|
+
else if (months > 0) {
|
|
38
|
+
return { value: months, unit: "months", isPast };
|
|
39
|
+
}
|
|
40
|
+
else if (weeks > 0) {
|
|
41
|
+
return { value: weeks, unit: "weeks", isPast };
|
|
42
|
+
}
|
|
43
|
+
else if (days > 0) {
|
|
44
|
+
return { value: days, unit: "days", isPast };
|
|
45
|
+
}
|
|
46
|
+
else if (hours > 0) {
|
|
47
|
+
return { value: hours, unit: "hours", isPast };
|
|
48
|
+
}
|
|
49
|
+
else if (minutes > 0) {
|
|
50
|
+
return { value: minutes, unit: "minutes", isPast };
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return { value: seconds, unit: "seconds", isPast };
|
|
54
|
+
}
|
|
24
55
|
};
|
|
25
56
|
exports.dateAgo = dateAgo;
|
|
26
57
|
const dateRangeOptionToDateRange = (option) => {
|
package/dist/esm/date/utils.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import { DateRangeOption } from "@thejob/schema";
|
|
2
2
|
import dayjs, { Dayjs } from "dayjs";
|
|
3
|
+
export type DateAgoUnit = "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years";
|
|
4
|
+
export interface DateAgoResult {
|
|
5
|
+
value: number;
|
|
6
|
+
unit: DateAgoUnit;
|
|
7
|
+
isPast: boolean;
|
|
8
|
+
}
|
|
3
9
|
export declare const toDayJS: ({ date, dateFormat, strict, utc, }?: {
|
|
4
10
|
date?: string | number | Dayjs;
|
|
5
11
|
dateFormat?: string;
|
|
6
12
|
strict?: boolean;
|
|
7
13
|
utc?: boolean;
|
|
8
14
|
}) => dayjs.Dayjs;
|
|
9
|
-
export declare const dateAgo: (value: number | string | Date) =>
|
|
15
|
+
export declare const dateAgo: (value: number | string | Date) => DateAgoResult;
|
|
10
16
|
export declare const dateRangeOptionToDateRange: (option: DateRangeOption) => {
|
|
11
17
|
startDate: string | undefined;
|
|
12
18
|
endDate: string | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/date/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAoB,MAAM,gBAAgB,CAAC;AACnE,OAAO,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAQrC,eAAO,MAAM,OAAO,GAAI,qCAKrB;IACD,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,GAAG,CAAC,EAAE,OAAO,CAAC;CACV,gBASL,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,OAAO,MAAM,GAAG,MAAM,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/date/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAoB,MAAM,gBAAgB,CAAC;AACnE,OAAO,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAQrC,MAAM,MAAM,WAAW,GACnB,SAAS,GACT,SAAS,GACT,OAAO,GACP,MAAM,GACN,OAAO,GACP,QAAQ,GACR,OAAO,CAAC;AAEZ,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,WAAW,CAAC;IAClB,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,eAAO,MAAM,OAAO,GAAI,qCAKrB;IACD,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,GAAG,CAAC,EAAE,OAAO,CAAC;CACV,gBASL,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,OAAO,MAAM,GAAG,MAAM,GAAG,IAAI,KAAG,aA6BvD,CAAC;AAEF,eAAO,MAAM,0BAA0B,GAAI,QAAQ,eAAe;;;CA4CjE,CAAC"}
|
package/dist/esm/date/utils.js
CHANGED
|
@@ -13,7 +13,38 @@ export const toDayJS = ({ date, dateFormat = SystemDateFormat, strict = false, u
|
|
|
13
13
|
}, strict);
|
|
14
14
|
};
|
|
15
15
|
export const dateAgo = (value) => {
|
|
16
|
-
|
|
16
|
+
const now = dayjs();
|
|
17
|
+
const date = dayjs(value);
|
|
18
|
+
const isPast = date.isBefore(now);
|
|
19
|
+
const diff = isPast ? now.diff(date) : date.diff(now);
|
|
20
|
+
const seconds = Math.floor(diff / 1000);
|
|
21
|
+
const minutes = Math.floor(seconds / 60);
|
|
22
|
+
const hours = Math.floor(minutes / 60);
|
|
23
|
+
const days = Math.floor(hours / 24);
|
|
24
|
+
const weeks = Math.floor(days / 7);
|
|
25
|
+
const months = Math.floor(days / 30);
|
|
26
|
+
const years = Math.floor(days / 365);
|
|
27
|
+
if (years > 0) {
|
|
28
|
+
return { value: years, unit: "years", isPast };
|
|
29
|
+
}
|
|
30
|
+
else if (months > 0) {
|
|
31
|
+
return { value: months, unit: "months", isPast };
|
|
32
|
+
}
|
|
33
|
+
else if (weeks > 0) {
|
|
34
|
+
return { value: weeks, unit: "weeks", isPast };
|
|
35
|
+
}
|
|
36
|
+
else if (days > 0) {
|
|
37
|
+
return { value: days, unit: "days", isPast };
|
|
38
|
+
}
|
|
39
|
+
else if (hours > 0) {
|
|
40
|
+
return { value: hours, unit: "hours", isPast };
|
|
41
|
+
}
|
|
42
|
+
else if (minutes > 0) {
|
|
43
|
+
return { value: minutes, unit: "minutes", isPast };
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return { value: seconds, unit: "seconds", isPast };
|
|
47
|
+
}
|
|
17
48
|
};
|
|
18
49
|
export const dateRangeOptionToDateRange = (option) => {
|
|
19
50
|
const today = toDayJS({ utc: true });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thejob/util",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.18",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/esm/index.d.ts",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"typescript": "^5.7.3"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@thejob/schema": "^1.0.
|
|
37
|
+
"@thejob/schema": "^1.0.67",
|
|
38
38
|
"dayjs": "^1.11.13",
|
|
39
39
|
"mongoose": "^8.11.0",
|
|
40
40
|
"nanoid": "3.3.5",
|