-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathexpression-functions.ts
996 lines (913 loc) · 31.7 KB
/
expression-functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
import dot from 'dot-object';
import escapeStringRegexp from 'escape-string-regexp';
import { ContextNotProvided } from 'src/core/contexts/context';
import { exprCastValue } from 'src/features/expressions';
import { ExprRuntimeError, NodeRelationNotFound } from 'src/features/expressions/errors';
import { ExprVal } from 'src/features/expressions/types';
import { addError } from 'src/features/expressions/validation';
import { makeIndexedId } from 'src/features/form/layout/utils/makeIndexedId';
import { CodeListPending } from 'src/features/options/CodeListsProvider';
import { SearchParams } from 'src/features/routing/AppRoutingContext';
import { buildAuthContext } from 'src/utils/authContext';
import { transposeDataBinding } from 'src/utils/databindings/DataBinding';
import { formatDateLocale } from 'src/utils/formatDateLocale';
import type { EvaluateExpressionParams } from 'src/features/expressions';
import type {
AnyExprArg,
ExprArgDef,
ExprDate,
ExprFunctionName,
ExprFunctions,
ExprValToActual,
} from 'src/features/expressions/types';
import type { ValidationContext } from 'src/features/expressions/validation';
import type { IDataModelReference } from 'src/layout/common.generated';
import type { IAuthContext, IInstanceDataSources } from 'src/types/shared';
import type { ExpressionDataSources } from 'src/utils/layout/useExpressionDataSources';
type ArgsToActual<T extends readonly AnyExprArg[]> = {
[Index in keyof T]: T[Index]['variant'] extends 'optional'
? ExprValToActual<T[Index]['type']> | null | undefined
: ExprValToActual<T[Index]['type']> | null;
};
export type AnyFuncDef = FuncDef<readonly AnyExprArg[], ExprVal>;
export interface FuncDef<Args extends readonly AnyExprArg[], Ret extends ExprVal> {
args: Args;
returns: Ret;
needs: readonly (keyof ExpressionDataSources)[];
}
export interface FuncValidationDef {
// Optional: Validator function which runs when the function is validated. This allows a function to add its own
// validation requirements. Use the addError() function if any errors are found.
// This runs after the automatic 'number of arguments validator', and will not run if the automatic validator fails.
validator?: (options: {
rawArgs: unknown[];
argTypes: (ExprVal | undefined)[];
ctx: ValidationContext;
path: string[];
}) => void;
// Optional: Set this to false if the automatic 'number of arguments validator' should NOT be run for this function.
// Defaults to true.
runNumArgsValidator?: boolean;
}
function required<T extends ExprVal>(type: T): ExprArgDef<T, 'required'> {
return { type, variant: 'required' };
}
function optional<T extends ExprVal>(type: T): ExprArgDef<T, 'optional'> {
return { type, variant: 'optional' };
}
function rest<T extends ExprVal>(type: T): ExprArgDef<T, 'rest'> {
return { type, variant: 'rest' };
}
function args<A extends readonly AnyExprArg[]>(...args: A): A {
return args;
}
type Source = keyof ExpressionDataSources;
function dataSources<S extends readonly Source[]>(...sources: S): S {
return sources;
}
const noSources: never[] = [];
/**
* All the function definitions available in expressions. The implementations themselves are located in
* @see ExprFunctionImplementations
*/
export const ExprFunctionDefinitions = {
argv: {
args: args(required(ExprVal.Number)),
returns: ExprVal.Any,
needs: noSources,
},
value: {
args: args(optional(ExprVal.String)),
returns: ExprVal.Any,
needs: noSources,
},
equals: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.Boolean,
needs: noSources,
},
notEquals: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.Boolean,
needs: noSources,
},
not: {
args: args(required(ExprVal.Boolean)),
returns: ExprVal.Boolean,
needs: noSources,
},
greaterThan: {
args: args(required(ExprVal.Number), required(ExprVal.Number)),
returns: ExprVal.Boolean,
needs: noSources,
},
greaterThanEq: {
args: args(required(ExprVal.Number), required(ExprVal.Number)),
returns: ExprVal.Boolean,
needs: noSources,
},
lessThan: {
args: args(required(ExprVal.Number), required(ExprVal.Number)),
returns: ExprVal.Boolean,
needs: noSources,
},
lessThanEq: {
args: args(required(ExprVal.Number), required(ExprVal.Number)),
returns: ExprVal.Boolean,
needs: noSources,
},
concat: {
args: args(rest(ExprVal.String)),
returns: ExprVal.String,
needs: noSources,
},
and: {
args: args(required(ExprVal.Boolean), rest(ExprVal.Boolean)),
returns: ExprVal.Boolean,
needs: noSources,
},
or: {
args: args(required(ExprVal.Boolean), rest(ExprVal.Boolean)),
returns: ExprVal.Boolean,
needs: noSources,
},
if: {
args: args(required(ExprVal.Boolean), required(ExprVal.Any), optional(ExprVal.String), optional(ExprVal.Any)),
returns: ExprVal.Any,
needs: noSources,
},
instanceContext: {
args: args(required(ExprVal.String)),
returns: ExprVal.String,
needs: dataSources('instanceDataSources'),
},
frontendSettings: {
args: args(required(ExprVal.String)),
returns: ExprVal.Any,
needs: dataSources('applicationSettings'),
},
authContext: {
args: args(required(ExprVal.String)),
returns: ExprVal.Boolean,
needs: dataSources('process'),
},
component: {
args: args(required(ExprVal.String)),
returns: ExprVal.Any,
needs: dataSources(
'layoutLookups',
'currentDataModelPath',
'isHiddenSelector',
'dataModelNames',
'formDataSelector',
),
},
dataModel: {
args: args(required(ExprVal.String), optional(ExprVal.String)),
returns: ExprVal.Any,
needs: dataSources('defaultDataType', 'currentDataModelPath', 'dataModelNames', 'formDataSelector'),
},
countDataElements: {
args: args(required(ExprVal.String)),
returns: ExprVal.Number,
needs: dataSources('dataElementSelector'),
},
externalApi: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.String,
needs: dataSources('externalApis'),
},
displayValue: {
args: args(required(ExprVal.String)),
returns: ExprVal.String,
needs: dataSources('displayValues', 'isHiddenSelector', 'currentDataModelPath', 'layoutLookups'),
},
optionLabel: {
args: args(required(ExprVal.String), required(ExprVal.Any)),
returns: ExprVal.String,
needs: dataSources('codeListSelector', 'langToolsSelector'),
},
formatDate: {
args: args(required(ExprVal.Date), optional(ExprVal.String)),
returns: ExprVal.String,
needs: dataSources('currentLanguage'),
},
compare: {
args: args(required(ExprVal.Any), required(ExprVal.Any), required(ExprVal.Any), optional(ExprVal.Any)),
returns: ExprVal.Boolean,
needs: noSources,
},
round: {
args: args(required(ExprVal.Number), optional(ExprVal.Number)),
returns: ExprVal.String,
needs: noSources,
},
text: {
args: args(required(ExprVal.String)),
returns: ExprVal.String,
needs: dataSources('langToolsSelector'),
},
linkToComponent: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.String,
needs: dataSources('layoutLookups', 'process', 'instanceDataSources', 'currentDataModelPath'),
},
linkToPage: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.String,
needs: dataSources('process', 'instanceDataSources'),
},
language: {
args: args(),
returns: ExprVal.String,
needs: dataSources('currentLanguage'),
},
contains: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.Boolean,
needs: noSources,
},
notContains: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.Boolean,
needs: noSources,
},
endsWith: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.Boolean,
needs: noSources,
},
startsWith: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.Boolean,
needs: noSources,
},
stringReplace: {
args: args(required(ExprVal.String), required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.String,
needs: noSources,
},
stringLength: {
args: args(required(ExprVal.String)),
returns: ExprVal.Number,
needs: noSources,
},
stringSlice: {
args: args(required(ExprVal.String), required(ExprVal.Number), optional(ExprVal.Number)),
returns: ExprVal.String,
needs: noSources,
},
stringIndexOf: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.Number,
needs: noSources,
},
commaContains: {
args: args(required(ExprVal.String), required(ExprVal.String)),
returns: ExprVal.Boolean,
needs: noSources,
},
lowerCase: {
args: args(required(ExprVal.String)),
returns: ExprVal.String,
needs: noSources,
},
upperCase: {
args: args(required(ExprVal.String)),
returns: ExprVal.String,
needs: noSources,
},
upperCaseFirst: {
args: args(required(ExprVal.String)),
returns: ExprVal.String,
needs: noSources,
},
lowerCaseFirst: {
args: args(required(ExprVal.String)),
returns: ExprVal.String,
needs: noSources,
},
_experimentalSelectAndMap: {
args: args(
required(ExprVal.String),
required(ExprVal.String),
optional(ExprVal.String),
optional(ExprVal.String),
optional(ExprVal.Boolean),
),
returns: ExprVal.String,
needs: dataSources('defaultDataType', 'formDataSelector'),
},
} satisfies { [key: string]: AnyFuncDef };
type Implementation<Name extends ExprFunctionName> = (
this: EvaluateExpressionParams<ExprFunctions[Name]['needs']>,
...params: ArgsToActual<ExprFunctions[Name]['args']>
) => ExprValToActual<ExprFunctions[Name]['returns']> | null;
export const ExprFunctionImplementations: { [K in ExprFunctionName]: Implementation<K> } = {
argv(idx) {
if (!this.positionalArguments?.length) {
throw new ExprRuntimeError(this.expr, this.path, 'No positional arguments available');
}
if (idx === null || idx < 0 || idx >= this.positionalArguments.length) {
throw new ExprRuntimeError(this.expr, this.path, `Index ${idx} out of range`);
}
return this.positionalArguments[idx];
},
value(key) {
const config = this.valueArguments;
if (!config) {
throw new ExprRuntimeError(this.expr, this.path, 'No value arguments available');
}
const realKey = key ?? config.defaultKey;
if (!realKey || typeof realKey === 'symbol') {
throw new ExprRuntimeError(
this.expr,
this.path,
`Invalid key (expected string, got ${realKey ? typeof realKey : 'null'})`,
);
}
if (!Object.prototype.hasOwnProperty.call(config.data, realKey)) {
throw new ExprRuntimeError(
this.expr,
this.path,
`Unknown key ${realKey}, Valid keys are: ${Object.keys(config.data).join(', ')}`,
);
}
const value = config.data[realKey];
return value ?? null;
},
equals(arg1, arg2) {
return compare(this, 'equals', arg1, arg2);
},
notEquals(arg1, arg2) {
return !compare(this, 'equals', arg1, arg2);
},
not: (arg) => !arg,
greaterThan(arg1, arg2) {
return compare(this, 'greaterThan', arg1, arg2);
},
greaterThanEq(arg1, arg2) {
return compare(this, 'greaterThanEq', arg1, arg2);
},
lessThan(arg1, arg2) {
return compare(this, 'lessThan', arg1, arg2);
},
lessThanEq(arg1, arg2) {
return compare(this, 'lessThanEq', arg1, arg2);
},
concat: (...args) => args.join(''),
and: (...args) => args.reduce((prev, cur) => prev && !!cur, true),
or: (...args) => args.reduce((prev, cur) => prev || !!cur, false),
if(condition, result, _, elseResult) {
if (condition === true) {
return result;
}
return elseResult === undefined ? null : elseResult;
},
instanceContext(key): string | null {
const instanceDataSourcesKeys: { [key in keyof IInstanceDataSources]: true } = {
instanceId: true,
appId: true,
instanceOwnerPartyId: true,
instanceOwnerPartyType: true,
instanceOwnerName: true,
};
if (key === null || instanceDataSourcesKeys[key] !== true) {
throw new ExprRuntimeError(this.expr, this.path, `Unknown Instance context property ${key}`);
}
return (this.dataSources.instanceDataSources && this.dataSources.instanceDataSources[key]) || null;
},
frontendSettings(key) {
if (key === null) {
throw new ExprRuntimeError(this.expr, this.path, `Value cannot be null. (Parameter 'key')`);
}
return (this.dataSources.applicationSettings && this.dataSources.applicationSettings[key]) || null;
},
authContext(key) {
const authContextKeys: { [key in keyof IAuthContext]: true } = {
read: true,
write: true,
instantiate: true,
confirm: true,
sign: true,
reject: true,
};
if (key === null || authContextKeys[key] !== true) {
throw new ExprRuntimeError(this.expr, this.path, `Unknown auth context property ${key}`);
}
const authContext = buildAuthContext(this.dataSources.process?.currentTask);
return Boolean(authContext?.[key]);
},
component(id) {
if (id === null) {
throw new ExprRuntimeError(this.expr, this.path, `Cannot lookup component null`);
}
const target = this.dataSources.layoutLookups.allComponents[id];
if (!target) {
throw new ExprRuntimeError(this.expr, this.path, `Unable to find component with identifier ${id}`);
}
const rawBinding =
target.dataModelBindings && 'simpleBinding' in target.dataModelBindings
? target.dataModelBindings.simpleBinding
: undefined;
if (!rawBinding) {
throw new ExprRuntimeError(this.expr, this.path, `Component ${id} does not have a simpleBinding`);
}
const targetId = makeIndexedId(target.id, this.dataSources.currentDataModelPath, this.dataSources.layoutLookups);
if (!targetId) {
throw new NodeRelationNotFound(this, id);
}
if (this.dataSources.isHiddenSelector(targetId)) {
// Not related to the current path, or currently hidden
return null;
}
if (this.dataSources.currentDataModelPath) {
const transposed = transposeDataBinding({
subject: rawBinding,
currentLocation: this.dataSources.currentDataModelPath,
});
return pickSimpleValue(transposed, this);
}
return pickSimpleValue(rawBinding, this);
},
dataModel(propertyPath, maybeDataType) {
if (propertyPath === null) {
throw new ExprRuntimeError(this.expr, this.path, `Cannot lookup dataModel null`);
}
const dataType = maybeDataType ?? this.dataSources.defaultDataType;
if (!dataType) {
throw new ExprRuntimeError(this.expr, this.path, `Cannot lookup dataType undefined`);
}
const reference: IDataModelReference = { dataType, field: propertyPath };
if (this.dataSources.currentDataModelPath?.dataType === dataType) {
const newReference = transposeDataBinding({
subject: reference,
currentLocation: this.dataSources.currentDataModelPath,
});
return pickSimpleValue(newReference, this);
}
return pickSimpleValue(reference, this);
},
countDataElements(dataType) {
if (dataType === null) {
throw new ExprRuntimeError(this.expr, this.path, `Expected dataType argument to be a string`);
}
const length = this.dataSources.dataElementSelector(
(elements) => elements.filter((e) => e.dataType === dataType).length,
[dataType],
);
if (length === ContextNotProvided) {
return 0; // Stateless never has any data elements
}
return length;
},
externalApi(externalApiId, path) {
if (externalApiId === null) {
throw new ExprRuntimeError(this.expr, this.path, `Expected an external API id`);
}
if (!path) {
return null;
}
const externalApiData: unknown = this.dataSources.externalApis.data[externalApiId];
const res =
externalApiData && typeof externalApiData === 'object' ? dot.pick(path, externalApiData) : externalApiData;
if (!res || typeof res === 'object') {
return null; // Return objects when the expression language supports them
}
return String(res);
},
displayValue(id) {
if (id === null) {
throw new ExprRuntimeError(this.expr, this.path, `Cannot lookup component null`);
}
const target = this.dataSources.layoutLookups.allComponents[id];
if (!target) {
throw new ExprRuntimeError(this.expr, this.path, `Unable to find component with identifier ${id}`);
}
const targetId = makeIndexedId(id, this.dataSources.currentDataModelPath, this.dataSources.layoutLookups);
if (!targetId) {
throw new NodeRelationNotFound(this, id);
}
if (this.dataSources.isHiddenSelector(targetId)) {
// Not related to the current path, or currently hidden
return null;
}
if (!(id in this.dataSources.displayValues)) {
throw new ExprRuntimeError(
this.expr,
this.path,
`Component of type '${target.type}' does not have a displayValue`,
);
}
return this.dataSources.displayValues[id] ?? '';
},
optionLabel(optionsId, value) {
if (optionsId === null) {
throw new ExprRuntimeError(this.expr, this.path, `Expected an options id`);
}
const options = this.dataSources.codeListSelector(optionsId);
if (!options) {
throw new ExprRuntimeError(this.expr, this.path, `Could not find options with id "${optionsId}"`);
}
if (options === CodeListPending) {
// We don't have the options yet, but it's not an error to ask for them.
return null;
}
// Lax comparison by design. Numbers in raw option lists will be cast to strings by useGetOptions(), so we cannot
// be strict about the type here.
const option = options.find((o) => o.value == value);
if (option) {
const nodeId = this.reference.type === 'node' ? this.reference.id : undefined;
return this.dataSources.langToolsSelector(nodeId).langAsNonProcessedString(option.label);
}
return null;
},
formatDate(date, format) {
if (date === null) {
return null;
}
const result = formatDateLocale(this.dataSources.currentLanguage, date, format ?? undefined);
if (result.includes('Unsupported: ')) {
throw new ExprRuntimeError(this.expr, this.path, `Unsupported date format token in '${format}'`);
}
return result;
},
compare(arg1, arg2, arg3, arg4) {
return arg2 === 'not'
? !compare(this, arg3 as CompareOperator, arg1, arg4, 0, 3)
: compare(this, arg2 as CompareOperator, arg1, arg3, 0, 2);
},
round(number, decimalPoints) {
const realNumber = number === null ? 0 : number;
const realDecimalPoints = decimalPoints === null || decimalPoints === undefined ? 0 : decimalPoints;
return parseFloat(`${realNumber}`).toFixed(realDecimalPoints);
},
text(key) {
if (key === null) {
return null;
}
const nodeId = this.reference.type === 'node' ? this.reference.id : undefined;
return this.dataSources.langToolsSelector(nodeId).langAsNonProcessedString(key);
},
linkToComponent(linkText, id) {
if (id == null) {
window.logWarn('Component id was empty but must be set for linkToComponent to work');
return null;
}
if (linkText == null) {
window.logWarn('Link text was empty but must be set for linkToComponent to work');
return null;
}
const target = this.dataSources.layoutLookups.allComponents[id];
const pageKey = this.dataSources.layoutLookups.componentToPage[id];
if (!target || !pageKey) {
throw new ExprRuntimeError(this.expr, this.path, `Unable to find component with identifier ${id}`);
}
const taskId = this.dataSources.process?.currentTask?.elementId;
const instanceId = this.dataSources.instanceDataSources?.instanceId;
let url = '';
if (taskId && instanceId) {
url = `/instance/${instanceId}/${taskId}/${pageKey}`;
} else {
url = `/${pageKey}`;
}
const relativeId = makeIndexedId(id, this.dataSources.currentDataModelPath, this.dataSources.layoutLookups);
if (!relativeId) {
throw new NodeRelationNotFound(this, id);
}
const searchParams = new URLSearchParams();
searchParams.set(SearchParams.FocusComponentId, relativeId);
const newUrl = `${url}?${searchParams.toString()}`;
return `<a href="${newUrl}" data-link-type="LinkToPotentialNode">${linkText}</a>`;
},
linkToPage(linkText, pageId) {
if (pageId == null) {
window.logWarn('Page id was empty but must be set for linkToPage to work');
return null;
}
if (linkText == null) {
window.logWarn('Link text was empty but must be set for linkToPage to work');
return null;
}
const taskId = this.dataSources.process?.currentTask?.elementId;
const instanceId = this.dataSources.instanceDataSources?.instanceId;
let url = '';
if (taskId && instanceId) {
url = `/instance/${instanceId}/${taskId}/${pageId}`;
} else {
url = `/${pageId}`;
}
return `<a href="${url}" data-link-type="LinkToPotentialPage">${linkText}</a>`;
},
language() {
return this.dataSources.currentLanguage;
},
contains(string, stringToContain) {
if (string === null || stringToContain === null) {
return false;
}
return string.includes(stringToContain);
},
notContains(string, stringToNotContain) {
if (string === null || stringToNotContain === null) {
return true;
}
return !string.includes(stringToNotContain);
},
endsWith(string: string, stringToMatch: string): boolean {
if (string === null || stringToMatch === null) {
return false;
}
return string.endsWith(stringToMatch);
},
startsWith(string: string, stringToMatch: string): boolean {
if (string === null || stringToMatch === null) {
return false;
}
return string.startsWith(stringToMatch);
},
stringReplace(string, search, _replace) {
if (!string || !search) {
return null;
}
const replace = _replace === null ? '' : _replace;
return string.replace(new RegExp(escapeStringRegexp(search), 'g'), replace);
},
stringLength: (string) => (string === null ? 0 : string.length),
stringSlice(string, start, end) {
if (start === null || end === null) {
throw new ExprRuntimeError(
this.expr,
this.path,
`Start/end index cannot be null (if you used an expression like stringIndexOf here, make sure to guard against null)`,
);
}
if (string === null) {
return null;
}
return string.slice(start, end);
},
stringIndexOf(string, search) {
if (!string || !search) {
return null;
}
const idx = string.indexOf(search);
return idx === -1 ? null : idx;
},
commaContains(commaSeparatedString, stringToMatch) {
if (commaSeparatedString === null || stringToMatch === null) {
return false;
}
// Split the comma separated string into an array and remove whitespace from each part
const parsedToArray = commaSeparatedString.split(',').map((part) => part.trim());
return parsedToArray.includes(stringToMatch);
},
lowerCase(string) {
if (string === null) {
return null;
}
return string.toLowerCase();
},
upperCase(string) {
if (string === null) {
return null;
}
return string.toUpperCase();
},
upperCaseFirst(string) {
if (string === null) {
return null;
}
return string.charAt(0).toUpperCase() + string.slice(1);
},
lowerCaseFirst(string) {
if (string === null) {
return null;
}
return string.charAt(0).toLowerCase() + string.slice(1);
},
_experimentalSelectAndMap(path, propertyToSelect, prepend, append, appendToLastElement = true) {
if (path === null || propertyToSelect == null) {
throw new ExprRuntimeError(this.expr, this.path, `Cannot lookup dataModel null`);
}
const dataType = this.dataSources.defaultDataType;
if (!dataType) {
throw new ExprRuntimeError(this.expr, this.path, `Cannot lookup dataType undefined`);
}
const array = this.dataSources.formDataSelector({ field: path, dataType });
if (typeof array != 'object' || !Array.isArray(array)) {
return '';
}
return array
.map((x, i) => {
const hideLastElement = i == array.length - 1 && !appendToLastElement;
const valueToPrepend = prepend == null ? '' : prepend;
const valueToAppend = append == null || hideLastElement ? '' : append;
return `${valueToPrepend}${x[propertyToSelect]}${valueToAppend}`;
})
.join(' ');
},
};
export const ExprFunctionValidationExtensions: { [K in ExprFunctionName]?: FuncValidationDef } = {
if: {
validator: ({ rawArgs, ctx, path }) => {
if (rawArgs.length === 2) {
return;
}
if (rawArgs.length > 2 && rawArgs[2] !== 'else') {
addError(ctx, [...path, '[2]'], 'Expected third argument to be "else"');
}
if (rawArgs.length === 4) {
return;
}
addError(ctx, path, 'Expected either 2 arguments (if) or 4 (if + else), got %s', `${rawArgs.length}`);
},
runNumArgsValidator: false,
},
dataModel: {
validator({ rawArgs, ctx, path }) {
if (rawArgs.length > 1 && rawArgs[1] !== null && typeof rawArgs[1] !== 'string') {
addError(ctx, [...path, '[2]'], 'The data type must be a string (expressions cannot be used here)');
}
},
},
compare: {
validator({ rawArgs, ctx, path }) {
if (rawArgs.length === 4 && rawArgs[1] !== 'not') {
addError(ctx, [...path, '[1]'], 'Second argument must be "not" when providing 4 arguments in total');
return;
}
const opIdx = rawArgs.length === 4 ? 2 : 1;
const op = rawArgs[opIdx];
if (!(typeof op === 'string')) {
addError(ctx, [...path, `[${opIdx + 1}]`], 'Invalid operator (it cannot be an expression or null)');
return;
}
const validOperators = Object.keys(CompareOperators);
if (!validOperators.includes(op)) {
const validList = validOperators.map((o) => `"${o}"`).join(', ');
addError(ctx, [...path, `[${opIdx + 1}]`], 'Invalid operator "%s", valid operators are %s', op, validList);
}
},
},
displayValue: {
validator({ rawArgs, ctx, path }) {
if (rawArgs.length > 1 && rawArgs[1] !== null && typeof rawArgs[1] !== 'string') {
addError(ctx, [...path, '[2]'], 'The second argument must be a component id (expressions cannot be used here)');
}
},
},
optionLabel: {
validator({ rawArgs, ctx, path }) {
const optionsId = rawArgs[0];
if (optionsId === null || typeof optionsId !== 'string') {
addError(ctx, [...path, '[1]'], 'The first argument must be a string (expressions cannot be used here)');
}
},
},
};
function pickSimpleValue(
path: IDataModelReference,
params: EvaluateExpressionParams<['dataModelNames', 'formDataSelector']>,
) {
const isValidDataType = params.dataSources.dataModelNames.includes(path.dataType);
if (!isValidDataType) {
throw new ExprRuntimeError(params.expr, params.path, `Data model with type ${path.dataType} not found`);
}
const value = params.dataSources.formDataSelector(path);
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
return value;
}
return null;
}
/**
* Allows you to cast an argument to a stricter type late during execution of an expression function, as opposed to
* before the function runs (as arguments are processed on the way in). This is useful in functions such as
* 'compare', where the operator will determine the type of the arguments, and cast them accordingly.
*/
function lateCastArg<T extends ExprVal>(
context: EvaluateExpressionParams<[]>,
arg: unknown,
argIndex: number,
type: T,
): ExprValToActual<T> | null {
const actualIndex = argIndex + 1; // Adding 1 because the function name is at index 0
const newContext = { ...context, path: [...context.path, `[${actualIndex}]`] };
return exprCastValue(arg, type, newContext);
}
type CompareOpArg<T extends ExprVal, BothReq extends boolean> = BothReq extends true
? ExprValToActual<T>
: ExprValToActual<T> | null;
type CompareOpImplementation<T extends ExprVal, BothReq extends boolean> = (
this: EvaluateExpressionParams,
a: CompareOpArg<T, BothReq>,
b: CompareOpArg<T, BothReq>,
) => boolean;
export interface CompareOperatorDef<T extends ExprVal, BothReq extends boolean> {
bothArgsMustBeValid: BothReq;
extraValidators?: ((this: EvaluateExpressionParams, a: ExprValToActual<T>, b: ExprValToActual<T>) => void)[];
argType: T;
impl: CompareOpImplementation<T, BothReq>;
}
function defineCompareOp<T extends ExprVal, BothReq extends boolean>(
def: CompareOperatorDef<T, BothReq>,
): CompareOperatorDef<T, BothReq> {
return def;
}
/**
* All the comparison operators available to execute inside the 'compare' function. This list of operators
* have the following behaviors:
*/
export const CompareOperators = {
equals: defineCompareOp({
bothArgsMustBeValid: false,
argType: ExprVal.String,
impl: (a, b) => a === b,
}),
greaterThan: defineCompareOp({
bothArgsMustBeValid: true,
argType: ExprVal.Number,
impl: (a, b) => a > b,
}),
greaterThanEq: defineCompareOp({
bothArgsMustBeValid: true,
argType: ExprVal.Number,
impl: (a: number, b: number) => a >= b,
}),
lessThan: defineCompareOp({
bothArgsMustBeValid: true,
argType: ExprVal.Number,
impl: (a: number, b: number) => a < b,
}),
lessThanEq: defineCompareOp({
bothArgsMustBeValid: true,
argType: ExprVal.Number,
impl: (a: number, b: number) => a <= b,
}),
isBefore: defineCompareOp({
bothArgsMustBeValid: true,
argType: ExprVal.Date,
extraValidators: [validateDates],
impl: (a, b) => a < b,
}),
isBeforeEq: defineCompareOp({
bothArgsMustBeValid: true,
argType: ExprVal.Date,
extraValidators: [validateDates],
impl: (a, b) => a <= b,
}),
isAfter: defineCompareOp({
bothArgsMustBeValid: true,
argType: ExprVal.Date,
extraValidators: [validateDates],
impl: (a, b) => a > b,
}),
isAfterEq: defineCompareOp({
bothArgsMustBeValid: true,
argType: ExprVal.Date,
extraValidators: [validateDates],
impl: (a, b) => a >= b,
}),
isSameDay: defineCompareOp({
bothArgsMustBeValid: true,
argType: ExprVal.Date,
extraValidators: [validateDates, validateDatesForSameDay],
impl: (a, b) => a.toDateString() === b.toDateString(),
}),
} as const;
type CompareOperator = keyof typeof CompareOperators;
function compare(
ctx: EvaluateExpressionParams<[]>,
operator: CompareOperator,
arg1: unknown,
arg2: unknown,
idxArg1 = 1,
idxArg2 = 2,
): boolean {
const def = CompareOperators[operator];
const a = lateCastArg(ctx, arg1, idxArg1, def.argType);
const b = lateCastArg(ctx, arg2, idxArg2, def.argType);
if (def.bothArgsMustBeValid && (a === null || b === null)) {
return false;
}
for (const validator of def.extraValidators ?? []) {
validator.call(ctx, a, b);
}
return def.impl.call(ctx, a, b);
}
function validateDatesForSameDay(this: EvaluateExpressionParams, a: ExprDate, b: ExprDate) {
if (a.exprDateExtensions.timeZone !== b.exprDateExtensions.timeZone) {
throw new ExprRuntimeError(
this.expr,
this.path,
`Can not figure out if timestamps in different timezones are in the same day`,
);
}
}
function validateDates(this: EvaluateExpressionParams, a: ExprDate, b: ExprDate) {
const sameTimezones = a.exprDateExtensions.timeZone === b.exprDateExtensions.timeZone;
const eitherIsLocal = a.exprDateExtensions.timeZone === 'local' || b.exprDateExtensions.timeZone === 'local';
if (!sameTimezones && eitherIsLocal) {
throw new ExprRuntimeError(this.expr, this.path, `Can not compare timestamps where only one specify timezone`);
}
}