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

complex-filter-stream

v0.1.4

Published

Transform stream with complex filter for objects

Downloads

11

Readme

complex-filter-stream

Build Status Coverage Status License: MIT
Filter out objects that don't match a series of filtering requirements.

TL;DR

Basic usage

const complexFilterStream = require('complex-filter-stream')
const readableStream = getaReadableObjectModeStreamSomeHow()

const queryStack = [
	{
		"type": "filter",
		"not": false,
		"searchFor": "chocolate",
		"searchOn": "flavor",
		"searchType": "contains"
	}
]

const myFilter = complexFilterStream(queryStack)

readableStream.pipe(myFilter)

How to build the queryStack.

complex-filter-stream is a Transform stream made out of a bundle of transform streams built based on a series of filtering requirements. They are passed to the stream as a json object.

A simple filter

[
	{
		"type": "filter",
		"not": false,
		"searchFor": "search string",
		"searchOn": "microscopist",
		"searchType": "contains"
	}
]

Notice that the filter object is part of an array. That is because complex-filter-stream let you stack filters. Now let's go through the specifics.

type

The type field can accept three values: filter, AND and OR. We will talk extensively about it soon enough.

not

The not field takes either false or true. If not present it defaults to false.

searchFor

The searchFor field takes what should be expected to see in the object for filter in.

searchOn

The searchOn field takes the fields of the object that will be filtered.

searchType

The searchType field is used to identify the type of filter it should be used. Currently, complex-filter-stream supports:

contains: passes if the value of searchFor matches anywhere in the object[searchOn].
exact: passes if the value of searchFor matches exactly the object[searchOn].
startsWith: passes if the value of searchFor matches the beginning of the object[searchOn].
endsWith: passes if the value of searchFor matches the end of the object[searchOn].
regex: passes if the javascript flavored regex in searchFor matches object[searchOn].

There is also support for values: exactValue: passes if the value of searchFor is exactly the value of object[searchOn].
lessThan: passes if the value of object[searchOn] is smaller than searchFor.
greaterThan: passes if the value of object[searchOn] is larger than searchFor.
between: passes if the value of object[searchOn] is in between two semi-colon separated values in searchFor.

In this cases, NOT also works and invert the search intervals if true

A complex filter.

Let's suppose that our objects to be filtered looks like this:

{
	"date": 1161302400,
	"NBCItaxID": 80880,
	"speciesName": "Hylemonella gracilis",
	"tiltSingleDual": 1,
	"defocus": -12,
	"dosage": 75,
	"tiltConstant": 1,
	"tiltMin": -63,
	"tiltMax": 60,
	"tiltStep": 0.9,
	"microscopist": "Gavin Murphy",
	"institution": "Caltech",
	"lab": "Jensen Lab",
	"sid": "gm2006-10-20-4"
}

and we would like to filter in all objects with NCBItaxID as 80880 AND microscopist as Gavin Murphy.

To do that we need two filter requirements, let's see how to organize them:

[
	{
		"type": "filter",
		"searchFor": 80880,
		"searchOn": "NCBItaxID",
		"searchType": "exact",
	},
	{
		"type": "filter",
		"searchFor": "Gavin Murphy",
		"searchOn": "microscopist",
		"searchType": "exact"
	}
]

so... the default association in complex-filter-stream is AND. As we add type:filter objects to the stack, they will be considered in sequence as in AND form.

Now, let's build a filter for objects with NCBItaxID as 80880 AND microscopist as Gavin Murphy OR Matt Swulius:

[
	{
		"type": "filter",
		"searchFor": 80880,
		"searchOn": "NCBItaxID",
		"searchType": "exact",
	},
	{
		"type": "OR",
		"args": [
			{
				"type": "filter",
				"searchFor": "Gavin Murphy",
				"searchOn": "microscopist",
				"searchType": "exact"
			},
			{	"type": "filter",
				"searchFor": "Matt Suwlius",
				"searchOn": "microscopist",
				"searchType": "exact",
			}
		]
	}
]

As we can see complex-filter-stream uses a different type of filter object to listen to associations.

Now, let's build a filter for objects with NCBItaxID as 80880 AND microscopist as Gavin Murphy OR Matt Swulius, but from all the objects with Matt Swulius let's only filter in the ones with lab:"Jensen Lab":

[
	{
		"type": "filter",
		"searchFor": 80880,
		"searchOn": "NCBItaxID",
		"searchType": "exact",
	},
	{
		"type": "OR",
		"args": [
			{
				"type": "filter",
				"searchFor": "Gavin Murphy",
				"searchOn": "microscopist",
				"searchType": "exact"
			},
			{
				"type": "AND",
				"args": [
					{	"type": "filter",
						"searchFor": "Matt Suwlius",
						"searchOn": "microscopist",
						"searchType": "exact",
					},
					{	"type": "filter",
						"searchFor": "Jensen Lab",
						"searchOn": "lab",
						"searchType": "exact",
					},
				]
			}
		]
	}
]

In this case, we had to declare another type of association (AND). So as a rule of thumb, everytime that there is a change in association type (AND or OR), a new type of association must be declared.

If we would like to make the lab requirement to all of them:

[
	{
		"type": "filter",
		"searchFor": 80880,
		"searchOn": "NCBItaxID",
		"searchType": "exact",
	},
	{	"type": "filter",
		"searchFor": "Jensen Lab",
		"searchOn": "lab",
		"searchType": "exact",
	},
	{
		"type": "OR",
		"args": [
			{
				"type": "filter",
				"searchFor": "Gavin Murphy",
				"searchOn": "microscopist",
				"searchType": "exact"
			},
			{	"type": "filter",
				"searchFor": "Matt Suwlius",
				"searchOn": "microscopist",
				"searchType": "exact",
			}
		]
	}
]