-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtypeguards.ts
293 lines (272 loc) · 7.5 KB
/
typeguards.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
/**
* Contains typeguards for geostyler-style
* https://basarat.gitbook.io/typescript/type-system/typeguard#user-defined-type-guards
*/
function isString(str: any){
if (str != null && typeof str.valueOf() === 'string') {
return true;
}
return false;
};
function isNumber(num: any){
if (num != null && typeof num.valueOf() === 'number') {
return true;
}
return false;
}
function isBoolean(bool: any){
if (bool != null && typeof bool.valueOf() === 'boolean') {
return true;
}
return false;
};
import {
CombinationFilter,
CombinationOperator,
ComparisonFilter,
ComparisonOperator,
FillSymbolizer,
Filter,
GrayChannel,
IconSymbolizer,
LineSymbolizer,
MarkSymbolizer,
NegationFilter,
NegationOperator,
Operator,
PropertyType,
RasterSymbolizer,
RGBChannel,
Rule,
ScaleDenominator,
TextSymbolizer,
Expression,
GeoStylerBooleanFunction,
GeoStylerNumberFunction,
GeoStylerStringFunction,
GeoStylerUnknownFunction,
GeoStylerFunction,
PointSymbolizer,
Symbolizer,
FunctionCall,
Sprite
} from './index';
export const isExpression = (got: any): got is Expression<any> => {
return isGeoStylerFunction(got) || isPropertyType(got);
};
/**
* @deprecated use isFunction instead
*/
export const isFunctionCall = (got: any): got is FunctionCall<any> => {
return got.type === 'functioncall' &&
got.hasOwnProperty('name') &&
isString(got.name) &&
got.hasOwnProperty('args') &&
Array.isArray(got.args) &&
got.args.every((arg: any) => isExpression(arg));
};
// PropertyValue
export const isPropertyType = (got: any): got is PropertyType => {
return isString(got) || isNumber(got) || isBoolean(got) || got === null;
};
// ScaleDenominator
export const isScaleDenominator = (got: any): got is ScaleDenominator => {
return !!((got?.min || got?.max) &&
((!!got.min) ? isGeoStylerNumberFunction(got.min) || isNumber(got.min) : true) &&
((!!got.max) ? isGeoStylerNumberFunction(got.max) || isNumber(got.min) : true));
};
// Operators
export const isOperator = (got: any): got is Operator => {
return isComparisonOperator(got) ||
isCombinationOperator(got) ||
isNegationOperator(got);
};
export const isComparisonOperator = (got: any): got is ComparisonOperator => {
return ['==', '*=' , '!=' , '<' , '<=' , '>' , '>=', '<=x<='].includes(got);
};
export const isCombinationOperator = (got: any): got is CombinationOperator => {
return ['&&', '||'].includes(got);
};
export const isNegationOperator = (got: any): got is NegationOperator => {
return got === '!';
};
// Filters
export const isFilter = (got: any): got is Filter => {
return isComparisonFilter(got) ||
isCombinationFilter(got) ||
isGeoStylerBooleanFunction(got) ||
isNegationFilter(got) ||
isGeoStylerBooleanFunction(got) ||
isBoolean(got);
};
export const isComparisonFilter = (got: any): got is ComparisonFilter => {
const expectedLength = got && got[0] === '<=x<=' ? 4 : 3;
return (
Array.isArray(got) &&
got.length === expectedLength &&
isComparisonOperator(got[0]) &&
isExpression(got[1]) &&
isExpression(got[2]) &&
(got[0] !== '<=x<=' || isNumber(got[3]))
);
};
export const isCombinationFilter = (got: any): got is CombinationFilter => {
return Array.isArray(got) &&
got.length >= 3 &&
isCombinationOperator(got[0]) &&
got.every((arg, index) => index === 0 || isFilter(arg));
};
export const isNegationFilter = (got: any): got is NegationFilter => {
return Array.isArray(got) &&
got.length === 2 &&
isNegationOperator(got[0]) &&
isFilter(got[1]);
};
// Symbolizers
export const isSymbolizer = (got: any): got is Symbolizer => {
return isPointSymbolizer(got) ||
isLineSymbolizer(got) ||
isFillSymbolizer(got) ||
isRasterSymbolizer(got);
};
export const isPointSymbolizer = (got: any): got is PointSymbolizer => {
return isIconSymbolizer(got) || isMarkSymbolizer(got) || isTextSymbolizer(got);
};
export const isIconSymbolizer = (got: any): got is IconSymbolizer => {
return got?.kind === 'Icon';
};
export const isTextSymbolizer = (got: any): got is TextSymbolizer => {
return got?.kind === 'Text';
};
export const isMarkSymbolizer = (got: any): got is MarkSymbolizer => {
return got?.kind === 'Mark' && isString(got?.wellKnownName);
};
export const isLineSymbolizer = (got: any): got is LineSymbolizer => {
return got?.kind === 'Line';
};
export const isFillSymbolizer = (got: any): got is FillSymbolizer => {
return got?.kind === 'Fill';
};
export const isRasterSymbolizer = (got: any): got is RasterSymbolizer => {
return got?.kind === 'Raster';
};
// Rule
export const isRule = (got: any): got is Rule => {
return !!(isString(got?.name) &&
(got?.filter ? isFilter(got.filter) : true) &&
(got?.scaleDenominator ? isScaleDenominator(got.scaleDenominator) : true) &&
got?.symbolizers?.every((arg: any) => isSymbolizer(arg)));
};
/**
* Checks if ChannelSelection is of type RGBChannel.
*/
export const isRgbChannel = (got: any): got is RGBChannel => {
return !!(
got?.redChannel !== undefined
|| got?.greenChannel !== undefined
|| got?.blueChannel !== undefined
);
};
/**
* Checks if ChannelSelection is of type GrayChannel.
*/
export const isGrayChannel = (got: any): got is GrayChannel => {
return !!(got?.grayChannel !== undefined);
};
// Functions
export const isGeoStylerNumberFunction = (got: any): got is GeoStylerNumberFunction => {
const functionNames: GeoStylerNumberFunction['name'][] = [
'abs',
'acos',
'add',
'asin',
'atan',
'atan2',
'ceil',
'cos',
'div',
'exp',
'floor',
'interpolate',
'log',
'max',
'min',
'modulo',
'mul',
'pi',
'pow',
'random',
'rint',
'round',
'sin',
'sqrt',
'strIndexOf',
'strLastIndexOf',
'strLength',
'sub',
'tan',
'toDegrees',
'toNumber',
'toRadians'
];
return functionNames.includes(got?.name);
};
export const isGeoStylerStringFunction = (got: any): got is GeoStylerStringFunction => {
const functionNames: GeoStylerStringFunction['name'][] = [
'numberFormat',
'strAbbreviate',
'strCapitalize',
'strConcat',
'strDefaultIfBlank',
'strReplace',
'strStripAccents',
'strSubstring',
'strSubstringStart',
'strToLowerCase',
'strToString',
'strToUpperCase',
'strTrim'
];
return functionNames.includes(got?.name);
};
export const isGeoStylerBooleanFunction = (got: any): got is GeoStylerBooleanFunction => {
const functionNames: GeoStylerBooleanFunction['name'][] = [
'all',
'any',
'between',
'double2bool',
'equalTo',
'greaterThan',
'greaterThanOrEqualTo',
'in',
'lessThan',
'lessThanOrEqualTo',
'not',
'notEqualTo',
'parseBoolean',
'strEndsWith',
'strEqualsIgnoreCase',
'strMatches',
'strStartsWith'
];
return functionNames.includes(got?.name);
};
export const isGeoStylerUnknownFunction = (got: any): got is GeoStylerUnknownFunction => {
const functionNames: GeoStylerUnknownFunction['name'][] = [
'case',
'property',
'step'
];
return functionNames.includes(got?.name);
};
export const isGeoStylerFunction = (got: any): got is GeoStylerFunction => {
return isGeoStylerBooleanFunction(got) ||
isGeoStylerNumberFunction(got) ||
isGeoStylerStringFunction(got) ||
isGeoStylerUnknownFunction(got);
};
export const isSprite = (got: any): got is Sprite => {
return typeof got?.source === 'string' || isGeoStylerFunction(got?.source) &&
Array.isArray(got.position) &&
Array.isArray(got.size);
};