Skip to content

Commit

Permalink
feat: allow null value in filter list
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Nov 27, 2022
1 parent 363e260 commit a264d6c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
19 changes: 17 additions & 2 deletions src/parameter/filters/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,23 @@ export function buildQueryFilters<T extends ObjectLiteral = ObjectLiteral>(
}

if (Array.isArray(input)) {
// todo: check array elements are string
output[key] = input.join(',');
// preserve null values
const data : string[] = [];
for (let i = 0; i < input.length; i++) {
if (input[i] === null) {
input[i] = 'null';
}

if (typeof input[i] === 'number') {
input[i] = `${input[i]}`;
}

if (typeof input[i] === 'string') {
data.push(input[i]);
}
}

output[key] = data.join(',');

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parameter/filters/utils/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function transformFilterValue(input: FilterValueSimple) : FilterValueSimp
}

return (input as unknown[])
.filter((n) => n === 0 || !!n) as FilterValueSimple;
.filter((n) => n === 0 || n === null || !!n) as FilterValueSimple;
}

if (typeof input === 'undefined' || input === null) {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ describe('src/build.ts', () => {
// with negation & in operator
record = buildQuery<Entity>({
filter: {
id: [1,2,3],
id: [null, 1,2,3],
},
});
expect(record).toEqual(buildURLQueryString({ [URLParameter.FILTERS]: { id: '1,2,3' } }));
expect(record).toEqual(buildURLQueryString({ [URLParameter.FILTERS]: { id: 'null,1,2,3' } }));

// with negation & like operator
record = buildQuery<Entity>({
Expand Down
2 changes: 1 addition & 1 deletion test/unit/filters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe('src/filter/index.ts', () => {
{
key: 'id',
operator: FilterComparisonOperator.IN,
value: [0, 1, 2, 3],
value: [null, 0, 1, 2, 3],
},
] as FiltersParseOutput);

Expand Down

0 comments on commit a264d6c

Please sign in to comment.