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

taser

v1.0.0

Published

A poor man's static typing

Downloads

30

Readme

Taser

Build Status npm version

A poor man's static typing.

Write better code, by being more strict with the types and values you expect in a function. Taser helps you check your input types and values and throws an error when they are not the expected values.

With taser, you can declare what your expectations are for your inputs and taser will throw an error whenever the input data doesn't meet your expectations. For example, if you have a function that multiplies two numbers together, you can declare your expectations with taser and write your function without any type checking.

var multiply = function (a, b) {
  // An error will be thrown if these variables are not numbers
  taser('number', a);
  taser('number', b);
  // Because these are numbers, we can guarantee that this 
  // function works as expected
  return a * b;
};

Examples

Checking for a single type

You can check a value for a specific type.

var sayHello = function (str) {
  taser(['string'], str);
  return 'Hello ' + str + '!';
};
sayHello('Jorge'); // Hello Jorge!
sayHello('Carlos'); // Hello Carlos!
sayHello(57); // Throws an error
sayHello(); // Throws an error

Checking for a multiple types

You can check a value for multiple types, if your function can take more than one type.

var loopThrouhValies = function (arrayOrObject, callback) {
  taser(['array', 'object'], arrayOrObject);
  taser(['function'], callback);
  if (Array.isArray(arrayOrObject)) {
    arrayOrObject.forEach(callback);
  } else {
    for (var key in arrayOrObject) {
      callback(arrayOrObject[key], key, arrayOrObject);
    }
  }
};
sayHello([1, 2, 3], console.log); // [1, 2, 3]
sayHello({ a: 1, b: 2, c: 3 }, console.log); // { a: 1, b: 2, c: 3 }
sayHello(57, console.log); // Throws an error
sayHello("wow", console.log); // Throws an error

Checking for specific values

You can check for types and values, passing an array of valid values.

var logNumberBetweeOneAndFive = function (num) {
  taser({
    type: 'number',
    values: [1, 2, 3, 4, 5]
  }, num);
  console.log('Number:', num);
};
logNumberBetweenOneAndFive(1); // 1
logNumberBetweenOneAndFive(4); // 4
logNumberBetweenOneAndFive(7); // Throws an error
logNumberBetweenOneAndFive('3'); // Throws an error

Interfaces

When declaring your checks in your function, often times you want to declare more complex dependencies. You might want to declare that an object would have certain properties of a certain type. You can do this with interfaces. After declaring your interfaces, you can pass those along to taser.

var taser = require('taser');
var userInterface = new taser.Interface({
  'first_name': taser('string'),
  'last_name':  taser('string'),
  'age':        taser('number'),
  'email':      taser('string')
});

// Will validate and return true
taser(userInterface, {
  first_name: 'jorge',
  last_name: 'silva',
  age: 25,
  email: '[email protected]'
});

// Will throw an error
taser(userInterface, {
  first_name: 'jorge',
  last_name: 'silva',
  age: '25', // This is a String, not a number!
  email: '[email protected]'
});

You can also pass

Types

| name | Examples | Notes | |-------------|--------------------------------------------|--------------------------------------------------------------------------------------------------------------| | 'string' | 'hello', 'goodbye', '1' | | | 'number' | 1, 2, 234.23423, -0.001 | | | 'boolean' | true, false | | | 'array' | [1, 2, 3], [{ hello: 'hello' }, 3, "adsf"] | | | 'object' | { hello: 'hello' }, { name: 'jorge' } | 'object' does not map directly to a JavaScript object. Arrays and null are not considered objects, by taser. | | 'function' | function (a, b) { return a + b } | | | 'null' | null | | | 'undefined' | undefined | |

Currying

If you only pass one argument to the main curry function, taser will return another function that validates a variable using that parameter.

var isString = taser('string');
isString('hello'); // true
isString(1); // Throws Error
var isAOrB = taser({ type: 'string', values: ['a', 'b' ]});
isAOrB('a'); // true
isAOrB('c'); // Throws Error

Legacy / No Currying

In v0.0.1, the value to be checked came first, followed by assertion declaration (string, array, or object). If you wish to use the function in that order, include taser as follows:

var taser = require('taser').noCurrying;
taser('hello', 'string');

This will also disable currying.