Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
jql-matcher
Advanced tools
Readme
JSON Query Language - Matcher. Filter a json-array based on a given query.
This library was developed and tested on Node Environment, but you can still use it for the web.
npm i -S jql-matcher
If you are using it for web app, I suggest using the built source codes:
import jql from 'jql-matcher/build';
If you are using it for NodeJS, you don't need to use the built source codes:
const jql = require('jql-matcher/src');
const data = [
{ id: 1 },
{ id: 2 },
{ id: 3 }
];
// select * from list where id === 3
const query = { id: 3 };
const result = jql(query, data);
jql-matcher
only accepts two arguments:
query
- The query object that will be used to filter the results.data
- An array
of JSON
data.JQL has standard operations
that it uses to execute this filtering process. Operation names are always prefixed with a $
sign.
const data = [
{
id: 1,
email: 'user1@email.com',
password: 'SSBjYW4gc2VlIHRoYXQgeW91IGxpa2Ugd29uZGVyaW5nIGRvd24gdGhlIHJhYmJpdCBob2xlLg=='
watchesAnime: false,
watchesCartoons: false,
watchesRickAndMorty: true
},
{
id: 2,
email: 'user5@email.com',
password: 'QXJlIHlvdSBwbGFubmluZyB0byBkZWNvZGUgYWxsIG9mIHRoZW0/',
watchesAnime: true,
watchesCartoons: true,
watchesRickAndMorty: true
},
{
id: 3,
email: 'user3@email.com',
password: 'UmVhbGx5Pw==',
watchesAnime: false,
watchesCartoons: true,
watchesRickAndMorty: true
},
{
id: 4,
email: 'user2@email.com',
password: 'SlNPTiBRdWVyeSBMYW5ndWFnZQ==',
watchesAnime: true,
watchesCartoons: false,
watchesRickAndMorty: true
}
];
// select * from users
// where
// email === "user1@email.com" or
// email === "user2@email.com" or
// email === "user3@email.com"
const query = {
email: {
$in: ['user1@email.com', 'user2@email.com', 'user3@email.com']
}
};
const result = jql(query, data);
Operators
are abstract, therefore, there could be different approach to achieving the same result and it's also prone to abuse. The example above is what I would call a good query
because it is short and precise. A bad counterpart would be:
const query = {
$or: [
{ email: 'user1@email.com' },
{ email: 'user2@email.com' },
{ email: 'user3@email.com' }
]
};
The query above gives you the same result but it's labeled as bad
because it's unnecessarily long and complex. JQL-Matcher
is designed and intended to be extremely performant (to a point of sacrificing a little dev experience points for the sake of keeping it performant) but it should not be abused. As a general rule of thumb ALWAYS REDUCE YOUR QUERY DOWN TO IT'S SIMPLEST FORM. Prefer the shortest and simplest code.
All queries are treated as and
, unless you explicitly use $or
.
// select * from list
// where
// watchesAnime === true and
// watchesCartoons === true and
// watchesRickAndMorty === true
{
watchesAnime: true
watchesCartoons: true
watchesRickAndMorty: true
}
PS: Rick and Morty is not a cartoon! It's a simulation.
Don't be afraid to go as deep as you need to. Given this sample data:
const sampleData = [
{
id: 1,
posts: {
id: 1,
body: 'Lorem ipsum dolor sit amet,',
comments: [
{
id: 1,
body: 'consectetur adipiscing elit.'
}
]
},
preferences: {
notifications: {
outsideNotifications: {
email: true
}
}
}
}
];
The query below will return all rows where preferences.notifications.outsideNotifications.email === true
.
// select * from list
// where
// preferences.notifications.outsideNotifications.email === true
const query = {
preferences: {
notifications: {
outsideNotifications: {
email: true
}
}
}
};
// ...
You can also filter even when a key contains an array value. The query below will return all rows if
preferences.notifications.outsideNotifications.email === true
// select * from list
// where
// posts.comments has id === 1 and
// preferences.notifications.outsideNotifications.email === true
const query = {
posts: {
comments: {
id: 1
}
},
preferences: {
notifications: {
outsideNotifications: {
email: true
}
}
}
};
// ...
You can do even further query where you query a value inside an array of an array inside another giant array.
You can also nest $or
and $and
operators together whenever it makes sense to do it. Just always keep the rule of thumb in mind, ALWAYS REDUCE YOUR QUERY DOWN TO IT'S SIMPLEST FORM. Prefer the shortest and simplest code.
docs.
FAQs
JSON Query Language - Matcher. Filter a json-array based on a given query.
We found that jql-matcher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).