Giter Club home page Giter Club logo

defaultify's Introduction

archived

this is basically Object.assign(options, defaultOptions)

defaultify

utility javascript module to set object literals used as options for functions

Used to setup option objects with default values.

Overwrite default values with user-given values

You have a function taking an object with the possible keys minValue (defaulting to 1) and maxValue (defaulting to 5). The default value are {minValue: 1, maxValue: 5}

// the user overwrites minValue, but uses the default value for maxValue
doStuff({
    minValue: 2
});

// doStuff should use the following object as options
{
    minValue: 2,
    maxValue: 5
}

// doStuff could accomplish this with:
function doStuff(options) {
    var defualtOpts = {
        minValue: 1,
        maxValue: 5
    };
    // opts is an object with the keys info (details about the change) and value (the options to use)
    var opts = defaultify(options, defualtOpts);
    opts = opts.value;
    
    // shorter, raising error if the user passes illigal options
    // var opts = defaultify(options, defualtOpts, true).value;
    
    ...

}

Transform user given values

You have a function taking a command (string) as one of the object values. But you want abbreviations to be allowed

// user uses abbreviation for test
doCommand({command: "t"});

// doCommand can use a transform function to change this type
function doCommand(options) {
    var defualtOpts = {
        command: "run"
    };
    var transform = {
        command: function(value) {
            // value is the value passed in by the user, in this case "t"
            if (value === "r") return "run";
            if (value === "t") return "test";
            return value;
        }
    };
    var opts = defaultify(options, defualtOpts, transform, true).value;
    
    ...
}

Transform functions as validators

Transform functions can also be used to validate user input. In the example above, the transform object can be changed to

var transform = {
    command: function(value) {
        if (value === "r" || value === "run") return "run";
        if (value === "t" || value === "test") return "test";
        throw new Error("Invalid command '" + value + "'");
    }
};

// calling doCommand with an invalid command text raises an error
try {
    doStuff({command: "say hello"});
} catch (e) {
    // e.message === "transform failed: Invalid command 'say hello' for command"
}

defaultify's People

Contributors

pretorh avatar

Stargazers

Kel avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.