I wrote this function that construct an object based if some values are null or not.
protected temporalFilter({ range }: Filter): W {
const temporalKey = ['created', 'bootedSince', 'firstComunication'];
if (temporalKey.includes(range.key)) {
if (range.from && !range.to) {
return {
[range.from as keyof T]: { gte: range.from }
} as unknown as W;
}
else if (!range.from && range.to) {
return {
[range.to as keyof T]: { lte: range.to }
} as unknown as W;
}
else if (range.from && range.to) {
return {
AND: [
{ [range.key as keyof T]: { lte: range.to } },
{ [range.key as keyof T]: { gte: range.from } }
]
} as unknown as W;
}
else return {} as unknown as W;
}
else return null;
}
But, there is a compact way to do that or some refactoring that can I do?
Filter. I assume it is from elastic search?