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

esker

v0.1.1

Published

Fluent interface query strings

Downloads

3

Readme

esker

Fluent query strings

companions/name.is.Amelia_and_age.is.greater.than.22

Queries

Esker looks at query strings as a sequence of one or more sentences. Sentences are conjuncted using the logical conjunctions _and_ and _or_.

Example:

year.is.2015_and_style.is.soul

Sentences

Sentences basically follow the structure <field><comparison><value>.

The <field> is the name of the property / field to filter against.

The <value> is the value the field should be compared against

The <comparison> is a verb than can be mixed with modificators (not) and optional conjunctions (than). Supported verbs are:

  • equality: is, equals
  • partial equality, membership: contains, has
  • greater than: greater, larger, more
  • smaller than: smaller, less

Note: when using greater / smaller than comparisons, you are free to still use the is, equals verbs. E.g. age.is.less.than.10

To make queries more readable, the optional than conjunctions can be added.

To inverse comparison, the not modificator can be added.

Here a few examples

// items that have the name 'Amelia'
name.is.Amelia
name.equals.Amelia
// items that do NOT have the name 'Amelia'
name.is.not.Amelia
// items whose name contains 'eli'
name.contains.eli
// items whose age is more than 20
age.is.more.than.20
age.greater.than.20
age.greater.20
// items whose price is less than 10
price.is.less.than.10
price.is.smaller.than.10
price.smaller.10
// items whose price is less than 10 (using not)
price.is.not.more.than.9

Usage

Esker's central (and currently only exposed) method is parse(<query-string>). To use Esker, first install it:

npm install esker --save

Import it as a module then use the parse() method on your query string.

const esker = require('esker')

let result = esker.parse('name.is.Amelia')

The result will contain an object containing an array of object (one for each sentence passed).

Each sentence is represented as an object with the properties field, verb, not, value, operatorLeft, operatorRight. The above example ('name.is.Amelia'), results in:

{
    'field': 'name',
    'verb': 'is',
    'not': false,
    'value': 'Amelia,
    'operatorLeft': 'none',
    'operatorRight': 'none'
}

The verb field may contain one of the following values:

  • 'is' - equality
  • 'has' - partial equality, membership
  • '>' - greater than
  • '<' - less than

The not field is set to true if the sentence contains the modifier 'not'.

The operatorLeft and operatorRight express and sentence's relation to its neighbours (see Logical Operators). It may contain one of the following values:

  • 'none'
  • 'and'
  • 'or'

Logical Operators

Sentences can be combined using the '_and_' and '_or_' operators. Here an example:

color.is.blue_and_price.is.10_or_color.is.red

Note that Esker currently does not support defining any operator precedence by means of brackets. Logical operators are simply added to the result's sentences items as operatorLeft and operatorRight properties.

How these operators are interpreted depends on the extension used (see section on Extensions)

Extensions

The parse() method takes a second (optional) attribute, an extension method. This can be any method take takes and array of sentences and returns an object.

The returned object is then added to the result as the value of the output property.

Currently Esker comes with a MongoDB extension. Here is how you use it:

const esker = require('esker')

let query = 'color.is.blue_and_price.is.10_or_color.is.red'

let result = esker.parse(query, esker.mongo)

// result.output contains an object that you now can use as the filter in a MongoDB find() call
let filter = result.output