Code Monkey home page Code Monkey logo

hydrargyrum-defaulter's Introduction

Hydrargyrum-Defaulter

Incredibly fast function defaulter.

defaults(fn, __defaults)

Return a new new function which will call fn, but which will provide a default value/vall to any values/functions passed ordinally into defaulter.

Example - Single Parameter

Wrapper defaulted function will fill in arguments if they are not provided.

function identity(a){ return a }
// use the default value
defaults(identity)() // => undefined
defualts(identity,1)() // => 1
defaults(identity,function(){return 2})() // => 2
// has a real valuue
defaults(identity)(3) // => 3
defaults(identity,1)(3) // => 3
defaults(identity,function(){return 2})(3) // => 3

Example - Deeper Parameters

When making wrapper defaulted function, an arbitrarily number of defaults can be provided.

function sum(a, b){ return a+b }
defaults(sum, 1, 1)() // => 2
defaults(sum, 1, 1)(2) // => 3
defaults(sum, 1, 1)(undefined, 3) // => 4
defaults(sum, function(){ return 2 }, 3)() // => 5
defaults(sum, 1)('ok') // => 'okundefined'
defaults(sum, undefined, 1)(5) // => 6

Wrapping Varidacs

Please note that this wrapper function is not varidac safe. It reflects on the argument length of the function it's wrapping and the number of defaults passed to it to determine how many arguments the wrapper will faithfully handle.

As a workaround, invoke defaults with a __defaults of the desired maximum length.

var __defaults = new Array(65)
__defaults[0] = function sumList(){ var n= arguments[0]; for(var i= 1; i< arguments.length; ++i) n += arguments[i]; return n}
__defaults[2] = 42
defaults.apply(null, __defaults)(-4, undefined, 1, 1, 1, 1) // => 42

By passing a long array of undefineds in, the resultant wrapped function will pass through length-1 arguments (argument[0] is the fn itself).

hydrargyrum-defaulter's People

Contributors

rektide avatar

Watchers

 avatar  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.