Skip to content

Commit

Permalink
util: store chainable behavior in a __methods object on ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Edmands committed Dec 1, 2013
1 parent 76ac685 commit 270f9d9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
18 changes: 8 additions & 10 deletions lib/chai/utils/addChainableMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ var call = Function.prototype.call,
* @api public
*/

module.exports = addChainableMethod = function (ctx, name, method, chainingBehavior) {
if (typeof chainingBehavior !== 'function')
module.exports = function (ctx, name, method, chainingBehavior) {
if (typeof chainingBehavior !== 'function') {
chainingBehavior = function () { };
}

var chainableBehavior = {
ctx: ctx
, method: method
method: method
, chainingBehavior: chainingBehavior
};

// save the methods so we can overwrite them later, if we need to.
if (!addChainableMethod.methods[name])
addChainableMethod.methods[name] = [];

addChainableMethod.methods[name].push(chainableBehavior);
if (!ctx.__methods) {
ctx.__methods = {};
}
ctx.__methods[name] = chainableBehavior;

Object.defineProperty(ctx, name,
{ get: function () {
Expand Down Expand Up @@ -104,5 +104,3 @@ module.exports = addChainableMethod = function (ctx, name, method, chainingBehav
, configurable: true
});
};

addChainableMethod.methods = {};
22 changes: 5 additions & 17 deletions lib/chai/utils/overwriteChainableMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* MIT Licensed
*/

var addChainableMethod = require('./addChainableMethod');

/**
* ### overwriteChainableMethod (ctx, name, fn)
*
Expand Down Expand Up @@ -39,26 +37,16 @@ var addChainableMethod = require('./addChainableMethod');
*/

module.exports = function (ctx, name, method, chainingBehavior) {
var index = 0;
var chainableMethods = addChainableMethod.methods[name];

// doing a brute-force sequential search for the reference to the object in
// question, so we can get its original method and chaining behavior. yep.
// there is a danger of this running very slowly (O of n), but it's difficult
// for me to imagine n ever getting longer than, well, 1.
while(index < chainableMethods.length) {
if (chainableMethods[index].ctx === ctx) break;
++index;
}
var chainableBehavior = ctx.__methods[name];

var _chainingBehavior = chainableMethods[index].chainingBehavior;
chainableMethods[index].chainingBehavior = function () {
var _chainingBehavior = chainableBehavior.chainingBehavior;
chainableBehavior.chainingBehavior = function () {
var result = chainingBehavior(_chainingBehavior).call(this);
return result === undefined ? this : result;
};

var _method = chainableMethods[index].method;
chainableMethods[index].method = function () {
var _method = chainableBehavior.method;
chainableBehavior.method = function () {
var result = method(_method).apply(this, arguments);
return result === undefined ? this : result;
};
Expand Down

0 comments on commit 270f9d9

Please sign in to comment.