All files / src/server/utils deprecation.notice.js

100% Statements 19/19
100% Branches 8/8
100% Functions 6/6
100% Lines 19/19

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                336x 5x 5x 5x                     24x 70x                         26x 2x     24x 24x   24x 22x     24x 734x   734x 334x   400x     734x       6x  
/**
 * @function wrapper
 * @description wrapper function used by notice to wrap properties with a log
 * @param {Function} func - function to wrap
 * @param {String} message - message to log
 * @return {Function}
 */
function wrapper(func, message) {
  return function () {
    let results = func(...arguments);
    console.log(message);
    return results;
  };
}
 
/**
 * @function isBasePrototype
 * @description Is the provided proto one of the base prototypes
 * @param {Object} proto - prototype to evaluate
 * @return {Boolean}
 */
function isBasePrototype(proto) {
  return [Array.prototype, Object.prototype, Function.prototype].some((val) => {
    return val === proto;
  });
}
 
/**
 * @function notice
 * @description Utility to wrap objects or functions and log when any function
 * associated with this is used. Only wraps properties on the top level.
 * @param {Function|Object} item - Item we intend to wrap with log messages
 * @param {String} message - Message for logging
 * @return {Function|Object} wrapped version of the given object
 */
function notice(item, message) {
  if (typeof item === 'function') {
    return wrapper(item, message);
  }
 
  let proto = Object.getPrototypeOf(item);
  let keys = Object.getOwnPropertyNames(item);
 
  if (proto && !isBasePrototype(proto)) {
    keys = keys.concat(Object.getOwnPropertyNames(proto));
  }
 
  return keys.reduce((all, key) => {
    let property = item[key];
 
    if (typeof property === 'function') {
      all[key] = wrapper(property, message);
    } else {
      all[key] = property;
    }
 
    return all;
  }, Object.create(Object.getPrototypeOf(item)));
}
 
module.exports = notice;