Git Product home page Git Product logo

wrap-defaults's Introduction

wrap-defaults v0.0.1

Generate a function that checks an object for missing properties and fills it with the given default values. The generated function returns the filled object.

Here are some performance comparisons:

  • +1000x faster than object spreads in Chrome
  • +1000x faster than Object.assign in Chrome
  • +150x faster than for...in loop in Chrome

Check out the jsperf to run the tests yourself.

const wrapDefaults = require('wrap-defaults')

const defaults = wrapDefaults({
  a: 1,
  b: 2,
  c: 3,
  d: 4,
})

defaults({
  a: 2,
  d: undefined,
  e: 3,
}) 
// => {a: 2, b: 2, c: 3, d: 4, e: 3}

The defaults function above is identical to:

var defaults = {}
function(props) {
  if (props.a === undefined) props.a = defaults.a
  if (props.b === undefined) props.b = defaults.b
  if (props.c === undefined) props.c = defaults.c
  if (props.d === undefined) props.d = defaults.d
}

You can achieve huge performance gains when applying default values in that way. You should avoid using wrapDefaults if it's not much of a hassle to do the inlining on your own.

Custom checks

Provide a function as the 2nd argument for custom value checking.

// Use the default value if property equals null or undefined.
let defaults = wrapDefaults({}, (prop) => prop + ' == null')

// Perform different checks based on property key.
defaults = wrapDefaults({}, (prop, key) => {
  // Avoid checking internal properties.
  if (key[0] == '_') return false
  return `typeof ${prop} == 'undefined'`
})

wrap-defaults's People

Contributors

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