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

@barreljs/json-filter

v0.0.2

Published

A simple filter for JSON.

Downloads

19

Readme

@barreljs/json-filter

A simple library to parse out specific elements of a larger JSON object or JSON array based on a provided JSON filter. This makes it easier to filter out key-value pairs on any given JSON object, even more complex filter object are supported if you feel like that (feel free to check out the tests on examples).

Installation

Install via npm

npm install @barreljs/json-filter

or yarn

yarn add @barreljs/json-filter

Getting started

Let's take a very simple JSON object as example ...

const json = {
    actions: [
        {
            type: 'visit',
            property: 'website'
        },
        {
            type: 'click',
            property: 'site_signup'
        },
        {
            type: 'click',
            property: 'site_pricing'
        }
    ],
    property: 'other'
}

... and define a simple filter to get all actions that are of {type: 'click'}

const filter = {
    type: 'click'
}

You can now easily filter the original JSON object with the given filter ✨

const JsonFilter = require('@barreljs/json-filter')

const result = JsonFilter(json, filter)
const elements = result.all()

This will match all elements from the JSON object that have a property type with a value of 'click'. As a result elements will look like this

[
    {
        type: 'click',
        property: 'site_signup'
    },
    {
        type: 'click',
        property: 'site_pricing'
    }
]

Filter

The filter can be any valid JSON object (arrays are not support at this point). An empty JSON filter ({}) would match all elements. I don't know why you would do that, but you can ¯\_(ツ)_/¯

RegEx

json-filter supports RegEx for filter properties

{
    property: /^site_\w*$/
}

$any

You can use the keyword $any on any filter property to match all possible values, like strings, numbers, boolean, objects, and arrays. This might not be useful in the example shown above, but can be helpful if your JSON object gets more complex.

{
    property: '$any'
}

Usage

JsonFilter(json, filter, trim)

Arguments

| Argument | Required | Type | Description | | -------- | -------- | ---- | ----------- | | json | yes | JSON | Any valid JSON object or array | | filter | yes | Object | Any valid JSON object | | trim | no | bool | A flag to indicate if your results should be trimmed to properties of your filter. If false the result will keep the structure of the original JSON, if true only properties defined in the filter will be returned. Default to false. |

Example

const result = JsonFilter(json, filter, trim)

with trim set to false the above example will match the structure of the original JSON

[
    {
        type: 'click',
        property: 'site_signup'
    },
    {
        type: 'click',
        property: 'site_pricing'
    }
]

with trim set to true, the result will be trimmed to the structure of the filter

[
    {
        type: 'click'
    },
    {
        type: 'click'
    }
]

Result

The result is an object with following properties:

| Property | Return Type | Description | | --------- | -------- | --------- | | length | Number | Number of matching elements | | all() | Array | Returns all matching elements as a JSON array. | | first() | Object | Returns the first matching element | | last() | Object | Returns the last matching element | | get(i) | Object | Returns the matching element on given index i |

Methods

.all()

Returns all matching elements as a JSON array.

const result = JsonFilter(json, filter)
const all = result.all() // returns element array

.first()

Returns the first matching element. If no elements match, returns undefined.

const result = JsonFilter(json, filter)
const first = result.first() // returns first element

.last()

Returns the last matching element. If no elements match, returns undefined. If only one element exists, it returns the same element as .first().

const result = JsonFilter(json, filter)
const last = result.last() // returns last element

.get(index)

Returns the matching element on given index or undefined if no element on that index exists. Index starts at 0.

const result = JsonFilter(json, filter)
const element = result.get(3) // returns 4th element

License

This project is licensed under the MIT license, Copyright (c) 2020 David Pichsenmeister | pichsenmeister.com. For more information see LICENSE.