stentor-time 1.61.2 → 1.61.3

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.
@@ -1,2 +1,2 @@
1
1
  /*! Copyright (c) 2019, XAPPmedia */
2
- export declare const SCHEDULE_DEBUG_FORMAT = "M/D/Y H:mm:ss Z";
2
+ export declare const SCHEDULE_DEBUG_FORMAT = "M/d/yyyy H:mm:ss Z";
package/lib/Constants.js CHANGED
@@ -3,5 +3,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SCHEDULE_DEBUG_FORMAT = void 0;
4
4
  /*! Copyright (c) 2019, XAPPmedia */
5
5
  // Simple format for communicating times in debug statements
6
- exports.SCHEDULE_DEBUG_FORMAT = "M/D/Y H:mm:ss Z";
6
+ exports.SCHEDULE_DEBUG_FORMAT = "M/d/yyyy H:mm:ss Z";
7
7
  //# sourceMappingURL=Constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Constants.js","sourceRoot":"","sources":["../src/Constants.ts"],"names":[],"mappings":";;;AAAA,oCAAoC;AACpC,4DAA4D;AAC/C,QAAA,qBAAqB,GAAG,iBAAiB,CAAC"}
1
+ {"version":3,"file":"Constants.js","sourceRoot":"","sources":["../src/Constants.ts"],"names":[],"mappings":";;;AAAA,oCAAoC;AACpC,4DAA4D;AAC/C,QAAA,qBAAqB,GAAG,oBAAoB,CAAC"}
@@ -1,7 +1,7 @@
1
1
  import { ConditionalCheck, DurationFormat } from "stentor-models";
2
2
  /**
3
3
  * Depending on the provided last active timestamp and current time, has the user
4
- * been active within
4
+ * been active within the specified duration.
5
5
  *
6
6
  * @param context - Contains the last active timestamp
7
7
  * @param amount - Duration amount
@@ -13,18 +13,17 @@ export declare function activeWithin(context: {
13
13
  /**
14
14
  * Is the current time within the provided schedule.
15
15
  *
16
- * @param start - Start date
16
+ * @param start - Start date string
17
17
  * @param startFormat - Format of the start date string
18
- * @param duration - Duration
18
+ * @param duration - Duration amount
19
19
  * @param durationFormat - Format of the duration
20
20
  * @param timeZone - Optional time zone
21
21
  */
22
22
  export declare function fitsSchedule(start: string, startFormat: string, duration: number, durationFormat: DurationFormat, timeZone?: string): boolean;
23
23
  /**
24
- * Generate a time conditional check for the provided
25
- * context with last active timestamp.
24
+ * Generate a time-based conditional check for the given context.
26
25
  *
27
- * @param context
26
+ * @param context - Object containing lastActiveTimestamp
28
27
  */
29
28
  export declare function TimeConditionalCheck<T extends object>(context: {
30
29
  lastActiveTimestamp: number;
@@ -4,28 +4,37 @@ exports.activeWithin = activeWithin;
4
4
  exports.fitsSchedule = fitsSchedule;
5
5
  exports.TimeConditionalCheck = TimeConditionalCheck;
6
6
  /*! Copyright (c) 2020, XAPPmedia */
7
- const Moment = require("moment");
8
- const moment_range_1 = require("moment-range");
9
7
  const stentor_logger_1 = require("stentor-logger");
10
8
  const stentor_guards_1 = require("stentor-guards");
9
+ const stentor_utils_1 = require("stentor-utils");
11
10
  const findTimeContextualMatch_1 = require("./findTimeContextualMatch");
12
11
  const findSchedulableMatch_1 = require("./findSchedulableMatch");
13
- // Add the range plugin to moment
14
- const moment = (0, moment_range_1.extendMoment)(Moment);
12
+ const normalizeLegacyFormat_1 = require("./normalizeLegacyFormat");
15
13
  /**
16
14
  * Depending on the provided last active timestamp and current time, has the user
17
- * been active within
15
+ * been active within the specified duration.
18
16
  *
19
17
  * @param context - Contains the last active timestamp
20
18
  * @param amount - Duration amount
21
19
  * @param format - Format of the duration amount
22
20
  */
23
21
  function activeWithin(context, amount, format) {
24
- const now = moment();
25
- const lastActive = context.lastActiveTimestamp !== undefined ? moment(context.lastActiveTimestamp) : undefined;
26
- const rangeStart = moment(now).subtract(amount, format);
27
- const range = moment.range(rangeStart, now);
28
- if (lastActive && range.contains(lastActive)) {
22
+ const now = Date.now();
23
+ const lastActive = context.lastActiveTimestamp;
24
+ if (typeof amount !== "number" || isNaN(amount) || amount < 0 || typeof format !== "string") {
25
+ (0, stentor_logger_1.log)().warn(`Invalid activeWithin params: amount=${amount}, format=${format}`);
26
+ return false;
27
+ }
28
+ let durationMs;
29
+ try {
30
+ durationMs = (0, stentor_utils_1.getDurationMs)(amount, format);
31
+ }
32
+ catch (err) {
33
+ (0, stentor_logger_1.log)().warn(`activeWithin failed to get durationMs for ${amount} ${format}`, err);
34
+ return false;
35
+ }
36
+ const rangeStart = now - durationMs;
37
+ if (lastActive !== undefined && lastActive >= rangeStart && lastActive <= now) {
29
38
  (0, stentor_logger_1.log)().debug(`User was active within ${amount} ${format}`);
30
39
  return true;
31
40
  }
@@ -35,40 +44,41 @@ function activeWithin(context, amount, format) {
35
44
  /**
36
45
  * Is the current time within the provided schedule.
37
46
  *
38
- * @param start - Start date
47
+ * @param start - Start date string
39
48
  * @param startFormat - Format of the start date string
40
- * @param duration - Duration
49
+ * @param duration - Duration amount
41
50
  * @param durationFormat - Format of the duration
42
51
  * @param timeZone - Optional time zone
43
52
  */
44
53
  function fitsSchedule(start, startFormat, duration, durationFormat, timeZone) {
54
+ // Convert legacy Moment.js format to Luxon format
55
+ const normalizedFormat = (0, normalizeLegacyFormat_1.normalizeLegacyFormat)(startFormat);
45
56
  const schedule = {
46
57
  schedule: {
47
58
  start: {
48
59
  time: start,
49
- format: startFormat,
50
- timeZone
60
+ format: normalizedFormat,
61
+ timeZone,
51
62
  },
52
63
  duration: {
53
64
  amount: duration,
54
- format: durationFormat
55
- }
56
- }
65
+ format: durationFormat,
66
+ },
67
+ },
57
68
  };
58
69
  const fitSchedule = !!(0, findSchedulableMatch_1.findSchedulableMatch)([schedule]);
59
70
  if (!fitSchedule) {
60
- (0, stentor_logger_1.log)().debug(`Schedule starting ${start}, with format ${startFormat}, and running for ${duration} ${durationFormat} did NOT fit. Current date ${new Date().toISOString()}`);
71
+ (0, stentor_logger_1.log)().debug(`Schedule starting ${start}, with format ${startFormat} (normalized: ${normalizedFormat}), and running for ${duration} ${durationFormat} did NOT fit. Current date ${new Date().toISOString()}`);
61
72
  }
62
73
  else {
63
- (0, stentor_logger_1.log)().debug(`Schedule starting ${start}, with format ${startFormat}, and running for ${duration} ${durationFormat} fits.`);
74
+ (0, stentor_logger_1.log)().debug(`Schedule starting ${start}, with format ${startFormat} (normalized: ${normalizedFormat}), and running for ${duration} ${durationFormat} fits.`);
64
75
  }
65
76
  return fitSchedule;
66
77
  }
67
78
  /**
68
- * Generate a time conditional check for the provided
69
- * context with last active timestamp.
79
+ * Generate a time-based conditional check for the given context.
70
80
  *
71
- * @param context
81
+ * @param context - Object containing lastActiveTimestamp
72
82
  */
73
83
  function TimeConditionalCheck(context) {
74
84
  return {
@@ -76,10 +86,7 @@ function TimeConditionalCheck(context) {
76
86
  check: (obj) => {
77
87
  return !!(0, findTimeContextualMatch_1.findTimeContextualMatch)([obj], context);
78
88
  },
79
- functions: [
80
- activeWithin.bind(null, context),
81
- fitsSchedule
82
- ]
89
+ functions: [activeWithin.bind(null, context), fitsSchedule],
83
90
  };
84
91
  }
85
92
  //# sourceMappingURL=TimeConditionalCheck.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"TimeConditionalCheck.js","sourceRoot":"","sources":["../src/TimeConditionalCheck.ts"],"names":[],"mappings":";;AAoBA,oCAcC;AAWD,oCAwBC;AAQD,oDAWC;AAxFD,oCAAoC;AACpC,iCAAiC;AACjC,+CAA4C;AAC5C,mDAAqC;AAErC,mDAAkD;AAClD,uEAAoE;AACpE,iEAA8D;AAE9D,iCAAiC;AACjC,MAAM,MAAM,GAAG,IAAA,2BAAY,EAAC,MAAM,CAAC,CAAC;AAEpC;;;;;;;GAOG;AACH,SAAgB,YAAY,CAAC,OAAwC,EAAE,MAAc,EAAE,MAAsB;IAEzG,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC;IACrB,MAAM,UAAU,GAAG,OAAO,CAAC,mBAAmB,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAE/G,MAAM,UAAU,GAAG,MAAM,CA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C;QACN,IAAA,oBAAG,GAAE,CAAC,KAAK,CACT,qBAAqB,KAAK,iBAAiB,WAAW,iBAAiB,gBAAgB,sBAAsB,QAAQ,IAAI,cAAc,QAAQ,CAChJ,CAAC;IACJ,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;AAED;;;;GAIG;AACH,SAAgB,oBAAoB,CAAmB,OAAwC;IAC7F,OAAO;QACL,IAAI,EAAE,iCAAgB;QACtB,KAAK,EAAE,CAAC,GAAsB,EAAW,EAAE;YACzC,OAAO,CAAC,CAAC,IAAA,iDAAuB,EAAC,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;QACnD,CAAC;QACD,SAAS,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,YAAY,CAAC;KAC5D,CAAC;AACJ,CAAC"}
@@ -2,9 +2,9 @@ import { LastActive } from "stentor-models";
2
2
  /**
3
3
  * Finds the most relevant contextual match.
4
4
  *
5
- * @param {((T | LastActive<T>)[])} responses
6
- * @param {{ lastActiveTimestamp?: number }} context
7
- * @returns {(LastActive<T> | undefined)}
5
+ * @param responses
6
+ * @param context
7
+ * @returns
8
8
  */
9
9
  export declare function findLastActiveMatch<T extends object>(responses: (T | LastActive<T>)[], context: {
10
10
  lastActiveTimestamp?: number;
@@ -1,65 +1,44 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.findLastActiveMatch = findLastActiveMatch;
4
- /*! Copyright (c) 2019, XAPPmedia */
4
+ /*! Copyright (c) 2025, XAPP AI */
5
5
  const stentor_guards_1 = require("stentor-guards");
6
- const Moment = require("moment");
7
- const moment_range_1 = require("moment-range");
8
- // Add the range plugin to moment
9
- const moment = (0, moment_range_1.extendMoment)(Moment);
6
+ const stentor_utils_1 = require("stentor-utils");
10
7
  /**
11
8
  * Finds the most relevant contextual match.
12
9
  *
13
- * @param {((T | LastActive<T>)[])} responses
14
- * @param {{ lastActiveTimestamp?: number }} context
15
- * @returns {(LastActive<T> | undefined)}
10
+ * @param responses
11
+ * @param context
12
+ * @returns
16
13
  */
17
14
  function findLastActiveMatch(responses, context) {
18
15
  if (!Array.isArray(responses) || !context) {
19
- // Exit conditions, no array or context then bail
20
16
  return undefined;
21
17
  }
22
- const now = moment();
23
- const lastActive = context.lastActiveTimestamp !== undefined ? moment(context.lastActiveTimestamp) : undefined;
18
+ const now = Date.now();
19
+ const lastActive = context.lastActiveTimestamp;
24
20
  let contextualResponse;
25
- let withinRange;
21
+ let smallestRangeMs;
26
22
  for (const response of responses) {
27
- // Make sure activeWithin exists, might have gotten bad data
28
23
  if ((0, stentor_guards_1.isActiveWithin)(response)) {
29
- const activeWithin = response.activeWithin;
30
- // Construct the date range by starting with the current time
31
- // and subtracting the activeWithin amount
32
- const rangeStart = moment(now).subtract(activeWithin.amount, activeWithin.format);
33
- const range = moment.range(rangeStart, now);
34
- // Then see if the last time we saw them is within the range
35
- if (lastActive && range.contains(lastActive)) {
36
- // It is in range, see if we keep it...
37
- if (!withinRange) {
38
- // If nothing previous, set it
24
+ const { amount, format } = response.activeWithin;
25
+ const durationMs = (0, stentor_utils_1.getDurationMs)(amount, format);
26
+ const rangeStart = now - durationMs;
27
+ if (lastActive !== undefined && lastActive >= rangeStart && lastActive <= now) {
28
+ if (contextualResponse === undefined || durationMs < smallestRangeMs) {
39
29
  contextualResponse = response;
40
- withinRange = range;
41
- }
42
- else if (range < withinRange) {
43
- // If the new range is smaller than the previous
44
- contextualResponse = response;
45
- withinRange = range;
30
+ smallestRangeMs = durationMs;
46
31
  }
47
32
  }
48
33
  }
49
34
  else if ((0, stentor_guards_1.isHaveNotSeenWithin)(response)) {
50
- const haveNotSeenWithin = response.haveNotSeenWithin;
51
- const rangeStart = moment(now).subtract(haveNotSeenWithin.amount, haveNotSeenWithin.format);
52
- const range = moment.range(rangeStart, now);
53
- // Range is between now and the haveNotSeenWithin duration
54
- // Make sure it is outside the range
55
- if (lastActive && !range.contains(lastActive)) {
56
- if (!withinRange) {
57
- contextualResponse = response;
58
- withinRange = range;
59
- }
60
- else if (range < withinRange) {
35
+ const { amount, format } = response.haveNotSeenWithin;
36
+ const durationMs = (0, stentor_utils_1.getDurationMs)(amount, format);
37
+ const rangeStart = now - durationMs;
38
+ if (lastActive !== undefined && (lastActive < rangeStart || lastActive > now)) {
39
+ if (contextualResponse === undefined || durationMs < smallestRangeMs) {
61
40
  contextualResponse = response;
62
- withinRange = range;
41
+ smallestRangeMs = durationMs;
63
42
  }
64
43
  }
65
44
  }
@@ -1 +1 @@
1
- {"version":3,"file":"findLastActiveMatch.js","sourceRoot":"","sources":["../src/findLastActiveMatch.ts"],"names":[],"mappings":";;AAgBA,kDA8DC;AA9ED,oCAAoC;AACpC,mDAAkF;AAElF,iCAAiC;AACjC,+CAAuD;AAEvD,iCAAiC;AACjC,MAAM,MAAM,GAAG,IAAA,2BAAY,EAAC,MAAM,CAAC,CAAC;AAEpC;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAC/B,SAAgC,EAChC,OAAyC;IAEzC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;QACxC,iDAAiD;QACjD,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC;IACrB,MAAM,UAAU,GAAG,OAAO,CAAC,mBAAmB,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAE/G,IAAI,kBAAiC,CAAC;IACtC,IAAI,WAAsB,CAAC;IAE3B,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QAC/B,4DAA4D;QAC5D,IAAI,IAAA,+BAAc,EAAC,QAAQ,CAAC,EAAE,CAAC;YAC3B,MAAM,YAAY,GAAG,QAAQ,CAAC,YAAY,CAAC;YAC3C,6DAA6D;YAC7D,0CAA0C;Y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UAAU,GAAG,eAAe,EAAE,CAAC;oBACrE,kBAAkB,GAAG,QAAQ,CAAC;oBAC9B,eAAe,GAAG,UAAU,CAAC;gBAC/B,CAAC;YACH,CAAC;QACH,CAAC;aAAM,IAAI,IAAA,oCAAmB,EAAC,QAAQ,CAAC,EAAE,CAAC;YACzC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,QAAQ,CAAC,iBAAiB,CAAC;YACtD,MAAM,UAAU,GAAG,IAAA,6BAAa,EAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YACjD,MAAM,UAAU,GAAG,GAAG,GAAG,UAAU,CAAC;YAEpC,IAAI,UAAU,KAAK,SAAS,IAAI,CAAC,UAAU,GAAG,UAAU,IAAI,UAAU,GAAG,GAAG,CAAC,EAAE,CAAC;gBAC9E,IAAI,kBAAkB,KAAK,SAAS,IAAI,UAAU,GAAG,eAAe,EAAE,CAAC;oBACrE,kBAAkB,GAAG,QAAQ,CAAC;oBAC9B,eAAe,GAAG,UAAU,CAAC;gBAC/B,CAAC;YACH,CAAC;QACH,CAAC;aAAM,IAAI,IAAA,4BAAW,EAAC,QAAQ,CAAC,EAAE,CAAC;YACjC,+CAA+C;YAC/C,qDAAqD;YACrD,oBAAoB;YACpB,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChB,kBAAkB,GAAG,QAAQ,CAAC;YAChC,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,kBAAkB,CAAC;AAC5B,CAAC"}
@@ -7,14 +7,5 @@ export interface ScheduleFormatScores {
7
7
  [format: string]: ScheduleFormatScore;
8
8
  }
9
9
  export declare const SCHEDULE_FORMAT_SCORES: ScheduleFormatScores;
10
- /**
11
- * Determine schedule specificity score, the higher the value
12
- * the more specific the schedule is.
13
- */
14
10
  export declare function determineSpecificityScore<T extends object>(scheduled: Scheduled<T> | undefined): number;
15
- /**
16
- * Finds the most specific schedule.
17
- *
18
- * If the array does not contain any scheduled objects, undefined is returned.
19
- */
20
11
  export declare function findMostSpecificSchedulable<T extends object>(schedules: (T | Scheduled<T>)[] | undefined): Scheduled<T> | undefined;
@@ -3,103 +3,63 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SCHEDULE_FORMAT_SCORES = void 0;
4
4
  exports.determineSpecificityScore = determineSpecificityScore;
5
5
  exports.findMostSpecificSchedulable = findMostSpecificSchedulable;
6
- /*! Copyright (c) 2019, XAPPmedia */
6
+ /*! Copyright (c) 2025, XAPP AI */
7
7
  const stentor_guards_1 = require("stentor-guards");
8
- const moment = require("moment-timezone");
9
8
  exports.SCHEDULE_FORMAT_SCORES = {
10
- // Year, applies to both YY and YYYY
11
- ["YY"]: { score: 1 },
12
- // Month, applies to both M and MM
13
- ["M"]: { score: 1 },
14
- // Day of month, applies to both D and DD
15
- ["D"]: { score: 1 },
16
- // Day of year, includes information about month and day of month
17
- ["DDD"]: { score: 2 },
18
- // Hour (24), includes information about hours and morning vs evening
19
- // applies to both H and HH
20
- ["H"]: { score: 2 },
21
- // Hour (12), includes information about hours
22
- // applies to both h and hh
23
- ["h"]: { score: 1 },
24
- // am/pm, includes information about morning or evening
25
- ["a"]: { score: 1 },
26
- // AM/PM
27
- ["A"]: { score: 1 },
28
- // Minutes, applies to both m and mm
29
- ["m"]: { score: 1 }
9
+ YYYY: { score: 1 }, // moment
10
+ YY: { score: 1 }, // moment
11
+ yyyy: { score: 1 }, // luxon
12
+ M: { score: 1 },
13
+ MM: { score: 1 },
14
+ D: { score: 1 },
15
+ DD: { score: 1 },
16
+ dd: { score: 1 },
17
+ DDD: { score: 2 },
18
+ H: { score: 2 },
19
+ HH: { score: 2 },
20
+ h: { score: 1 },
21
+ hh: { score: 1 },
22
+ a: { score: 1 },
23
+ A: { score: 1 },
24
+ m: { score: 1 },
25
+ mm: { score: 1 },
30
26
  };
31
- /**
32
- * Determine schedule specificity score, the higher the value
33
- * the more specific the schedule is.
34
- */
35
27
  function determineSpecificityScore(scheduled) {
36
- // exit conditions
28
+ var _a, _b;
37
29
  if (!scheduled || !scheduled.schedule) {
38
30
  return 0;
39
31
  }
40
32
  const schedule = scheduled.schedule;
41
33
  const start = schedule.start;
42
- let format = start.format;
43
- // Format doesn't always exist
44
- if (!format) {
45
- // If it doesn't use the defaultFormat from moment
46
- // which is what we use as the default
47
- format = moment.defaultFormat;
48
- }
34
+ const format = (_a = start.format) !== null && _a !== void 0 ? _a : "yyyy-MM-dd'T'HH:mm:ssZZ";
35
+ const tokenRegex = /Y{2,4}|M{1,2}|D{1,4}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|a|A|Z{1,2}/g;
36
+ const tokens = (_b = format.match(tokenRegex)) !== null && _b !== void 0 ? _b : [];
49
37
  let score = 0;
50
- // Not in the format but adds specificity
51
38
  if (start.dayOfWeek) {
52
39
  score += 1;
53
40
  }
54
- // Iterate through the formats adding the score
55
- Object.keys(exports.SCHEDULE_FORMAT_SCORES).forEach(key => {
56
- const formatScore = exports.SCHEDULE_FORMAT_SCORES[key];
57
- // If it contains it
58
- if (format.includes(key)) {
59
- // add the score
60
- score += formatScore.score;
41
+ for (const token of tokens) {
42
+ const tokenScore = exports.SCHEDULE_FORMAT_SCORES[token];
43
+ if (tokenScore) {
44
+ score += tokenScore.score;
61
45
  }
62
- });
46
+ }
63
47
  return score;
64
48
  }
65
- /**
66
- * Finds the most specific schedule.
67
- *
68
- * If the array does not contain any scheduled objects, undefined is returned.
69
- */
70
49
  function findMostSpecificSchedulable(schedules) {
71
- // Fast exit conditions
72
- // doesn't exist
73
- if (!schedules) {
50
+ if (!Array.isArray(schedules) || schedules.length === 0)
74
51
  return undefined;
75
- }
76
- // empty array
77
- if (schedules.length === 0) {
78
- return undefined;
79
- }
80
- // only one in the array, just return it
81
- if (schedules.length === 1) {
82
- const firstItem = schedules[0];
83
- if ((0, stentor_guards_1.isScheduled)(firstItem)) {
84
- return firstItem;
85
- }
86
- else {
87
- return undefined;
88
- }
89
- }
90
52
  let mostSpecific;
91
53
  let currentScore = 0;
92
- schedules.forEach(scheduled => {
93
- if (!(0, stentor_guards_1.isScheduled)(scheduled)) {
94
- // bail if it doesn't have a schedule
95
- return;
96
- }
54
+ for (const scheduled of schedules) {
55
+ if (!(0, stentor_guards_1.isScheduled)(scheduled))
56
+ continue;
97
57
  const score = determineSpecificityScore(scheduled);
98
58
  if (score > currentScore) {
99
59
  mostSpecific = scheduled;
100
60
  currentScore = score;
101
61
  }
102
- });
62
+ }
103
63
  return mostSpecific;
104
64
  }
105
65
  //# sourceMappingURL=findMostSpecificSchedulable.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"findMostSpecificSchedulable.js","sourceRoot":"","sources":["../src/findMostSpecificSchedulable.ts"],"names":[],"mappings":";;;AAyCA,8DAmCC;AAOD,kEAqCC;AAxHD,oCAAoC;AACpC,mDAA6C;AAE7C,0CAA0C;AAW7B,QAAA,sBAAsB,GAAyB;IACxD,oCAAoC;IACpC,CAAC,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;IACpB,kCAAkC;IAClC,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;IACnB,yCAAyC;IACzC,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;IACnB,iEAAiE;IACjE,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;IACrB,qEAAqE;IACrE,2BAA2B;IAC3B,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;IACnB,8CAA8C;IAC9C,2BAA2B;IAC3B,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;IACnB,uDAAuD;IACvD,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;IACnB,QAAQ;IACR,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;IACnB,oCAAoC;IACpC,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;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B,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAgB,2BAA2B,CACzC,SAA2C;IAE3C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IAE1E,IAAI,YAAsC,CAAC;IAC3C,IAAI,YAAY,GAAG,CAAC,CAAC;IAErB,KAAK,MAAM,SAAS,IAAI,SAAS,EAAE,CAAC;QAClC,IAAI,CAAC,IAAA,4BAAW,EAAC,SAAS,CAAC;YAAE,SAAS;QACtC,MAAM,KAAK,GAAG,yBAAyB,CAAC,SAAS,CAAC,CAAC;QACnD,IAAI,KAAK,GAAG,YAAY,EAAE,CAAC;YACzB,YAAY,GAAG,SAAS,CAAC;YACzB,YAAY,GAAG,KAAK,CAAC;QACvB,CAAC;IACH,CAAC;IAED,OAAO,YAAY,CAAC;AACtB,CAAC"}
@@ -1,7 +1,8 @@
1
+ /*! Copyright (c) 2025, XAPP AI */
2
+ import { DateTime } from "luxon";
1
3
  import { Scheduled } from "stentor-models";
2
- import * as moment from "moment-timezone";
3
4
  /**
4
5
  * Within a list of Schedulables, find a match for the provided time
5
6
  * or current time if no time is provided.
6
7
  */
7
- export declare function findSchedulableMatch<T extends object>(schedules: (T | Scheduled<T>)[] | undefined, now?: moment.Moment): Scheduled<T> | undefined;
8
+ export declare function findSchedulableMatch<T extends object>(schedules: (T | Scheduled<T>)[] | undefined, baseNowInput?: DateTime): Scheduled<T> | undefined;
@@ -1,88 +1,149 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.findSchedulableMatch = findSchedulableMatch;
4
- /*! Copyright (c) 2019, XAPPmedia */
4
+ /*! Copyright (c) 2025, XAPP AI */
5
+ const luxon_1 = require("luxon");
5
6
  const stentor_guards_1 = require("stentor-guards");
6
- const moment = require("moment-timezone");
7
7
  const findMostSpecificSchedulable_1 = require("./findMostSpecificSchedulable");
8
8
  const stentor_logger_1 = require("stentor-logger");
9
+ function normalizeDurationUnit(unit) {
10
+ switch (unit.toLowerCase()) {
11
+ case "ms":
12
+ case "millisecond":
13
+ case "milliseconds":
14
+ return "milliseconds";
15
+ case "s":
16
+ case "second":
17
+ case "seconds":
18
+ return "seconds";
19
+ case "m":
20
+ case "minute":
21
+ case "minutes":
22
+ return "minutes";
23
+ case "h":
24
+ case "hour":
25
+ case "hours":
26
+ return "hours";
27
+ case "d":
28
+ case "day":
29
+ case "days":
30
+ return "days";
31
+ default:
32
+ throw new Error(`Invalid unit ${unit}`);
33
+ }
34
+ }
9
35
  /**
10
36
  * Within a list of Schedulables, find a match for the provided time
11
37
  * or current time if no time is provided.
12
38
  */
13
- function findSchedulableMatch(schedules, now = moment()) {
39
+ function findSchedulableMatch(schedules, baseNowInput = luxon_1.DateTime.now().toUTC()) {
14
40
  if (!Array.isArray(schedules)) {
15
- // bail if no schedule was passed in
16
41
  return undefined;
17
42
  }
43
+ const baseNow = baseNowInput !== null && baseNowInput !== void 0 ? baseNowInput : luxon_1.DateTime.now().toUTC();
18
44
  const matches = [];
19
- schedules.forEach(item => {
45
+ for (const item of schedules) {
20
46
  if (!(0, stentor_guards_1.isScheduled)(item)) {
21
- // bail if it doesn't have a schedule
22
- return;
47
+ (0, stentor_logger_1.log)().debug(`Item is not scheduled: ${JSON.stringify(item)}, skipping`);
48
+ continue;
23
49
  }
24
- let start;
25
50
  const { time, format, timeZone, dayOfWeek } = item.schedule.start;
26
- start = moment(time, format);
27
- // Convert to correct timezone if available
28
- if (timeZone) {
29
- // Update now with the tz
30
- now = now.tz(timeZone);
31
- // Create the start time with the provided time, format and timezone
32
- start = moment.tz(time, format, timeZone);
33
- // We now may need to set the year, month, date for the current
34
- // day if it wasn't provided on the format
35
- // The order here is important as if you try to update the date first
36
- // for a month that doesn't have that many days, it will default back to one
37
- // and you will be really confused.
38
- // They didn't give a year, set it.
39
- if (!format.includes("Y")) {
40
- const year = now.year();
41
- start.year(year);
51
+ const now = timeZone ? baseNow.setZone(timeZone) : baseNow;
52
+ let start;
53
+ if (format) {
54
+ let patchedFormat = format;
55
+ const patchedTime = time;
56
+ // Legacy format handling: H:mm Z D
57
+ if (format === "H:mm Z D") {
58
+ const match = time.match(/^(\d{1,2}:\d{2}) ([+-]\d{4}) (\d)$/);
59
+ if (match) {
60
+ const [, timePart, offset, legacyDayOfWeek] = match;
61
+ // Legacy format day mapping: directly map to the target date in March 2017
62
+ // The 'D' token represents day-of-month, not day-of-week
63
+ const legacyDay = parseInt(legacyDayOfWeek);
64
+ const targetDate = luxon_1.DateTime.fromObject({ year: 2017, month: 3, day: legacyDay, hour: 0 }, { zone: "America/New_York" });
65
+ const fullDate = targetDate.toFormat("yyyy-MM-dd");
66
+ const [h, m] = timePart.split(":");
67
+ const normalizedTime = `${h.padStart(2, "0")}:${m}`;
68
+ const fullTime = `${fullDate} ${normalizedTime} ${offset}`;
69
+ const parsed = luxon_1.DateTime.fromFormat(fullTime, "yyyy-MM-dd HH:mm ZZZ", {
70
+ setZone: true,
71
+ locale: "en",
72
+ });
73
+ start = parsed;
74
+ }
42
75
  }
43
- // Since DDD or DDDD, day of year (1-365) includes both
44
- // data for day of month and month,
45
- // skip these two
46
- if (!format.includes("DDD")) {
47
- // 1st case, they don't give month
48
- if (!format.includes("M")) {
49
- const month = now.month();
50
- start.month(month);
76
+ else {
77
+ // Normal format-based parsing
78
+ if ((format === null || format === void 0 ? void 0 : format.includes("Z")) && /\bZ\b/.test(time)) {
79
+ patchedFormat = format.replace("Z", "'Z'");
51
80
  }
52
- // 2nd case, they don't give date
53
- if (!format.includes("D")) {
54
- const date = now.date();
55
- start.date(date);
81
+ start = luxon_1.DateTime.fromFormat(patchedTime, patchedFormat, {
82
+ zone: timeZone || baseNow.zone,
83
+ });
84
+ }
85
+ }
86
+ else {
87
+ start = luxon_1.DateTime.fromISO(time, { zone: timeZone || baseNow.zone });
88
+ }
89
+ if (!start.isValid) {
90
+ (0, stentor_logger_1.log)().debug(`Invalid start date for schedule: time=${time}, format=${format}`, start.invalidReason, start.invalidExplanation);
91
+ continue;
92
+ }
93
+ // Patch missing date parts like moment-timezone
94
+ if (format) {
95
+ const hasYear = format.includes("y") || format.includes("Y");
96
+ const hasMonth = format.includes("M");
97
+ const hasDay = format.includes("d") || format.includes("D");
98
+ const hasDayOfYear = format.includes("o") || format.includes("D"); // luxon uses `o` for day-of-year
99
+ if (!(hasYear && hasMonth && hasDay)) {
100
+ if (!hasYear) {
101
+ start = start.set({ year: now.year });
102
+ }
103
+ if (!hasDayOfYear) {
104
+ if (!hasMonth) {
105
+ start = start.set({ month: now.month });
106
+ }
107
+ if (!hasDay) {
108
+ start = start.set({ day: now.day });
109
+ }
56
110
  }
57
111
  }
58
112
  }
59
- const duration = item.schedule.duration;
60
- // calculate the end time based on the duration set
61
- const end = moment(start).add(duration.amount, duration.format);
113
+ // Compute end time
114
+ const durationAmount = item.schedule.duration.amount;
115
+ const durationFormat = item.schedule.duration.format;
116
+ let duration;
117
+ try {
118
+ const normalized = normalizeDurationUnit(durationFormat);
119
+ duration = luxon_1.Duration.fromObject({ [normalized]: durationAmount });
120
+ }
121
+ catch (err) {
122
+ (0, stentor_logger_1.log)().debug(`Unsupported duration format: ${durationFormat}`);
123
+ continue;
124
+ }
125
+ const end = start.plus(duration);
126
+ // Check day of week if applicable (skip for legacy format since it's already encoded in start time)
62
127
  let correctDayOfWeek = true;
63
- // see if we have one.
64
- if (dayOfWeek) {
65
- // alright we have one, if it isn't included in the
66
- // day of week string, make that a false
67
- // This uses includes because dayOfWeek can be `0124` to denote multiple days of the week
68
- if (!item.schedule.start.dayOfWeek.includes(now.weekday().toString())) {
128
+ if (dayOfWeek && format !== "H:mm Z D") {
129
+ const currentDow = now.weekday % 7; // 0 (Sun) – 6 (Sat)
130
+ if (!dayOfWeek.includes(currentDow.toString())) {
69
131
  correctDayOfWeek = false;
70
132
  }
71
133
  }
134
+ // Match if now is within [start, end)
72
135
  if (correctDayOfWeek) {
73
- // See if it is in between, [) means we include
74
- // the start but exclude the end
75
- if (now.isBetween(start, end, undefined, "[)")) {
136
+ if (now >= start && now < end) {
76
137
  matches.push(item);
77
138
  }
78
139
  else {
79
- (0, stentor_logger_1.log)().debug(`Schedule starting ${time}, with format ${format}, and duration ${duration.amount} ${duration.format} was NOT in-between ${start.format()} --> ${end.format()}`);
140
+ (0, stentor_logger_1.log)().debug(`Schedule starting ${time}, with format ${format}, and duration ${durationAmount} ${durationFormat} was NOT in-between ${start.toISO()} --> ${end.toISO()}`);
80
141
  }
81
142
  }
82
143
  else {
83
- (0, stentor_logger_1.log)().debug(`Incorrect day of the week for schedule starting ${time}, with format ${format}, and duration ${duration.amount} ${duration.format}`);
144
+ (0, stentor_logger_1.log)().debug(`Incorrect day of the week for schedule starting ${time}, with format ${format}, and duration ${durationAmount} ${durationFormat}`);
84
145
  }
85
- });
146
+ }
86
147
  return (0, findMostSpecificSchedulable_1.findMostSpecificSchedulable)(matches);
87
148
  }
88
149
  //# sourceMappingURL=findSchedulableMatch.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"findSchedulableMatch.js","sourceRoot":"","sources":["../src/findSchedulableMatch.ts"],"names":[],"mappings":";;AAWA,oDAmFC;AA9FD,oCAAoC;AACpC,mDAA6C;AAE7C,0CAA0C;AAC1C,+EAA4E;AAC5E,mDAAqC;AAErC;;;GAGG;AACH,SAAgB,oBAAoB,CAChC,SAA2C,EAC3C,GAAG,GAAG,MAAM,EAAE;IAEd,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;QAC5B,oCAAoC;QACpC,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,MAAM,OAAO,GAAmB,EAAE,CAAC;IAEnC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QACrB,IAAI,CAAC,IAAA,4BAAW,EAAC,IAAI,CAAC,EAAE,CAAC;YACrB,qCAAqC;YACrC,OAAO;QACX,CAAC;QACD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@ -0,0 +1,7 @@
1
+ /*! Copyright (c) 2025, XAPP AI */
2
+ /**
3
+ * Normalizes legacy Moment.js format strings to Luxon format strings
4
+ * @param momentFormat - The Moment.js format string
5
+ * @returns The equivalent Luxon format string
6
+ */
7
+ export declare function normalizeLegacyFormat(momentFormat: string): string;
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ /*! Copyright (c) 2025, XAPP AI */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.normalizeLegacyFormat = normalizeLegacyFormat;
5
+ // Helper function to escape special regex characters
6
+ function escapeRegex(string) {
7
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
8
+ }
9
+ /**
10
+ * Normalizes legacy Moment.js format strings to Luxon format strings
11
+ * @param momentFormat - The Moment.js format string
12
+ * @returns The equivalent Luxon format string
13
+ */
14
+ function normalizeLegacyFormat(momentFormat) {
15
+ // Special case: handle YYYY-MM-DDTmm:ss which appears to be a typo/legacy format
16
+ // where mm:ss should be HH:mm (hours:minutes not minutes:seconds)
17
+ if (momentFormat === "YYYY-MM-DDTmm:ss") {
18
+ return "yyyy-MM-dd'T'HH:mm";
19
+ }
20
+ // Quick check: if format looks like it's already Luxon-style (lowercase yyyy, dd in date context), return as-is
21
+ // Use bounded matching to prevent ReDoS attacks
22
+ if (/yyyy[^M]{0,10}-MM-dd/.test(momentFormat) || /dd[^M]{0,10}-MM-yyyy/.test(momentFormat)) {
23
+ return momentFormat;
24
+ }
25
+ // For mixed formats with dd in date context, we need to be careful not to convert dd to weekday
26
+ // Use non-greedy, bounded matching to prevent ReDoS attacks
27
+ const hasMixedDateFormat = /(?:YYYY|yyyy)[^M]{0,10}-MM-dd/.test(momentFormat) || /dd[^M]{0,10}-MM-(?:YYYY|yyyy)/.test(momentFormat);
28
+ // Handle escaped characters in brackets first
29
+ // Limit content within brackets to prevent ReDoS attacks
30
+ const escapedChars = [];
31
+ let result = momentFormat.replace(/\[([^\]]{0,100})\]/g, (_, content) => {
32
+ const placeholder = `###ESC${escapedChars.length}###`;
33
+ escapedChars.push(content);
34
+ return placeholder;
35
+ });
36
+ // Define token replacements in order of precedence (longer patterns first)
37
+ // Use placeholder system to prevent re-processing of already converted tokens
38
+ // Use numbers to avoid character conflicts
39
+ const tokenReplacements = [
40
+ // 4-character tokens
41
+ { moment: /YYYY/g, luxon: "@@1@@" },
42
+ { moment: /MMMM/g, luxon: "@@2@@" },
43
+ { moment: /dddd/g, luxon: "@@3@@" },
44
+ { moment: /DDDD/g, luxon: "@@4@@" },
45
+ // 3-character tokens
46
+ { moment: /MMM/g, luxon: "@@5@@" },
47
+ { moment: /ddd/g, luxon: "@@6@@" },
48
+ { moment: /DDD/g, luxon: "@@7@@" },
49
+ { moment: /SSS/g, luxon: "@@8@@" },
50
+ { moment: /ZZZ/g, luxon: "@@9@@" },
51
+ // 2-character tokens
52
+ { moment: /Do/g, luxon: "@@10@@" },
53
+ { moment: /DD/g, luxon: "@@11@@" }, // Day of month
54
+ ...(hasMixedDateFormat ? [] : [{ moment: /dd/g, luxon: "@@12@@" }]), // Skip weekday conversion in date context
55
+ { moment: /MM/g, luxon: "@@13@@" },
56
+ { moment: /YY/g, luxon: "@@14@@" },
57
+ { moment: /HH/g, luxon: "@@15@@" },
58
+ { moment: /hh/g, luxon: "@@16@@" },
59
+ { moment: /mm/g, luxon: "@@17@@" },
60
+ { moment: /ss/g, luxon: "@@18@@" },
61
+ { moment: /SS/g, luxon: "@@19@@" },
62
+ { moment: /ww/g, luxon: "@@20@@" },
63
+ { moment: /ZZ/g, luxon: "@@21@@" },
64
+ // 1-character tokens (only match when not part of words)
65
+ { moment: /(?<!\w)D(?!\w)/g, luxon: "@@22@@" }, // Day of month
66
+ ...(hasMixedDateFormat ? [] : [{ moment: /(?<!\w)d(?!\w)/g, luxon: "@@23@@" }]), // Skip weekday conversion in date context
67
+ { moment: /(?<!\w)M(?!\w)/g, luxon: "@@24@@" },
68
+ { moment: /(?<!\w)H(?!\w)/g, luxon: "@@25@@" },
69
+ { moment: /(?<!\w)h(?!\w)/g, luxon: "@@26@@" },
70
+ { moment: /(?<!\w)m(?!\w)/g, luxon: "@@27@@" },
71
+ { moment: /(?<!\w)s(?!\w)/g, luxon: "@@28@@" },
72
+ { moment: /(?<!\w)S(?!\w)/g, luxon: "@@29@@" },
73
+ { moment: /(?<!\w)A(?!\w)/g, luxon: "@@30@@" },
74
+ { moment: /(?<!\w)a(?!\w)/g, luxon: "@@31@@" },
75
+ { moment: /(?<!\w)Z(?!\w)/g, luxon: "@@32@@" },
76
+ { moment: /(?<!\w)Q(?!\w)/g, luxon: "@@33@@" },
77
+ { moment: /(?<!\w)w(?!\w)/g, luxon: "@@34@@" },
78
+ { moment: /(?<!\w)X(?!\w)/g, luxon: "@@35@@" },
79
+ { moment: /(?<!\w)x(?!\w)/g, luxon: "@@36@@" },
80
+ ];
81
+ // Apply replacements in order
82
+ for (const replacement of tokenReplacements) {
83
+ result = result.replace(replacement.moment, replacement.luxon);
84
+ }
85
+ // Replace numeric placeholders with actual Luxon tokens
86
+ const placeholderMap = {
87
+ "@@1@@": "yyyy", // YYYY
88
+ "@@2@@": "MMMM", // MMMM
89
+ "@@3@@": "cccc", // dddd
90
+ "@@4@@": "ooo", // DDDD
91
+ "@@5@@": "MMM", // MMM
92
+ "@@6@@": "ccc", // ddd
93
+ "@@7@@": "o", // DDD
94
+ "@@8@@": "SSS", // SSS
95
+ "@@9@@": "ZZZZ", // ZZZ
96
+ "@@10@@": "Do", // Do
97
+ "@@11@@": "dd", // DD (day of month)
98
+ "@@12@@": "cc", // dd (weekday)
99
+ "@@13@@": "MM", // MM
100
+ "@@14@@": "yy", // YY
101
+ "@@15@@": "HH", // HH
102
+ "@@16@@": "hh", // hh
103
+ "@@17@@": "mm", // mm
104
+ "@@18@@": "ss", // ss
105
+ "@@19@@": "SS", // SS
106
+ "@@20@@": "WW", // ww
107
+ "@@21@@": "ZZZ", // ZZ
108
+ "@@22@@": "d", // D (day of month)
109
+ "@@23@@": "c", // d (weekday)
110
+ "@@24@@": "M", // M
111
+ "@@25@@": "H", // H
112
+ "@@26@@": "h", // h
113
+ "@@27@@": "m", // m
114
+ "@@28@@": "s", // s
115
+ "@@29@@": "S", // S
116
+ "@@30@@": "a", // A
117
+ "@@31@@": "a", // a
118
+ "@@32@@": "ZZ", // Z
119
+ "@@33@@": "q", // Q
120
+ "@@34@@": "W", // w
121
+ "@@35@@": "X", // X
122
+ "@@36@@": "x", // x
123
+ };
124
+ // Replace all placeholders
125
+ for (const [placeholder, luxonToken] of Object.entries(placeholderMap)) {
126
+ result = result.replace(new RegExp(escapeRegex(placeholder), "g"), luxonToken);
127
+ }
128
+ // Restore escaped characters (Luxon uses single quotes for literals)
129
+ escapedChars.forEach((content, index) => {
130
+ const placeholder = `###ESC${index}###`;
131
+ result = result.replace(placeholder, `'${content}'`);
132
+ });
133
+ return result;
134
+ }
135
+ //# sourceMappingURL=normalizeLegacyFormat.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"normalizeLegacyFormat.js","sourceRoot":"","sources":["../src/normalizeLegacyFormat.ts"],"names":[],"mappings":";AAAA,kCAAkC;;AAYlC,sDAoIC;AA9ID,qDAAqD;AACrD,SAAS,WAAW,CAAC,MAAc;IACjC,OAAO,MAAM,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;AACvD,CAAC;AAED;;;;GAIG;AACH,SAAgB,qBAAqB,CAAC,YAAoB;IACxD,iFAAiF;IACjF,kEAAkE;IAClE,IAAI,YAAY,KAAK,kBAAkB,EAAE,CAAC;QACxC,OAAO,oBAAoB,CAAC;IAC9B,CAAC;IAED,kHAAkH;IAClH,gDAAgD;IAChD,IAAI,sBAAsB,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,sBAAsB,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3F,OAAO,YAAY,CAAC;IACtB,CAAC;IAED,gGAAgG;IAChG,4DAA4D;IAC5D,MAAM,kBAAkB,GAAG,+BA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AAC,OAAO,EAAE,KAAK,EAAE,EAAE;QACtC,MAAM,WAAW,GAAG,SAAS,KAAK,KAAK,CAAC;QACxC,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,IAAI,OAAO,GAAG,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "1.61.2",
7
+ "version": "1.61.3",
8
8
  "description": "Time for 📣 stentor",
9
9
  "types": "lib/index",
10
10
  "main": "lib/index",
@@ -16,6 +16,7 @@
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/chai": "4.3.20",
19
+ "@types/luxon": "3.0.0",
19
20
  "@types/mocha": "10.0.10",
20
21
  "@types/sinon": "17.0.4",
21
22
  "@xapp/config": "0.2.3",
@@ -27,12 +28,10 @@
27
28
  "typescript": "5.9.2"
28
29
  },
29
30
  "dependencies": {
30
- "moment": "2.30.1",
31
- "moment-range": "4.0.2",
32
- "moment-timezone": "0.5.48",
31
+ "luxon": "3.4.0",
33
32
  "stentor-guards": "1.61.2",
34
- "stentor-logger": "1.61.2",
35
- "stentor-utils": "1.61.2"
33
+ "stentor-logger": "1.61.3",
34
+ "stentor-utils": "1.61.3"
36
35
  },
37
36
  "peerDependencies": {
38
37
  "stentor-models": "1.x"
@@ -42,5 +41,5 @@
42
41
  "clean": "rm -rf ./lib/*",
43
42
  "test": "mocha --recursive -r ts-node/register \"./src/**/*.test.ts\""
44
43
  },
45
- "gitHead": "591ba842caec4185be8aa31a4553260cd0e4bbb7"
44
+ "gitHead": "905a49091228ace13cc4e23082f90ac83d53c9fd"
46
45
  }