ba-hooker.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*! JavaScript Hooker - v0.2.3 - 1/29/2012
  2. * http://github.com/cowboy/javascript-hooker
  3. * Copyright (c) 2012 "Cowboy" Ben Alman; Licensed MIT */
  4. (function(exports) {
  5. // Get an array from an array-like object with slice.call(arrayLikeObject).
  6. var slice = [].slice;
  7. // Get an "[object [[Class]]]" string with toString.call(value).
  8. var toString = {}.toString;
  9. // I can't think of a better way to ensure a value is a specific type other
  10. // than to create instances and use the `instanceof` operator.
  11. function HookerOverride(v) { this.value = v; }
  12. function HookerPreempt(v) { this.value = v; }
  13. function HookerFilter(c, a) { this.context = c; this.args = a; }
  14. // When a pre- or post-hook returns the result of this function, the value
  15. // passed will be used in place of the original function's return value. Any
  16. // post-hook override value will take precedence over a pre-hook override
  17. // value.
  18. exports.override = function(value) {
  19. return new HookerOverride(value);
  20. };
  21. // When a pre-hook returns the result of this function, the value passed will
  22. // be used in place of the original function's return value, and the original
  23. // function will NOT be executed.
  24. exports.preempt = function(value) {
  25. return new HookerPreempt(value);
  26. };
  27. // When a pre-hook returns the result of this function, the context and
  28. // arguments passed will be applied into the original function.
  29. exports.filter = function(context, args) {
  30. return new HookerFilter(context, args);
  31. };
  32. // Execute callback(s) for properties of the specified object.
  33. function forMethods(obj, props, callback) {
  34. var prop;
  35. if (typeof props === "string") {
  36. // A single prop string was passed. Create an array.
  37. props = [props];
  38. } else if (props == null) {
  39. // No props were passed, so iterate over all properties, building an
  40. // array. Unfortunately, Object.keys(obj) doesn't work everywhere yet, so
  41. // this has to be done manually.
  42. props = [];
  43. for (prop in obj) {
  44. if (obj.hasOwnProperty(prop)) {
  45. props.push(prop);
  46. }
  47. }
  48. }
  49. // Execute callback for every method in the props array.
  50. var i = props.length;
  51. while (i--) {
  52. // If the property isn't a function...
  53. if (toString.call(obj[props[i]]) !== "[object Function]" ||
  54. // ...or the callback returns false...
  55. callback(obj, props[i]) === false) {
  56. // ...remove it from the props array to be returned.
  57. props.splice(i, 1);
  58. }
  59. }
  60. // Return an array of method names for which the callback didn't fail.
  61. return props;
  62. }
  63. // Monkey-patch (hook) a method of an object.
  64. exports.hook = function(obj, props, options) {
  65. // If the props argument was omitted, shuffle the arguments.
  66. if (options == null) {
  67. options = props;
  68. props = null;
  69. }
  70. // If just a function is passed instead of an options hash, use that as a
  71. // pre-hook function.
  72. if (typeof options === "function") {
  73. options = {pre: options};
  74. }
  75. // Hook the specified method of the object.
  76. return forMethods(obj, props, function(obj, prop) {
  77. // The original (current) method.
  78. var orig = obj[prop];
  79. // The new hooked function.
  80. function hooked() {
  81. var result, origResult, tmp;
  82. // Get an array of arguments.
  83. var args = slice.call(arguments);
  84. // If passName option is specified, prepend prop to the args array,
  85. // passing it as the first argument to any specified hook functions.
  86. if (options.passName) {
  87. args.unshift(prop);
  88. }
  89. // If a pre-hook function was specified, invoke it in the current
  90. // context with the passed-in arguments, and store its result.
  91. if (options.pre) {
  92. result = options.pre.apply(this, args);
  93. }
  94. if (result instanceof HookerFilter) {
  95. // If the pre-hook returned hooker.filter(context, args), invoke the
  96. // original function with that context and arguments, and store its
  97. // result.
  98. origResult = result = orig.apply(result.context, result.args);
  99. } else if (result instanceof HookerPreempt) {
  100. // If the pre-hook returned hooker.preempt(value) just use the passed
  101. // value and don't execute the original function.
  102. origResult = result = result.value;
  103. } else {
  104. // Invoke the original function in the current context with the
  105. // passed-in arguments, and store its result.
  106. origResult = orig.apply(this, arguments);
  107. // If the pre-hook returned hooker.override(value), use the passed
  108. // value, otherwise use the original function's result.
  109. result = result instanceof HookerOverride ? result.value : origResult;
  110. }
  111. if (options.post) {
  112. // If a post-hook function was specified, invoke it in the current
  113. // context, passing in the result of the original function as the
  114. // first argument, followed by any passed-in arguments.
  115. tmp = options.post.apply(this, [origResult].concat(args));
  116. if (tmp instanceof HookerOverride) {
  117. // If the post-hook returned hooker.override(value), use the passed
  118. // value, otherwise use the previously computed result.
  119. result = tmp.value;
  120. }
  121. }
  122. // Unhook if the "once" option was specified.
  123. if (options.once) {
  124. exports.unhook(obj, prop);
  125. }
  126. // Return the result!
  127. return result;
  128. }
  129. // Re-define the method.
  130. obj[prop] = hooked;
  131. // Fail if the function couldn't be hooked.
  132. if (obj[prop] !== hooked) { return false; }
  133. // Store a reference to the original method as a property on the new one.
  134. obj[prop]._orig = orig;
  135. });
  136. };
  137. // Get a reference to the original method from a hooked function.
  138. exports.orig = function(obj, prop) {
  139. return obj[prop]._orig;
  140. };
  141. // Un-monkey-patch (unhook) a method of an object.
  142. exports.unhook = function(obj, props) {
  143. return forMethods(obj, props, function(obj, prop) {
  144. // Get a reference to the original method, if it exists.
  145. var orig = exports.orig(obj, prop);
  146. // If there's no original method, it can't be unhooked, so fail.
  147. if (!orig) { return false; }
  148. // Unhook the method.
  149. obj[prop] = orig;
  150. });
  151. };
  152. }(typeof exports === "object" && exports || this));