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

filter-expressions

v0.4.2

Published

Basic array structure that can conditionally validate objects

Downloads

202

Readme

Overview

Simple util to provide the ability to evaluate targets based on simple evaluation conditions expressed as arrays. Primarily created so that external JSON files can express conditional expressions.

Example

The below example shows the simple filter expressions on a collection.

// Import evaluate from the library
import { evaluate } from 'filter-expressions';

// JSON definition of filter
const json = {
  "filter": [
    "all",
    ["==", "type", "document"],
    ["in", "tags", "example", "other"]
  ]
};

// Example objects
const objects = [
  { type: 'document', tags: ['example'], src: 'https://alpacamaps/document1.json' },
  { type: 'document', tags: ['something'], src: 'https://alpacamaps/document2.json' },
  { type: 'document', tags: ['other'], src: 'https://alpacamaps/document3.json' },
  { type: 'image', tags: ['example'], src: 'https://alpacamaps/image.png' }
];

// Match the objects
const matched = objects.filter(obj => evaluate(json.filter, obj));
// Returns [
//   { type: "document", src: "https://alpacamaps/document1.json", tags: ["example"] },
//   { type: "document", src: "https://alpacamaps/document3.json", tags: ["other"] }
// ]

Installation

You can add this using NPM

$ npm install filter-expressions

Supported Comparison Operators

  • Existence: exists / exist / !exists / !exist / empty / !empty
  • Comparison: == / != / > / >= / < / <=
  • Membership: in / !in
  • String Comparision: match
  • Combining: all / any / none
  • Geospatial comparisons: geo-within / geo-contains / geo-disjoint / geo-crosses / geo-overlap

Extensions

Custom Comparison Operators

You can provide your own comparison operator implementations.

import { evaluate } from 'filter-expressions';

const options = {
  comparisons: {
    custom: (a, b) => a === b,
  },
};

// evaluate(expression, object, [options])
const result = evaluate(['custom', 'foo', 'bar'], { 'foo': 'bar' }, options);

console.log(result); // Prints true

Custom Value Modifiers

You can implement your own value modifiers.

import { evaluate } from 'filter-expressions';

const options = {
  modifiers: {
    not: (val) => !val,
  },
};

// evaluate(expression, object, [options])
const result = evaluate(['==', 'not(a)', false], { a: true }, options);

console.log(result); // Prints true

Custom Operators

You can implement custom operators to perform transformations

import { evaluate } from 'filter-expressions';

const options = {
  operators: {
    at: (target, index, array) => array && array[index],
    'to-number': (target, val) => Number(val),
  },
};

console.log(evaluate(['at', 1, [2, 4, 6]])); // Prints 4
console.log(evaluate(['to-number', '4'])); // Prints 4

Conversion of query

// Import asMongoDbQueryDocument from the library
import { asMongoDbQueryDocument } from 'filter-expressions';

// JSON definition of an expression that can run both on a local collection
const json = [
  "all",
  ["==", "type", "document"],
  ["in", "tags", "example", "other"]
];

// Convert the evaluation into a mongodb query document that can run on
// a mongo db collection
const query = asMongoDbQueryDocument(json);

Notes

  • Comparisons are done as using lodash "isEqual".
  • Run npm t to run tests
  • Inspired based on Mapbox style filters