default-to

2.0.1 • Public • Published

defaultTo

Easily set defaults to an object at multiple depths

npm install default-to --save

This function is essentially the opposite of my replaceValues function.

JS Usage

This is how you would use it in a javascript file

Single level

var defaultTo = require('default-to');
 
var example = { one: 1 };
 
var defaultToExample = defaultTo(example, {
    one: 'one',
    two: 'two',
    three: 'three'
});

In this case example would now equal this:

{
    one: 1,
    two: 'two',
    three: 'three'
}

Nested

The function can also assign defaults to nested objects

var defaultTo = require('default-to');
 
var example = {
    one: {
        number: 10,
    }
};
 
var defaultToExample = defaultTo(example, {
    one: {
        number: 1,
        string: 'one'
    },
    two: {
        number: 2,
        string: 'two'
    }
});

In this case defaultToExample would now equal this:

{
    one: {
        number: 10,
        string: 'one'
    },
    two: {
        number: 2,
        string: 'two'
    }
}

Flat

You can also just use it on flat variables

var example = defaultTo(variable, 'default value');

in the above code snippet, if variable is defined, example would equal variable else it will equal 'default value'. It is the equivelant of writing this:

var example = typeof variable !== 'undefined' ? variable : 'default value';

Pug and Jade usage

This npm package also comes with Pug and Jade versions of the function. To use the function in your pug/jade templates add this to the top of your base pug/jade file:

include ../../node_modules/default-to/defaultTo

Make sure the path is correct, it will be different depending on your folder structure. It is a relative path from the file it is being included in. Also, note that there is no file extension in the example.

After including that line, you can use it in the same ways that I have used it above except you need to make sure you nest the js inside a js block. This is especially usefull for mixins

mixin module(spec)
    -
        spec = defaultTo(spec, {
            classes: {
                outer: 'outer',
                inner: 'inner'
            },
        })
    .module(class=spec.classes.outer)
        .module__inner(class=spec.classes.inner)

More functionality

These features are only available in the js version and they assume you are able to use ES6 syntax.

applyDefaults

This is a small variation on the defaultTo function that overides the origional variable without having to reference it first.

import { applyDefaults } from 'default-to';
 
var example = {
    two: {
        a: 'zzz'
    }
};
 
applyDefaults(example, {
    one: 1,
    two : {
        a: 'aaa',
        b: 'bbb'
    },
    three: 3
});
 
//example now equals { one: 1, two: {a:'zzz', b: 'bbb' }, three: 3}

WARNING!!! in order to use this version of defaultTo, example must already be defined as something.

In the standard defaultTo function you can get away with the variable being undefined when parsing it into the function but applyDefaults needs to at least have an empty object to be parsed into the function for it to work.

You can get around this by using it like this inside a function when es6 syntax is available to you.

function funcName(variable = {}){
    applyDefaults(variable, {
        one: 'one',
        two: 'two',
        three: 'three'
    });
}

Breaking changes

v2.0.0: The replaceValues function has been moved from the default-to package into it's own seperate npm package. Also the replaceValues function syntax is more reminiscent of the defaultTo function syntax in how it is used now rather than the applyDefaults syntax.

Developers

When publishing, use the babel-it npm plugin to to convert the js files into es5 syntax for better environment support.

Readme

Keywords

Package Sidebar

Install

npm i default-to

Weekly Downloads

56

Version

2.0.1

License

MIT

Last publish

Collaborators

  • dan503