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 2.2.0 to 2.3.0

3

index.js

@@ -15,3 +15,4 @@ "use strict"

before.__beforeFns = [beforeFn]
before.__proto__ = fn
before.prototype = fn.prototype
function before() {

@@ -18,0 +19,0 @@ var self = this

{
"name": "beforefn",
"version": "2.2.0",
"version": "2.3.0",
"main": "index.js",

@@ -5,0 +5,0 @@ "scripts": {

# beforefn
Execute a function before a function.
#### Execute a function before a function.
```js
var before = require('beforefn')
// replace user.save with a function that executes
// customBeheviour() before every .save call
user.save = before(user.save, function() {
customBehaviour()
})
// roughly equivalent to
var oldSave = user.oldSave
user.save = function() {
customBehaviour()
return oldSave.apply(this, arguments)
}
```
## Examples
```js
var before = require('beforefn')

@@ -105,2 +120,37 @@ var user = {

## Performance
As of `beforefn` 2.2.0, `beforefn` performs comparably to a manually replaced function.
```
iterations 700000
...
366ms - manually replaced function
299ms - using before function
...
```
```js
test('manually replaced function', function() {
var user = setup()
var s = user.speak
user.speak = function() {
this.name = this.name.toUpperCase()
return s.apply(this, arguments)
}
user.speak()
})
test('using before function', function() {
var user = setup()
function a() {
this.name = this.name.toUpperCase()
}
user.speak = before(user.speak, a)
user.speak()
})
```
Other runnable benchmarks are located in [bench/index](/bench/index).
## API Facts

@@ -113,2 +163,3 @@

* Original function `this` context is maintained.
* Properties and prototype are inherited though function arity will not be preserved.

@@ -119,2 +170,3 @@ ## See Also

* [timoxley/afterfn](http://github.com/timoxley/afterfn)
* [timoxley/namefn](http://github.com/timoxley/namefn)

@@ -121,0 +173,0 @@ ## License

@@ -175,1 +175,26 @@ "use strict"

})
test('function keys are copied across', function(t) {
function Thing() {}
Thing.copied = true
var anotherThing = {}
Thing.prototype = anotherThing
var NewThing = before(Thing, function() {})
t.ok(NewThing.copied)
t.equal(NewThing.prototype, anotherThing)
t.end()
})
test('function keys are available on fn.fn', function(t) {
function Thing() {}
Thing.copied = true
var anotherThing = {}
Thing.prototype = anotherThing
before(Thing, function fn() {
t.ok(fn.fn.copied)
t.equal(fn.fn.prototype, anotherThing)
t.end()
})()
})
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