npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

default-to

v2.0.1

Published

Easily set defaults to an object at multiple depths

Downloads

152

Readme

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.