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

viceroy-query

v0.1.0

Published

Viceroy ORM's query language implementation

Downloads

5

Readme

Viceroy Query

Viceroy Query is a query language developed for the (soon to be released) Viceroy ORM.

Designed as a standalone package to allow other libraries to share a common query language. It can be used to filter arrays, and check for matches.

Use it with node or in the browser using browserify.

npm install viceroy-query

NPM version

Browser Support

Example

var query = require('viceroy-query');

// create a query for fresh apples.
var q = query({
  kind: 'apple',
  sweetness: { $gt: 60, $lt: 90 }
});

// our array of fruits.
var fruits = [
  { kind: 'apple', sweetness: 74 },
  { kind: 'bannana', sweetness: 54 },
  { kind: 'mango', sweetness: 81 },
  { kind: 'apple', sweetness: 99 },
  { kind: 'mango', sweetness: 40 },
  { kind: 'apple', sweetness: 44 },
  { kind: 'apple', sweetness: 61 },
  { kind: 'bannana', sweetness: 97 }
];

// filter the fresh apples from the lot.
var freshApples = q.filter(fruits);
freshApples => [
  { kind: 'apple', sweetness: 74 },
  { kind: 'apple', sweetness: 61 }
];

// our a random fruit.
var randomFruit = { kind: 'apple', sweetness: 74 };

// check to see if the fruit matches the query.
var isMatch = q.match(randomFruit);
isMatch => true

Possible Query Properties

Below is an example of the possible fields in a query object.

{
  field: 'value',
  field: /pattern/,
  field: { $in: ['value1', 'value2'] },
  field: { $notIn: ['notvalue1', 'notvalue2'] },
  field: { $not: 'notvalue' },
  field: { $gt: 1 },
  field: { $gt: 'a' },
  field: { $ln: 1 },
  field: { $ln: 'a' },
  field: { $exists: true },
  $fields: ['field1', 'field2'],
  $offset: 0,
  $limit: 10
}

Query

query(Object query) => Query q

Creates a new query. The query can be used to filter an array of objects, or deterime if a single item is a match.

The query itself supports a collection of operators, as well as regex matching.

Note that operators can be used together.

Direct Match

{ field: 'val' }

Will match any object containing the property field with the value 'val'.

Regex

{ field: /pattern/ }

Will match any object containing the property field with a value matching the /pattern/.

Exists

{ field: { $exists: true } }

Will match any object containing the property field regardless of the value.

Less Than

{ field: { $lt: 1 } }

Will match any object containing the property field with a value less than 1. Less than works with strings and numbers.

Greater Than

{ field: { $gt: 1 } }

Will match any object containing the property field with a value greater than 1. Greater than works with strings and numbers.

Not

{ field: { $not: 'val' } }

Will match any object containing the property field with a value not equal to 'val'.

In

{ field: { $in: ['val1', 'val2'] } }

Will match any object containing the property field with a value matching any within the array. In this case 'val1' and 'val2'.

Not In

{ field: { $notIn: ['val1', 'val2'] } }

Will match any object containing the property field with a value not matching any within the array. In this case anything not equal to 'val1' and 'val2'.

Offset

{ $offset: 10 }

Causes .filter to skip the first 10 matches.

Limit

{ $limit: 10 }

Causes .filter to skip the limit the results to 10 matches. Limit will only be applied after offset. Thus if the offset is 10 then matches 11 through 20 will be included in the results.

fields

{ $fields: ['field1', 'field2'] }

Limits the fields added to the results returned from .filter. In this example, the results will only contain the fields 'field1' and 'field2'.

Match

q.match(Object data) => Boolean isMatch;

Checks an object against the query. For controlling the results see $offset, $limit and $fields above.

Filter

q.filter(Array dataSet) => Array matches;

Checks each item in a data set against the query. Returns an array containing the matching items.