-
Notifications
You must be signed in to change notification settings - Fork 51
/
filter-functions.ts
137 lines (130 loc) · 3.84 KB
/
filter-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
import {
Field,
StringFilterFunction,
BooleanFilterFunction,
filterFunction,
filterFunctions as filterFunctionsCommon,
FilterFunctionsType as FilterFunctionsCommonType,
Time,
FilterFunctionNames as FilterFunctionNamesCommon
} from '@sap-cloud-sdk/odata-common/internal';
import BigNumber from 'bignumber.js';
import {
DeSerializers,
defaultDeSerializers,
mergeDefaultDeSerializersWith
} from './de-serializers';
import { Entity } from './entity';
/* String Functions */
/**
* Build a filter function to test whether a string is a substring of the other. Evaluates to boolean.
* @param substr - The substring to test for. This can either be a string, a reference to a field or another filter function.
* @param str - The string to test. This can either be a string, a reference to a field or another filter function.
* @returns The newly created filter function.
*/
export function substringOf<EntityT extends Entity>(
substr:
| string
| Field<EntityT, boolean, boolean>
| StringFilterFunction<EntityT>,
str: string | Field<EntityT, boolean, boolean> | StringFilterFunction<EntityT>
): BooleanFilterFunction<EntityT> {
return filterFunction('substringof', 'boolean', substr, str);
}
/**
* Build a filter function to replace the occurrence of a search string with another string. Evaluates to string.
* @param str - The string to get the index from. This can either be a string, a reference to a field or another filter function.
* @param searchStr - The substring to get the index for. This can either be a string, a reference to a field or another filter function.
* @param replaceStr - The substring to get the index for. This can either be a string, a reference to a field or another filter function.
* @returns The newly created filter function.
*/
export function replace<EntityT extends Entity>(
str:
| string
| Field<EntityT, boolean, boolean>
| StringFilterFunction<EntityT>,
searchStr:
| string
| Field<EntityT, boolean, boolean>
| StringFilterFunction<EntityT>,
replaceStr:
| string
| Field<EntityT, boolean, boolean>
| StringFilterFunction<EntityT>
): StringFilterFunction<EntityT> {
return filterFunction('replace', 'string', str, searchStr, replaceStr);
}
/**
* Export length filter function for backwards compatibility.
*/
export const length = filterFunctionsCommon(defaultDeSerializers).length;
/**
* Export substring filter function for backwards compatibility.
*/
export const substring = filterFunctionsCommon(defaultDeSerializers).substring;
/**
* OData v2 specific filter functions.
* @param deSerializers - DeSerializer used in the filter.
* @returns Filter functions object.
*/
export function filterFunctions<
BinaryT = string,
BooleanT = boolean,
ByteT = number,
DecimalT = BigNumber,
DoubleT = number,
FloatT = number,
Int16T = number,
Int32T = number,
Int64T = BigNumber,
GuidT = string,
SByteT = number,
SingleT = number,
StringT = string,
AnyT = any,
DateTimeT = moment.Moment,
DateTimeOffsetT = moment.Moment,
TimeT = Time
>(
deSerializers: Partial<
DeSerializers<
BinaryT,
BooleanT,
ByteT,
DecimalT,
DoubleT,
FloatT,
Int16T,
Int32T,
Int64T,
GuidT,
SByteT,
SingleT,
StringT,
AnyT,
DateTimeT,
DateTimeOffsetT,
TimeT
>
> = defaultDeSerializers as any
): FilterFunctionsType {
return {
...filterFunctionsCommon(mergeDefaultDeSerializersWith(deSerializers)),
substringOf,
replace
};
}
/**
* Extending the common filter function by the OData v2 specific ones`substringOf` and `replace`.
*/
export type FilterFunctionsType = FilterFunctionsCommonType & {
substringOf: typeof substringOf;
replace: typeof replace;
};
/**
* @internal
*/
export type FilterFunctionNames =
| FilterFunctionNamesCommon
| 'substringOf'
| 'replace';