queryl
queryl copied to clipboard
Feature request: $in
Feature request for $in
import queryl from 'queryl'
const query = {
$in: {
sports: ['golf', 'basketball', 'running']
}
}
const data = {
sports: ['football', 'hockey', 'swimming', 'golf', 'tennis']
}
queryl.match(query, data) // <-- true
I've temporarily created a recursive $or creator - if this is the preferred approach then close this issue 😊
type Operator = '$not' | '$equal' | '$contain' | '$match' | '$gt' | '$lt'
type Input = {
[operator in Operator]?: {
[key: string]: any
}
}[]
type RTN = {
$or?: {
[operation: string]: {}
}
}
const createNestedOr = (input: Input, obj: RTN = {}): RTN => {
return input.reduceRight((acc, item) => {
const rtn: RTN = {
...acc,
$or: {
...item,
},
}
const nextOr = createNestedOr(input.slice(1), rtn['$or'])
if (nextOr) rtn.$or = nextOr
return rtn
}, obj)
}
const nested = createNestedOr([
{ $lt: { someNumber: 5 }, $equal: { hello: "world" } },
{ $contain: { sports: "bowling" }, $gt: { someNum: 18 } },
{ $contain: { sports: "jogging" } },
{ $contain: { sports: "golf" } }
]);
console.log("res", nested);
OUTPUT
{
"$or": {
"$lt": {
"someNumber": 5
},
"$equal": {
"hello": "world"
},
"$or": {
"$contain": {
"sports": "bowling"
},
"$gt": {
"someNum": 18
},
"$or": {
"$contain": {
"sports": "jogging"
},
"$or": {
"$contain": {
"sports": "golf"
}
}
}
}
}
}
Hey there! Not sure I follow the first example. Is the intention to return true if the intersection of both arrays is non-empty?