Skip to content


Subversion checkout URL

You can clone with
Download ZIP
`extender` is a library that helps in making chainable APIs, by creating a function that accepts different values and returns an object decorated with functions based on the type.
Tree: f194d5165d

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.

build status


extender is a library that helps in making chainable APIs, by creating a function that accepts different values and returns an object decorated with functions based on the type.

Why Is Extender Different?

Extender is different than normal chaining because is does more than return this. It decorates your values in a type safe manner.

For example if you return an array from a string based method then the returned value will be decorated with array methods and not the string methods. This allow you as the developer to focus on your API and not worrying about how to properly build and connect your API.


npm install extender

Or download the source (minified)

Note extender depends on declare.js.


To use with requirejs place the extend source in the root scripts directory

define(["extender"], function(extender){


extender.define(tester, decorations)

To create your own extender call the extender.define function.

This function accepts an optional tester which is used to determine a value should be decorated with the specified decorations

function isString(obj) {
    return !isUndefinedOrNull(obj) && (typeof obj === "string" || obj instanceof String);

var myExtender = extender.define(isString, {
        multiply: function (str, times) {
            var ret = str;
            for (var i = 1; i < times; i++) {
                ret += str;
            return ret;
        toArray: function (str, delim) {
            delim = delim || "";
            return str.split(delim);

myExtender("hello").multiply(2).value(); //hellohello

If you do not specify a tester function and just pass in an object of functions then all values passed in will be decorated with methods.

function isUndefined(obj) {
    var undef;
    return obj === undef;

function isUndefinedOrNull(obj) {
    var undef;
    return obj === undef || obj === null;

function isArray(obj) {
    return === "[object Array]";

function isBoolean(obj) {
    var undef, type = typeof obj;
    return !isUndefinedOrNull(obj) && type === "boolean" || type === "Boolean";

function isString(obj) {
    return !isUndefinedOrNull(obj) && (typeof obj === "string" || obj instanceof String);

var myExtender = extender.define({
    isUndefined : isUndefined,
    isUndefinedOrNull : isUndefinedOrNull,
    isArray : isArray,
    isBoolean : isBoolean,
    isString : isString

To use

var undef;
myExtender("hello").isUndefined().value(); //false
myExtender(undef).isUndefined().value(); //true

You can also chain extenders so that they accept multiple types and decorates accordingly.

    .define(isArray, {
        pluck: function (arr, m) {
            var ret = [];
            for (var i = 0, l = arr.length; i < l; i++) {
            return ret;
    .define(isBoolean, {
        invert: function (val) {
            return !val;

myExtender([{a: "a"},{a: "b"},{a: "c"}]).pluck("a").value(); //["a", "b", "c"]
myExtender("I love javascript!").toArray(/\s+/).pluck("0"); //["I", "l", "j"]

Notice that we reuse the same extender as defined above.

Return Values

When creating an extender if you return a value from one of the decoration functions then that value will also be decorated. If you do not return any values then the extender will be returned.

Default decoration methods

By default every value passed into an extender is decorated with the following methods.

  • value : The value this extender represents.
  • eq(otherValue) : Tests strict equality of the currently represented value to the otherValue
  • neq(oterValue) : Tests strict inequality of the currently represented value.
  • print : logs the current value to the console.

Extender initialization

When creating an extender you can also specify a constructor which will be invoked with the current value.

myExtender.define(isString, {
    constructor : function(val){
        //set our value to the string trimmed
        this._value = val.trimRight().trimLeft();


extender also allows you to specify methods that should not have the value wrapped providing a cleaner exit function other than value().

For example suppose you have an API that allows you to build a validator, rather than forcing the user to invoke the value method you could add a method called validator which makes more syntactic sense.

var myValidator = extender.define({
    //chainable validation methods
    //end chainable validation methods

    noWrap : {
        validator : function(){
            //return your validator

myValidator().isNotNull().isEmailAddress().validator(); //now you dont need to call .value()

Using instanceof

When using extenders you can test if a value is an instanceof of an extender by using the instanceof operator.

var str = myExtender("hello");

str instanceof myExtender; //true


To see more examples click here

Something went wrong with that request. Please try again.