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

psycho-type

v1.1.0

Published

Simple and lightweight module for dynamic type checking.

Downloads

86

Readme

PsychoType

license Version Size

Simple and lightweight type validation library for node.

const type = require('psycho-type')
const referece = {
  'foo': type.string(),
  'bar': type.number()
}

type.check( reference, {
  'foo': 'string',
  'bar': 3
})
// => true

Installation

This is a node.js module available via npm. Requires node.js 4.9.1 or highter.

$ npm i psycho-type

Usage

This module can be used for runtime type validation for one item or (nested) object.

/* Validating single item */
const valid_type = type.string()

/* You can do it with check function */
type.check( valid_type, 'string' )
//=> true

/* Or only with valid type function */
valid_type( 'string' )
//=> true

Object validation is a little similar to TypeScript Interfaces.

/* Validating objects */
const reference = {          // Reference object, contain types.
  'foo': type.string(),      //
  'bar': type.boolean(),     //
  'fizz': {                  //
    'buzz': type.number()    //
  }
}

const object = {             // Object to validate.
  'foo': 'String',           //
  'bar': true,               //
  'fizz': {                  //
    'buzz': 15               //
  }
}

type.check( reference, object )
//=> true

Types / Methods

Basic Types

Any

Just returns true on everything.

    Param {Any} item
    Return {Boolean}
const any = type.any()

any( * )
//=> true

String

Allow both ' "string" ' and ' new String() '.

    Param {Any} item
    Return {Boolean}
const string = type.string()

string( 's' )
//=> true

string( 3 )
//=> false

string( new String() )
//=> true

Numbers

Any Number

Allow number, bigInt or NaN.

    Param {Any} item
    Return {Boolean}
const anyNumber = type.anyNumber()

anyNumber( 3 )
//=> true

anyNumber( BigInt() )
//=> true

anyNumber( '3' )
//=> false

Number

Allow only regular number.

    Param {Any} item
    Return {Boolean}
const number = type.number()

number( 3 )
//=> true

number( BigInt() )
//=> false

number( '3' )
//=> false

BigInt

Allow only BigInt. Have two aliases:

  • bigInt
  • bigNumber
    Param {Any} item
    Return {Boolean}
const bigint = type.bigInt()

bigint( 3 )
//=> false

bigint( BigInt() )
//=> true

bigint( '3n' )
//=> false

Boolean

Allow only boolean. Have two aliases:

  • bool
  • boolean
    Param {Any} item
    Return {Boolean}
const bool = type.bool()

bool( 3 )
//=> false

bool( true )
//=> true

bool( new Boolean(1) )
//=> true

Function

Allow only functions.

! Any class/constructor will return true !

    Param {Any} item
    Return {Boolean}
const func = type.function()

func( 3 )
//=> false

func( () => {} )
//=> true

func( Boolean ) // Because ' Boolean ' it's the constructor
//=> true

Object

Allow only objects.

! Doesn't allow null or basic data types even if it's created by ' new Object() ' !

    Param {Any} item
    Return {Boolean}
const object = type.object()

object( new Object() )
//=> true

object( new Object(3) )
//=> false

object( {} )
//=> true

Array

Allow array with any value.

    Param {Any} item
    Return {Boolean}
const array = type.array()

array( [] )
//=> true

array( new array( 3, 4 ) )
//=> true

array( {} )
//=> false

Symbol

Allow symbol.

    Param {Any} item
    Return {Boolean}
const symbol = type.symbol()

symbol( [] )
//=> false

symbol( Symbol( '3' ) )
//=> true

symbol( {} )
//=> false

Undefined

Allow only undefined.

    Param {Any} item
    Return {Boolean}
const not_defined = type.undefined()

not_defined( [] )
//=> false

not_defined( null )
//=> false

not_defined( undefined )
//=> true

Null

Allow only null.

    Param {Any} item
    Return {Boolean}
const null_type = type.null()

null_type( [] )
//=> false

null_type( null )
//=> true

null_type( undefined )
//=> false

NaN

Allow only NaN. Have two aliases:

  • nan
  • NaN
    Param {Any} item
    Return {Boolean}
const null_type = type.null()

null_type( [] )
//=> false

null_type( null )
//=> true

null_type( undefined )
//=> false

Complex Types

No Value

Allow ' no value ' types such as undefined, null, NaN.

    Param {Any} item
    Return {Boolean}
const no_value = type.noValue()

no_value( [] )
//=> false

no_value( null )
//=> true

no_value( undefined )
//=> true

Empty

Allow ' empty ' values such as {}, [], '' and 0.

! Doesn't work NOW with values created from constructor ( like ' new String() ' ) !

    Param {Any} item
    Return {Boolean}
const empty = type.empty()

empty( [] )
//=> true

empty( null )
//=> false

empty( new String() )
//=> false

Array Of <types>

Allow array with some types.

    Param {Array} ...types
    Return {function}:
        Param {Array} item
        Return {Boolean}
const array = type.arrayOf(
                type.string(),
                type.boolean()
              )

array([ 'string', true ])
//=> true

array( null )
//=> false

array( new Boolean() )
//=> true

Enum <values>

Allow only some values.

! Doesn't work NOW for values, created by like ' new Number() ' !

    Param {Array} ...values
    Return {function}:
        Param {Any} item
        Return {Boolean}
const enum = type.enum(
               'value',
               3
             )

enum( 'value' )
//=> true

enum( 3 )
//=> true

enum( 4 )
//=> false

Some Of <types>

Allow some types.

    Param {Array} ...types
    Return {function}:
        Param {Any} item
        Return {Boolean}
const some = type.someOf(
               type.string(),
               type.boolean()
             )

some( 'value' )
//=> true

some( 3 )
//=> false

some( false )
//=> true

Not <types>

Inverted ' someOf '. Disallow some types.

    Param {Array} ...types
    Return {function}:
        Param {Any} item
        Return {Boolean}
const not = type.not(
              type.string(),
              type.boolean()
            )

some( 'value' )
//=> false

some( 3 )
//=> true

some( false )
//=> false

'Check' Method

Compare types of input object with reference.

! Method will return false if reference is not valid, without any exeption !

    Param {Object|Function} reference/type
    Return {Boolean}
const reference = {          // Reference object, contain types.
  'foo': type.string(),      //
  'bar': type.boolean(),     //
  'fizz': {                  //
    'buzz': type.number()    //
  }
}

const object = {             // Valid object.
  'foo': 'String',           //
  'bar': true,               //
  'fizz': {                  //
    'buzz': 15               //
  }
}

type.check( reference, object )
//=> true

'Of' Method

Returns type of input even if input created like ' new Object(3) ' and has custom types: 'null', 'NaN'.

    Param {Any} item
    Return {String}
type.of( Number );
//=> 'function'

type.of( null );
//=> 'null'

type.of( NaN );
//=> 'NaN'

type.of( new Object('string') );
//=> 'string'

Contacts

If you have any questions or suggestions please contact me: [email protected]