1.3.1 • Published 9 years ago

existential-assign v1.3.1

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

existential-assign

Last version Build Status Dependency status Dev Dependencies Status NPM Status Donate

Check for the existential value of a variable/object. Assign one if the value doesn't exist.

Very useful to setup a object with default values if the user preferences are empty. Works fine with deep objects:

var defaults = {
  timeout: 3000,
  cb: function() {},
  user: {
    name: 'someone'
    url: 'github.com'
  }
}

var objt = {
  user: {
    name: 'Kiko Beats'
    url: 'github.com'
  }
}

existsAssign(defaults, objt)
// {
//   timeout: 3000,
//   cb: function() {},
//   user: {
//     name: 'Kiko Beats'
//     url: 'github.com'
//   }
// }

Notes that is slightly different than Object.assign.

Install

npm install existential-assign

If you want to use in the browser (powered by Browserify):

bower install existential-assign --save

and later link in your HTML:

<script src="bower_components/existential-assign/dist/existential-assign.js"></script>

Usage

First load the library:

var existsAssign = require('existential-assign');

Working with something that is not a Object:

existsAssign('hello world', null); // => 'hello world'
existsAssign('world', 'hello'); // => 'hello'

Working with Object:

existsAssign({hello: 'world'}, null) // => {hello: 'world'}
existsAssign({hello: 'Aloha'}, {hello: 'world'}) // => {hello: 'world'}

You can provide more than one source:

existsAssign({hello: 'world'}, null, undefined) // => {hello: 'world'}

It's equivalent to:

existsAssign(existsAssign({hello: 'world'}, null), undefined) // => {hello: 'world'}

License

MIT © Kiko Beats

1.3.1

9 years ago

1.3.0

9 years ago

1.2.5

9 years ago

1.2.4

10 years ago

1.2.3

10 years ago

1.2.2

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.0

10 years ago