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

als-json-filter

v1.0.0

Published

A versatile JSON filtering utility that mimics database query functionality for JSON objects.

Downloads

6

Readme

als-json-filter

als-json-filter is a versatile utility for filtering JSON objects based on a variety of conditions, similar to querying databases but applicable in general JavaScript environments. This package allows users to apply complex query conditions to JSON data structures, making it ideal for data processing tasks in both frontend and backend applications.

Installation

Install the package via npm:

npm install als-json-filter

Usage

To use als-json-filter, import the function and define your filter criteria:

const createFilter = require('als-json-filter');

const filter = createFilter({
    age: { $gt: 18, $lt: 65 },
    name: { $regex: '^[a-zA-Z]+$' }
});

const data = [
    { name: 'Alice', age: 25 },
    { name: 'Bob', age: 17 },
    { name: 'Charlie', age: 70 }
];

const filteredData = data.filter(filter);
console.log(filteredData);

This will output:

[
    { name: 'Alice', age: 25 }
]

Examples of Filter Operators

Greater Than ($gt)

const filter = createFilter({ age: { $gt: 30 } });
console.log(filter({ age: 31 })); // true
console.log(filter({ age: 30 })); // false

Less Than or Equal To ($lte)

const filter = createFilter({ age: { $lte: 20 } });
console.log(filter({ age: 20 })); // true
console.log(filter({ age: 21 })); // false

Regex Match ($regex)

const filter = createFilter({ username: { $regex: '^[a-z0-9_-]{3,16}$' } });
console.log(filter({ username: 'user_123' })); // true
console.log(filter({ username: 'not_valid!' })); // false

Logical OR ($or)

const filter = createFilter({
    $or: [
        { age: { $lt: 20 } },
        { age: { $gt: 50 } }
    ]
});
console.log(filter({ age: 18 })); // true
console.log(filter({ age: 30 })); // false
console.log(filter({ age: 60 })); // true

Property Exists ($exists)

const filter = createFilter({ email: { $exists: true } });
console.log(filter({ email: '[email protected]' })); // true
console.log(filter({ username: 'user123' })); // false

Type Matching ($type)

const filter = createFilter({ count: { $type: 'number' } });
console.log(filter({ count: 10 })); // true
console.log(filter({ count: 'ten' })); // false

Supported Filter Operators

als-json-filter supports a range of operators, similar to those found in MongoDB:

  • $gt: Greater than
  • $gte: Greater than or equal to
  • $lt: Less than
  • $lte: Less than or equal to
  • $eq: Equal
  • $ne: Not equal
  • $in: Included in an array
  • $nin: Not included in an array
  • $regex: Matches regex
  • $or: Logical OR
  • $and: Logical AND
  • $not: Logical NOT
  • $nor: Logical NOR
  • $exists: Property exists
  • $type: Type of property matches

License

als-json-filter is MIT licensed.