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

check-complex-types

v1.15.3

Published

Checks if variable has specific type. It can be used for primitives but its power lies in complex types like interfaces and in optional logic.

Downloads

62

Readme

Check Complex Types "check-complex-types"

Build Status Downloads Downloads NPM version

Checks if variable has specific type. It can be used for primitives but its power lies in complex types like interfaces and in optional logic.

install

npm install check-complex-types

usage


import types from "check-complex-types";

const testedInterface = types.INTERFACE({
    someVariable: types.NUMBER,
    otherInterface: types.INTERFACE({
        innerVariable: types.STRING
    })
});

testedInterface.test({
    wrongProperty: "someValue"
});// equal false

testedInterface.test({
    someVariable: 1234,
    otherInterface: {
        innerVariable: "some string"
    }
});// equal true

API

Types

There are number of types:

ANY

for any type

import types from "check-complex-types";

types.ANY.test("testString");// equal true
types.ANY.test(true);// equal true
types.ANY.test(false);// equal true
types.ANY.test(12345);// equal true

STRING or STRING.args([conditions])

for string primitives and string objects

import types from "check-complex-types";

types.STRING.test("testString");// equal true
types.STRING.test(true);// equal false
string conditions
minLength: positive number

Check if string length is bigger then minLength value

import types from "check-complex-types";

types.STRING.args({ minLength: 3 }).test("testString");// equal true
types.STRING.args({ minLength: 20 }).test("testString");// equal false
maxLength: positive number

Check if string length is shorter then maxLength value

import types from "check-complex-types";

types.STRING.args({ maxLength: 20 }).test("testString");// equal true
types.STRING.args({ maxLength: 3 }).test("testString");// equal false
equal: string

Check if string is equal to defined value

import types from "check-complex-types";

types.STRING.args({ equal: "testString" }).test("testString");// equal true
types.STRING.args({ equal: "otherString" }).test("testString");// equal false
match: string

Check if string match regexp

import types from "check-complex-types";

types.STRING.args({ match: /^[0-9]*$/ }).test("1234556");// equal true
types.STRING.args({ match: /^[0-9]*$/ }).test("abcdef");// equal false

ARRAY([elementType]) or ARRAY([elementType]).args([conditions])

checks if value is array. If elementType is provided it also checks if every array element has specified type.

import types from "check-complex-types";

types.ARRAY.test("testString");// equal false
types.ARRAY.test(["testStringA", "something", true]);// equal true


types.ARRAY(types.STRING).test(["testStringA", "something", true]);// equal false
types.ARRAY(types.STRING).test(["testStringA", "something", "somethingElse"]);// equal true
array conditions
minLength: positive number

Check if array length is bigger then minLength value

import types from "check-complex-types";

types.ARRAY.args({ minLength: 3 }).test(["A", "B", "C", "D"]);// equal true
types.ARRAY.args({ minLength: 20 }).test(["A", "B", "C", "D"]);// equal false
maxLength: positive number

Check if array length is shorter then maxLength value

import types from "check-complex-types";

types.ARRAY.args({ maxLength: 20 }).test(["A", "B", "C", "D"]);// equal true
types.ARRAY.args({ maxLength: 3 }).test(["A", "B", "C", "D"]);// equal false

NUMBER or NUMBER.args([conditions])

for number primitive and number object

import types from "check-complex-types";

types.NUMBER.test(100);// equal true
types.NUMBER.test(true);// equal false
number conditions
equal: string

Check if number is equal to defined value

import types from "check-complex-types";

types.NUMBER.args({ equal: 100 }).test(100);// equal true
types.NUMBER.args({ equal: 200 }).test(100);// equal false
minValue: string

Check if number is greater than defined value

import types from "check-complex-types";

types.NUMBER.args({ minValue: 100 }).test(200);// equal true
types.NUMBER.args({ minValue: 200 }).test(100);// equal false
maxValue: string

Check if number is lower than defined value

import types from "check-complex-types";

types.NUMBER.args({ maxValue: 100 }).test(200);// equal false
types.NUMBER.args({ maxValue: 200 }).test(100);// equal true

OBJECT

for objects with exception for null and primitive wrappers (primitive objects like Number, String, Boolean)

FUNCTION

for functions

BOOLEAN

for boolean primitive and boolean object

SYMBOL

for symbols

UNDEFINED

for undefined

NULL

for null

OPTIONAL(optionalType)

check if value has specific type optionalType or doesn't exist (is undefined)

import types from "check-complex-types";

types.OPTIONAL(types.NUMBER).test(1234);// equal true
types.OPTIONAL(types.NUMBER).test();// equal true

INTERFACE(interface)

for checking if tested object has interface properties with correct types interface is an object with property names and description

example

import types from "check-complex-types";

const someObject = {
    someFunction: () => {},
    someString: "test",
    someValue: 1234,
    someObject: {
        someInnerString: "inner"
    }
};

types.INTERFACE({
    someFunction: types.FUNCTION,
    someString: types.STRING,
    someValue: types.NUMBER,
    someObject: types.INTERFACE({
        someInnerString: types.STRING
    })
}).test(someObject);// equal true

INSTANCE(class)

check if argument instance of class

SOME(arrayOfTypes)

for checking if value has one or more types from array of types arrayOfTypes

import types from "check-complex-types";

types.SOME([types.STRING, types.NUMBER]).test("testString");// equal true
types.SOME([types.STRING, types.NUMBER]).test(true);// equal false

EVERY(arrayOfTypes)

for checking if value match all types in array of types arrayOfTypes

import types from "check-complex-types";

types.EVERY([types.STRING, types.NUMBER]).test("testString");// equal false

types.EVERY([
    types.INTERFACE({ someProp: types.NUMBER }),
    types.INTERFACE({ otherProp: types.STRING })
]).test({ someProp: 123, otherProp: "test" }); // equal true

NOT(type)

Accept different type and return true if provided type is false.

import types from "check-complex-types";

types.NOT(types.STRING).test("testString");// equal false
types.NOT(types.NUMBER).test("testString");// equal true

NONE(arrayOfTypes)

for checking if value not match any types in array of types arrayOfTypes

import types from "check-complex-types";

types.NONE([types.STRING, types.NUMBER]).test("testString");// equal false
types.NONE([types.BOOLEAN, types.NUMBER]).test("testString");// equal true

Types methods

Each type can be used as function to pass additional properties or uses as is. Each type has methods

test(testedValue): boolean

Method is used to check if testedValue match specific type. It will return true if it match or false otherwise.

Example

We would like to check if specific value is a string

import types from "check-complex-types";

const someValue = "test";

types.STRING.test(someValue);// equal true

args(conditions)

Some types have additional conditions like min/max length and it can be set by this method. It returns object with method test.

types.STRING.args(someConditions).test(someValue);// equal true

More complex example

We would like to check if tested object has some specific properties with specific types

import types from "check-complex-types";

const someObject = {
    someFunction: () => {},
    someString: "test",
    someValue: 1234,
    someObject: {
        someInnerString: "inner"
    }
};

types.INTERFACE({
    someFunction: types.FUNCTION,
    someString: types.STRING,
    someValue: types.NUMBER,
    someObject: types.INTERFACE({
        someInnerString: types.STRING
    })
}).test(someObject);// equal true

License

MIT