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

obj-sieve

v0.0.7

Published

Skim an object of unnecessary data

Downloads

6

Readme

obj-sieve

Skim an object of unnecessary data.

Why

Every time you have a big json to save to a db or to send through the wire you should remove all unneccesary data. This library reduce all the work to a function call with an easy to understand expression.

Importing and creating a sieve instance

const Sieve = require('obj-sieve');
const sieve = new Sieve();

Example

This is our original object:

const characters = {
  heroes: [
    {
      title: 'mr',
      name: 'Bruce Wayne',
      secretIdentity: 'batman',
      base: 'batcave',
    },
    {
      title: 'mr',
      name: 'Clarke Kent',
      secretIdentity: 'superman',
      base: 'fortress of solitude',
    },
    {
      title: 'princess',
      name: 'Diana Prince',
      secretIdentity: 'wonder woman',
      base: 'Themyscira',
    }
  ],
  villains: [
    {
      title: 'mr',
      name: 'Jack Napier',
      secretIdentity: 'the joker',
      base: 'Unknown',
    }    
  ]
};

Let's say you are only interested in heroes, and only their cover identities (you don't want to save secret informations on our db after all!). You can use this expression:

sieve.include('heroes[:][name|title]');

That means every item in "heroes" array, but only the fields "name" and "title" (I explain the path expressions below). You can use the method include multiple times, to add different part of the object you are interested in keeping.

const filteredCharacters = sieve.apply(characters);

filteredCharacter contains:

const filteredCharacters = {
  heroes: [
    {
      title: 'mr',
      name: 'Bruce Wayne',
    },
    {
      title: 'mr',
      name: 'Clarke Kent',
    },
    {
      title: 'princess',
      name: 'Diana Prince',
    }
  ],
};

Path expressions

You can find a full explanation of path expressions here: https://github.com/sithmel/obj-path-expression-parser

A path expression is composed by comma separated paths. Like:

hello.world,x.y

Every path contains a certain number of fragments. "hello", "world", "x" and "y" are fragments. Fragments tries to match a value in an object. For example, the expression will find 2 matches in this object:

{
  hello: {
    world: 'here'
  },
  x: {
    y: 'and here'
  }
}

A fragment can use the globbing syntax to match multiple properties.

hello[*]

will match:

{
  hello: {
    world: 'here'
    mars: 'and here'
  },
  x: {
    y: 'not here'
  }
}

You can notice that you can use a dot or square brackets to separate the fragments. Between square brackets you can use any character (but you'll have to escape other square brackets with a backslash). You can also use escaping to match characters used in globbing (* and ? for example). Here's what you can do with globbing:

  • xyz: it matches only the attribute "xyz"
  • xyz|abc: it matches both "xyz" and "abc"
  • * : it matches all attributes
  • !abc: it matches everything except abc
  • test?: it matches "test1", "test2", "test3". It doesn't match "test" or "test10"
  • test*: it matches "test1", "test2", "test3", "test" and "test10"

When matching an array you can use the slice notation. It uses ":" to separate 2 indexes (it uses the same syntax as Array.prototype.slice, or Python slices). The first number is where the slice starts. If omitted it will be considered 0. The second number is where the slice ends. If omitted it will be considered equal to the length of the array. Negative numbers are calculated from the end of the array. For Example:

  • [:] of [1, 2, 3, 4] = [1, 2, 3, 4] all
  • [1:] of [1, 2, 3, 4] = [2, 3, 4] all excepti the first
  • [:1] of [1, 2, 3, 4] = [1] to the one with index 1
  • [:-1] of [1, 2, 3, 4] = [1, 2, 3] to the one before the last
  • [1:-1] of [1, 2, 3, 4] = [2, 3] from the one with index one to the one before the last
  • [-2:] of [1, 2, 3, 4] = [3, 4] the last 2

Nested path expressions

A fragment can contain a nested path expression (using round parenthesis):

users(x,y,z)name

This will match:

{
  users: {
    x: { name: 'mr X' },
    y: { name: 'mr Y' },
    z: { name: 'mr Z' },
  }
}

Custom functions

A custom function can be used to enable a more complex filtering. You can add a custom function with:

sieve.setCustomFunctions({
  '=': (path, funcArgument, parent) => {
    const [fieldName, value] = funcArgument.split(',')
    if (_isPlainObject(parent) && _get(parent, fieldName).toString() === value) {
      return [path]
    }
    return []
  }
})

Then you can use the custom function like this:

sieve.include('heroes[:]{= title,mr}[name]')

You put the custom function between curly braces. The name of the function is the first string ("=" in this case), the argument is the rest of the fragment (title,mr). A custom function takes the current path, the argument, and the current object. It should returns an arrays of paths. In the example I am either passing an empty array or an array with a single path. I am filtering what path include and what don't.

exclude

In case you need to filter out some of the content you are including, there is a method exclude. This method takes a path expression (just like the include method) and uses this to identify what part of the final object we need to remove.

sieve.include('heroes');
sieve.exclude('heroes[:][secretIdentity|base]');
sieve.apply(characters);

This is importing the heroes array, and then removing "secretIdentity" and "base".

Additional features

The sieve object is JSON serializable:

const json = JSON.stringify(sieve);

You can deserialize it like this:

const sieve = new Sieve(JSON.parse(json));

A shorthand is also available:

Sieve.filter('hello,world', { hello: 1, world: 2, other: 3});
// returns: { hello: 1, world: 2 }

that is equivalent to:

const sieve = new Sieve();
sieve.include('hello');
sieve.include('world');
sieve.apply({ hello: 1, world: 2, other: 3});
// returns: { hello: 1, world: 2 }

ES compatibility

This package is compatible with ES2015 (ES6) as it uses ES generators.