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

@sunshineo/jcheck

v1.0.5

Published

A library to check if JSON satisfy condition defined in another JSON

Downloads

12

Readme

jcheck

A library check if a JSON satisfies conditions. The conditions are specified in JSON as well so it can be easily displayed/edited by UI, passed around in http JSON calls, and saved in database or file

Install

npm i @sunshineo/jcheck

Import

NodeJS

const jcheck = require('@sunshineo/jcheck')

TypeScript

import * as jcheck from '@sunshineo/jcheck'

Examples

// No field with name field1
const condition1JSON = {
    field: {
        fieldName: 'field1',
        fieldValue: {
            allowedType: 'undefined',
        }
    }
}
const condition1 = new jcheck.ObjectCondition(condition1JSON)
condition1.check({}) // true
condition1.check({ field1: null }) // false

// Field1 is null
const condition2JSON = {
    field: {
        fieldName: 'field1',
        fieldValue: {
            allowedType: 'null',
        }
    }
}

// Field1 does not exists or is null
const condition3JSON = {
    any: [
        condition1JSON,
        condition2JSON
    ]
}

// Field2 is string 'a'
const condition4JSON = {
    field: {
        fieldName: 'field2',
        fieldValue: {
            allowedType: 'string',
            stringCondition: {
                eq: 'a'
            }
        }
    }
}
// Field2 is string 'a' or 'b'
const condition5JSON = {
    field: {
        fieldName: 'field2',
        fieldValue: {
            allowedType: 'string',
            stringCondition: {
                any: [
                    { eq: 'a' },
                    { eq: 'b' }
                ]
            }
        }
    }
}

// Field3 is number between 0 and 1
const condition6JSON = {
    field: {
        fieldName: 'field3',
        fieldValue: {
            allowedType: 'number',
            numberCondition: {
                all: [
                    { gt: 0 },
                    { lt: 1 }
                ]
            }
        }
    }
}

// Put above together:
// Field1 does not exists or is null. AND
// Field2 is string 'a' or 'b'. AND
// Field3 is number between 0 and 1
const condition7JSON = {
    all: [
        condition3JSON,
        condition5JSON,
        condition6JSON
    ]
}
// See below for more advanced features

References

ObjectCondition

Class constructor input allows one and only one of the following

  • field: FieldCondition
    • Specify the condition on one field in the object. See FieldCondition for details
  • fieldNames: ArrayCondition
    • Specify the condition based on all the field names in the object. For example, have more than 3 fields { fieldNames: { gt: 3 }} . See ArrayCondition
  • fieldValues: ArrayCondition
    • Specify the condition based on all the field values in the object.
  • all?: ObjectCondition[]
    • An array of child ObjectCondition. Only returns true if all of the child conditions returns true.
  • any?: ObjectCondition[]
    • An array of child ObjectCondition. Returns true if any of the child conditions returns true.
  • not?: ObjectCondition
    • A child ObjectCondition. Returns the opposite of the child condition result

FieldCondition

Class constructor input has the following

  • fieldName: string
    • Required. The name of the field

One and only one of

  • fieldValue?: FieldValueCondition
    • The required condition for the value. See FieldValueCondition for details
  • all?: FieldValueCondition[]
    • An array of child FieldValueCondition. Only returns true if all of the child conditions returns true.
  • any?: FieldValueCondition[]
    • An array of child FieldValueCondition. Returns true if any of the child conditions returns true.
  • not?: FieldValueCondition
    • A child FieldValueCondition. Returns the opposite of the child condition result

FieldValueCondition

Class constructor input has the following

  • allowedType: string
    • Required. One of the following string values:
      • "undefined": The field does not exist in the object
      • "null": The field exists in the object and value is null
      • "boolean": The field value is true or false. Can specify additional requirement using booleanCondition below
      • "number": The field is a number. Can specify additional requirement using numberCondition below
      • "string": The field is a string. Can specify additional requirement using stringCondition below
      • "array": The field is a array. Can specify additional requirement using arrayCondition below
      • "object": The field is a object. Can specify additional requirement using objectCondition below
      • "date": The field is a string representing a date time. Can specify additional requirement using dateCondition below

One and only one of the following, matching the allowedType specified above

  • booleanCondition?: BooleanCondition
  • stringCondition?: StringCondition
  • numberCondition?: NumberCondition
  • objectCondition?: ObjectCondition
  • arrayCondition?: ArrayCondition
  • dateCondition?: DateCondition

BooleanCondition

Class constructor input allows one and only one of the following

  • eq?: boolean
    • The value equals specified true or false
  • ne?: boolean
    • The value does not equal specified true or false

NumberCondition

Class constructor input allows one and only one of the following

  • eq?: number
    • The value equals to the specified number
  • ne?: number
    • The value does not equal to the specified number
  • gt?: number
    • The value greater than the specified number
  • gte?: number
    • The value greater than or equal to the specified number
  • lt?: number
    • The value less than the specified number
  • lte?: number
    • The value less than or equal to the specified number
  • all?: NumberCondition[]
    • An array of child NumberCondition. Only returns true if all of the child conditions returns true.
  • any?: NumberCondition[]
    • An array of child NumberCondition. Returns true if any of the child conditions returns true.
  • not?: NumberCondition
    • A child NumberCondition. Returns the opposite of the child condition result

StringCondition

Class constructor input allows one and only one of the following

  • eq?: string
    • The value equals to the specified string
  • ne?: string
    • The value does not equals to the specified string
  • startsWith?: string
    • The value string starts with to the specified string
  • endsWith?: string
    • The value string ends with to the specified string
  • lengthCondition?: NumberCondition
    • The value string length satisfies the specified NumberCondition
  • regex?: RegExp
    • The value string satisfies the specified regular expression
  • all?: StringCondition[]
    • An array of child StringCondition. Only returns true if all of the child conditions returns true.
  • any?: StringCondition[]
    • An array of child StringCondition. Returns true if any of the child conditions returns true.
  • not?: StringCondition
    • A child StringCondition. Returns the opposite of the child condition result

Optional config

  • caseInsensitive: boolean = false
    • Default to false. If set to true, eq, ne, startsWith, endsWith, will test regardless of case

DateCondition

Class constructor input allows one and only one of the following

  • before?: string
    • Requires a string that JavaScript can parse into a JavaScript Date type. For example: "2022-05-16T07:45:07.172Z" or "Mon May 16 2022"
    • If the value is also a string of a Date and before this, returns true
  • after?: string
    • If the value is also a string of a Date and after this, returns true
  • all?: DateCondition[]
    • An array of child DateCondition. Only returns true if all of the child conditions returns true.
  • any?: DateCondition[]
    • An array of child DateCondition. Returns true if any of the child conditions returns true.
  • not?: DateCondition
    • A child DateCondition. Returns the opposite of the child condition result

ArrayCondition

Class constructor input allows one and only one of the following

  • size?: NumberCondition
    • Size of the array satisfies the NumberCondition
  • hasElement?: FieldValueCondition
    • Array has any element that satisfies the FieldValueCondition
  • hasNoElement?: FieldValueCondition
    • Array does not have any element that satisfies the FieldValueCondition
  • filterArray?: FilterArrayCondition
    • Array elements satisfies FilterArrayCondition. See below for details
  • all?: ArrayCondition[]
    • An array of child ArrayCondition. Only returns true if all of the child conditions returns true.
  • any?: ArrayCondition[]
    • An array of child ArrayCondition. Returns true if any of the child conditions returns true.
  • not?: ArrayCondition
    • A child ArrayCondition. Returns the opposite of the child condition result

FilterArrayCondition

Class constructor input requires the following:

  • elementFilter: FieldValueCondition
    • A FieldValueCondition that will check each element with
  • passFilterCount: NumberCondition
    • For elements passed the filter, check the count witht the specified NumberCondition