Comparing version 0.1.1 to 0.2.0
17
index.js
@@ -18,12 +18,9 @@ 'use strict' | ||
* | ||
* @param {Function|*} fn | ||
* @param {Function|*} func | ||
* Function to be invoked. | ||
* | ||
* @param {Array} [args] | ||
* @param {Array|*} [args] | ||
* Arguments to apply to function. | ||
* | ||
* @param {*} def | ||
* Default value to return if `fn` is not a function. | ||
* | ||
* @param {*} self | ||
* @param {*} [self] | ||
* `this` value. | ||
@@ -35,6 +32,6 @@ * | ||
function apply (fn, args, def, self) { | ||
return (typeof fn === 'function') | ||
? fn.apply(self, array(args)) | ||
: def | ||
function apply (func, args, self) { | ||
return (typeof func === 'function') | ||
? func.apply(self, array(args)) | ||
: func | ||
} |
{ | ||
"name": "apply-or", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Invoke .apply if value is a function, otherwise, return default value.", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "nodemon -V -x 'npm run test' -e 'js json'", | ||
"dev": "nodemon -x 'npm run test' -e 'js json'", | ||
"standard": "standard", | ||
@@ -9,0 +9,0 @@ "test": "node test.js test/**/*.js | tap-spec", |
@@ -9,2 +9,6 @@ # apply-or | ||
## Why? | ||
`Function.prototype.bind` 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. | ||
## Example | ||
@@ -14,13 +18,23 @@ | ||
function max () { | ||
return Math.max.apply(null, this.val) | ||
function divmax (divisor) { | ||
return Math.max.apply(null, this.val) / divisor | ||
} | ||
var data = { | ||
val: [3, 2, 1] | ||
val: [9, 7, 15, 12] | ||
} | ||
apply(max, null, null, data) | ||
apply(divmax, 5, data) | ||
//=> divmax.apply({ val: [9, 7, 15, 12] }, [5]) | ||
//=> 15 / 5 | ||
//=> 3 | ||
## API | ||
###### `apply(func, args, self)` | ||
* `func` Function to be invoked. | ||
* `args` Arguments to apply to function. | ||
* `self` `this` value. | ||
## License | ||
@@ -27,0 +41,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4324
5
42
29