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

@pennions/joq

v1.4.0

Published

This is a library designed to make querying JSON objects a breeze.

Downloads

7

Readme

Welcome to Jelmers Object Query

This is a library designed to make querying JSON objects a breeze.

Installation

Simply add it to your project using npm or yarn:

npm

npm install @pennions/joq

yarn

yarn add @pennions/joq

Then import it in your code as follows:

import JOQ from "@pennions/joq"

CDN

Just add

<script src="https://cdn.jsdelivr.net/npm/@pennions/joq"></script>

and use it like

const queryable = new joq(yourJsonArray);

Getting started

The library exposed a class, which takes an object array as single argument:

const queryableObject = new JOQ(myObjectArray);

It makes a hard copy of the object. So in this case myObjectArray will not be mutated itself.

Functions

Listed below are all the functions you can call on the queryableObject which is mentioned in the Getting started section.

example:

queryableObject.select("name");
queryableObject.where("name", "like", "john");

const result = queryableObject.execute();

Sort / Order

propertyName

The case-sensitive name of the property you want to sort on.

direction (SortDirection) Can be one of the following:

"asc" for ascending order

"desc" for descending order

SortDetail

{
    propertyName: string;
    direction: SortDirection;
}

Functions

orderBy(propertyName: string, direction: SortDirection)
thenOrderBy(propertyName: string, direction: SortDirection) 

Example:

queryableObject.orderBy("name", "asc");
queryableObject.thenOrderBy("age", "desc");
const result = queryableObject.execute();

Or if you want to add all the details at once in an array, you can use the sort function.

sort(sortDetails: Array<SortDetail>

Filter / Where

propertyName

The case-sensitive name of the property you want to search on.

operator (FilterOperator) Can be one of the following:

">" for GreaterThan

"<" for LesserThan

">=" for EqualsOrGreater

"<=" for EqualsOrLesser

"is" for "==" for Equals

"!=" for NotEquals

"===" for SuperEquals (includes typecheck)

"!==" for SuperNotEquals (includes typecheck)

"like" for Like (string comparison)

"!like" for NotLike (string comparison)

"contains" for Contains (string comparison)

"!contains for NotContains (string comparison)

FilterType

Can be "and" / "or"

FilterDetail

{
    propertyName: string, 
    value: any, 
    operator: FilterOperator, 
    type?: FilterType, /** optional, defaults to "and" **/
    ignoreCase?: boolean /** optional, defaults to "false" **/
}

N.B. ignoreCase only works with 'equals' operator, 'like' already ignores case.

Functions

where(propertyName: string, operator: FilterOperator, value: any, type?: FilterType, ignoreCase?: boolean) 

Implicitly adds "and" type, but where does this as well. So treat it as syntactic sugar.

andWhere(propertyName: string, operator: FilterOperator, value: any, ignoreCase?: boolean)

Implicitly adds "or" type

orWhere(propertyName: string, operator: FilterOperator, value: any, ignoreCase?: boolean)

Or if you want to add all the details at once in an array, you can use the filter function.

filter(filterDetails: Array<FilterDetail>) 

Group

propertyName

The case-sensitive name of the property you want to group on.

Functions

groupBy(propertyName: string)

This is syntactic sugar, adding an additional where does the same.

thenGroupBy(propertyName: string)

Or supply all the properties at once, order matters.

group(groupByProperties: Array<string>)

Select

If you want to make a subselection, you can provide a string or an array of strings with the case-sensitive property names. It will only return objects with those properties

select(selection: Array<string> | string) 

Distinct

This function takes an array of values which will be treated as unique. Merging the properties from the objects that also have the exact same value of this property. The optional concatenation token will default to ', ' but in some cases you want to use a different token, for example when you are working with csv.

distinct(properties: Array<string> | string, concatenationToken?: string) 

N.B. this will also sum any numeric fields. If you do not want this behaviour, make that field distinct as well.

Execute

This function will start all the operations you added and returns the result.

execute() 

Sum

This is a special function, because it returns a single object with the totals of the provided properties. So you cant use execute with this one.

sum(sumProperties: Array<string>, jsonArray: Array<any>)

Example:

const result = queryableObject.sum("age");

But you can also supply your own json array, for example if you grouped it before, you get an array of arrays. So you can also use sum for this subselection easily without creating a new JOQ instance

Example:

const sum = queryableObject.sum("age", result[0]);

Support us

If you are using this for paid products and services please consider:

  • Becoming a supporter on Patreon.com
  • Doing a one time donation on Ko-Fi.
  • If you want to donate but are not able to do so on these platforms, please contact us at www.pennions.com so we can provide an iDeal link.