-
Notifications
You must be signed in to change notification settings - Fork 3
/
carrot_rcc_lib.ts
113 lines (107 loc) · 3.82 KB
/
carrot_rcc_lib.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
import { TypedValue } from "camunda-external-task-client-js";
export const isInteger = (a: any): boolean =>
Number.isInteger(a) && a >= -Math.pow(2, 31) && a <= Math.pow(2, 31) - 1;
export const isLong = (a: any): boolean => Number.isInteger(a) && !isInteger(a);
export const isDouble = (a: any): boolean =>
typeof a === "number" && !Number.isInteger(a);
export const isBoolean = (a: any): boolean => typeof a === "boolean";
export const isString = (a: any): boolean => typeof a === "string";
export const isJson = (a: any): boolean => typeof a === "object";
export const isDate = (a: any): boolean =>
a instanceof Date ||
!!(typeof a === "string" && a.match(/^\d{4}-\d{2}-\d{2}[T\s]?[0-9:.Z\-+]*$/));
export const offsetToString = (offset: number): string => {
const base =
(Math.floor(Math.abs(offset) / 60) * 100 + (Math.abs(offset) % 60)) *
(offset > 0 ? -1 : 1);
if (base >= 1000) {
return `+${base}`;
} else if (base >= 100) {
return `+0${base}`;
} else if (base >= 10) {
return `+00${base}`;
} else if (base >= 1) {
return `+000${base}`;
} else if (base < 0 && base <= -1000) {
return `-${Math.abs(base)}`;
} else if (base < 0 && base <= -100) {
return `-0${Math.abs(base)}`;
} else if (base < 0 && base <= -10) {
return `-00${Math.abs(base)}`;
} else if (base < 0 && base <= -1) {
return `-000${Math.abs(base)}`;
} else {
return "+0000";
}
};
export const TZ: string = ((): string => {
const date = new Date();
const offset = date.getTimezoneOffset();
return offsetToString(offset);
})();
export const isEqual = (old: TypedValue | undefined, current: any): boolean => {
if (old === undefined) {
return false;
}
const currType = inferType(current);
if (old.type.toUpperCase() !== currType.toUpperCase()) {
return false;
}
switch (currType) {
case "File":
return false;
case "Date":
return new Date(old.value).getTime() === new Date(current).getTime();
default:
return old.value === current;
}
};
export const inferType = (current: any): string => {
if (isBoolean(current)) {
return "Boolean";
} else if (isDate(current)) {
return "Date";
} else if (isString(current)) {
return "String";
} else if (isInteger(current)) {
return "Integer";
} else if (isDouble(current)) {
return "Double";
} else if (isLong(current)) {
return "Long";
}
return "Json";
};
export const toCamundaDateString = (value: Date | string): string => {
// Should be compatible with ISO strings and Robot Framework timestamps
// https://robotframework.org/robotframework/latest/libraries/DateTime.html
if (value instanceof Date) {
return value.toISOString().replace(/Z$/, "+0000");
} else {
if (value.match(/^\d{4}-\d{2}-\d{2}$/)) {
// Simple date string
return `${value}T00:00:00.000${TZ}`;
} else if (value.match(/^\d{4}-\d{2}-\d{2}[T\s]?\d{2}:\d{2}$/)) {
// Naive datetime string without seconds and timezone
return `${value.substring(0, 10)}T${value.substring(11)}:00.000${TZ}`;
} else if (value.match(/^\d{4}-\d{2}-\d{2}[T\s]?\d{2}:\d{2}:\d{2}$/)) {
// Naive datetime string without milliseconds and timezone
return `${value.substring(0, 10)}T${value.substring(11)}.000${TZ}`;
} else if (
value.match(/^\d{4}-\d{2}-\d{2}[T\s]?\d{2}:\d{2}:\d{2}\.\d{3}$/)
) {
// Naive datetime string without timezone
return `${value.substring(0, 10)}T${value.substring(11)}${TZ}`;
} else if (
value.match(/^\d{4}-\d{2}-\d{2}[T\s]?\d{2}:\d{2}:\d{2}\.\d{3}[\-+]\d{4}$/)
) {
// Datetime string with timezone
return new Date(`${value.substring(0, 10)}T${value.substring(11)}`)
.toISOString()
.replace(/Z$/, "+0000");
} else {
// Datetime string in GMT
return new Date(value).toISOString().replace(/Z$/, "+0000");
}
}
};