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

ad-hoc-json-filter

v1.0.12

Published

Dynamic filtering for JSON data

Downloads

70

Readme

ad-hoc-json-filter

Filtering JSON data with variable filter conditions at runtime.

$ npm install ad-hoc-json-filter

Why?

Filtering JSON in JS is simple. At least when you know the filter conditions at dev time.

But when it comes to runtime (for example if you offered variable filter inputs to your users), the filter conditions (except the values itself) can't be changed as the code can't change at runtime.

That's the point this library is intended for.

How?

Build an array like this:

[
  { key: 'details.age', op: '>=', value: 18 }, 
  { key: 'isActive', op: '=', value: true },
  { key: 'registration', op: '<=', value: '2020-01-01' },
  { grp: '(' }
    { key: 'details.address.state', op: '=', value: 'Colorado' }, 
    { con: '||' }, 
    { key: 'details.address.state', op: '=', value: 'California' }, 
  { grp: ')' }
]

That's it. Just put your JSON array and the filter array into the filter function and be happy.

The details!

The filter() function takes a JSON array and a filter array and will return the filtered result as new array:

function filter(json: Array<unknown>, filterExpressions: Array<expressionFilter | expressionConnector | expressionGroup>);

The json parameter must be an array with JSON objects (not stringified JSON!).

The filterExpressions parameter can contain three different types of filter expressions:

type expressionFilter = {
    key: string; // The property/sub-property name to filter
    op: '=' | '!=' | '<' | '<=' | '>' | '>=' | 'cont' | 'sw' | 'ew'; // The comarison operation to perform
    val: string | number | boolean | null; // The value to compare with
};
type expressionConnector = {
    con: '&&' | '||'; // AND / OR connection
};
type expressionGroup = {
    grp: '(' | ')'; // Opening or closing a group
};

If you don't pass a 'expressionConnector' between each 'expressionFilter', they will automatically be connect with AND logic.

If you pass a string as filter value, but the data value isn't a string, the lib will perform a .toString() on number | bigint | boolean and a .toISOString() on Date. A null value in the data will be compared as 'null'.

And always remember: shit in, shit out :)