Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/helpers/filters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function convertCondition({ field, operator, value, isEmptyIgnored }) {
if (isEmptyIgnored && !value) return [];
if (isEmptyIgnored && !isValuelessOperator(operator) && isEmptyValue(value)) return [];
switch (operator) {
case '$eq':
return [field, 'eq', typeof value === 'string' ? `"${escape(value)}"` : value];
Expand All @@ -25,6 +25,10 @@ export function convertCondition({ field, operator, value, isEmptyIgnored }) {
return [field, 'is', 'null'];
case '$ne:null':
return [field, 'not.is', 'null'];
case '$eq:null:text':
return `or(${field}.is.null,${field}.eq."")`;
case '$ne:null:text':
return `and(${field}.not.is.null,${field}.neq."")`;
case '$in':
return [field, 'in', `(${value})`];
case '$notIn':
Expand All @@ -45,7 +49,9 @@ export function generateFilter(config) {
if (!config.link || !config.conditions || config.if === false) return '';
const conditions = config.conditions
.map(condition => {
return condition.link ? generateFilter(condition) : convertCondition(condition).join('.');
if (condition.link) return generateFilter(condition);
const converted = convertCondition(condition);
return typeof converted === 'string' ? converted : converted.join('.');
})
.filter(condition => condition);

Expand All @@ -60,3 +66,17 @@ function escape(value) {
// weird but it's how back slash match
return value.replaceAll('\\', '\\\\\\\\').replaceAll('"', '\\"');
}

function isValuelessOperator(operator) {
return typeof operator === 'string' && operator.includes(':null');
}

function isEmptyValue(value) {
return (
value === undefined ||
value === null ||
value === '' ||
(Array.isArray(value) && value.length === 0) ||
(typeof value === 'object' && JSON.stringify(value) === '{}')
);
}
6 changes: 4 additions & 2 deletions ww-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ export default {
},
{ label: 'Ends with', value: '$iLike:endsWith', acceptedTypes: ['string'], defaultValue: 'end' },
{ label: 'Is exactly', value: '$eq', acceptedTypes: ['array', 'object'] },
{ label: 'Is empty', value: '$eq:null', acceptedTypes: ['string', 'number', 'array', 'object'] },
{ label: 'Is empty', value: '$eq:null:text', acceptedTypes: ['string'] },
{ label: 'Is not empty', value: '$ne:null:text', acceptedTypes: ['string'] },
{ label: 'Is empty', value: '$eq:null', acceptedTypes: ['number', 'array', 'object'] },
{
label: 'Is not empty',
value: '$ne:null',
acceptedTypes: ['string', 'number', 'array', 'object'],
acceptedTypes: ['number', 'array', 'object'],
},
{ label: 'Is in', value: '$in', acceptedTypes: ['string', 'number'], defaultValue: [] },
{ label: 'Is not in', value: '$notIn', acceptedTypes: ['string', 'number'], defaultValue: [] },
Expand Down