@fincity/kirun-js 2.7.0 → 2.8.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.
Files changed (101) hide show
  1. package/__tests__/engine/function/system/WaitTest.ts +1 -1
  2. package/__tests__/engine/function/system/array/AddFirstTest.ts +2 -6
  3. package/__tests__/engine/function/system/array/IndexOfTest.ts +8 -13
  4. package/__tests__/engine/function/system/array/LastIndexOfTest.ts +9 -28
  5. package/__tests__/engine/function/system/date/AddSubtractTimeTest.ts +93 -0
  6. package/__tests__/engine/function/system/date/DateFunctionRepositoryTest.ts +96 -0
  7. package/__tests__/engine/function/system/date/DifferenceTest.ts +93 -0
  8. package/__tests__/engine/function/system/date/EpochToTimestampTest.ts +133 -0
  9. package/__tests__/engine/function/system/date/FromDateStringTest.ts +51 -0
  10. package/__tests__/engine/function/system/date/FromNowTest.ts +71 -0
  11. package/__tests__/engine/function/system/date/GetCurrentTimeStampTest.ts +19 -27
  12. package/__tests__/engine/function/system/date/GetNamesTest.ts +105 -0
  13. package/__tests__/engine/function/system/date/IsBetweenTest.ts +53 -0
  14. package/__tests__/engine/function/system/date/IsValidISODateTest.ts +42 -54
  15. package/__tests__/engine/function/system/date/LastFirstOfTest.ts +64 -0
  16. package/__tests__/engine/function/system/date/SetTimeZoneTest.ts +48 -0
  17. package/__tests__/engine/function/system/date/StartEndOfTest.ts +53 -0
  18. package/__tests__/engine/function/system/date/TimeAsTest.ts +24 -0
  19. package/__tests__/engine/function/system/date/TimestampToEpochTest.ts +45 -0
  20. package/__tests__/engine/function/system/date/ToDateStringTest.ts +48 -0
  21. package/__tests__/engine/function/system/math/RandomFloatTest.ts +21 -22
  22. package/__tests__/engine/function/system/math/RandomIntTest.ts +5 -4
  23. package/__tests__/engine/repository/RepositoryFilterTest.ts +4 -1
  24. package/__tests__/engine/runtime/expression/ExpressionEqualityTest.ts +76 -0
  25. package/dist/index.js +1 -1
  26. package/dist/index.js.map +1 -1
  27. package/dist/module.js +1 -1
  28. package/dist/module.js.map +1 -1
  29. package/dist/types.d.ts +14 -13
  30. package/dist/types.d.ts.map +1 -1
  31. package/generator/generateValidationCSV.ts +51 -42
  32. package/generator/validation-js.csv +0 -0
  33. package/package.json +9 -7
  34. package/src/engine/function/AbstractFunction.ts +1 -1
  35. package/src/engine/function/system/Print.ts +19 -5
  36. package/src/engine/function/system/Wait.ts +1 -1
  37. package/src/engine/function/system/array/AbstractArrayFunction.ts +3 -3
  38. package/src/engine/function/system/array/Fill.ts +1 -1
  39. package/src/engine/function/system/array/Frequency.ts +0 -1
  40. package/src/engine/function/system/array/IndexOf.ts +4 -4
  41. package/src/engine/function/system/array/LastIndexOf.ts +3 -3
  42. package/src/engine/function/system/array/Rotate.ts +1 -1
  43. package/src/engine/function/system/array/Shuffle.ts +1 -1
  44. package/src/engine/function/system/array/Sort.ts +1 -1
  45. package/src/engine/function/system/date/AbstractDateFunction.ts +229 -111
  46. package/src/engine/function/system/date/AddSubtractTime.ts +99 -0
  47. package/src/engine/function/system/date/DateFunctionRepository.ts +228 -122
  48. package/src/engine/function/system/date/EpochToTimestamp.ts +75 -0
  49. package/src/engine/function/system/date/FromDateString.ts +44 -0
  50. package/src/engine/function/system/date/FromNow.ts +77 -0
  51. package/src/engine/function/system/date/GetCurrent.ts +20 -0
  52. package/src/engine/function/system/date/GetNames.ts +74 -0
  53. package/src/engine/function/system/date/IsBetween.ts +62 -0
  54. package/src/engine/function/system/date/IsValidISODate.ts +54 -40
  55. package/src/engine/function/system/date/LastFirstOf.ts +72 -0
  56. package/src/engine/function/system/date/SetTimeZone.ts +43 -0
  57. package/src/engine/function/system/date/StartEndOf.ts +44 -0
  58. package/src/engine/function/system/date/TimeAs.ts +64 -0
  59. package/src/engine/function/system/date/TimestampToEpoch.ts +54 -0
  60. package/src/engine/function/system/date/ToDateString.ts +49 -0
  61. package/src/engine/function/system/date/common.ts +9 -0
  62. package/src/engine/function/system/math/MathFunctionRepository.ts +43 -4
  63. package/src/engine/function/system/math/RandomAny.ts +57 -0
  64. package/src/engine/function/system/object/ObjectConvert.ts +45 -19
  65. package/src/engine/function/system/object/ObjectFunctionRepository.ts +2 -1
  66. package/src/engine/function/system/string/AbstractStringFunction.ts +42 -82
  67. package/src/engine/function/system/string/StringFunctionRepository.ts +39 -20
  68. package/src/engine/json/schema/SchemaUtil.ts +1 -1
  69. package/src/engine/repository/KIRunSchemaRepository.ts +57 -3
  70. package/src/engine/runtime/expression/tokenextractor/TokenValueExtractor.ts +14 -21
  71. package/src/engine/util/string/StringUtil.ts +12 -0
  72. package/tsconfig.json +1 -2
  73. package/__tests__/engine/function/system/date/AddTimeTest.ts +0 -115
  74. package/__tests__/engine/function/system/date/DateToEpochTest.ts +0 -74
  75. package/__tests__/engine/function/system/date/DifferenceOfTimestampsTest.ts +0 -53
  76. package/__tests__/engine/function/system/date/EpochToDateTest.ts +0 -105
  77. package/__tests__/engine/function/system/date/GetDateTest.ts +0 -85
  78. package/__tests__/engine/function/system/date/GetDayTest.ts +0 -85
  79. package/__tests__/engine/function/system/date/GetFullYearTest.ts +0 -85
  80. package/__tests__/engine/function/system/date/GetHoursTest.ts +0 -85
  81. package/__tests__/engine/function/system/date/GetMilliSecondsTest.ts +0 -85
  82. package/__tests__/engine/function/system/date/GetMinutesTest.ts +0 -85
  83. package/__tests__/engine/function/system/date/GetMonthTest.ts +0 -85
  84. package/__tests__/engine/function/system/date/GetSecondsTest.ts +0 -85
  85. package/__tests__/engine/function/system/date/GetTimeAsArrayTest.ts +0 -59
  86. package/__tests__/engine/function/system/date/GetTimeAsObjectTest.ts +0 -83
  87. package/__tests__/engine/function/system/date/GetTimeTest.ts +0 -86
  88. package/__tests__/engine/function/system/date/IsLeapYearTest.ts +0 -85
  89. package/__tests__/engine/function/system/date/MaximumTimestampTest.ts +0 -55
  90. package/__tests__/engine/function/system/date/MinimumTimestampTest.ts +0 -54
  91. package/__tests__/engine/function/system/date/SubtractTimeTest.ts +0 -95
  92. package/src/engine/function/system/date/DateToEpoch.ts +0 -39
  93. package/src/engine/function/system/date/DifferenceOfTimestamps.ts +0 -45
  94. package/src/engine/function/system/date/EpochToDate.ts +0 -76
  95. package/src/engine/function/system/date/GetCurrentTimeStamp.ts +0 -36
  96. package/src/engine/function/system/date/GetTimeAsArray.ts +0 -48
  97. package/src/engine/function/system/date/GetTimeAsObject.ts +0 -66
  98. package/src/engine/function/system/date/MaximumTimestamp.ts +0 -73
  99. package/src/engine/function/system/date/MinimumTimestamp.ts +0 -74
  100. package/src/engine/function/system/math/RandomFloat.ts +0 -56
  101. package/src/engine/function/system/math/RandomInt.ts +0 -56
@@ -1,147 +1,254 @@
1
- import { Namespaces } from "../../../namespaces/Namespaces";
2
- import { Repository } from "../../../Repository";
3
- import { MapUtil } from "../../../util/MapUtil";
4
- import { AbstractDateFunction } from "./AbstractDateFunction";
1
+ import { Namespaces } from '../../../namespaces/Namespaces';
2
+ import { Event } from '../../../model/Event';
3
+ import { Repository } from '../../../Repository';
4
+ import { MapUtil } from '../../../util/MapUtil';
5
5
  import { Function } from '../../Function';
6
- import { KIRuntimeException } from "../../../exception/KIRuntimeException";
7
- import mapEntry from '../../../util/mapEntry';
8
- import { DateToEpoch } from "./DateToEpoch";
9
- import { DifferenceOfTimestamps } from "./DifferenceOfTimestamps";
10
- import { EpochToDate } from "./EpochToDate";
11
- import { GetCurrentTimeStamp } from "./GetCurrentTimeStamp";
12
- import { GetTimeAsArray } from "./GetTimeAsArray";
13
- import { GetTimeAsObject } from "./GetTimeAsObject";
14
- import { IsValidISODate } from "./IsValidISODate";
15
- import { MaximumTimestamp } from "./MaximumTimestamp";
16
- import { MinimumTimestamp } from "./MinimumTimestamp";
17
-
6
+ import { AbstractDateFunction } from './AbstractDateFunction';
7
+ import { AddSubtractTime } from './AddSubtractTime';
8
+ import { getDateTime } from './common';
9
+ import { EpochToTimestamp } from './EpochToTimestamp';
10
+ import { TimestampToEpoch } from './TimestampToEpoch';
11
+ import { ToDateString } from './ToDateString';
12
+ import { Schema } from '../../../json/schema/Schema';
13
+ import { DateTimeUnit, DurationUnits } from 'luxon';
14
+ import { SetTimeZone } from './SetTimeZone';
15
+ import { IsBetween } from './IsBetween';
16
+ import { LastFirstOf } from './LastFirstOf';
17
+ import { TimeAs } from './TimeAs';
18
+ import { StartEndOf } from './StartEndOf';
19
+ import { GetNames } from './GetNames';
20
+ import { IsValidISODate } from './IsValidISODate';
21
+ import { FromNow } from './FromNow';
22
+ import { FromDateString } from './FromDateString';
23
+ import { GetCurrentTimestamp } from './GetCurrent';
18
24
 
19
25
  export class DateFunctionRepository implements Repository<Function> {
20
-
21
26
  private static readonly repoMap: Map<string, Function> = MapUtil.ofArrayEntries(
27
+ ['EpochSecondsToTimestamp', new EpochToTimestamp('EpochSecondsToTimestamp', true)],
28
+ [
29
+ 'EpochMillisecondsToTimestamp',
30
+ new EpochToTimestamp('EpochMillisecondsToTimestamp', false),
31
+ ],
32
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
33
+ 'GetDay',
34
+ (isoTimestamp: string) => getDateTime(isoTimestamp).day,
35
+ ),
36
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
37
+ 'GetDayOfWeek',
38
+ (isoTimestamp: string) => getDateTime(isoTimestamp).weekday,
39
+ ),
40
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
41
+ 'GetMonth',
42
+ (isoTimestamp: string) => getDateTime(isoTimestamp).month,
43
+ ),
44
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
45
+ 'GetYear',
46
+ (isoTimestamp: string) => getDateTime(isoTimestamp).year,
47
+ ),
48
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
49
+ 'GetHours',
50
+ (isoTimestamp: string) => getDateTime(isoTimestamp).hour,
51
+ ),
52
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
53
+ 'GetMinutes',
54
+ (isoTimestamp: string) => getDateTime(isoTimestamp).minute,
55
+ ),
56
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
57
+ 'GetSeconds',
58
+ (isoTimestamp: string) => getDateTime(isoTimestamp).second,
59
+ ),
60
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
61
+ 'GetMilliseconds',
62
+ (isoTimestamp: string) => getDateTime(isoTimestamp).millisecond,
63
+ ),
64
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
65
+ 'GetDaysInMonth',
66
+ (isoTimestamp: string) => getDateTime(isoTimestamp).daysInMonth!,
67
+ ),
68
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
69
+ 'GetDaysInYear',
70
+ (isoTimestamp: string) => getDateTime(isoTimestamp).daysInYear!,
71
+ ),
72
+ ['TimestampToEpochSeconds', new TimestampToEpoch('TimestampToEpochSeconds', true)],
73
+ [
74
+ 'TimestampToEpochMilliseconds',
75
+ new TimestampToEpoch('TimestampToEpochMilliseconds', false),
76
+ ],
77
+ AbstractDateFunction.ofEntryTimestampAndStringOutput(
78
+ 'GetTimeZoneName',
79
+ (isoTimestamp: string) => getDateTime(isoTimestamp).zoneName!,
80
+ ),
81
+ AbstractDateFunction.ofEntryTimestampAndStringOutput(
82
+ 'GetTimeZoneOffsetLong',
83
+ (isoTimestamp: string) => getDateTime(isoTimestamp).offsetNameLong!,
84
+ ),
85
+ AbstractDateFunction.ofEntryTimestampAndStringOutput(
86
+ 'GetTimeZoneOffsetShort',
87
+ (isoTimestamp: string) => getDateTime(isoTimestamp).offsetNameShort!,
88
+ ),
89
+ AbstractDateFunction.ofEntryTimestampAndIntegerOutput(
90
+ 'GetTimeZoneOffset',
91
+ (isoTimestamp: string) => getDateTime(isoTimestamp).offset,
92
+ ),
93
+ ['ToDateString', new ToDateString()],
94
+ ['AddTime', new AddSubtractTime(true)],
95
+ ['SubtractTime', new AddSubtractTime(false)],
96
+ ['GetCurrentTimestamp', new GetCurrentTimestamp()],
22
97
 
23
- mapEntry(new DateToEpoch()),
24
- mapEntry(new EpochToDate()),
25
- mapEntry(new GetCurrentTimeStamp()),
26
- mapEntry(new DifferenceOfTimestamps()),
27
- mapEntry(new IsValidISODate()),
28
- mapEntry(new GetTimeAsArray()),
29
- mapEntry(new GetTimeAsObject()),
30
- mapEntry(new MaximumTimestamp()),
31
- mapEntry(new MinimumTimestamp()),
32
-
33
- AbstractDateFunction.ofEntryDateAndBooleanOutput("IsLeapYear", date => {
34
- const year = new Date(date).getUTCFullYear();
35
- return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
36
- }),
37
-
38
- AbstractDateFunction.ofEntryDateAndIntegerOutput("GetDate", date => new Date(date).getUTCDate()),
98
+ AbstractDateFunction.ofEntryTimestampTimestampAndTOutput<any>(
99
+ 'Difference',
100
+ new Event(
101
+ Event.OUTPUT,
102
+ MapUtil.of(
103
+ AbstractDateFunction.EVENT_RESULT_NAME,
104
+ Schema.ofRef(`${Namespaces.DATE}.Duration`),
105
+ ),
106
+ ),
107
+ (ts1: string, ts2: string, extraParams: any[]) => {
108
+ const dt1 = getDateTime(ts1);
109
+ const dt2 = getDateTime(ts2);
110
+ let units: DurationUnits | undefined = undefined;
111
+ if (extraParams?.[0]?.length) {
112
+ units = extraParams[0]
113
+ ?.filter((e: any) => !!e)
114
+ .map((e: string) => e.toLowerCase() as DateTimeUnit);
115
+ }
116
+ const duration = dt1.diff(dt2, units);
117
+ return duration.toObject();
118
+ },
119
+ AbstractDateFunction.PARAMETER_VARIABLE_UNIT,
120
+ ),
39
121
 
40
- AbstractDateFunction.ofEntryDateAndIntegerOutput("GetDay", date => new Date(date).getUTCDay()),
122
+ AbstractDateFunction.ofEntryTimestampIntegerAndTimestampOutput(
123
+ 'SetDay',
124
+ (isoTimestamp: string, day: number) => getDateTime(isoTimestamp).set({ day }).toISO()!,
125
+ ),
41
126
 
42
- AbstractDateFunction.ofEntryDateAndIntegerOutput("GetFullYear", date => new Date(date).getUTCFullYear()),
127
+ AbstractDateFunction.ofEntryTimestampIntegerAndTimestampOutput(
128
+ 'SetMonth',
129
+ (isoTimestamp: string, month: number) =>
130
+ getDateTime(isoTimestamp).set({ month }).toISO()!,
131
+ ),
43
132
 
44
- AbstractDateFunction.ofEntryDateAndIntegerOutput("GetMonth", date => new Date(date).getUTCMonth()),
133
+ AbstractDateFunction.ofEntryTimestampIntegerAndTimestampOutput(
134
+ 'SetYear',
135
+ (isoTimestamp: string, year: number) =>
136
+ getDateTime(isoTimestamp).set({ year }).toISO()!,
137
+ ),
45
138
 
46
- AbstractDateFunction.ofEntryDateAndIntegerOutput("GetHours", date => new Date(date).getUTCHours()),
139
+ AbstractDateFunction.ofEntryTimestampIntegerAndTimestampOutput(
140
+ 'SetHours',
141
+ (isoTimestamp: string, hour: number) =>
142
+ getDateTime(isoTimestamp).set({ hour }).toISO()!,
143
+ ),
47
144
 
48
- AbstractDateFunction.ofEntryDateAndIntegerOutput("GetMinutes", date => new Date(date).getUTCMinutes()),
145
+ AbstractDateFunction.ofEntryTimestampIntegerAndTimestampOutput(
146
+ 'SetMinutes',
147
+ (isoTimestamp: string, minute: number) =>
148
+ getDateTime(isoTimestamp).set({ minute }).toISO()!,
149
+ ),
49
150
 
50
- AbstractDateFunction.ofEntryDateAndIntegerOutput("GetSeconds", date => new Date(date).getUTCSeconds()),
151
+ AbstractDateFunction.ofEntryTimestampIntegerAndTimestampOutput(
152
+ 'SetSeconds',
153
+ (isoTimestamp: string, second: number) =>
154
+ getDateTime(isoTimestamp).set({ second }).toISO()!,
155
+ ),
51
156
 
52
- AbstractDateFunction.ofEntryDateAndIntegerOutput("GetMilliSeconds", date => new Date(date).getUTCMilliseconds()),
157
+ AbstractDateFunction.ofEntryTimestampIntegerAndTimestampOutput(
158
+ 'SetMilliseconds',
159
+ (isoTimestamp: string, millisecond: number) =>
160
+ getDateTime(isoTimestamp).set({ millisecond }).toISO()!,
161
+ ),
53
162
 
54
- AbstractDateFunction.ofEntryDateAndIntegerOutput("GetTime" , date => new Date(date).getTime()),
163
+ ['SetTimeZone', new SetTimeZone()],
55
164
 
56
- AbstractDateFunction.ofEntryDateWithIntegerUnitWithOutputName("AddTime",
57
- (date , value , unit) => this.changeAmountToUnit(date, unit, value, true)
165
+ AbstractDateFunction.ofEntryTimestampTimestampAndTOutput<boolean>(
166
+ 'IsBefore',
167
+ new Event(
168
+ Event.OUTPUT,
169
+ MapUtil.of(
170
+ AbstractDateFunction.EVENT_RESULT_NAME,
171
+ Schema.ofBoolean(AbstractDateFunction.EVENT_RESULT_NAME),
172
+ ),
173
+ ),
174
+ (t1: string, t2: string) => getDateTime(t1) < getDateTime(t2),
58
175
  ),
59
176
 
60
- AbstractDateFunction.ofEntryDateWithIntegerUnitWithOutputName("SubtractTime",
61
- (date , value , unit) => this.changeAmountToUnit(date, unit, value, false)
62
- )
63
- );
177
+ AbstractDateFunction.ofEntryTimestampTimestampAndTOutput<boolean>(
178
+ 'IsAfter',
179
+ new Event(
180
+ Event.OUTPUT,
181
+ MapUtil.of(
182
+ AbstractDateFunction.EVENT_RESULT_NAME,
183
+ Schema.ofBoolean(AbstractDateFunction.EVENT_RESULT_NAME),
184
+ ),
185
+ ),
186
+ (t1: string, t2: string) => getDateTime(t1) > getDateTime(t2),
187
+ ),
64
188
 
65
- private static readonly filterableNames = Array.from(
66
- DateFunctionRepository.repoMap.values(),
67
- ).map((e) => e.getSignature().getFullName());
189
+ AbstractDateFunction.ofEntryTimestampTimestampAndTOutput<boolean>(
190
+ 'IsSame',
191
+ new Event(
192
+ Event.OUTPUT,
193
+ MapUtil.of(
194
+ AbstractDateFunction.EVENT_RESULT_NAME,
195
+ Schema.ofBoolean(AbstractDateFunction.EVENT_RESULT_NAME),
196
+ ),
197
+ ),
198
+ (t1: string, t2: string) => getDateTime(t1) === getDateTime(t2),
199
+ ),
68
200
 
69
- private static changeAmountToUnit(inputDate: string , unit: string, value: number, isAdd: boolean): string{
70
-
71
- const amount = isAdd ? value : -1 * value;
72
-
73
- const date = this.getFullUTCDate(inputDate);
74
- const originalOffset = this.getTimezoneOffset(inputDate);
75
-
76
- switch(unit){
77
-
78
- case "MILLISECOND" : date.setMilliseconds(date.getMilliseconds() + amount);
79
- break;
80
- case "SECOND" : date.setSeconds(date.getSeconds() + amount);
81
- break;
82
- case "MINUTE" : date.setMinutes(date.getMinutes() + amount);
83
- break;
84
- case "HOUR" : date.setHours(date.getHours() + amount);
85
- break;
86
- case "DAY" : date.setDate(date.getDate() + amount);
87
- break;
88
- case "MONTH" : date.setMonth(date.getMonth() + amount);
89
- break;
90
- case "YEAR" : date.setFullYear(date.getFullYear() + amount);
91
- break;
92
- default :
93
- throw new KIRuntimeException("No such unit: " + unit)
201
+ AbstractDateFunction.ofEntryTimestampTimestampAndTOutput<boolean>(
202
+ 'IsSameOrBefore',
203
+ new Event(
204
+ Event.OUTPUT,
205
+ MapUtil.of(
206
+ AbstractDateFunction.EVENT_RESULT_NAME,
207
+ Schema.ofBoolean(AbstractDateFunction.EVENT_RESULT_NAME),
208
+ ),
209
+ ),
210
+ (t1: string, t2: string) => getDateTime(t1) <= getDateTime(t2),
211
+ ),
94
212
 
95
- }
213
+ AbstractDateFunction.ofEntryTimestampTimestampAndTOutput<boolean>(
214
+ 'IsSameOrAfter',
215
+ new Event(
216
+ Event.OUTPUT,
217
+ MapUtil.of(
218
+ AbstractDateFunction.EVENT_RESULT_NAME,
219
+ Schema.ofBoolean(AbstractDateFunction.EVENT_RESULT_NAME),
220
+ ),
221
+ ),
222
+ (t1: string, t2: string) => getDateTime(t1) >= getDateTime(t2),
223
+ ),
96
224
 
97
- return this.formatDate(date, originalOffset);
98
- }
99
-
100
- private static getTimezoneOffset(dateString: string): string {
101
- const lastChar = dateString.charAt(dateString.length - 1);
102
- if (lastChar === 'Z') return '+00:00';
103
-
104
- const offsetStart = dateString.indexOf('+') !== -1 ? dateString.lastIndexOf('+') : dateString.lastIndexOf('-');
105
- if (offsetStart === -1) return '+00:00';
106
-
107
- let offset = dateString.substring(offsetStart);
108
- if (offset.length === 3) {
109
- offset = offset.substring(0, 1) + '0' + offset.substring(1) + ':00';
110
- } else if (offset.length === 5) {
111
- offset = offset.substring(0, 3) + ':' + offset.substring(3);
112
- }
113
-
114
- return offset;
115
- }
225
+ AbstractDateFunction.ofEntryTimestampAndBooleanOutput(
226
+ 'IsInLeapYear',
227
+ (isoTimestamp: string) => getDateTime(isoTimestamp).isInLeapYear,
228
+ ),
116
229
 
117
- private static getFullUTCDate(inputDate:string): Date {
230
+ AbstractDateFunction.ofEntryTimestampAndBooleanOutput(
231
+ 'IsInDST',
232
+ (isoTimestamp: string) => getDateTime(isoTimestamp).isInDST,
233
+ ),
118
234
 
119
- if(inputDate.lastIndexOf('+') !== -1)
120
- inputDate = inputDate.substring(0, inputDate.lastIndexOf('+')) + 'Z';
121
- else if(inputDate.lastIndexOf('-') !== -1)
122
- inputDate = inputDate.substring(0, inputDate.lastIndexOf('-')) + 'Z';
235
+ ['IsBetween', new IsBetween()],
236
+ ['LastOf', new LastFirstOf(true)],
237
+ ['FirstOf', new LastFirstOf(false)],
238
+ ['StartOf', new StartEndOf(true)],
239
+ ['EndOf', new StartEndOf(false)],
240
+ ['TimeAsObject', new TimeAs(false)],
241
+ ['TimeAsArray', new TimeAs(true)],
242
+ ['GetNames', new GetNames()],
243
+ ['IsValidISODate', new IsValidISODate()],
244
+ ['FromNow', new FromNow()],
245
+ ['FromDateString', new FromDateString()],
246
+ );
123
247
 
124
- const date: Date = new Date(inputDate);
125
-
126
- return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1,
127
- date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(),
128
- date.getUTCSeconds(), date.getUTCMilliseconds()));
129
- }
248
+ private static readonly filterableNames = Array.from(
249
+ DateFunctionRepository.repoMap.values(),
250
+ ).map((e) => e.getSignature().getFullName());
130
251
 
131
- private static formatDate(date: Date, offset: string): string {
132
- const pad = (num: number) => num.toString().padStart(2, '0');
133
-
134
- const year = date.getUTCMonth() === 0 ? date.getUTCFullYear() - 1 : date.getUTCFullYear();
135
- const month = pad(date.getUTCMonth() === 0 ? 12 : date.getUTCMonth());
136
- const day = pad(date.getUTCDate());
137
- const hours = pad(date.getUTCHours());
138
- const minutes = pad(date.getUTCMinutes());
139
- const seconds = pad(date.getUTCSeconds());
140
- const milliseconds = pad(date.getUTCMilliseconds());
141
-
142
- return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}${offset}`;
143
- }
144
-
145
252
  find(namespace: string, name: string): Promise<Function | undefined> {
146
253
  if (namespace != Namespaces.DATE) {
147
254
  return Promise.resolve(undefined);
@@ -149,11 +256,10 @@ export class DateFunctionRepository implements Repository<Function> {
149
256
  return Promise.resolve(DateFunctionRepository.repoMap.get(name));
150
257
  }
151
258
  filter(name: string): Promise<string[]> {
152
-
153
259
  return Promise.resolve(
154
260
  DateFunctionRepository.filterableNames.filter(
155
261
  (e) => e.toLowerCase().indexOf(name.toLowerCase()) !== -1,
156
262
  ),
157
263
  );
158
264
  }
159
- }
265
+ }
@@ -0,0 +1,75 @@
1
+ import { Schema } from '../../../json/schema/Schema';
2
+ import { SchemaType } from '../../../json/schema/type/SchemaType';
3
+ import { TypeUtil } from '../../../json/schema/type/TypeUtil';
4
+ import { EventResult } from '../../../model/EventResult';
5
+ import { FunctionOutput } from '../../../model/FunctionOutput';
6
+ import { FunctionSignature } from '../../../model/FunctionSignature';
7
+ import { Parameter } from '../../../model/Parameter';
8
+ import { Namespaces } from '../../../namespaces/Namespaces';
9
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
10
+ import { MapUtil } from '../../../util/MapUtil';
11
+ import { AbstractFunction } from '../../AbstractFunction';
12
+ import { AbstractDateFunction } from './AbstractDateFunction';
13
+
14
+ export class EpochToTimestamp extends AbstractFunction {
15
+ private readonly signature: FunctionSignature;
16
+ private readonly isSeconds: boolean;
17
+ private readonly paramName: string;
18
+
19
+ constructor(name: string, isSeconds: boolean) {
20
+ super();
21
+ this.paramName = `epoch${isSeconds ? 'Seconds' : 'Milliseconds'}`;
22
+ this.isSeconds = isSeconds;
23
+ this.signature = new FunctionSignature(name)
24
+ .setNamespace(Namespaces.DATE)
25
+ .setParameters(
26
+ new Map([
27
+ [
28
+ this.paramName,
29
+ Parameter.of(
30
+ this.paramName,
31
+ new Schema()
32
+ .setName(this.paramName)
33
+ .setType(
34
+ TypeUtil.of(
35
+ SchemaType.LONG,
36
+ SchemaType.INTEGER,
37
+ SchemaType.STRING,
38
+ ),
39
+ ),
40
+ ),
41
+ ],
42
+ ]),
43
+ )
44
+ .setEvents(
45
+ new Map([
46
+ [
47
+ AbstractDateFunction.EVENT_TIMESTAMP_NAME,
48
+ AbstractDateFunction.EVENT_TIMESTAMP,
49
+ ],
50
+ ]),
51
+ );
52
+ }
53
+
54
+ public getSignature(): FunctionSignature {
55
+ return this.signature;
56
+ }
57
+
58
+ protected internalExecute(context: FunctionExecutionParameters): Promise<FunctionOutput> {
59
+ const epoch = parseInt(context.getArguments()?.get(this.paramName));
60
+ const timestamp = this.isSeconds ? epoch * 1000 : epoch;
61
+ if (isNaN(timestamp)) {
62
+ throw new Error(`Please provide a valid value for ${this.paramName}.`);
63
+ }
64
+ return Promise.resolve(
65
+ new FunctionOutput([
66
+ EventResult.outputOf(
67
+ MapUtil.of(
68
+ AbstractDateFunction.EVENT_TIMESTAMP_NAME,
69
+ new Date(timestamp).toISOString(),
70
+ ),
71
+ ),
72
+ ]),
73
+ );
74
+ }
75
+ }
@@ -0,0 +1,44 @@
1
+ import { DateTime } from 'luxon';
2
+ import { Schema } from '../../../json/schema/Schema';
3
+ import { EventResult } from '../../../model/EventResult';
4
+ import { FunctionOutput } from '../../../model/FunctionOutput';
5
+ import { Parameter } from '../../../model/Parameter';
6
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
7
+ import { MapUtil } from '../../../util/MapUtil';
8
+ import { AbstractDateFunction } from './AbstractDateFunction';
9
+
10
+ export class FromDateString extends AbstractDateFunction {
11
+ public static readonly PARAMETER_FORMAT_NAME = 'format';
12
+
13
+ public static readonly PARAMETER_TIMESTAMP_STRING_NAME = 'timestampString';
14
+
15
+ constructor() {
16
+ super(
17
+ 'FromDateString',
18
+ AbstractDateFunction.EVENT_TIMESTAMP,
19
+ Parameter.of(
20
+ FromDateString.PARAMETER_TIMESTAMP_STRING_NAME,
21
+ Schema.ofString(FromDateString.PARAMETER_TIMESTAMP_STRING_NAME),
22
+ ),
23
+ Parameter.of(
24
+ FromDateString.PARAMETER_FORMAT_NAME,
25
+ Schema.ofString(FromDateString.PARAMETER_FORMAT_NAME),
26
+ ),
27
+ );
28
+ }
29
+ protected async internalExecute(context: FunctionExecutionParameters): Promise<FunctionOutput> {
30
+ const timestampString = context
31
+ .getArguments()
32
+ ?.get(FromDateString.PARAMETER_TIMESTAMP_STRING_NAME);
33
+
34
+ const format = context.getArguments()?.get(FromDateString.PARAMETER_FORMAT_NAME);
35
+
36
+ const dateTime = DateTime.fromFormat(timestampString, format);
37
+
38
+ return new FunctionOutput([
39
+ EventResult.outputOf(
40
+ MapUtil.of(AbstractDateFunction.EVENT_RESULT_NAME, dateTime.toISO()),
41
+ ),
42
+ ]);
43
+ }
44
+ }
@@ -0,0 +1,77 @@
1
+ import { DateTime } from 'luxon';
2
+ import { Schema } from '../../../json/schema/Schema';
3
+ import { EventResult } from '../../../model/EventResult';
4
+ import { FunctionOutput } from '../../../model/FunctionOutput';
5
+ import { Parameter } from '../../../model/Parameter';
6
+ import { Namespaces } from '../../../namespaces/Namespaces';
7
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
8
+ import { MapUtil } from '../../../util/MapUtil';
9
+ import { AbstractDateFunction } from './AbstractDateFunction';
10
+
11
+ export class FromNow extends AbstractDateFunction {
12
+ public static readonly PARAMETER_FROM_NAME = 'from';
13
+ public static readonly PARAMETER_FROM = new Parameter(
14
+ FromNow.PARAMETER_FROM_NAME,
15
+ Schema.ofRef(Namespaces.DATE + '.Timestamp').setDefaultValue(''),
16
+ );
17
+ public static readonly PARAMETER_LOCALE_NAME = 'locale';
18
+ public static readonly PARAMETER_LOCALE = new Parameter(
19
+ FromNow.PARAMETER_LOCALE_NAME,
20
+ Schema.ofString(FromNow.PARAMETER_LOCALE_NAME).setDefaultValue('system'),
21
+ );
22
+
23
+ public static readonly PARAMETER_FORMAT_NAME = 'format';
24
+ public static readonly PARAMETER_FORMAT = new Parameter(
25
+ FromNow.PARAMETER_FORMAT_NAME,
26
+ Schema.ofString(FromNow.PARAMETER_FORMAT_NAME)
27
+ .setEnums(['LONG', 'SHORT', 'NARROW'])
28
+ .setDefaultValue('LONG'),
29
+ );
30
+
31
+ public static readonly PARAMETER_ROUND_NAME = 'round';
32
+ public static readonly PARAMETER_ROUND = new Parameter(
33
+ FromNow.PARAMETER_ROUND_NAME,
34
+ Schema.ofBoolean(FromNow.PARAMETER_ROUND_NAME).setDefaultValue(true),
35
+ );
36
+
37
+ public constructor() {
38
+ super(
39
+ 'FromNow',
40
+ AbstractDateFunction.EVENT_STRING,
41
+ AbstractDateFunction.PARAMETER_TIMESTAMP,
42
+ FromNow.PARAMETER_FORMAT,
43
+ FromNow.PARAMETER_FROM,
44
+ AbstractDateFunction.PARAMETER_VARIABLE_UNIT,
45
+ FromNow.PARAMETER_ROUND,
46
+ FromNow.PARAMETER_LOCALE,
47
+ );
48
+ }
49
+
50
+ public internalExecute(context: FunctionExecutionParameters): Promise<FunctionOutput> {
51
+ const from1 = context.getArguments()?.get(FromNow.PARAMETER_FROM_NAME);
52
+ const fromDate = from1 === '' ? DateTime.now() : DateTime.fromISO(from1);
53
+ const given = context.getArguments()?.get(AbstractDateFunction.PARAMETER_TIMESTAMP_NAME);
54
+ const givenDate = DateTime.fromISO(given);
55
+
56
+ const units = context.getArguments()?.get(FromNow.PARAMETER_UNIT_NAME);
57
+ const format = context.getArguments()?.get(FromNow.PARAMETER_FORMAT_NAME);
58
+ const round = context.getArguments()?.get(FromNow.PARAMETER_ROUND_NAME);
59
+ const locale = context.getArguments()?.get(FromNow.PARAMETER_LOCALE_NAME);
60
+
61
+ const options: any = { base: fromDate, style: format?.toLowerCase(), round, locale };
62
+ if (units?.length > 0) {
63
+ options.unit = units.map((e: string) => e.toLowerCase());
64
+ }
65
+
66
+ return Promise.resolve(
67
+ new FunctionOutput([
68
+ EventResult.outputOf(
69
+ MapUtil.of(
70
+ AbstractDateFunction.EVENT_RESULT_NAME,
71
+ givenDate.toRelative(options) ?? 'Unknown',
72
+ ),
73
+ ),
74
+ ]),
75
+ );
76
+ }
77
+ }
@@ -0,0 +1,20 @@
1
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
2
+ import { EventResult } from '../../../model/EventResult';
3
+ import { FunctionOutput } from '../../../model/FunctionOutput';
4
+ import { AbstractDateFunction } from './AbstractDateFunction';
5
+ import { MapUtil } from '../../../util/MapUtil';
6
+ import { DateTime } from 'luxon';
7
+
8
+ export class GetCurrentTimestamp extends AbstractDateFunction {
9
+ public constructor() {
10
+ super('GetCurrentTimestamp', AbstractDateFunction.EVENT_TIMESTAMP);
11
+ }
12
+
13
+ protected async internalExecute(context: FunctionExecutionParameters): Promise<FunctionOutput> {
14
+ return new FunctionOutput([
15
+ EventResult.outputOf(
16
+ MapUtil.of(AbstractDateFunction.EVENT_TIMESTAMP_NAME, DateTime.now().toISO()),
17
+ ),
18
+ ]);
19
+ }
20
+ }