node package manager



Upcast is a low-level JavaScript type checking and casting library. Upcast simplifies type-checking and converts between types in a more sensible and predictable way than using plain ol' JavaScript.

Current Version: 1.0.4
Automated Build Status: Build Status
Node Support: 0.6, 0.8, 0.10
Browser Support: Android Browser 2.2–4.2, Firefox 3.6, Firefox 4–19, Google Chrome 14–25, Internet Explorer 6–10, Mobile Safari iOS 3–6, Opera 12.10, Safari 5–6

Getting Started

You can use Upcast on the server side with Node.js and npm:

$ npm install upcast

On the client side, you can either install Upcast through Bower/Component:

$ bower install upcast
$ component install rowanmanning/upcast

or by simply including upcast.js in your page:

<script src="path/to/lib/upcast.js"></script>


In Node.js or using Component, you can include Upcast in your script by using require:

var upcast = require('upcast');

Upcast also works with AMD-style module loaders, just specify it as a dependency.

If you're just including with a <script>, upcast is available as a global variable.

Upcast exposes three simple functions:

  • type: get the type of an object
  • is: check whether an object is of a given type
  • to: convert an object to a specific type


Get the type of an object. This accepts a single argument:
val: (mixed) The object to get the type of.

Types in Upcast are different to typeof in what is reported for arrays and null. See the example below:

upcast.type([]); // 'array' 
upcast.type(true); // 'boolean' 
upcast.type(function () {}); // 'function' 
upcast.type(null); // 'null' 
upcast.type(123); // 'number' 
upcast.type({}); // 'object' 
upcast.type('foo'); // 'string' 
upcast.type(undefined); // 'undefined'

Check whether an object is of a given type. This accepts two arguments:
val: (mixed) The object to check the type of.
type: (string) The type to check for. One of array, boolean, function, null, number, object, string or undefined.

This function follows the same rules outlined in upcast.type and allows you to use type aliases.'foo', 'string'); // true, 'string'); // false[], 'array'); // true[], 'object'); // false, 'null'); // true, 'object'); // false

Convert an object to a specific type. This accepts two arguments:
val: (mixed) The object to convert.
type: (string) The type to convert to. One of array, boolean, function, null, number, object, string or undefined.

The way types are converted aims to be sensible and allow easy switching back-and-forth of common types. For example, switching between strings and arrays is quite fluid:'foo', 'array'); // ['f', 'o', 'o']['f', 'o', 'o'], 'string'); // 'foo' 

You can use type aliases with this function. The examples below illustrate the way types are converted.

Converting to an array

Converting to an array from a boolean, function, number or object simply wraps the value in an array:, 'array'); // [123] 

Strings are handled differently, an array is returned with each character in the string as an item:'foo', 'array'); // ['f', 'o', 'o'] 

Null and undefined are converted to an empty array:, 'array'); // [] 

Converting to a boolean

Boolean conversion simply converts to true or false based on whether the value is truthy or not. The only case where this doesn't follow JavaScript's standard behaviour is with empty arrays which are converted to false:[1, 2, 3], 'boolean') // true[], 'boolean') // false 

Converting to a function

When converting to a function, the original value is simply wrapped in a new function. This function returns the original value:'foo', 'function'); // function () { return 'foo'; } 

Converting to null

As expected, converting to null will always return null:'foo', 'null'); // null 

Converting to a number

Converting to a number from a boolean, function, null or object simply calls Number with the original value as an argument, returning the expected value:'true', 'number'); // 1 

Arrays and strings are handled differently, an array is joined to create a string, then evaluated with parseInt; strings are simply evaluated with parseInt:[1, 2, 3], 'number'); // 123'123', 'number'); // 123'foo', 'number'); // 0 

Undefined is converted to 0 rather than NaN:, 'number'); // 0 

Converting to an object

Converting to an object simply calls Object with the value as a first argument. The following are equivalent:'foo', 'object');

Converting to a string

Converting to a string from a boolean, function, number or object simply returns the value added to an empty string, using JavaScript's default type conversion:, 'string'); // 'true', 'string'); // '123' 

Arrays are handled differently, they are joined with an empty string:['f', 'o', 'o'], 'string'); // 'foo' 

Null and undefined are converted to an empty string rather than 'null' and 'undefined':, 'string'); // '' 

Converting to undefined

As expected, converting to undefined will always return undefined:'foo', 'undefined'); // undefined 

Type aliases

The is and to functions allow you to use aliases to certain core types. The following are equivalent:[], 'array');[], 'arr');[], 'a');

The aliases available by default are:

  • array: arr, a
  • boolean: bool, b
  • function: fn, f
  • number: num, n
  • object: obj, o
  • string: str, s


To develop Upcast, you'll need to clone the repo and install dependencies with make deps. If you're on Windows, you'll also need to install Make for Windows.

Once you're set up, you can run the following commands:

$ make deps         # Install dependencies 
$ make lint         # Run JSHint with the correct config 
$ make test         # Run unit tests in Node 
$ make test-server  # Run a server for browser unit testing (visit localhost:3000) 

When no build target is specified, make will run deps lint test. This means you can use the following command for brevity:

$ make

Code with lint errors or no/failing tests will not be accepted, please use the build tools outlined above.


Upcast is licensed under the MIT license.