Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pemrouz committed May 4, 2015
0 parents commit f169351
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules
coverage
*.log
7 changes: 7 additions & 0 deletions .travis.yml
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "0.12"
- "0.11"
- "iojs"

after_script: NODE_ENV=test istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
12 changes: 12 additions & 0 deletions index.js
@@ -0,0 +1,12 @@
module.exports = function(target, source) {
var i = 1, n = arguments.length, method
while (++i < n) target[method = arguments[i]] = rebind(target, source, source[method])
return target
}

function rebind(target, source, method) {
return function() {
var value = method.apply(source, arguments)
return value === source ? target : value
}
}
18 changes: 18 additions & 0 deletions package.json
@@ -0,0 +1,18 @@
{
"name": "rebind",
"version": "0.0.0",
"main": "index.js",
"scripts": {
"test": "istanbul test ./node_modules/mocha/bin/_mocha --report html -- -R spec",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && rm -rf ./coverage"
},
"author": "Pedram Emrouznejad (https://github.com/pemrouz)",
"license": "pemrouz.mit-license.org",
"devDependencies": {
"chai": "^2.3.0",
"coveralls": "^2.11.2",
"istanbul": "^0.3.13",
"mocha": "^2.2.4",
"mocha-lcov-reporter": "0.0.2"
}
}
44 changes: 44 additions & 0 deletions test.js
@@ -0,0 +1,44 @@
var expect = require('chai').expect
, rebind = require('./')

describe('rebind', function() {

it("source function always has source as context", function() {
var target = {}, source = {method: function() { that = this }}, that
rebind(target, source, "method")
expect((target.method(), that)).to.be.eql(source)
expect((target.method.call({}), that)).to.be.eql(source)
})

it("source function receives target function's arguments", function() {
var target = {}, source = {method: function() { those = Array.prototype.slice.call(arguments) }}, those
rebind(target, source, "method")
expect((target.method(), those)).to.be.eql([])
expect((target.method(1), those)).to.be.eql([1])
expect((target.method(null), those)).to.be.eql([null])
expect((target.method(source, source, 1), those)).to.be.eql([source, source, 1])
})

it("target function returns target if source function returns source", function() {
var target = {}, source = {method: function(value) { return value ? source : 42 }}
rebind(target, source, "method")
expect(target.method(true)).to.be.eql(target)
expect(target.method(false)).to.be.eql(42)
})

it("can bind multiple methods", function() {
var target = {}, source = {
foo: function() { return 1 }
, bar: function() { return 2 }
}
rebind(target, source, "foo", "bar")
expect(target.foo()).to.be.eql(1)
expect(target.bar()).to.be.eql(2)
})

it("returns the target object", function() {
var target = {}, source = { foo: String }
expect(rebind(target, source, "foo")).to.be.eql(target)
})

})

0 comments on commit f169351

Please sign in to comment.