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

array-filter-query-builder

v0.5.3

Published

Provides a query builder and filter to filter array of items.

Downloads

6

Readme

Array filter & Query builder

Provides a filter and query builder, with queries that can be serialised and passed around, cloned, edited, and re-run.

Why?

There are plenty of other query builders out there, for example array-query. In a lot of respects 'array-query' is a more comprehensive module. However, I needed a way of saving my current query, and then cloning it, editing the clone, and running the new query, so that I could preemptively return the results for a user interaction. For example graying-out a filter button that wouldn't yield any results. This was far easier to do by using the previous filter as a starting point and then editing it, rather than recreating an entire filter description.

To do this I needed to separate out the filter mechanism and the queries so that each query was simply an object that could be passed to a filter, in an idempotent manor.

Usage


    // ## Basic setup.
    import { QueryBuilder, Filter } from 'array-filter-query-builder'
    const filter = new Filter()
    const fixtures = [
      {
        title: 'Article 1',
        published: true,
        tags: ['Tech']
      },
      {
        title: 'Article 2',
        published: false,
        tags: ['Politics', 'Health']
      },
      {
        title: 'Article 2',
        published: true,
        tags: ['Politics', 'Tech']
      }
    ]

    // Queries can be initialised as empty queries or with pre-defined arguments
    // Empty query:
    const query = new QueryBuilder()
    const results = filter.run(fixtures, query)
    // -> returns all fixtures

    // Single pre-defined argument:
    const query = new QueryBuilder('published', {
      is: true
    })
    const results = filter.run(fixtures, query)
    // -> returns all published items in fixtures

    // Multiple pre-defined arguments:
    const query = new QueryBuilder([
      ['published', 'is', true],
      ['tags', 'anyMatchesAny', ['Politics', 'Tech']]
    ])
    const results = filter.run(fixtures, query)
    // -> returns all published items in fixtures that has of the tags
    // 'Politics' or 'Tech'.


    // ## Adding arguments to queries.
    // Single or multiple arguments can be added to a query in the same manor
    // as when initialising the query.

    // Add a single argument:
    query.add('published', {
      is: true
    })

    // Add multiple arguments:
    query.add([
      ['published', 'is', true],
      ['tags', 'anyMatchesAny', ['Politics', 'Tech']]
    ])

    // The filter can then be re-run with the same query object.
    const results = filter.run(fixtures, query)

    // ## Cloning queries.
    // If you wish to preserve the previous query, you can create a clone, and
    // edit that instead of the original. For example:
    const query2 = query.clone()
    query.add('published', {
      is: false
    })

    // Methods can be chained, so it is possible to replace the above with the
    // following:
    const query2 = query
      .clone()
      .add('published', {
        is: true
      })

    // ## Resetting queries and query argument keys
    // Individual arguments, or whole queries can be reset.
    // Please note that when resetting a query argument, any argument acting on
    // the given key will be reset. You can gain a finer degree of control over
    // query arguments by serialising, editing and then deserialising the query
    // arguments.

    // Reset an individual argument key.
    query.reset('published')

    // Reset multiple argument keys.
    query.reset(['published', 'tags'])

    // Rest all query arguments.
    query.reset()

    // Replace query arguments by key.
    // The two steps above can be combined by using the `replace` method.
    query.replace('published', {
      is: false
    })

    // ## Serialising / deserialising query arguments
    // All query arguments for a query can be serialised into a plain JS array,
    // and then restored to a query object with the add method.

    // Serialise:
    const serialised = query.serialise()
    // -> returns an array of query arguments in the form:
    // [
    //   [argumentKey, comparator, valueToCompare],
    //   [argumentKey, comparator, valueToCompare]
    // ]

    // Deserialise:
    // You can use an existing query, or initialise a new query with the
    // serialised query arguments.

    // Existing:
    query.reset().add(serialised)

    // New:
    const query = new QueryBuilder(serialised)


    // ## Comparator methods:
    // * is
    // * isNot
    // * isShallowEqual
    // * isNotShallowEqual
    // * isOneOf
    // * anyMatchesAny
    // * isAtLeast
    // * isLessThanOrEqualTo
    // * isMoreThan
    // * isLessThan
    // * isEqual

For more detailed examples, please see /src/test/.