All files monkey-patch.js

50.7% Statements 36/71
24.07% Branches 13/54
35.29% Functions 6/17
51.42% Lines 36/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206        31x                             31x               31x                           31x                     31x               31x                   31x                               31x                               31x               31x               31x 1x   1x   1x 1x                     31x 1x 1x                     31x 1x 1x                                 31x 18x 18x                     31x 4x 3x 1x   1x   1x 1x           31x 248x     248x     248x 24x      
/**
 * Operators object that maps operator names to their corresponding symbols.
 * @type {Object}
 */
let Operators = {
  add: '+',
  mul: '*',
  div: '/',
  equals: '==',
  pow: '**',
  neg: '-',
  lessThan: '<',
  greaterThan: '>'
};
 
/**
 * Object that handles boolean operations.
 * @type {Object}
 */
let booleanHandler = Object.create(null);
 
/**
 * Handles boolean operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @returns {*} - The result of the operation.
 */
booleanHandler.boolean = function (op, other) {
  if (op === 'equals') {
    return this == other;
  }
  let className = other?.constructor?.name || typeof other;
  throw new Error(`Boolean "${this}" does not support "${Operators[op] || op}" for "${other}"`);
};
 
/**
 * Handles object operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @returns {*} - The result of the operation.
 */
booleanHandler.object = function (op, other) {
  if (op === 'add') {
    return other.add(Number(this));
  }
  throw new Error(`Boolean "${this}" does not support "${Operators[op] || op}" for "${other}"`);
};
 
/**
 * Object that handles function operations.
 * @type {Object}
 */
let functionHandler = Object.create(null);
 
/**
 * Handles boolean operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @returns {Function} - A function that performs the operation.
 */
functionHandler.boolean = function (op, other) {
  return (...x) => this(...x)[op](Number(other));
};
 
/**
 * Handles function operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @returns {Function} - A function that performs the operation.
 */
functionHandler.function = function (op, other) {
  return (...x) => {
    try {
      return this(...x)[op](other(...x));
    } catch (e) {
      throw new Error(`Unsupported ${op} for ${other}`);
    }
  };
};
 
/**
 * Handles object operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @returns {Function} - A function that performs the operation.
 */
functionHandler.object = function (op, other) {
  return (...x) => {
    try {
      return this(...x)[op](other);
    } catch (e) {
      throw new Error(`Unsupported ${op} for ${other}`);
    }
  };
};
 
/**
 * Default handler for boolean and function operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @throws {Error} - Throws an error indicating the operation is not supported.
 */
booleanHandler.default = functionHandler.default = function (op, other) {
  throw new Error(`Boolean "${this}" does not support "${Operators[op] || op}" for "${other}"`);
};
 
/**
 * Object that handles string operations.
 * @type {Object}
 */
let stringHandler = Object.create(null);
 
/**
 * Default handler for string operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @throws {Error} - Throws an error indicating the operation is not supported.
 */
stringHandler.default = function (op, other) {
  Iif (op == 'equals') {
    return this == other;
  } else Iif (op == 'lessThan') {
    return this < other;
  } else Eif (op == 'greaterThan') {
    return this > other;
  }
  throw new Error(`String "${this}" does not support "${Operators[op] || op}" for "${other}"`);
};
 
/**
 * Handles function operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @returns {string} - The result of the operation.
 */
stringHandler.function = function (op, other) {
  Eif (op == 'add') {
    return this + String(other);
  }
  throw new Error(`String "${this}" does not support "${Operators[op] || op}" for "${other}"`);
};
 
/**
 * Handles boolean operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @returns {string} - The result of the operation.
 */
stringHandler.boolean = function (op, other) {
  Eif (op == 'add') {
    return this + String(other);
  } else if (op == 'equals') {
    return this == other;
  } else if (op == 'lessThan') {
    return this < other;
  } else if (op == 'greaterThan') {
    return this > other;
  }
  throw new Error(`String "${this}" does not support "${Operators[op] || op}" for "${other}"`);
};
 
/**
 * Handles object operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @returns {string} - The result of the operation.
 */
stringHandler.object = function (op, other) {
  Eif (op == 'add') {
    return this + String(other);
  }
  throw new Error(`String "${this}" does not support "${Operators[op] || op}" for "${other}"`);
};
 
/**
 * Handles string operations.
 * @param {string} op - The operator.
 * @param {*} other - The other operand.
 * @returns {string} - The result of the operation.
 */
stringHandler.string = function (op, other) {
  if (op == 'add') {
    return this + other;
  } else Iif (op == 'equals') {
    return this == other;
  } else Iif (op == 'lessThan') {
    return this < other;
  } else Eif (op == 'greaterThan') {
    return this > other;
  }
  throw new Error(`String "${this}" does not support "${Operators[op] || op}" for "${other}"`);
};
 
// Monkey patching the prototype objects
for (let op in Operators) {
  Boolean.prototype[op] = function (other) {
    return booleanHandler[typeof other]?.call(this, op, other) || booleanHandler.default.call(this, op, other);
  };
  Function.prototype[op] = function (other) {
    return functionHandler[typeof other]?.call(this, op, other) || functionHandler.default.call(this, op, other);
  };
  String.prototype[op] = function (other) {
    return stringHandler[typeof other]?.call(this, op, other) || stringHandler.default.call(this, op, other);
  };
}