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-type

v0.4.11

Published

Library to check variable type and properties in object.

Downloads

29,286

Readme

Check Type

Build Status GitHub version Dependency Status devDependency Status

A type checking framework for Javascript.

Check type lets you check variable types and structures with the format check(x).is("y") and check(x).has("a.b")


Table of Contents


Dependencies

Installation

Node.js

check-type is available through the npm package manager.

NPM version

For Node.js package management, you can install using npm.

npm install check-type

Bower

check-type is available through the bower package manager.

Bower version

For front-end package management, you can install using Bower.

bower install check-type

Component

check-type is available through the component package manager.

For package management, you can install using Component.

component install alistairjcbrown/check-type

Manual

For manual management, you can grab the Javascript file directly or clone the git repository. You will need to grab underscore.js as well.

# Grab the file
wget https://raw.githubusercontent.com/alistairjcbrown/check-type/master/check-type.min.js

# Clone the repo
git clone [email protected]:alistairjcbrown/check-type.git

Use

Node.js

var check = require("check-type").init();

The module can be required using Node.js built in require (See example).

Browser

<script src="/path/to/underscore"></script>
<script src="/path/to/check-type/check-type.min.js"></script>
<script>
    check.init();
</script>

The module can be used in the browser through the <script> tag and will bind to window (See example).

RequireJS

The module supports the AMD format and uses define if available. Therefore it can be used as a RequireJS module (See Browser example, See Node.js example).

define([ "check-type" ], function(check) {
    check.init();
});

Testing

Built in tests and linting using Grunt to call JSHint and Mocha.

Get all of the developer dependecies by running:

npm install           # install dev dependencies

Test in Node.js

grunt lint            # Lint the code with JSHint
grunt test --nodejs   # Run all tests in node.js
grunt test --browser  # Run all tests in phantomjs
grunt test            # Run all tests in both environments
grunt go              # Run everything above

Test in Browser

Open lib/test/check-type.test.html in browser

No Conflict

To prevent conlicts with other libraries using window.check, if check-type binds to window it will also provide a check.noConflict function to restore the variable to its previous value.

check-type will only bind to window in the browser environment when RequireJS is not available.

Init

check-type does not come with type checking functionality. Instead, it simply provides the check interface. Type checking functions should be provided when calling check.init.

check.init can be called without parameters which will use the type checking functions from Underscore.js.

check.init can be called multiple times and will add type checking functions which have not already been defined. To override a previously defined type checking function, pass boolean true as the second parameter.

Simple use

var check = require("check-type").init();

More complex use

var check = require("check-type"),
    custom_functions = {
        "isEmail": function(value) {
            return value.indexOf("@") !== -1
        },
        "isEmpty": function(value) {
            return value === "empty";
        }
    },

// Initialise check with underscore type checking functions
//  and custom checking functions, overriding underscore's isEmpty function
check.init()
     .init(custom_functions, true);

Clear

This clears all of the internal stored type checking functions.

check.clear();

Type checking

Once the check function has been initialised, it can utilise any defined type checking functions using is.

Example: Checking for string using is

var my_string = "hello world";
check.init();

check(my_string).is("string"); // true
check(my_string).is("number"); // false

check(my_string).is("foo");    // throws Error for unsupported type

You can also negate the check with is.not

Example: Checking for string using is.not

var my_string = "hello world";
check.init();

check(my_string).is.not("string"); // false
check(my_string).is.not("number"); // true

check(my_string).is.not("foo");    // throws Error for unsupported type

Object path checking

check-type can check for the presence of values within an object under a particular path.

Example: Checking object path using has

var my_object = {
    hello: {
        world: false
    }
};

check(my_object).has("hello.world"); // true
check(my_object).has("foo.bar");     // false

Object structure checking

check-type can check an object properties for specific types

Example: Checking object properties using matches

var my_object = {
    "customer_number": 123456789,
    "password":        "abc123"
};

check(my_object).matches({
    "customer_number": "number",
    "password":        "string"
});
// true

check(my_object).matches({
    "username": "string",
    "password": "string"
});
// false

Complex example

The functionality of check can be used in combination, for example when validating response data.

See the example below as a jsfiddle.

Set up check-type

var custom_types = {};

custom_types.isUsername = function(value) {
    return /^\w+$/.test(value);
};

custom_types.isAuthObject = function(value) {
    return check(value).matches({
        "username": "username",
        "password": "string"
    });
};

// Initialise with underscorejs functions
check.init();

// Add custom functions too
check.init(custom_types);

Retrieve username and password from authentication request

function handleAuthentication(request) {
    var username, password;

    if (check(request).has("auth") &&
        check(request.auth).is("AuthObject")) {

        username = request.auth.username;
        password = request.auth.password;

        return {
            username: username,
            password: password
        };

    }

    return false;
}

Contact

Twitter @alistairjcbrown

Code signed using keybase as alistairjcbrown. Verify with keybase dir verify