All files global.js

88.63% Statements 39/44
79.16% Branches 19/24
88.88% Functions 8/9
88.37% Lines 38/43

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 1405x 5x               5x               5x 55x                     14x                           5x 14x 14x     14x 14x     14x 34x   34x   34x               34x 34x 34x         34x 25x       1x 1x         25x 25x     25x     9x       9x       14x               5x                             5x       1x 2x 1x 1x     1x   1x 1x       5x  
const { join } = require('path');
const crypto = require('crypto');
 
/**
 * Set context path
 *
 * @type {string}
 * @private
 */
const contextPath = process.cwd();
 
/**
 * Simple consistent hash from content.
 *
 * @param {Array|object|*} content
 * @returns {string}
 */
const generateHash = content =>
  crypto
    .createHash('sha1')
    .update(JSON.stringify({ value: (typeof content === 'function' && content.toString()) || content }))
    .digest('hex');
 
/**
 * Check if "is a Promise", "Promise like".
 *
 * @param {Promise|*} obj
 * @returns {boolean}
 */
const isPromise = obj => /^\[object (Promise|Async|AsyncFunction)]/.test(Object.prototype.toString.call(obj));
 
/**
 * Simple argument based memoize with adjustable limit, and extendable cache expire.
 * Expiration expands until a pause in use happens. Also allows for promises
 * and promise-like functions. For promises and promise-like functions it's the
 * consumers responsibility to await or process the resolve/reject.
 *
 * @param {Function} func A function or promise/promise-like function to memoize
 * @param {object} options
 * @param {number} options.cacheLimit Number of entries to cache before overwriting previous entries
 * @param {number} options.expire Expandable milliseconds until cache expires. The more you use it the longer it takes to expire.
 * @returns {Function}
 */
const memo = (func, { cacheLimit = 1, expire } = {}) => {
  const isFuncPromise = isPromise(func);
  const updatedExpire = Number.parseInt(expire, 10) || undefined;
 
  // eslint-disable-next-line func-names
  const ized = function () {
    const cache = [];
    let timeout;
 
    return (...args) => {
      const isMemo = cacheLimit > 0 && args.length;
      let key;
      let keyIndex = -1;
 
      Iif (typeof updatedExpire === 'number') {
        clearTimeout(timeout);
 
        timeout = setTimeout(() => {
          cache.length = 0;
        }, updatedExpire);
      }
 
      if (isMemo) {
        key = generateHash(args);
        keyIndex = cache.indexOf(key);
      } else E{
        cache.length = 0;
      }
 
      if (keyIndex < 0) {
        cache.unshift(
          key,
          (isFuncPromise &&
            Promise.all([func.call(null, ...args)]).then(result => {
              cache[cache.indexOf(key) + 1] = result?.[0];
              return result?.[0];
            })) ||
            func.call(null, ...args)
        );
 
        Eif (isMemo) {
          cache.length = cacheLimit * 2;
        }
 
        return cache[1];
      }
 
      Iif (isFuncPromise) {
        return Promise.resolve(cache[keyIndex + 1]);
      }
 
      return cache[keyIndex + 1];
    };
  };
 
  return ized();
};
 
/**
 * Set a base config for apidocs, apply apidoc-mock custom comment parser.
 *
 * @type {{silent: boolean, dryRun: boolean, src: undefined, parsers: {apimock: string}, dest: undefined}}
 */
const apiDocBaseConfig = {
  src: undefined,
  dest: undefined,
  dryRun: process.env.NODE_ENV === 'test',
  silent: process.env.NODE_ENV === 'test',
  parsers: {
    apimock: join(__dirname, './apidocConfig.js')
  }
};
 
/**
 * Global options/settings. One time _set, then freeze.
 *
 * @type {{contextPath: string, _set: *}}
 */
const OPTIONS = {
  contextPath,
  apiDocBaseConfig,
  set _set(obj) {
    Object.entries(obj).forEach(([key, value]) => {
      if (typeof value === 'function') {
        this[key] = value.call(this);
        return;
      }
 
      this[key] = value;
    });
    delete this._set;
    Object.freeze(this);
  }
};
 
module.exports = { contextPath, generateHash, memo, OPTIONS };