set-default

A node.js module for defaulting object values

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