set-default

A node.js module for defaulting object values

npm install set-default
3 downloads in the last week
12 downloads in the last month

Build Status

set-default

A node.js module for defaulting object values.

Installation

$ npm install set-default

Usage

var setDefault = require('set-default');

function logDetailsOf(person)
{
    setDefault(person).to({
        name: 'John',
        age: 23,
        gender: 'male'
    });

    console.log(person);
};

var person = {name:'Jack'};

logDetailsOf(person); // => {name:'Jack', age:23, gender:'male'}

Setting the default value of an object that can possibly be undefined


function logDetailsOf(person)
{
    // because person can be undefined, capture the result
    // back into the person variable
    person = setDefault(person).to({
        name: 'John',
        age: 23,
    });

    console.log(person);
};

logDetailsOf(); // => {name:'John', age:23}

Setting the default value of an object property rather than the entire object


var person = {name:'John'};

setDefault('age').of(person).to(25)

console.log(person); // => {name:'John', age:25}

Works with simple objects like strings and numbers too


var name = setDefault(name).to('James');

console.log(name); // => James

var age = 10;

age = setDefault(age).to(50);

console.log(age); // => 10
npm loves you