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

js-criteria

v2.1.2

Published

lib/index.js

Downloads

35

Readme

js-criteria

npm package Build Status codecov

Motivation

Aims to be a complete solution for query on json and javascript map objects.

Type Docs

Installation

npm install --save js-criteria

Usage

  • import
import {Criteria, Order, Restrictions} from "js-criteria";
  • operate on Criteria
        const result = criteria
            .setLimit(10)
            .setOffset(0)
            .sort("age", OrderType.asc)
            .asc("name")
            .desc("surname")
            .isTrue("enabled")
            .isFalse("deleted")
            .isNull("name")
            .isNotNull("name")
            .eq("name", "sample")
            .neq("name", "sample")
            .gt("age", 18)
            .gte("age", 18)
            .lt("age", 18)
            .lte("age", 18)
            .between("age", 18, 24)
            .like("name", "sample")
            .likeIn("name", ["sample"])
            .in("name", ["sample"])
            .startsWith("name", "sample")
            .endsWith("name", "sample")
            .query("sample")
            .list();
        console.log(result.total);
        console.log(result.data);
  • add restriction

const data = [
    {
        name: "Nami",
        age: 16,
    },
    {
        name: "Monkey D. Luffy",
        age: 16,
    },
    {
        name: "Gol D. Roger",
        age: 32,
    },
    {
        name: "Chopper",
        age: 16,
    }
];

const criteria = new Criteria(data);
criteria.add(Restrictions.eq("name", "Gol D. Roger"));
let result = criteria.list();
console.log(result.total);
console.log(result.data);

criteria.clear();
criteria.add(Restrictions.eq("age", 16));
result = criteria.list();
console.log(result.total);
console.log(result.data);

criteria.clear();
criteria.add(Restrictions.eq("age", 16));
result = criteria.list();
console.log(result.total);
console.log(result.data);
  • add order
const data = [
    {
        name: "Nami",
        age: 16,
    },
    {
        name: "Monkey D. Luffy",
        age: 16,
    },
    {
        name: "Gol D. Roger",
        age: 32,
    },
    {
        name: "Chopper",
        age: 16,
    },
];

const criteria = new Criteria(data);
criteria.addOrder(Order.asc("name"));
let result = criteria.list();
console.log(result.total);
console.log(result.data);
  • add query
const data: any = [
    {
        name: "Nami",
        age: 16,
    },
    {
        name: "Monkey D. Luffy",
        age: 16,
    },
    {
        name: "Gol D. Roger",
        age: 32,
    },
    {
        name: "Chopper",
        age: 16,
    },
];

let criteria = new Criteria(data);
criteria.add(Restrictions.query("o"));
let result = criteria.list();
console.log(result.total);
console.log(result.data);

const data2: any = [
    {id: 1, name: "John", surname: "Doe"},
    {name: "Jane", surname: "Roe", id: 2},
];

criteria2 = new Criteria(data2);
criteria2.addQuery(Restrictions.query("J"));
let result2 = criteria.list();
console.log(result2.total);
console.log(result2.data);
  • reset criteria as initial state.

const criteria = new Criteria(data);

criteria.add(Restrictions.eq("name", "Gol D. Roger"));
criteria.addOrder(Order.asc("name"));

let result = criteria.list();

console.log(result.total);
console.log(result.data);

// reset criteria
criteria.clear();

result = criteria.list();

console.log(result.total);
console.log(result.data);  // show all data.
  • All methods defined on Criteria

    • public get offset()
    • public setOffset(firstResult: number): Criteria<E>
    • public get limit()
    • public setLimit(limit: number): Criteria<E>
    • public sort(name: string, orderType: OrderType): Criteria<E>
    • public asc(name: string): Criteria<E>
    • public desc(name: string): Criteria<E>
    • public isTrue(name: string): Criteria<E>
    • public isFalse(name: string): Criteria<E>
    • public isNull(name: string): Criteria<E>
    • public isNotNull(name: string): Criteria<E>
    • public eq(name: string, value: any, caseSensitive?: boolean): Criteria<E>
    • public neq(name: string, value: any, caseSensitive?: boolean): Criteria<E>
    • public gt(name: string, value: any): Criteria<E>
    • public gte(name: string, value: any): Criteria<E>
    • public lt(name: string, value: any): Criteria<E>
    • public lte(name: string, value: any): Criteria<E>
    • public between(name: string, leftValue: any, rightValue: any): Criteria<E>
    • public like(name: string, value: any, caseSensitive?: boolean): Criteria<E>
    • public likeIn(name: string, values: any[], caseSensitive?: boolean): Criteria<E>
    • public in(name: string, values: any[], caseSensitive?: boolean): Criteria<E>
    • public startsWith(name: string, value: any, caseSensitive?: boolean): Criteria<E>
    • public endsWith(name: string, value: any, caseSensitive?: boolean): Criteria<E>
    • public query(value: any, ignoreList?: string[]): Criteria<E>
    • public addOrder(orderItem: OrderItem<E>): Criteria<E>
    • public removeOrder(key: string): Criteria<E>
    • public clearOrder(): Criteria<E>
    • public get total(): number
    • public add(restriction: RestrictionItem): Criteria<E>
    • public removeRestrictionByKey(key: string): Criteria<E>
    • public removeRestriction(key: string, op: string): Criteria<E>
    • public clearRestriction(): Criteria<E>
    • public list(): CriteriaResult<E>
    • public clear(): Criteria<E>

LICENSE

The MIT License (MIT)

Copyright (c) 2016 Robe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.