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

@pepperi-addons/pepperi-filters

v1.0.41

Published

Useful utilies for working with **Pepperi** `SQL` clause filters & **Pepperi** JSON Filters

Downloads

216

Readme

pepperi-filters

Useful utilies for working with Pepperi SQL clause filters & Pepperi JSON Filters

Installation

Install by running

npm install @pepperi-addons/pepperi-filters

concat

concat multiple filters into one

Usage

import { concat } from '@pepperi-addons/pepperi-filters'

const filter = concat(
    true, 
    {
        FieldType: 'String',
        ApiName: 'TSAString',
        Operation: 'Contains'
        Values: ['hi']
    }, 
    {
        FieldType: 'Double',
        ApiName: 'TSADouble',
        Operation: '='
        Values: ['123.98']
    }
);
console.log(JSON.stringify(filter, null, 4)); 

Output

{
    "Operation": "AND",
    "LeftNode": {
        "FieldType": "String",
        "ApiName": "TSAString",
        "Operation": "Contains",
        "Values": [ "hi" ]
    },
    "RightNode": {
        "FieldType": "Double",
        "ApiName": "TSADouble",
        "Operation": "=",
        "Values": ["123.98"]
    }
}

It also works on sql like filters

import { concat } from '@pepperi-addons/pepperi-filters'

const result = concat(
    true, 
    "TSAString LIKE '%Hello%'", 
    'TSADouble >= 1.2'
);
console.log(result); 

Output

(TSAString LIKE '%Hello%' AND TSADouble >= 1.2)

parse

Convert an SQL clause into a JSON filter.

Usage

import { parse } from '@pepperi-addons/pepperi-filters'

const filter = parse("TSAString LIKE '%Hello%'", new Map([
    ['TSAString', 'String']
]));
console.log(filter); 

Output

{
    "FieldType": "String",
    "ApiName": "TSAString",
    "Operation": "Contains",
    "Values": [
        "Hello"
    ]
}

filter

Filter JSON objects using a JSONFilter

Usage

import { filter } from '@pepperi-addons/pepperi-filters'

const before = [
    {
        TSAString: 'Hi',
        TSADouble: 123.4
    },
    {
        TSAString: 'Bye',
        TSADouble: 53.6
    },
]

const after = filter(before, {
    FieldType: 'String',
    ApiName: 'TSAString',
    Operation: 'StartWith',
    Values: [ 'H' ]
});
console.log(after); 

Output

[{
    "TSAString": "Hi",
    "TSADouble": 123.4
}]

sqlWhereClause

Convert a JSONFilter into a SQL style where clause

Usage

import { sqlWhereClause, concat } from '@pepperi-addons/pepperi-filters'

const where = sqlWhereClause(concat({
    FieldType: 'String',
    ApiName: 'TSAString',
    Operation: 'Contains'
    Values: ['hi']
}, {
    FieldType: 'Double',
    ApiName: 'TSADouble',
    Operation: '='
    Values: ['123.98']
}, true));
console.log(where); 

Output

TSAString LIKE '%hi%' AND TSADouble = 123.0

transform

Travese through a JSON filter tree and transform nodes or emit them entirely. This is useful when we create an API than returns some tranformed model from one or more other APIs.

example

For example we will use the data_views API (https://papi.pepperi.com/v1.0/meta_data/data_views). It supports all the regular API functions (GET, POST, etc.), but saves & gets the data based on the UIControls API (https://papi.pepperi.com/v1.0/UIControls).

So lets say the data_views endpoint gets a filter in a SQL WHERE clause like so:

(Hidden = false) AND (Context.Name = 'OrderMenu') AND (Type = 'Grid' OR CreationDate > '2020-06-01T07:33:33.707Z')

Taking this query apart:

Hidden = false - can be sent to the UIControl endpoint

Context.Name = 'OrderMenu' - translates on the UIControl endpoint to Type LIKE '%OrderMenu%'

Type = 'Grid' - has no translation in the UIControl API

CreationDate > '2020-06-01T07:33:33.707Z' - can also be sent to the UIControl

We want to transform queries that need to be transformed. We want to emlinate queries that aren't supported on the API. We also need to eliminate any queries that are OR'd with that query b/c we need the call to the UIControl API to be >= to the call to the data_views API call. In summary the query we want to send the following filter to the UIControl API

(Hidden = false) AND (Type LIKE '%OrderMenu%')

Usage

import { transform, parse, sqlWhereClause } from '@pepperi-addons/pepperi-filters'

const where = "(Hidden = false) AND (Context.Name = 'OrderMenu') AND (Type = 'Grid' OR CreationDate > '2020-06-01T07:33:33.707Z')";

// first let's turn this into a JSON Filter
const filter = parse(where, new Map([
    ['Hidden', 'Bool'], ['Context.Name', 'String'], ['Type', 'String'], ['CreationDate', 'DateTime']
]));
const transformed = transform(filter, new Map([
    
    // Type doesn't exist on UIControls
    ['Type', (node: JSONBaseFilter): boolean => { return false } ],
    
    // Context.Name maps to part of UIControl.Type
    ['Context.Name', (node: JSONBaseFilter) => { 

        // no matter what the operation - it is alway only contains
        node.Operation = 'Contains'
        node.ApiName = 'Type'

        return true 
    } ]
]);
const where2 = sqlWhereClause(transformed);
console.log(where2);

Output

Hidden = true AND Type LIKE '%OrderMenu%'