0.5.2 • Published 10 years ago

apply-or v0.5.2

Weekly downloads
5
License
MIT
Repository
github
Last release
10 years ago

apply-or

Invoke .apply if value is a function, otherwise, return default value.

Build Status Code Climate js-standard-style

npm install apply-or --save
npm stats

npm NPM downloads Dependency Status

Why?

Function.prototype.apply is normally sufficient; however, there are situations where it is useful to treat a value as a Function (invoke it) only if it is indeed a Function; otherwise, return it as-is. Calling .apply on a value that is not a function would cause an error.

Examples

delay.js
var apply = require('apply-or')

function delay (cb) {
  setTimeout(apply.bind(null, cb))
}

delay('Hello') // does nothing (because argument is not a function)
delay(console.log.bind(console, 'Hello, World')) // prints 'Hello, World' to stdout
divmax.js
var apply = require('apply-or')

function divmax (divisor) {
  return Math.max.apply(null, this.val) / divisor
}

var data = {
  val: [9, 7, 15, 12]
}

apply(divmax, 5, data)
//=> divmax.apply({ val: [9, 7, 15, 12] }, [5])
//=> 15 / 5
//=> 3

API

apply(func, args, self)

arguments
  • func: (Function) Function to be invoked.
  • args: (Array|*) Arguments to apply to function.
  • self: (Object) this value.
returns
  • (*) Result of applying function or default value.

Licenses

GitHub license

0.5.2

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.2

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.5

10 years ago

0.3.4

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago