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 | 5x 5x 5x 5x 102x 20x 5x 20x 20x 20x 20x 20x 20x 93x 93x 12x 12x 2x 93x 12x 12x 10x 10x 10x 81x 81x 55x 21x 11x 11x 2x 11x 21x 34x 34x 10x 28x 10x 10x 55x 55x 81x 81x 81x 14x 5x 14x 14x 67x 36x 67x 20x 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 cache limit, and extendable cache expire. * * - `Zero-arg caching`: Zero-argument calls are memoized. To disable caching and perform a manual reset on every call, set cacheLimit <= 0. * - `Expiration`: Expiration expands until a pause in use happens. All results, regardless of type, will be expired. * - `Promises`: Allows for promises and promise-like functions * - `Errors`: It's on the consumer to catch function errors and await or process a Promise resolve/reject/catch. * * @param {Function} func A function or promise/promise-like function to memoize * @param {object} [options] * @param {boolean} [options.cacheErrors=true] - Memoize errors, or don't. * @param {number} [options.cacheLimit=1] - Number of entries to cache before overwriting previous entries * @param {Function} [options.debug=Function.prototype] - Unsure what you cached, just want to test, add a callback that's called * with `{ type: string, value: unknown, cache: Array<unknown> }` * @param {number} [options.expire] Expandable milliseconds until cache expires. The more you use the memoized function, or * promise/promise-like function, the longer it takes to expire. * @returns {Function} */ const memo = (func, { cacheErrors = true, cacheLimit = 1, debug = Function.prototype, expire } = {}) => { const isCacheErrors = Boolean(cacheErrors); const isFuncPromise = isPromise(func); const updatedExpire = Number.parseInt(expire, 10) || undefined; const ized = function () { const cache = []; let timeout; return (...args) => { const isMemo = cacheLimit > 0; if (typeof updatedExpire === 'number') { clearTimeout(timeout); timeout = setTimeout(() => { cache.length = 0; }, updatedExpire); } // Zero cacheLimit, reset and bypass memoization if (isMemo === false) { cache.length = 0; const bypassValue = func.call(null, ...args); debug({ type: 'memo bypass', value: () => bypassValue, cache: [] }); return bypassValue; } const key = generateHash(args); // Parse, memoize and return the original value if (cache.indexOf(key) < 0) { if (isFuncPromise) { const promiseResolve = Promise .resolve(func.call(null, ...args)) .catch(error => { const promiseKeyIndex = cache.indexOf(key); if (isCacheErrors === false && promiseKeyIndex >= 0) { cache.splice(promiseKeyIndex, 2); } return Promise.reject(error); }); cache.unshift(key, promiseResolve); } else { try { cache.unshift(key, func.call(null, ...args)); } catch (error) { const errorFunc = () => { throw error; }; errorFunc.isError = true; cache.unshift(key, errorFunc); } } // Run after cache update to trim Eif (isMemo) { cache.length = cacheLimit * 2; } } // Return memoized value const updatedKeyIndex = cache.indexOf(key); const cachedValue = cache[updatedKeyIndex + 1]; if (cachedValue?.isError === true) { if (isCacheErrors === false) { cache.splice(updatedKeyIndex, 2); } debug({ type: 'memo error', value: cachedValue, cache: [...cache] }); return cachedValue(); } debug({ type: `memo${(isFuncPromise && ' promise') || ''}`, value: () => cachedValue, cache: [...cache] }); return cachedValue; }; }; 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 }; |