From 2b147fb0b7d3f941534f1dfa0269722038fa10ec Mon Sep 17 00:00:00 2001 From: Matt Zabriskie Date: Mon, 14 Dec 2015 20:06:16 -0700 Subject: [PATCH] Moving bind into it's own file --- lib/helpers/bind.js | 11 +++++++++++ test/specs/helpers/bind.spec.js | 12 ++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 lib/helpers/bind.js create mode 100644 test/specs/helpers/bind.spec.js diff --git a/lib/helpers/bind.js b/lib/helpers/bind.js new file mode 100644 index 0000000..6147c60 --- /dev/null +++ b/lib/helpers/bind.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = function bind(fn, thisArg) { + return function wrap() { + var args = new Array(arguments.length); + for (var i = 0; i < args.length; i++) { + args[i] = arguments[i]; + } + return fn.apply(thisArg, args); + }; +}; diff --git a/test/specs/helpers/bind.spec.js b/test/specs/helpers/bind.spec.js new file mode 100644 index 0000000..a02c462 --- /dev/null +++ b/test/specs/helpers/bind.spec.js @@ -0,0 +1,12 @@ +var bind = require('../../../lib/helpers/bind'); + +describe('bind', function () { + it('should bind an object to a function', function () { + var o = { val: 123 }; + var f = bind(function (num) { + return this.val * num; + }, o); + + expect(f(2)).toEqual(246); + }); +});