npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-query-matcher

v1.2.1

Published

Evaluates mongo-db style queries

Downloads

33

Readme

json-query-matcher

Evaluates a mongodb-style query

Why?

I'm working on an app in which I often have arrays of objects in memory and I want to perform some operations on a subset of these arrays.

The criteria used to select these subsets can vary greatly depending on the use cases, and I want to let the users specify these criteria in a config file that will be loaded and evaluated at runtime.

I chose to have the users specify their criteria in a JSON object very strongly inspired by the syntax used to build queries in MongoDB: https://docs.mongodb.com/manual/tutorial/query-documents/

And now I'm writing this library to evaluate an object against these criteria.

Installation

npm install --save json-query-matcher

Usage

const { evaluateMatch } = require('json-query-matcher');

const items = [
    { firstName: "Gaël", lastName: "Haméon" },
    { firstName: "Gaël", lastName: "Monfils" },
    { firstName: "Alix", lastName: "Haméon" }
]

let query, result;

query = { "firstName": "Gaël" };
result = items.filter(item => evaluateMatch(item, query));
// expect(result).to.eql([{ firstName: "Gaël", lastName: "Haméon" }, { firstName: "Gaël", lastName: "Monfils" }]);

query = { "lastName": "Haméon" };
result = items.filter(item => evaluateMatch(item, query));
// expect(result).to.eql([{ firstName: "Gaël", lastName: "Haméon" }, { firstName: "Alix", lastName: "Haméon" }]);

query = { "firstName": "Gaël", "lastName": "Haméon" };
result = items.filter(item => evaluateMatch(item, query));
// expect(result).to.eql([{ firstName: "Gaël", lastName: "Haméon" }]);

query = { "$or": [{ "firstName": "Alix" }, { "lastName": "Monfils" }] };
result = items.filter(item => evaluateMatch(item, query));
// expect(result).to.eql([{ firstName: "Gaël", lastName: "Monfils" }, { firstName: "Alix", lastName: "Haméon" }]);

query = { "firstName": { "$ne": "Gaël" } };
result = items.filter(item => evaluateMatch(item, query));
// expect(result).to.eql([{ firstName: "Alix", lastName: "Haméon" }]);

Features

Operators

Comparison operators

These 8 comparison operators are available and are meant to behave the same way as in MongoDB

  • $eq: equal
  • $ne: not equal
  • $gt: greater than
  • $gte: greater than or equal
  • $lt: less than
  • $lte: less than or equal
  • $in: in
  • $nin: not in

Logical operators

These 4 logical operators are available and are meant to behave the same way as in MongoDB

  • $or
  • $and
  • $nor
  • $not

Array operators

MongoDB has 3 array operators:

  • $elemMatch
  • $all
  • $size

As of now, only the $elemMatch operator is implemented. See the Arrays section below.

Other operators

I plan on adding operators as I need them. Up next are probably the missing array operators

Arrays

Simple queries on arrays of values

When the property that is targeted by a query is an array, the query will be considered valid as soon as one of the objects in the array matches it.

const item = {
  firstName: "Gaël",
  lastName: "Haméon",
  nickNames: ["Gaga", "Galinou"],
};

const query1 = { nickNames: "Gaga" };
const result1 = evaluateMatch(item, query1);
expect(result1).to.eql(true);

const query2 = { nickNames: { $in: ["Galinou", "Alixou"] } };
const result2 = evaluateMatch(item, query2);
expect(result2).to.eql(true);

Compound queries on arrays of objects - use of $elemMatch operator

If the targeted array contains objects and the query is compound, the query will be considered valid as soon as each part of the query, taken separately, is valid for at least one of the objects in the array.

const item = {
  firstName: "Gaël",
  lastName: "Haméon",
  pets: [
    {
      name: "César",
      species: "Dog"
    },
    {
      name: "Capsule",
      species: "Cat"
    }
  ]
};

const query1 = { pets: {"name": "César", "species": "Cat"} };
const result1 = evaluateMatch(item, query1);
expect(result1).to.eql(true);

In the example above, the query could be translated as "Check if among the pets of this person, at least one is named César, and at least one is a Cat".

If you want to check if the person has a Cat named César, you have to use the $elemMatch operator:

const item = {
  firstName: "Gaël",
  lastName: "Haméon",
  pets: [
    {
      name: "César",
      species: "Dog"
    },
    {
      name: "Capsule",
      species: "Cat"
    }
  ]
};

const query1 = { pets: { $elemMatch: {"name": "César", "species": "Cat"} } };
const result1 = evaluateMatch(item, query1);
expect(result1).to.eql(false);

Dot notation

Like in MongoDB, you can use the dot notation to access deeper properties in an object.

If we have the object below:

{
    "trip": {
        "trpNumber": "123456",
        "trpType": "1",
        "tripPoints": [
            {
                "trpptPlace": {
                    "code": "CJV",
                    "name": "Cergy-le-Haut"
                },
                "trpptArrivalTime": "09:35:15",
                "trpptDepartureTime": "09:35:50"
            },
            {
                "trpptPlace": {
                    "code": "CJP",
                    "name": "Cergy-Préfecture"
                },
                "trpptArrivalTime": "09:40:00",
                "trpptDepartureTime": "09:40:50"
            }
        ]
    },
    "block": {}
}

You can write trip.tripPoints.1.trpptPlace.name to get to "Cergy-Préfecture".