Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    set-defaultpublic

    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
     

    Keywords

    install

    npm i set-default

    Downloadsweekly downloads

    22

    version

    0.3.0

    license

    MIT

    repository

    githubgithub

    last publish

    collaborators

    • avatar