@8ms/helpers 2.0.12 → 2.0.13
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/.yarn/install-state.gz +0 -0
- package/date/calculation.d.ts +13 -0
- package/date/calculation.js +38 -2
- package/package.json +1 -1
package/.yarn/install-state.gz
CHANGED
|
Binary file
|
package/date/calculation.d.ts
CHANGED
|
@@ -4,12 +4,25 @@ import { InputDate, Timeframe } from "./";
|
|
|
4
4
|
* Convert an client's input (any time-zone) ready to be used by the server (UTC).
|
|
5
5
|
*/
|
|
6
6
|
export declare const getUtcDate: (input: InputDate, inputZone?: Zone) => DateTime<boolean>;
|
|
7
|
+
export declare const getUnix: (input?: InputDate) => number;
|
|
7
8
|
/**
|
|
8
9
|
* Assume the input is always UTC.
|
|
9
10
|
*
|
|
10
11
|
* If it's not UTC, it needs to be converted to UTC before being used.
|
|
11
12
|
*/
|
|
12
13
|
export declare const getDate: (input?: InputDate, setMidnight?: boolean) => DateTime<boolean>;
|
|
14
|
+
/**
|
|
15
|
+
* Determines if a given date falls on a weekend.
|
|
16
|
+
*
|
|
17
|
+
* This function checks whether the day of the week for the provided input corresponds
|
|
18
|
+
* to Saturday or Sunday, where weekday values of 6 and 7 represent Saturday and Sunday, respectively.
|
|
19
|
+
*/
|
|
20
|
+
export declare const isWeekend: (input: InputDate) => boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Calculates the difference in business days between two dates.
|
|
23
|
+
* Business days are considered as weekdays (Monday to Friday).
|
|
24
|
+
*/
|
|
25
|
+
export declare const differenceInBusinessDays: (start: InputDate, end: InputDate) => number;
|
|
13
26
|
/**
|
|
14
27
|
* Similar to date-fns' eachDayOfInterval, however the timezone messes this function up.
|
|
15
28
|
* https://date-fns.org/v2.28.0/docs/eachDayOfInterval
|
package/date/calculation.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isLastWeek = exports.isThisWeek = exports.getYesterday = exports.getWeeksAgo = exports.getToday = exports.getTwoWeeksAgo = exports.getThisWeek = exports.getLastWeek = exports.getSunday = exports.getMonday = exports.getMin = exports.getMax = exports.getDatesBetween = exports.getDate = exports.getUtcDate = void 0;
|
|
3
|
+
exports.isLastWeek = exports.isThisWeek = exports.getYesterday = exports.getWeeksAgo = exports.getToday = exports.getTwoWeeksAgo = exports.getThisWeek = exports.getLastWeek = exports.getSunday = exports.getMonday = exports.getMin = exports.getMax = exports.getDatesBetween = exports.differenceInBusinessDays = exports.isWeekend = exports.getDate = exports.getUnix = exports.getUtcDate = void 0;
|
|
4
4
|
const luxon_1 = require("luxon");
|
|
5
5
|
/**
|
|
6
6
|
* Convert an client's input (any time-zone) ready to be used by the server (UTC).
|
|
@@ -24,6 +24,11 @@ const getUtcDate = (input, inputZone) => {
|
|
|
24
24
|
return instance.toUTC();
|
|
25
25
|
};
|
|
26
26
|
exports.getUtcDate = getUtcDate;
|
|
27
|
+
const getUnix = (input) => {
|
|
28
|
+
return (0, exports.getDate)(input)
|
|
29
|
+
.toUnixInteger();
|
|
30
|
+
};
|
|
31
|
+
exports.getUnix = getUnix;
|
|
27
32
|
/**
|
|
28
33
|
* Assume the input is always UTC.
|
|
29
34
|
*
|
|
@@ -33,7 +38,8 @@ const getDate = (input, setMidnight) => {
|
|
|
33
38
|
let instance = luxon_1.DateTime.now();
|
|
34
39
|
if (input) {
|
|
35
40
|
if (input instanceof Date) {
|
|
36
|
-
instance = luxon_1.DateTime.fromJSDate(input)
|
|
41
|
+
instance = luxon_1.DateTime.fromJSDate(input)
|
|
42
|
+
.toUTC();
|
|
37
43
|
}
|
|
38
44
|
else if (input instanceof luxon_1.DateTime) {
|
|
39
45
|
instance = input.toUTC();
|
|
@@ -57,6 +63,36 @@ const getDate = (input, setMidnight) => {
|
|
|
57
63
|
return instance;
|
|
58
64
|
};
|
|
59
65
|
exports.getDate = getDate;
|
|
66
|
+
/**
|
|
67
|
+
* Determines if a given date falls on a weekend.
|
|
68
|
+
*
|
|
69
|
+
* This function checks whether the day of the week for the provided input corresponds
|
|
70
|
+
* to Saturday or Sunday, where weekday values of 6 and 7 represent Saturday and Sunday, respectively.
|
|
71
|
+
*/
|
|
72
|
+
const isWeekend = (input) => {
|
|
73
|
+
const date = (0, exports.getDate)(input);
|
|
74
|
+
return date.weekday >= 6;
|
|
75
|
+
};
|
|
76
|
+
exports.isWeekend = isWeekend;
|
|
77
|
+
/**
|
|
78
|
+
* Calculates the difference in business days between two dates.
|
|
79
|
+
* Business days are considered as weekdays (Monday to Friday).
|
|
80
|
+
*/
|
|
81
|
+
const differenceInBusinessDays = (start, end) => {
|
|
82
|
+
const startDate = (0, exports.getDate)(start);
|
|
83
|
+
const endDate = (0, exports.getDate)(end);
|
|
84
|
+
let businessDays = 0;
|
|
85
|
+
let current = startDate.startOf("day");
|
|
86
|
+
while (current < endDate.startOf("day")) {
|
|
87
|
+
// weekday 1-5 are Monday-Friday (business days)
|
|
88
|
+
if (current.weekday <= 5) {
|
|
89
|
+
businessDays++;
|
|
90
|
+
}
|
|
91
|
+
current = current.plus({ days: 1 });
|
|
92
|
+
}
|
|
93
|
+
return businessDays;
|
|
94
|
+
};
|
|
95
|
+
exports.differenceInBusinessDays = differenceInBusinessDays;
|
|
60
96
|
/**
|
|
61
97
|
* Similar to date-fns' eachDayOfInterval, however the timezone messes this function up.
|
|
62
98
|
* https://date-fns.org/v2.28.0/docs/eachDayOfInterval
|