@microsoft/feature-management 2.1.0 → 2.3.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/dist/commonjs/common/targetingEvaluator.js.map +1 -1
- package/dist/commonjs/featureManager.js +5 -5
- package/dist/commonjs/featureManager.js.map +1 -1
- package/dist/commonjs/featureProvider.js.map +1 -1
- package/dist/commonjs/filter/recurrence/evaluator.js +142 -0
- package/dist/commonjs/filter/recurrence/evaluator.js.map +1 -0
- package/dist/commonjs/filter/recurrence/model.js +52 -0
- package/dist/commonjs/filter/recurrence/model.js.map +1 -0
- package/dist/commonjs/filter/recurrence/utils.js +53 -0
- package/dist/commonjs/filter/recurrence/utils.js.map +1 -0
- package/dist/commonjs/filter/recurrence/validator.js +197 -0
- package/dist/commonjs/filter/recurrence/validator.js.map +1 -0
- package/dist/commonjs/filter/{TargetingFilter.js → targetingFilter.js} +1 -1
- package/dist/commonjs/filter/{TargetingFilter.js.map → targetingFilter.js.map} +1 -1
- package/dist/commonjs/filter/timeWindowFilter.js +49 -0
- package/dist/commonjs/filter/timeWindowFilter.js.map +1 -0
- package/dist/commonjs/filter/utils.js +16 -0
- package/dist/commonjs/filter/utils.js.map +1 -0
- package/dist/commonjs/schema/model.js.map +1 -1
- package/dist/commonjs/schema/validator.js.map +1 -1
- package/dist/commonjs/telemetry/featureEvaluationEvent.js.map +1 -1
- package/dist/commonjs/variant/{Variant.js → variant.js} +1 -1
- package/dist/commonjs/variant/variant.js.map +1 -0
- package/dist/commonjs/version.js +1 -1
- package/dist/commonjs/version.js.map +1 -1
- package/dist/esm/common/targetingEvaluator.js.map +1 -1
- package/dist/esm/featureManager.js +3 -3
- package/dist/esm/featureManager.js.map +1 -1
- package/dist/esm/featureProvider.js.map +1 -1
- package/dist/esm/filter/recurrence/evaluator.js +140 -0
- package/dist/esm/filter/recurrence/evaluator.js.map +1 -0
- package/dist/esm/filter/recurrence/model.js +49 -0
- package/dist/esm/filter/recurrence/model.js.map +1 -0
- package/dist/esm/filter/recurrence/utils.js +48 -0
- package/dist/esm/filter/recurrence/utils.js.map +1 -0
- package/dist/esm/filter/recurrence/validator.js +184 -0
- package/dist/esm/filter/recurrence/validator.js.map +1 -0
- package/dist/esm/filter/{TargetingFilter.js → targetingFilter.js} +1 -1
- package/dist/esm/filter/{TargetingFilter.js.map → targetingFilter.js.map} +1 -1
- package/dist/esm/filter/timeWindowFilter.js +47 -0
- package/dist/esm/filter/timeWindowFilter.js.map +1 -0
- package/dist/esm/filter/utils.js +11 -0
- package/dist/esm/filter/utils.js.map +1 -0
- package/dist/esm/schema/model.js.map +1 -1
- package/dist/esm/schema/validator.js.map +1 -1
- package/dist/esm/telemetry/featureEvaluationEvent.js.map +1 -1
- package/dist/esm/variant/{Variant.js → variant.js} +1 -1
- package/dist/esm/variant/variant.js.map +1 -0
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/{types → dist/types}/index.d.ts +43 -42
- package/dist/umd/index.js +439 -3
- package/dist/umd/index.js.map +1 -1
- package/package.json +41 -26
- package/dist/commonjs/filter/TimeWindowFilter.js +0 -22
- package/dist/commonjs/filter/TimeWindowFilter.js.map +0 -1
- package/dist/commonjs/variant/Variant.js.map +0 -1
- package/dist/esm/filter/TimeWindowFilter.js +0 -20
- package/dist/esm/filter/TimeWindowFilter.js.map +0 -1
- package/dist/esm/variant/Variant.js.map +0 -1
package/dist/umd/index.js
CHANGED
|
@@ -4,6 +4,419 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FeatureManagement = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
+
// Copyright (c) Microsoft Corporation.
|
|
8
|
+
// Licensed under the MIT license.
|
|
9
|
+
const VALUE_OUT_OF_RANGE_ERROR_MESSAGE = "The value is out of the accepted range.";
|
|
10
|
+
const UNRECOGNIZABLE_VALUE_ERROR_MESSAGE = "The value is unrecognizable.";
|
|
11
|
+
const REQUIRED_PARAMETER_MISSING_ERROR_MESSAGE = "Value cannot be undefined or empty.";
|
|
12
|
+
function buildInvalidParameterErrorMessage(parameterName, additionalInfo) {
|
|
13
|
+
return `The ${parameterName} parameter is not valid. ` + (additionalInfo ?? "");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Copyright (c) Microsoft Corporation.
|
|
17
|
+
// Licensed under the MIT license.
|
|
18
|
+
const DAYS_PER_WEEK = 7;
|
|
19
|
+
const ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
|
|
20
|
+
var DayOfWeek;
|
|
21
|
+
(function (DayOfWeek) {
|
|
22
|
+
DayOfWeek[DayOfWeek["Sunday"] = 0] = "Sunday";
|
|
23
|
+
DayOfWeek[DayOfWeek["Monday"] = 1] = "Monday";
|
|
24
|
+
DayOfWeek[DayOfWeek["Tuesday"] = 2] = "Tuesday";
|
|
25
|
+
DayOfWeek[DayOfWeek["Wednesday"] = 3] = "Wednesday";
|
|
26
|
+
DayOfWeek[DayOfWeek["Thursday"] = 4] = "Thursday";
|
|
27
|
+
DayOfWeek[DayOfWeek["Friday"] = 5] = "Friday";
|
|
28
|
+
DayOfWeek[DayOfWeek["Saturday"] = 6] = "Saturday";
|
|
29
|
+
})(DayOfWeek || (DayOfWeek = {}));
|
|
30
|
+
/**
|
|
31
|
+
* The recurrence pattern describes the frequency by which the time window repeats
|
|
32
|
+
*/
|
|
33
|
+
var RecurrencePatternType;
|
|
34
|
+
(function (RecurrencePatternType) {
|
|
35
|
+
/**
|
|
36
|
+
* The pattern where the time window will repeat based on the number of days specified by interval between occurrences
|
|
37
|
+
*/
|
|
38
|
+
RecurrencePatternType[RecurrencePatternType["Daily"] = 0] = "Daily";
|
|
39
|
+
/**
|
|
40
|
+
* The pattern where the time window will repeat on the same day or days of the week, based on the number of weeks between each set of occurrences
|
|
41
|
+
*/
|
|
42
|
+
RecurrencePatternType[RecurrencePatternType["Weekly"] = 1] = "Weekly";
|
|
43
|
+
})(RecurrencePatternType || (RecurrencePatternType = {}));
|
|
44
|
+
/**
|
|
45
|
+
* The recurrence range specifies the date range over which the time window repeats
|
|
46
|
+
*/
|
|
47
|
+
var RecurrenceRangeType;
|
|
48
|
+
(function (RecurrenceRangeType) {
|
|
49
|
+
/**
|
|
50
|
+
* The recurrence has no end and repeats on all the days that fit the corresponding pattern
|
|
51
|
+
*/
|
|
52
|
+
RecurrenceRangeType[RecurrenceRangeType["NoEnd"] = 0] = "NoEnd";
|
|
53
|
+
/**
|
|
54
|
+
* The recurrence repeats on all the days that fit the corresponding pattern until or on the specified end date
|
|
55
|
+
*/
|
|
56
|
+
RecurrenceRangeType[RecurrenceRangeType["EndDate"] = 1] = "EndDate";
|
|
57
|
+
/**
|
|
58
|
+
* The recurrence repeats for the specified number of occurrences that match the pattern
|
|
59
|
+
*/
|
|
60
|
+
RecurrenceRangeType[RecurrenceRangeType["Numbered"] = 2] = "Numbered";
|
|
61
|
+
})(RecurrenceRangeType || (RecurrenceRangeType = {}));
|
|
62
|
+
|
|
63
|
+
// Copyright (c) Microsoft Corporation.
|
|
64
|
+
// Licensed under the MIT license.
|
|
65
|
+
/**
|
|
66
|
+
* Calculates the offset in days between two given days of the week.
|
|
67
|
+
* @param day1 A day of week
|
|
68
|
+
* @param day2 A day of week
|
|
69
|
+
* @returns The number of days to be added to day2 to reach day1
|
|
70
|
+
*/
|
|
71
|
+
function calculateWeeklyDayOffset(day1, day2) {
|
|
72
|
+
return (day1 - day2 + DAYS_PER_WEEK) % DAYS_PER_WEEK;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Sorts a collection of days of week based on their offsets from a specified first day of week.
|
|
76
|
+
* @param daysOfWeek A collection of days of week
|
|
77
|
+
* @param firstDayOfWeek The first day of week which will be the first element in the sorted result
|
|
78
|
+
* @returns The sorted days of week
|
|
79
|
+
*/
|
|
80
|
+
function sortDaysOfWeek(daysOfWeek, firstDayOfWeek) {
|
|
81
|
+
const sortedDaysOfWeek = daysOfWeek.slice();
|
|
82
|
+
sortedDaysOfWeek.sort((x, y) => calculateWeeklyDayOffset(x, firstDayOfWeek) - calculateWeeklyDayOffset(y, firstDayOfWeek));
|
|
83
|
+
return sortedDaysOfWeek;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Gets the day of week of a given date based on the timezone offset.
|
|
87
|
+
* @param date A UTC date
|
|
88
|
+
* @param timezoneOffsetInMs The timezone offset in milliseconds
|
|
89
|
+
* @returns The day of week (0 for Sunday, 1 for Monday, ..., 6 for Saturday)
|
|
90
|
+
*/
|
|
91
|
+
function getDayOfWeek(date, timezoneOffsetInMs) {
|
|
92
|
+
const alignedDate = new Date(date.getTime() + timezoneOffsetInMs);
|
|
93
|
+
return alignedDate.getUTCDay();
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Adds a specified number of days to a given date.
|
|
97
|
+
* @param date The date to add days to
|
|
98
|
+
* @param days The number of days to add
|
|
99
|
+
* @returns The new date
|
|
100
|
+
*/
|
|
101
|
+
function addDays(date, days) {
|
|
102
|
+
const result = new Date(date);
|
|
103
|
+
result.setDate(result.getDate() + days);
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Copyright (c) Microsoft Corporation.
|
|
108
|
+
// Licensed under the MIT license.
|
|
109
|
+
const START_NOT_MATCHED_ERROR_MESSAGE = "Start date is not a valid first occurrence.";
|
|
110
|
+
const TIME_WINDOW_DURATION_OUT_OF_RANGE_ERROR_MESSAGE = "Time window duration cannot be longer than how frequently it occurs or be longer than 10 years.";
|
|
111
|
+
const PATTERN = "Recurrence.Pattern";
|
|
112
|
+
const PATTERN_TYPE = "Recurrence.Pattern.Type";
|
|
113
|
+
const INTERVAL = "Recurrence.Pattern.Interval";
|
|
114
|
+
const DAYS_OF_WEEK = "Recurrence.Pattern.DaysOfWeek";
|
|
115
|
+
const FIRST_DAY_OF_WEEK = "Recurrence.Pattern.FirstDayOfWeek";
|
|
116
|
+
const RANGE = "Recurrence.Range";
|
|
117
|
+
const RANGE_TYPE = "Recurrence.Range.Type";
|
|
118
|
+
const END_DATE = "Recurrence.Range.EndDate";
|
|
119
|
+
const NUMBER_OF_OCCURRENCES = "Recurrence.Range.NumberOfOccurrences";
|
|
120
|
+
/**
|
|
121
|
+
* Parses @see RecurrenceParameters into a @see RecurrenceSpec object. If the parameter is invalid, an error will be thrown.
|
|
122
|
+
* @param startTime The start time of the base time window
|
|
123
|
+
* @param day2 The end time of the base time window
|
|
124
|
+
* @param recurrenceParameters The @see RecurrenceParameters to parse
|
|
125
|
+
* @param TimeZoneOffset The time zone offset in milliseconds, by default 0
|
|
126
|
+
* @returns A @see RecurrenceSpec object
|
|
127
|
+
*/
|
|
128
|
+
function parseRecurrenceParameter(startTime, endTime, recurrenceParameters, TimeZoneOffset = 0) {
|
|
129
|
+
if (startTime === undefined) {
|
|
130
|
+
throw new Error(buildInvalidParameterErrorMessage("Start", REQUIRED_PARAMETER_MISSING_ERROR_MESSAGE));
|
|
131
|
+
}
|
|
132
|
+
if (endTime === undefined) {
|
|
133
|
+
throw new Error(buildInvalidParameterErrorMessage("End", REQUIRED_PARAMETER_MISSING_ERROR_MESSAGE));
|
|
134
|
+
}
|
|
135
|
+
if (startTime >= endTime) {
|
|
136
|
+
throw new Error(buildInvalidParameterErrorMessage("End", VALUE_OUT_OF_RANGE_ERROR_MESSAGE));
|
|
137
|
+
}
|
|
138
|
+
const timeWindowDuration = endTime.getTime() - startTime.getTime();
|
|
139
|
+
if (timeWindowDuration > 10 * 365 * ONE_DAY_IN_MILLISECONDS) { // time window duration cannot be longer than 10 years
|
|
140
|
+
throw new Error(buildInvalidParameterErrorMessage("End", TIME_WINDOW_DURATION_OUT_OF_RANGE_ERROR_MESSAGE));
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
startTime: startTime,
|
|
144
|
+
duration: timeWindowDuration,
|
|
145
|
+
pattern: parseRecurrencePattern(startTime, endTime, recurrenceParameters, TimeZoneOffset),
|
|
146
|
+
range: parseRecurrenceRange(startTime, recurrenceParameters),
|
|
147
|
+
timezoneOffset: TimeZoneOffset
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function parseRecurrencePattern(startTime, endTime, recurrenceParameters, timeZoneOffset) {
|
|
151
|
+
const rawPattern = recurrenceParameters.Pattern;
|
|
152
|
+
if (rawPattern === undefined) {
|
|
153
|
+
throw new Error(buildInvalidParameterErrorMessage(PATTERN, REQUIRED_PARAMETER_MISSING_ERROR_MESSAGE));
|
|
154
|
+
}
|
|
155
|
+
if (rawPattern.Type === undefined) {
|
|
156
|
+
throw new Error(buildInvalidParameterErrorMessage(PATTERN_TYPE, REQUIRED_PARAMETER_MISSING_ERROR_MESSAGE));
|
|
157
|
+
}
|
|
158
|
+
const patternType = RecurrencePatternType[rawPattern.Type];
|
|
159
|
+
if (patternType === undefined) {
|
|
160
|
+
throw new Error(buildInvalidParameterErrorMessage(PATTERN_TYPE, UNRECOGNIZABLE_VALUE_ERROR_MESSAGE));
|
|
161
|
+
}
|
|
162
|
+
let interval = rawPattern.Interval;
|
|
163
|
+
if (interval !== undefined) {
|
|
164
|
+
if (typeof interval !== "number") {
|
|
165
|
+
throw new Error(buildInvalidParameterErrorMessage(INTERVAL, UNRECOGNIZABLE_VALUE_ERROR_MESSAGE));
|
|
166
|
+
}
|
|
167
|
+
else if (interval <= 0 || !Number.isInteger(interval)) {
|
|
168
|
+
throw new Error(buildInvalidParameterErrorMessage(INTERVAL, VALUE_OUT_OF_RANGE_ERROR_MESSAGE));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
interval = 1;
|
|
173
|
+
}
|
|
174
|
+
const parsedPattern = {
|
|
175
|
+
type: patternType,
|
|
176
|
+
interval: interval
|
|
177
|
+
};
|
|
178
|
+
const timeWindowDuration = endTime.getTime() - startTime.getTime();
|
|
179
|
+
if (patternType === RecurrencePatternType.Daily) {
|
|
180
|
+
if (timeWindowDuration > interval * ONE_DAY_IN_MILLISECONDS) {
|
|
181
|
+
throw new Error(buildInvalidParameterErrorMessage("End", TIME_WINDOW_DURATION_OUT_OF_RANGE_ERROR_MESSAGE));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else if (patternType === RecurrencePatternType.Weekly) {
|
|
185
|
+
let firstDayOfWeek;
|
|
186
|
+
if (rawPattern.FirstDayOfWeek !== undefined) {
|
|
187
|
+
firstDayOfWeek = DayOfWeek[rawPattern.FirstDayOfWeek];
|
|
188
|
+
if (firstDayOfWeek === undefined) {
|
|
189
|
+
throw new Error(buildInvalidParameterErrorMessage(FIRST_DAY_OF_WEEK, UNRECOGNIZABLE_VALUE_ERROR_MESSAGE));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
firstDayOfWeek = DayOfWeek.Sunday;
|
|
194
|
+
}
|
|
195
|
+
parsedPattern.firstDayOfWeek = firstDayOfWeek;
|
|
196
|
+
if (rawPattern.DaysOfWeek === undefined || rawPattern.DaysOfWeek.length === 0) {
|
|
197
|
+
throw new Error(buildInvalidParameterErrorMessage(DAYS_OF_WEEK, REQUIRED_PARAMETER_MISSING_ERROR_MESSAGE));
|
|
198
|
+
}
|
|
199
|
+
if (!Array.isArray(rawPattern.DaysOfWeek)) {
|
|
200
|
+
throw new Error(buildInvalidParameterErrorMessage(DAYS_OF_WEEK, UNRECOGNIZABLE_VALUE_ERROR_MESSAGE));
|
|
201
|
+
}
|
|
202
|
+
const daysOfWeek = [...new Set(rawPattern.DaysOfWeek.map(day => DayOfWeek[day]))]; // dedup array
|
|
203
|
+
if (daysOfWeek.some(day => day === undefined)) {
|
|
204
|
+
throw new Error(buildInvalidParameterErrorMessage(DAYS_OF_WEEK, UNRECOGNIZABLE_VALUE_ERROR_MESSAGE));
|
|
205
|
+
}
|
|
206
|
+
if (timeWindowDuration > interval * DAYS_PER_WEEK * ONE_DAY_IN_MILLISECONDS ||
|
|
207
|
+
!isDurationCompliantWithDaysOfWeek(timeWindowDuration, interval, daysOfWeek, firstDayOfWeek)) {
|
|
208
|
+
throw new Error(buildInvalidParameterErrorMessage("End", TIME_WINDOW_DURATION_OUT_OF_RANGE_ERROR_MESSAGE));
|
|
209
|
+
}
|
|
210
|
+
parsedPattern.daysOfWeek = daysOfWeek;
|
|
211
|
+
// check whether "Start" is a valid first occurrence
|
|
212
|
+
const alignedStartDay = getDayOfWeek(startTime, timeZoneOffset);
|
|
213
|
+
if (!daysOfWeek.find(day => day === alignedStartDay)) {
|
|
214
|
+
throw new Error(buildInvalidParameterErrorMessage("Start", START_NOT_MATCHED_ERROR_MESSAGE));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return parsedPattern;
|
|
218
|
+
}
|
|
219
|
+
function parseRecurrenceRange(startTime, recurrenceParameters) {
|
|
220
|
+
const rawRange = recurrenceParameters.Range;
|
|
221
|
+
if (rawRange === undefined) {
|
|
222
|
+
throw new Error(buildInvalidParameterErrorMessage(RANGE, REQUIRED_PARAMETER_MISSING_ERROR_MESSAGE));
|
|
223
|
+
}
|
|
224
|
+
if (rawRange.Type === undefined) {
|
|
225
|
+
throw new Error(buildInvalidParameterErrorMessage(RANGE_TYPE, REQUIRED_PARAMETER_MISSING_ERROR_MESSAGE));
|
|
226
|
+
}
|
|
227
|
+
const rangeType = RecurrenceRangeType[rawRange.Type];
|
|
228
|
+
if (rangeType === undefined) {
|
|
229
|
+
throw new Error(buildInvalidParameterErrorMessage(RANGE_TYPE, UNRECOGNIZABLE_VALUE_ERROR_MESSAGE));
|
|
230
|
+
}
|
|
231
|
+
const parsedRange = { type: rangeType };
|
|
232
|
+
if (rangeType === RecurrenceRangeType.EndDate) {
|
|
233
|
+
let endDate;
|
|
234
|
+
if (rawRange.EndDate !== undefined) {
|
|
235
|
+
endDate = new Date(rawRange.EndDate);
|
|
236
|
+
if (isNaN(endDate.getTime())) {
|
|
237
|
+
throw new Error(buildInvalidParameterErrorMessage(END_DATE, UNRECOGNIZABLE_VALUE_ERROR_MESSAGE));
|
|
238
|
+
}
|
|
239
|
+
if (endDate < startTime) {
|
|
240
|
+
throw new Error(buildInvalidParameterErrorMessage(END_DATE, VALUE_OUT_OF_RANGE_ERROR_MESSAGE));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
endDate = new Date(8.64e15); // the maximum date in ECMAScript: https://262.ecma-international.org/5.1/#sec-15.9.1.1
|
|
245
|
+
}
|
|
246
|
+
parsedRange.endDate = endDate;
|
|
247
|
+
}
|
|
248
|
+
else if (rangeType === RecurrenceRangeType.Numbered) {
|
|
249
|
+
let numberOfOccurrences = rawRange.NumberOfOccurrences;
|
|
250
|
+
if (numberOfOccurrences !== undefined) {
|
|
251
|
+
if (typeof numberOfOccurrences !== "number") {
|
|
252
|
+
throw new Error(buildInvalidParameterErrorMessage(NUMBER_OF_OCCURRENCES, UNRECOGNIZABLE_VALUE_ERROR_MESSAGE));
|
|
253
|
+
}
|
|
254
|
+
else if (numberOfOccurrences <= 0 || !Number.isInteger(numberOfOccurrences)) {
|
|
255
|
+
throw new Error(buildInvalidParameterErrorMessage(NUMBER_OF_OCCURRENCES, VALUE_OUT_OF_RANGE_ERROR_MESSAGE));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
numberOfOccurrences = Number.MAX_SAFE_INTEGER;
|
|
260
|
+
}
|
|
261
|
+
parsedRange.numberOfOccurrences = numberOfOccurrences;
|
|
262
|
+
}
|
|
263
|
+
return parsedRange;
|
|
264
|
+
}
|
|
265
|
+
function isDurationCompliantWithDaysOfWeek(duration, interval, daysOfWeek, firstDayOfWeek) {
|
|
266
|
+
if (daysOfWeek.length === 1) {
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
const sortedDaysOfWeek = sortDaysOfWeek(daysOfWeek, firstDayOfWeek);
|
|
270
|
+
let prev = sortedDaysOfWeek[0]; // the closest occurrence day to the first day of week
|
|
271
|
+
let minGap = DAYS_PER_WEEK * ONE_DAY_IN_MILLISECONDS;
|
|
272
|
+
for (let i = 1; i < sortedDaysOfWeek.length; i++) { // skip the first day
|
|
273
|
+
const gap = calculateWeeklyDayOffset(sortedDaysOfWeek[i], prev) * ONE_DAY_IN_MILLISECONDS;
|
|
274
|
+
minGap = gap < minGap ? gap : minGap;
|
|
275
|
+
prev = sortedDaysOfWeek[i];
|
|
276
|
+
}
|
|
277
|
+
// It may across weeks. Check the next week if the interval is one week.
|
|
278
|
+
if (interval == 1) {
|
|
279
|
+
const gap = calculateWeeklyDayOffset(sortedDaysOfWeek[0], prev) * ONE_DAY_IN_MILLISECONDS;
|
|
280
|
+
minGap = gap < minGap ? gap : minGap;
|
|
281
|
+
}
|
|
282
|
+
return minGap >= duration;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Copyright (c) Microsoft Corporation.
|
|
286
|
+
// Licensed under the MIT license.
|
|
287
|
+
/**
|
|
288
|
+
* Checks if a provided datetime is within any recurring time window specified by the recurrence information
|
|
289
|
+
* @param time A datetime
|
|
290
|
+
* @param recurrenceSpec The recurrence spcification
|
|
291
|
+
* @returns True if the given time is within any recurring time window; otherwise, false
|
|
292
|
+
*/
|
|
293
|
+
function matchRecurrence(time, recurrenceSpec) {
|
|
294
|
+
const recurrenceState = findPreviousRecurrence(time, recurrenceSpec);
|
|
295
|
+
if (recurrenceState) {
|
|
296
|
+
return time.getTime() < recurrenceState.previousOccurrence.getTime() + recurrenceSpec.duration;
|
|
297
|
+
}
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Finds the closest previous recurrence occurrence before the given time according to the recurrence information
|
|
302
|
+
* @param time A datetime
|
|
303
|
+
* @param recurrenceSpec The recurrence specification
|
|
304
|
+
* @returns The recurrence state if any previous occurrence is found; otherwise, undefined
|
|
305
|
+
*/
|
|
306
|
+
function findPreviousRecurrence(time, recurrenceSpec) {
|
|
307
|
+
if (time < recurrenceSpec.startTime) {
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
let result;
|
|
311
|
+
const pattern = recurrenceSpec.pattern;
|
|
312
|
+
if (pattern.type === RecurrencePatternType.Daily) {
|
|
313
|
+
result = findPreviousDailyRecurrence(time, recurrenceSpec);
|
|
314
|
+
}
|
|
315
|
+
else if (pattern.type === RecurrencePatternType.Weekly) {
|
|
316
|
+
result = findPreviousWeeklyRecurrence(time, recurrenceSpec);
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
throw new Error("Unsupported recurrence pattern type.");
|
|
320
|
+
}
|
|
321
|
+
const { previousOccurrence, numberOfOccurrences } = result;
|
|
322
|
+
const range = recurrenceSpec.range;
|
|
323
|
+
if (range.type === RecurrenceRangeType.EndDate) {
|
|
324
|
+
if (previousOccurrence > range.endDate) {
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
else if (range.type === RecurrenceRangeType.Numbered) {
|
|
329
|
+
if (numberOfOccurrences > range.numberOfOccurrences) {
|
|
330
|
+
return undefined;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return result;
|
|
334
|
+
}
|
|
335
|
+
function findPreviousDailyRecurrence(time, recurrenceSpec) {
|
|
336
|
+
const startTime = recurrenceSpec.startTime;
|
|
337
|
+
const timeGap = time.getTime() - startTime.getTime();
|
|
338
|
+
const pattern = recurrenceSpec.pattern;
|
|
339
|
+
const numberOfIntervals = Math.floor(timeGap / (pattern.interval * ONE_DAY_IN_MILLISECONDS));
|
|
340
|
+
return {
|
|
341
|
+
previousOccurrence: addDays(startTime, numberOfIntervals * pattern.interval),
|
|
342
|
+
numberOfOccurrences: numberOfIntervals + 1
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function findPreviousWeeklyRecurrence(time, recurrenceSpec) {
|
|
346
|
+
/*
|
|
347
|
+
* Algorithm:
|
|
348
|
+
* 1. first find day 0 (d0), it's the day representing the start day on the week of `Start`.
|
|
349
|
+
* 2. find start day of the most recent occurring week d0 + floor((time - d0) / (interval * 7)) * (interval * 7)
|
|
350
|
+
* 3. if that's over 7 days ago, then previous occurence is the day with the max offset of the last occurring week
|
|
351
|
+
* 4. if gotten this far, then the current week is the most recent occurring week:
|
|
352
|
+
i. if time > day with min offset, then previous occurence is the day with max offset less than current
|
|
353
|
+
ii. if time < day with min offset, then previous occurence is the day with the max offset of previous occurring week
|
|
354
|
+
*/
|
|
355
|
+
const startTime = recurrenceSpec.startTime;
|
|
356
|
+
const startDay = getDayOfWeek(startTime, recurrenceSpec.timezoneOffset);
|
|
357
|
+
const pattern = recurrenceSpec.pattern;
|
|
358
|
+
const sortedDaysOfWeek = sortDaysOfWeek(pattern.daysOfWeek, pattern.firstDayOfWeek);
|
|
359
|
+
/*
|
|
360
|
+
* Example:
|
|
361
|
+
* startTime = 2024-12-11 (Tue)
|
|
362
|
+
* pattern.interval = 2 pattern.firstDayOfWeek = Sun pattern.daysOfWeek = [Wed, Sun]
|
|
363
|
+
* sortedDaysOfWeek = [Sun, Wed]
|
|
364
|
+
* firstDayofStartWeek = 2024-12-08 (Sun)
|
|
365
|
+
*
|
|
366
|
+
* time = 2024-12-23 (Mon) timeGap = 15 days
|
|
367
|
+
* the most recent occurring week: 2024-12-22 ~ 2024-12-28
|
|
368
|
+
* number of intervals before the most recent occurring week = 15 / (2 * 7) = 1 (2024-12-08 ~ 2023-12-21)
|
|
369
|
+
* number of occurrences before the most recent occurring week = 1 * 2 - 1 = 1 (2024-12-11)
|
|
370
|
+
* firstDayOfLastOccurringWeek = 2024-12-22
|
|
371
|
+
*/
|
|
372
|
+
const firstDayofStartWeek = addDays(startTime, -calculateWeeklyDayOffset(startDay, pattern.firstDayOfWeek));
|
|
373
|
+
const timeGap = time.getTime() - firstDayofStartWeek.getTime();
|
|
374
|
+
// number of intervals before the most recent occurring week
|
|
375
|
+
const numberOfIntervals = Math.floor(timeGap / (pattern.interval * DAYS_PER_WEEK * ONE_DAY_IN_MILLISECONDS));
|
|
376
|
+
// number of occurrences before the most recent occurring week, it is possible to be negative
|
|
377
|
+
let numberOfOccurrences = numberOfIntervals * sortedDaysOfWeek.length - sortedDaysOfWeek.indexOf(startDay);
|
|
378
|
+
const firstDayOfLatestOccurringWeek = addDays(firstDayofStartWeek, numberOfIntervals * pattern.interval * DAYS_PER_WEEK);
|
|
379
|
+
// the current time is out of the last occurring week
|
|
380
|
+
if (time > addDays(firstDayOfLatestOccurringWeek, DAYS_PER_WEEK)) {
|
|
381
|
+
numberOfOccurrences += sortDaysOfWeek.length;
|
|
382
|
+
// day with max offset in the last occurring week
|
|
383
|
+
const previousOccurrence = addDays(firstDayOfLatestOccurringWeek, calculateWeeklyDayOffset(sortedDaysOfWeek.at(-1), pattern.firstDayOfWeek));
|
|
384
|
+
return {
|
|
385
|
+
previousOccurrence: previousOccurrence,
|
|
386
|
+
numberOfOccurrences: numberOfOccurrences
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
let dayWithMinOffset = addDays(firstDayOfLatestOccurringWeek, calculateWeeklyDayOffset(sortedDaysOfWeek[0], pattern.firstDayOfWeek));
|
|
390
|
+
if (dayWithMinOffset < startTime) {
|
|
391
|
+
numberOfOccurrences = 0;
|
|
392
|
+
dayWithMinOffset = startTime;
|
|
393
|
+
}
|
|
394
|
+
let previousOccurrence;
|
|
395
|
+
if (time >= dayWithMinOffset) {
|
|
396
|
+
// the previous occurence is the day with max offset less than current
|
|
397
|
+
previousOccurrence = dayWithMinOffset;
|
|
398
|
+
numberOfOccurrences += 1;
|
|
399
|
+
const dayWithMinOffsetIndex = sortedDaysOfWeek.indexOf(getDayOfWeek(dayWithMinOffset, recurrenceSpec.timezoneOffset));
|
|
400
|
+
for (let i = dayWithMinOffsetIndex + 1; i < sortedDaysOfWeek.length; i++) {
|
|
401
|
+
const day = addDays(firstDayOfLatestOccurringWeek, calculateWeeklyDayOffset(sortedDaysOfWeek[i], pattern.firstDayOfWeek));
|
|
402
|
+
if (time < day) {
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
previousOccurrence = day;
|
|
406
|
+
numberOfOccurrences += 1;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
const firstDayOfPreviousOccurringWeek = addDays(firstDayOfLatestOccurringWeek, -pattern.interval * DAYS_PER_WEEK);
|
|
411
|
+
// the previous occurence is the day with the max offset of previous occurring week
|
|
412
|
+
previousOccurrence = addDays(firstDayOfPreviousOccurringWeek, calculateWeeklyDayOffset(sortedDaysOfWeek.at(-1), pattern.firstDayOfWeek));
|
|
413
|
+
}
|
|
414
|
+
return {
|
|
415
|
+
previousOccurrence: previousOccurrence,
|
|
416
|
+
numberOfOccurrences: numberOfOccurrences
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
7
420
|
// Copyright (c) Microsoft Corporation.
|
|
8
421
|
// Licensed under the MIT license.
|
|
9
422
|
class TimeWindowFilter {
|
|
@@ -12,13 +425,36 @@
|
|
|
12
425
|
const { featureName, parameters } = context;
|
|
13
426
|
const startTime = parameters.Start !== undefined ? new Date(parameters.Start) : undefined;
|
|
14
427
|
const endTime = parameters.End !== undefined ? new Date(parameters.End) : undefined;
|
|
428
|
+
const baseErrorMessage = `The ${this.name} feature filter is not valid for feature ${featureName}. `;
|
|
15
429
|
if (startTime === undefined && endTime === undefined) {
|
|
16
430
|
// If neither start nor end time is specified, then the filter is not applicable.
|
|
17
|
-
console.warn(
|
|
431
|
+
console.warn(baseErrorMessage + "It must specify either 'Start', 'End', or both.");
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
if (startTime !== undefined && isNaN(startTime.getTime())) {
|
|
435
|
+
console.warn(baseErrorMessage + buildInvalidParameterErrorMessage("Start", UNRECOGNIZABLE_VALUE_ERROR_MESSAGE));
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
if (endTime !== undefined && isNaN(endTime.getTime())) {
|
|
439
|
+
console.warn(baseErrorMessage + buildInvalidParameterErrorMessage("End", UNRECOGNIZABLE_VALUE_ERROR_MESSAGE));
|
|
18
440
|
return false;
|
|
19
441
|
}
|
|
20
442
|
const now = new Date();
|
|
21
|
-
|
|
443
|
+
if ((startTime === undefined || startTime <= now) && (endTime === undefined || now < endTime)) {
|
|
444
|
+
return true;
|
|
445
|
+
}
|
|
446
|
+
if (parameters.Recurrence !== undefined) {
|
|
447
|
+
let recurrence;
|
|
448
|
+
try {
|
|
449
|
+
recurrence = parseRecurrenceParameter(startTime, endTime, parameters.Recurrence);
|
|
450
|
+
}
|
|
451
|
+
catch (error) {
|
|
452
|
+
console.warn(baseErrorMessage + error.message);
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
return matchRecurrence(now, recurrence);
|
|
456
|
+
}
|
|
457
|
+
return false;
|
|
22
458
|
}
|
|
23
459
|
}
|
|
24
460
|
|
|
@@ -696,7 +1132,7 @@
|
|
|
696
1132
|
|
|
697
1133
|
// Copyright (c) Microsoft Corporation.
|
|
698
1134
|
// Licensed under the MIT license.
|
|
699
|
-
const VERSION$1 = "2.
|
|
1135
|
+
const VERSION$1 = "2.3.0";
|
|
700
1136
|
const EVALUATION_EVENT_VERSION = "1.0.0";
|
|
701
1137
|
|
|
702
1138
|
// Copyright (c) Microsoft Corporation.
|