@adobe/spacecat-shared-utils 1.42.4 → 1.43.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/spacecat-shared-utils-v1.43.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.42.4...@adobe/spacecat-shared-utils-v1.43.0) (2025-07-23)
2
+
3
+
4
+ ### Features
5
+
6
+ * add shared get-calendar-week to shared ([#865](https://github.com/adobe/spacecat-shared/issues/865)) ([f44c57f](https://github.com/adobe/spacecat-shared/commit/f44c57f8d0517af7a2c95f41e097f866e2722651))
7
+
1
8
  # [@adobe/spacecat-shared-utils-v1.42.4](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.42.3...@adobe/spacecat-shared-utils-v1.42.4) (2025-07-19)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.42.4",
3
+ "version": "1.43.0",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "engines": {
@@ -0,0 +1,121 @@
1
+ /*
2
+ * Copyright 2025 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ const MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000;
13
+ const MILLISECONDS_IN_A_WEEK = 7 * MILLISECONDS_IN_A_DAY;
14
+
15
+ const getFirstMondayOfYear = (year) => {
16
+ const jan4 = new Date(Date.UTC(year, 0, 4));
17
+ return new Date(Date.UTC(year, 0, 4 - (jan4.getUTCDay() || 7) + 1));
18
+ };
19
+
20
+ const getThursdayOfWeek = (date) => {
21
+ const thursday = new Date(date.getTime());
22
+ thursday.setUTCDate(date.getUTCDate() + 4 - (date.getUTCDay() || 7));
23
+ return thursday;
24
+ };
25
+
26
+ const has53CalendarWeeks = (year) => {
27
+ const lastDayOfYear = new Date(Date.UTC(year, 11, 31));
28
+ const lastThursday = getThursdayOfWeek(lastDayOfYear);
29
+ const firstMonday = getFirstMondayOfYear(year);
30
+ const thursdayOfFirstWeek = new Date(firstMonday.getTime() + 3 * MILLISECONDS_IN_A_DAY);
31
+
32
+ const maxWeek = Math.ceil((lastThursday.getTime() - thursdayOfFirstWeek.getTime())
33
+ / MILLISECONDS_IN_A_WEEK) + 1;
34
+
35
+ return maxWeek === 53;
36
+ };
37
+
38
+ const isValidWeek = (week, year) => {
39
+ if (year < 100 || week < 1) return false;
40
+ if (week === 53) return has53CalendarWeeks(year);
41
+ return week <= 52;
42
+ };
43
+
44
+ const getLastFullCalendarWeek = () => {
45
+ const currentDate = new Date();
46
+ currentDate.setUTCHours(0, 0, 0, 0);
47
+
48
+ const previousWeekDate = new Date(currentDate.getTime() - MILLISECONDS_IN_A_WEEK);
49
+
50
+ const thursdayOfPreviousWeek = getThursdayOfWeek(previousWeekDate);
51
+ const year = thursdayOfPreviousWeek.getUTCFullYear();
52
+
53
+ const firstMonday = getFirstMondayOfYear(year);
54
+ const thursdayOfFirstWeek = new Date(firstMonday.getTime() + 3 * MILLISECONDS_IN_A_DAY);
55
+
56
+ const week = Math.ceil((thursdayOfPreviousWeek.getTime() - thursdayOfFirstWeek.getTime())
57
+ / MILLISECONDS_IN_A_WEEK) + 1;
58
+
59
+ return { week, year };
60
+ };
61
+
62
+ export function getDateRanges(week, year) {
63
+ let effectiveWeek = week;
64
+ let effectiveYear = year;
65
+
66
+ if (!isValidWeek(effectiveWeek, effectiveYear)) {
67
+ const lastFullWeek = getLastFullCalendarWeek();
68
+ effectiveWeek = lastFullWeek.week;
69
+ effectiveYear = lastFullWeek.year;
70
+ }
71
+
72
+ const firstMonday = getFirstMondayOfYear(effectiveYear);
73
+ const startDate = new Date(firstMonday.getTime() + (effectiveWeek - 1) * MILLISECONDS_IN_A_WEEK);
74
+ const endDate = new Date(startDate.getTime() + 6 * MILLISECONDS_IN_A_DAY);
75
+ endDate.setUTCHours(23, 59, 59, 999);
76
+ const startMonth = startDate.getUTCMonth() + 1;
77
+ const endMonth = endDate.getUTCMonth() + 1;
78
+ const startYear = startDate.getUTCFullYear();
79
+
80
+ if (startMonth === endMonth) {
81
+ return [{
82
+ year: startYear,
83
+ month: startMonth,
84
+ startTime: startDate.toISOString(),
85
+ endTime: endDate.toISOString(),
86
+ }];
87
+ }
88
+
89
+ const endYear = endDate.getUTCFullYear();
90
+
91
+ const endOfFirstMonth = new Date(Date.UTC(
92
+ startYear,
93
+ startDate.getUTCMonth() + 1,
94
+ 0,
95
+ 23,
96
+ 59,
97
+ 59,
98
+ 999,
99
+ )).toISOString();
100
+
101
+ const startOfSecondMonth = new Date(Date.UTC(
102
+ endYear,
103
+ endDate.getUTCMonth(),
104
+ 1,
105
+ )).toISOString();
106
+
107
+ return [
108
+ {
109
+ year: startYear,
110
+ month: startMonth,
111
+ startTime: startDate.toISOString(),
112
+ endTime: endOfFirstMonth,
113
+ },
114
+ {
115
+ year: endYear,
116
+ month: endMonth,
117
+ startTime: startOfSecondMonth,
118
+ endTime: endDate.toISOString(),
119
+ },
120
+ ];
121
+ }
package/src/index.js CHANGED
@@ -77,3 +77,5 @@ export {
77
77
  } from './formcalc.js';
78
78
 
79
79
  export { retrievePageAuthentication, getAccessToken } from './auth.js';
80
+
81
+ export { getDateRanges } from './calendar-week-helper.js';