fix tests + support value to be number or string

Signed-off-by: shmaram <shaharshmaram@gmail.com>
This commit is contained in:
shmaram
2024-01-26 12:46:03 +02:00
parent 20843193e8
commit c47460ba98
2 changed files with 21 additions and 8 deletions
+5 -5
View File
@@ -23,13 +23,13 @@ describe('config', () => {
const mockConfig = new MockConfigApi({
field: 'pathname',
operator: '==',
value: '3',
value: '/home',
});
const res = readFilterConfig(mockConfig);
expect(res).toEqual({
field: 'pathname',
operator: '==',
value: '3',
value: '/home',
});
});
@@ -66,11 +66,11 @@ describe('config', () => {
]);
});
it('returns only invalid filters', async () => {
it('returns only valid filters', async () => {
const mockValidConfig = new MockConfigApi({
field: 'id',
operator: '==',
value: '3',
value: 3,
});
const mockInvalidConfig = new MockConfigApi({
myField: 'pathname',
@@ -81,7 +81,7 @@ describe('config', () => {
mockValidConfig,
mockInvalidConfig,
]);
expect(res).toEqual([{ field: 'id', operator: '==', value: '3' }]);
expect(res).toEqual([{ field: 'id', operator: '==', value: 3 }]);
});
});
});
+16 -3
View File
@@ -40,9 +40,8 @@ export function readFilterConfig(config: Config):
try {
const field = config.getString('field') as keyof Visit;
const operator = config.getString('operator');
const value =
config.getOptionalNumber('value') ?? config.getString('value');
if (isOperator(operator)) {
const value = getValue(config);
if (isOperator(operator) && value !== undefined) {
return { field, operator, value };
}
return undefined;
@@ -52,6 +51,20 @@ export function readFilterConfig(config: Config):
}
}
function getValue(config: Config) {
let value = undefined;
try {
value = config.getString('value');
} catch (error) {
try {
value = config.getNumber('value');
} catch {
// value is not string or number - ignore filter
}
}
return value;
}
/**
* Reads a set of filter by configs.
*