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

@ts-awesome/simple-query

v1.2.0

Published

TypeScript friendly SimpleQuery evaluator

Downloads

601

Readme

@ts-awesome/simple-query

TypeScript friendly SimpleQuery evaluator. Heavily inspired by Mongo Query Language

Base use

import {match} from "@ts-awesome/simple-query";

const query = {
  a: 5, // short cut for {$eq: {a: 5}
};

const candidate = {a: 5, b: 6};

const result = match(candidate, query);

Works with iterables as well

import {filter} from "@ts-awesome/simple-query";

const query = {
  a: 5, // short cut for {$eq: {a: 5}
};

const candidate = [{a: 5, b: 6}, {a: 2}, {a: 5, b: 1}];

const result = filter(candidate, query);

We have sorting too

import {sort} from "@ts-awesome/simple-query";

const candidate = [{a: 5, b: 6}, {a: 2}, {a: 5, b: 1}];

const orderedByA = sort(candidate, 'a');
const orderedByAdB = sort(candidate, 'a-,b+');
// ordering with typescript validation on keys
const orderedByAdB2 = sort(candidate, [{a: 'DESC'}, {b: 'DESC'}]);

Collections

Wrap you Iterable into Collection and do queries and ordering. Result is calculated when collection is iterated or valueOf is invoked.

import {Collection} from "@ts-awesome/simple-query";

interface IModel {
  a: number;
  b?: string;
}

const data: Iterable<IModel>;
const collecton = new Collection(data);

const result = collecton
  .where({$gt: {a: 3}})
  .sort('a-, b');

for(const item of result) {
  console.log(item);
}

Query language

Binary operators

  • {$eq: {[left]: right}} equivalent to data[left] === right
  • {$neq: {[left]: right}} equivalent to data[left] !== right
  • {$gt: {[left]: right}} equivalent to data[left] > right
  • {$gte: {[left]: right}} equivalent to data[left] >= right
  • {$lt: {[left]: right}} equivalent to data[left] < right
  • {$lte: {[left]: right}} equivalent to data[left] <= right
  • {$lte: {[left]: right}} equivalent to data[left] <= right
  • {$regex: {[left]: right}} equivalent to right.test(data[left])
  • {$like: {[left]: right}} checks if to data[left] matches like pattern in right, % is any characters
  • {$in: {[left]: right}} equivalent to right.indexOf(data[left]) >= 0
  • {$contains: {[left]: right}} equivalent to data[left].indexOf(right) >= 0

Logical

  • {$not: {/*condition*/}} equivalent to !(/*condition*/)
  • {$and: [{/*condition 1*/}, {/*condition 2*/}] equivalent to (/*condition 1*/) && (/*condition 2*/)
  • {$or: [{/*condition 1*/}, {/*condition 2*/}] equivalent to (/*condition 1*/) || (/*condition 2*/)

Reference

In some cases you need to check again other prop value. {$ref: 'prop'} does exactly that.

Example: {$neq: {a: {$ref: 'b'}}}

Advanced use

Functions like evaluate, match, filter, sort and Collection support optional ReferenceResolverFactory. By default standardResolverFactory is used

import {ISimpleQuery, ValidQueryModelSignature, ReferenceResolver} from "@ts-awesome/simple-query";

function standardResolverFor<T extends ValidQueryModelSignature<T>>(obj: T): ReferenceResolver<T> {
  return (ref, value?: ISimpleQuery) => {
    // operators must start with $, then second argument `value` is passed
    if (ref.startsWith('$')) {
      // here you can support custom operators
      throw new Error(`Unknown operator ${ref}`);
    }
    
    // here is simplest name resolution, but you can do all kinds of magic here 
    return obj[ref];
  }
}

Providing own ReferenceResolverFactory opens lots of new possibilities

License

May be freely distributed under the MIT license.

Copyright (c) 2022 Volodymyr Iatsyshyn and other contributors