Comparing version 1.0.0 to 2.0.0
14
index.js
"use strict" | ||
module.exports = beforeQueue | ||
var slice = require('sliced') | ||
@@ -18,4 +19,8 @@ function beforeQueue(fn, beforeFn) { | ||
var args = fns.reduce(function(args, doBefore) { | ||
var newArgs = doBefore.apply(context, args) | ||
if (!doBefore.__args) return args | ||
doBefore.args = slice(args) | ||
doBefore.fn = fn | ||
doBefore.apply(context, doBefore.args) | ||
var newArgs = doBefore.args | ||
delete doBefore.args | ||
delete doBefore.fn | ||
return newArgs | ||
@@ -27,6 +32,1 @@ }, arguments) | ||
} | ||
beforeQueue.args = function beforeQueueArgs(fn, beforeFn) { | ||
beforeFn.__args = true | ||
return beforeQueue(fn, beforeFn) | ||
} |
{ | ||
"name": "beforefn", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"main": "index.js", | ||
@@ -19,2 +19,3 @@ "scripts": { | ||
"dependencies": { | ||
"sliced": "0.0.5" | ||
}, | ||
@@ -28,3 +29,9 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/timoxley/beforefn" | ||
"homepage": "https://github.com/timoxley/beforefn", | ||
"keywords": [ | ||
"aspect", | ||
"aop", | ||
"functional", | ||
"oriented" | ||
] | ||
} |
@@ -28,2 +28,19 @@ # beforefn | ||
``` | ||
### Modify arguments | ||
```js | ||
function add(a, b) { | ||
return a + b | ||
} | ||
var addByTen = before(add, function fn(a,b) { | ||
fn.args = [a * 10, b * 10] | ||
}) | ||
console.log(add(1,2)) // => 3 | ||
console.log(addByTen(1,2)) // => 30 | ||
``` | ||
## API Facts | ||
@@ -37,6 +54,4 @@ | ||
## License | ||
MIT |
@@ -36,2 +36,11 @@ "use strict" | ||
test('before is passed args on fn', function(t) { | ||
t.plan(2) | ||
before(function(a, b, c) { | ||
t.deepEqual([].slice.call(arguments), [1,2,3]) | ||
}, function fn() { | ||
t.deepEqual(fn.args, [1,2,3]) | ||
})(1,2,3) | ||
}) | ||
test('returns correct value', function(t) { | ||
@@ -47,7 +56,7 @@ var result = before(function(a, b) { | ||
test('before.args allows changing args', function(t) { | ||
var result = before.args(function(a, b) { | ||
test('before allows changing args via fn.args', function(t) { | ||
var result = before(function(a, b) { | ||
return a + b | ||
}, function(a, b) { | ||
return [a * 10, b * 10] | ||
}, function fn(a, b) { | ||
fn.args = [a * 10, b * 10] | ||
}) | ||
@@ -54,0 +63,0 @@ t.equal(result(2,3), 50) |
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
5684
8
121
56
1
+ Addedsliced@0.0.5
+ Addedsliced@0.0.5(transitive)