-
Notifications
You must be signed in to change notification settings - Fork 2
/
OrigamApiHelpers.ts
110 lines (97 loc) · 3.44 KB
/
OrigamApiHelpers.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
/*
Copyright 2005 - 2021 Advantage Solutions, s. r. o.
This file is part of ORIGAM (http://www.origam.org).
ORIGAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ORIGAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ORIGAM. If not, see <http://www.gnu.org/licenses/>.
*/
import moment, { Moment } from "moment";
import { IFilter } from "./types/IFilter";
import { toOrigamServerString } from "utils/moment";
export function joinWithAND(filterItems: string[]) {
if (filterItems.length === 0) return "";
if (filterItems.length === 1) return filterItems[0];
return '["$AND", ' + filterItems.join(", ") + "]";
}
export function joinWithOR(filterItems: string[]) {
if (filterItems.length === 0) return "";
if (filterItems.length === 1) return filterItems[0];
return '["$OR", ' + filterItems.join(", ") + "]";
}
export function filterToFilterItem(filter: IFilter) {
return toFilterItem(
filter.propertyId,
filter.dataType,
filter.setting.type,
filter.setting.filterValue1,
filter.setting.filterValue2
);
}
function arrayToString(array: any[]) {
return `[${array.join(", ")}]`;
}
function valuesToRightHandSide(val1?: any, val2?: any) {
const val1IsArray = Array.isArray(val1);
const val2IsArray = Array.isArray(val2);
if (val1 !== undefined && !val1IsArray && val2 !== undefined && !val2IsArray) {
return arrayToString([toFilterValueForm(val1), toFilterValueForm(val2)]);
}
if (val1IsArray && val2 === undefined) {
return arrayToString(val1.map((x: any) => toFilterValueForm(x)));
} else if (!val1IsArray && val2 === undefined) {
return toFilterValueForm(val1);
} else if (val1 === undefined && !val2IsArray) {
return toFilterValueForm(val2);
}
throw new Error(`Cannot convert values "${val1}" and "${val2}" to a right hand side`);
}
export function toFilterItem(
columnId: string,
dataType: string | null,
operator: string,
val1?: any,
val2?: any
) {
if (
dataType === "Date" &&
(operator === "eq" || operator === "neq") &&
val1 !== undefined &&
val1 !== "" &&
val1 !== null &&
val1.endsWith("T00:00:00.000")
) {
const upperLimit = new Date(val1);
upperLimit.setDate(upperLimit.getDate() + 1);
const dateTimeFormat = new Intl.DateTimeFormat("en", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});
const [{value: month}, , {value: day}, , {value: year}] = dateTimeFormat.formatToParts(
upperLimit
);
const upperLimitString = year.concat("-", month, "-", day, "T00:00:00.000");
const substitutedOperator = operator === "eq" ? "between" : "nbetween";
return `["${columnId}", "${substitutedOperator}", ${valuesToRightHandSide(
val1,
upperLimitString
)}]`;
}
const val1Formatted = moment.isMoment(val1)
? toOrigamServerString(val1 as Moment)
: val1;
return `["${columnId}", "${operator}", ${valuesToRightHandSide(val1Formatted, val2)}]`;
}
function toFilterValueForm(value: any) {
if (value === undefined) {
return null;
}
return isNaN(value) ? '"' + value + '"' : value;
}