Comparing version 2.2.0 to 2.3.0
@@ -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() | ||
})() | ||
}) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16109
425
173
0