Open roysharon opened 9 months ago
For anybody encountering this bug here is a workaround until it is fixed:
import parser from 'search-query-parser'
const options = { keywords: ['brakes'], offsets: false }
const orgQuery = { brakes:['foo bar', 'choo'], exclude: {} }
let stringified = parser.stringify(orgQuery, options)
// stringified = 'brakes:"foo bar",choo'
// workaround start
const replacements = []
stringified = stringified.replace(/"([^"]*)"/g, (match, p1) => {
replacements.push(p1)
return `__${replacements.length - 1}__`
})
const destQuery = parser.parse(stringified, options)
Object.entries(destQuery).forEach(([key, value]) => {
if (Array.isArray(value)) {
destQuery[key] = value.map(v => v.replace(/__(\d+)__/g, (match, p1) => replacements[p1]))
}
})
// workaround end
// destQuery = { brakes: [ 'foo bar', 'choo' ], exclude: {} }
When using a keyword with multiple values that one of them includes a space,
stringify
method works as expected, butparse
does not produce the same query: