The utils module

This module provides simple helper functions, some of them being very specific to CasperJS though.

Functions reference

Usage is pretty much straightforward:

var utils = require('utils');

utils.dump({plop: 42});

betterTypeOf()

Signature: betterTypeOf(input)

Provides a better typeof operator equivalent, eg. able to retrieve the Array type.

betterInstanceOf()

New in version 1.1.

Signature: betterInstanceOf(input, constructor)

Provides a better instanceof operator equivalent, is able to retrieve the Array instance or to deal with inheritance.

dump()

Signature: dump(value)

Dumps a JSON representation of passed argument to the standard output. Useful for debugging your scripts.

fileExt()

Signature: fileExt(file)

Retrieves the extension of passed filename.

fillBlanks()

Signature: fillBlanks(text, pad)

Fills a string with trailing spaces to match pad length.

format()

Signature: format(f)

Formats a string against passed args. sprintf equivalent.

Note

This is a port of nodejs util.format().

getPropertyPath()

Signature: getPropertyPath(Object obj, String path)

New in version 1.0.

Retrieves the value of an Object foreign property using a dot-separated path string:

var account = {
    username: 'chuck',
    skills: {
        kick: {
            roundhouse: true
        }
    }
}
utils.getPropertyPath(account, 'skills.kick.roundhouse'); // true

Warning

This function doesn’t handle object key names containing a dot.

inherits()

Signature: inherits(ctor, superCtor)

Makes a constructor inheriting from another. Useful for subclassing and extending.

Note

This is a port of nodejs util.inherits().

isArray()

Signature: isArray(value)

Checks if passed argument is an instance of Array.

isCasperObject()

Signature: isCasperObject(value)

Checks if passed argument is an instance of Casper.

isClipRect()

Signature: isClipRect(value)

Checks if passed argument is a cliprect object.

isFalsy()

Signature: isFalsy(subject)

New in version 1.0.

Returns subject falsiness.

isFunction()

Signature: isFunction(value)

Checks if passed argument is a function.

isJsFile()

Signature: isJsFile(file)

Checks if passed filename is a Javascript one (by checking if it has a .js or .coffee file extension).

isNull()

Signature: isNull(value)

Checks if passed argument is a null.

isNumber()

Signature: isNumber(value)

Checks if passed argument is an instance of Number.

isObject()

Signature: isObject(value)

Checks if passed argument is an object.

isString()

Signature: isString(value)

Checks if passed argument is an instance of String.

isTruthy()

Signature: isTruthy(subject)

New in version 1.0.

Returns subject truthiness.

isType()

Signature: isType(what, type)

Checks if passed argument has its type matching the type argument.

isUndefined()

Signature: isUndefined(value)

Checks if passed argument is undefined.

isWebPage()

Signature: isWebPage(what)

Checks if passed argument is an instance of native PhantomJS’ WebPage object.

mergeObjects()

Signature: mergeObjects(origin, add[, Object opts])

Merges two objects recursively.

Add opts.keepReferences if cloning of internal objects is not needed.

node()

Signature: node(name, attributes)

Creates an (HT|X)ML element, having optional attributes added.

serialize()

Signature: serialize(value)

Serializes a value using JSON format. Will serialize functions as strings. Useful for debugging and comparing objects.

unique()

Signature: unique(array)

Retrieves unique values from within a given Array.