Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

beforefn

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

beforefn - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

examples/args.js

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)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc