diff --git a/index.js b/index.js index dfaa0fe..0470853 100644 --- a/index.js +++ b/index.js @@ -17,25 +17,27 @@ function Understudy() { this.before = registrar('_before_interceptors'); this._before_interceptors = null; this._after_interceptors = null; + this._interceptor_registrar = registrar; return this; } -function registrar(property) { +function registrar(property, fn) { + fn = fn || 'push'; + return function (action, callback) { - if (typeof action === 'string') { - if (typeof callback === 'function') { - this[property] || (this[property] = {}); - this[property][action] || (this[property][action] = []); - var interceptors = this[property][action]; - interceptors[interceptors.length] = callback; - return this; - } - else { - throw new Error('callback must be a function'); - } + if (typeof action !== 'string') { + throw new Error('event must be a string'); + } else if (typeof callback !== 'function') { + throw new Error('callback must be a function'); } - throw new Error('event must be a string'); - } + + this[property] = this[property] || {}; + var interceptors = this[property][action] + = this[property][action] || []; + + interceptors[fn](callback); + return this; + }; } function perform(action /* , args..., performFn, callback*/) {