From f169351634e793de89bec5634311cd6db62f4e66 Mon Sep 17 00:00:00 2001 From: pemrouz Date: Mon, 4 May 2015 16:38:41 +0100 Subject: [PATCH] init commit --- .gitignore | 3 +++ .travis.yml | 7 +++++++ index.js | 12 ++++++++++++ package.json | 18 ++++++++++++++++++ test.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 index.js create mode 100644 package.json create mode 100644 test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..193ad77 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +coverage +*.log \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..469cb82 --- /dev/null +++ b/.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 \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..c6a17a0 --- /dev/null +++ b/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 + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..76b1f29 --- /dev/null +++ b/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" + } +} \ No newline at end of file diff --git a/test.js b/test.js new file mode 100644 index 0000000..87ae885 --- /dev/null +++ b/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) + }) + +}) \ No newline at end of file