queryl icon indicating copy to clipboard operation
queryl copied to clipboard

Feature request: $in

Open ZEAL-MATCH-LTD opened this issue 3 years ago • 2 comments

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

ZEAL-MATCH-LTD avatar Aug 22 '22 18:08 ZEAL-MATCH-LTD

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"
                    }
                }
            }
        }
    }
}

ZEAL-MATCH-LTD avatar Aug 22 '22 19:08 ZEAL-MATCH-LTD

Hey there! Not sure I follow the first example. Is the intention to return true if the intersection of both arrays is non-empty?

jviotti avatar Sep 05 '22 15:09 jviotti