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

simpletype-js

v2.0.6

Published

A quick and easy, multi-purpose type checker for Node.js

Downloads

4

Readme

simpleType Logo

A quick and easy, lightweight, array-safe, multi-purpose type checker for Node.js

Build status AppVeyor tests npm

const st = require('simpletype-js')

function myStrictFunction(name, age, income, pets) {
    //Example usage for functions:
    let tcheck = st.checkSimpleSync("string", "number", ["string", "number"], "array", arguments)
    if (tcheck.correct) {
        //all arguments were of correct type
        
    } else {
        //one or more arguments were not of correct type
        //use tcheck.failed for specific info
    }
}

Installation

Installation is done using the npm install command:

$ npm i simpletype-js

Getting started

With the checkSimple() and checkSimpleSync() method, simpleType takes multiple type string arguments (or Array arguments for multiple acceptable types), then checks an ordered Array/Object of values, returning a tcheck object:

Synchronous

const st = require('simpletype-js')

let tcheck = st.checkSimpleSync("string", "array", ["number", "boolean"], [ "johndoe", [123, 456, 789], 42 ])
if (tcheck.correct) {
    //all values were of correct type

} else {
    //one or more values were of incorrect type

}

Asynchronous

The check() and checkSimple() methods behave exactly the same as the synchronous versions, just as promises:

const st = require('simpletype-js')

st.checkSimple("string", "array", ["number", "boolean"], [ "johndoe", [123, 456, 789], 42 ])
.then((tcheck) => {
    if (tcheck.correct) {
        //all values were of correct type
    } else {
        //one or more values were of incorrect type
    }
})
.catch((err) => {
    //an error occurred
})

How it works

simpleType returns type information using a tcheck object, such as the examples below...

If all values passed to simpleType match the required types, the tcheck object will only have one property, correct:

{ correct: true }

If one or more failed the check, tcheck.correct will be false, and a tcheck.failed array is added to provide specifics on the failed values:

{
  correct: false,
  failed: [ { index: 1, type: 'number', expected: 'string' } ]
}

As seen above, for values passed in ordered-Arrays, an index number is provided for each failed value, as well as the type provided and expected.

For values passed in Objects, the index property provides the key name of the failed value instead:

{
  correct: false,
  failed: [ { index: "username", type: 'number', expected: 'string' } ]
}

Extended Features

Using the check() andcheckSync() methods, simpleType can take an Array or Object of required type strings as the first argument:

let tcheck = st.checkSync( ["string", "boolean"], { username: "johndoe", haspets: true } )
/* { correct: true } */

If you pass both types and values as Objects, the order of values no longer matter! simpleType will check the type of each item by key:

let tcheck = st.checkSync( {haspets: "boolean", username: "string" }, { username: "johndoe", haspets: true } )
/* { correct: true } */

It is important to note that when using the check() andcheckSync() methods, simpleType will only accept exactly 2 arguments...

Extras

Array-safe typeof()

The vanilla JS typeof() function doesn't know the difference between Arrays [] and Objects {} normally, so some basic extra code is required to check for arrays.

Since this function is used often in simpleTypes, it is exported as an extra feature in case it's ever needed:

st.typeof( {foo: "bar", bar: "foo"} ) // "object"
st.typeof( ["foo", "bar"] ) // "array"

Happy typing!