-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsanitize.ts
More file actions
105 lines (89 loc) · 3.23 KB
/
sanitize.ts
File metadata and controls
105 lines (89 loc) · 3.23 KB
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
import { SplitIO } from '../../types';
import { IDependencyMatcherValue } from '../types';
import { ILogger } from '../../logger/types';
import { isObject, uniq, toString, toNumber } from '../../utils/lang';
import { zeroSinceHH, zeroSinceSS } from '../convertions';
import { matcherTypes, dataTypes } from '../matchers/matcherTypes';
import { ENGINE_SANITIZE } from '../../logger/constants';
function sanitizeNumber(val: any): number | undefined {
const num = toNumber(val);
return isNaN(num) ? undefined : num;
}
function sanitizeString(val: any): string | undefined {
let valueToSanitize = val;
if (isObject(val)) {
// If the value is an object and is not a key, discard it.
valueToSanitize = val.matchingKey ? val.matchingKey : undefined;
}
const str = toString(valueToSanitize);
return str ? str : undefined;
}
function sanitizeArray(val: any): string[] | undefined {
const arr = Array.isArray(val) ? uniq(val.map(e => e + '')) : [];
return arr.length ? arr : undefined;
}
export function sanitizeBoolean(val: any): boolean | undefined {
if (val === true || val === false) return val;
if (typeof val === 'string') {
const lowerCaseValue = val.toLocaleLowerCase();
if (lowerCaseValue === 'true') return true;
if (lowerCaseValue === 'false') return false;
}
return undefined;
}
function dependencyProcessor(sanitizedValue: string, attributes: SplitIO.Attributes): IDependencyMatcherValue {
return {
key: sanitizedValue,
attributes
};
}
/**
* We can define a pre-processing for the value, to be executed prior to matcher evaluation.
*/
function getProcessingFunction(matcherTypeID: number, dataType: string) {
switch (matcherTypeID) {
case matcherTypes.EQUAL_TO:
return dataType === 'DATETIME' ? zeroSinceHH : undefined;
case matcherTypes.GREATER_THAN_OR_EQUAL_TO:
case matcherTypes.LESS_THAN_OR_EQUAL_TO:
case matcherTypes.BETWEEN:
return dataType === 'DATETIME' ? zeroSinceSS : undefined;
case matcherTypes.IN_SPLIT_TREATMENT:
return dependencyProcessor;
default:
return undefined;
}
}
/**
* Sanitize matcher value
*/
export default function sanitize(log: ILogger, matcherTypeID: number, value: string | number | boolean | Array<string | number> | undefined, dataType: string, attributes: SplitIO.Attributes) {
const processor = getProcessingFunction(matcherTypeID, dataType);
let sanitizedValue: string | number | boolean | Array<string | number> | IDependencyMatcherValue | undefined;
switch (dataType) {
case dataTypes.NUMBER:
case dataTypes.DATETIME:
sanitizedValue = sanitizeNumber(value);
break;
case dataTypes.STRING:
sanitizedValue = sanitizeString(value);
break;
case dataTypes.SET:
sanitizedValue = sanitizeArray(value);
break;
case dataTypes.BOOLEAN:
sanitizedValue = sanitizeBoolean(value);
break;
case dataTypes.NOT_SPECIFIED:
sanitizedValue = value;
break;
default:
sanitizedValue = undefined;
}
if (processor) {
// @ts-ignore
sanitizedValue = processor(sanitizedValue, attributes);
}
log.debug(ENGINE_SANITIZE, [value, dataType, sanitizedValue instanceof Object ? JSON.stringify(sanitizedValue) : sanitizedValue]);
return sanitizedValue;
}