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

argument-spec

v3.1.3

Published

Package to validate function arguments

Downloads

20

Readme

argument-spec

A library to ease argument checking, especially complex arguments like options objects.

Usage

Node, Browserify

At command prompt

npm install argument-spec

In code

argumentSpec = require('argument-spec');

RequireJS

Download argument-spec.js from the most recent release at https://github.com/jrootham/argument-spec/releases and put it in your lib directory.

var argumentSpec = require('argument-spec');

Script tag

<script type="text/javascript"
        src="https://cdn.rawgit.com/jrootham/argument-spec/v3.1.0/argument-spec.js">
</script>

Example of Use

var write = function(file, data, fetch) {
    var fileSpec = {
        name:argumentSpec.every([argumentSpec.length(10), '[a-z]+']) ,
        extension: "jpg|gif"
    };

    var dataSpec = {
        width: argumentSpec.range(20, 500),
        height: argumentSpec.range(20, 500),
        buffer: argumentSpec.instance(Buffer)
    };

    var errorArray = argumentSpec.validate('file', fileSpec, file);
    errorArray = errorArray.concat(argumentSpec.validate('data', dataSpec, data));
    errorArray = errorArray.concat(argumentSpec.validate('fetch', function(){}, fetch));

    if (errorArray.length > 0) {
        throw new Error(errorArray.join('\n'));
    }
}

Specification meanings

|Specification|Valid argument| -----------|---------- |undefined|anything| |''|string| |'regex'|string argument matching regex| |0 (or any number)|number| |true (or false)|boolean| |[]|any Array| |[spec]|Array all of whose elements match spec| |[spec1, spec2, ...]|Exact match for the array argument| |function(a1,a2, ...)|Argument is function with matching number of arguments| |{}|Any Object| |{key1:spec, key2:spec2,...}| Object containing key1, key2,... where each property matches the corresponding spec. Keys are included in the name part of any error message.| |argumentSpec.Base->{validate:function, spec:{key1:spec, key2:spec2,...}}| A function that validates an argument using a spec object (see below).|

Array and object specs nest.

Validation Functions

Functions and related specifications are defined as properties of objects created by the function argumentSpec.Base.

Function| definition ------------|--- some([spec1, spec2,...])| some spec is true for argument every([spec1, spec2,...])| every spec is true for argument (useful for composing specs) range(low, high)| numeric argument in low..high range (inclusive) integer()| integer (fractional part is 0) length(maxLength) | argument.length <= maxLength (argument.length must exist) instance(object)|instanceof object exact(object)|exact match for object properties, useful for options without defaults optional(object)|permit missing properties in the argument, useful for options with defaults isFalse()|argument is exactly false You can write your own validation functions. Here is an example:

/*
 *      Validation function that tests if the argument is an instance of another object
 */

var instance = function (thing) {
    var instance = new Base();

    instance.validate = function(name, argument) {
        if (! (argument instanceof thing)) {
            return [name + " is not an instance"]
        }

        return [];
    }

    return instance;
}