Skip to content

Commit

Permalink
support top level “and” with nested “or”
Browse files Browse the repository at this point in the history
  • Loading branch information
ellanan committed Feb 7, 2025
1 parent c8877f2 commit a16190b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
13 changes: 10 additions & 3 deletions src/utils/formatQueryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ export function formatQueryString(key, value) {
}

if (key === 'filter') {
const filterValues = []

// Handle 'or' conditions if they exist
if (value.or && Array.isArray(value.or)) {
const orQueries = value.or.map(filterGroup => Object.keys(filterGroup)
.map(filterType => formatFilterString(filterType, filterGroup[filterType]))
.join(':')
)
return `${key}=(${orQueries.join('|')})`
filterValues.push(`(${orQueries.join('|')})`)
}

const filterValues = Object.keys(value).map(filter => formatFilterString(filter, value[filter])
)
// Handle other filter types
Object.keys(value).forEach(filter => {
if (filter !== 'or') {
filterValues.push(formatFilterString(filter, value[filter]))
}
})

return `${key}=${filterValues.join(':')}`
}
Expand Down
19 changes: 14 additions & 5 deletions test/utils/formatQueryString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,28 @@ describe('Format query string', () => {

it('should handle complex OR conditions with multiple filter types', () => {
const res = formatQueryString('filter', {
eq: {
enabled: '"true"',
stackable: '"true"'
},
or: [
{
eq: { category: 'hoodies' },
gt: { price: '10' }
le: {
start: '"2025-02-06T22:19:56.492Z"'
},
gt: {
end: '"2025-02-06T22:19:56.492Z"'
}
},
{
eq: { category: 't-shirts' },
lt: { price: '20' }
le: {
end: '"2025-02-06T22:19:56.492Z"'
}
}
]
})
expect(res).to.equal(
'filter=(eq(category,hoodies):gt(price,10)|eq(category,t-shirts):lt(price,20))'
'filter=(le(start,"2025-02-06T22:19:56.492Z"):gt(end,"2025-02-06T22:19:56.492Z")|le(end,"2025-02-06T22:19:56.492Z")):eq(enabled,"true"):eq(stackable,"true")'
)
})

Expand Down

0 comments on commit a16190b

Please sign in to comment.