@awesomeeng/awesome-utils
Advanced tools
Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "@awesomeeng/awesome-utils", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"author": "the awesome engineering company", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -7,3 +7,3 @@ // (c) 2018, The Awesome Engineering Company, https://awesomeneg.com | ||
const ANONYMOUS_RE = /\(<anonymous>\)/; | ||
const STACK_PARSER = /^\s*(at)\s((.+)\s\(|)(.+)(:(\d+))(:(\d+))\)?$|^\s*(at)\s(.+)\s\((<anonymous>)\)?$/; | ||
@@ -47,2 +47,36 @@ /** | ||
/** | ||
* Removes a module from the require cache, thus making it | ||
* reload again if required. Also, any children that | ||
* were loaded by the given module are also removed. | ||
* | ||
* Returns the total number of modules removed. | ||
* | ||
* You may optional indicate if the unrequire should remove | ||
* dependant children as well. This can have unwanted side-effects | ||
* so use with caution. | ||
* | ||
* @param {module|string} mod | ||
* @param {boolean} [removeChildren=false] removeChildren | ||
* @return {number} | ||
*/ | ||
unrequire(mod,removeChildren=false) { | ||
if (typeof mod!=="string" && mod.id) mod = mod.id; | ||
if (typeof mod!=="string") throw new Error("module argument must be a module or a string."); | ||
let count = 0; | ||
let cached = require.cache[mod]; | ||
if (cached) { | ||
if (removeChildren) { | ||
(cached.children||[]).forEach((childmod)=>{ | ||
count += this.unrequire(childmod,removeChildren); | ||
}); | ||
} | ||
delete require.cache[mod]; | ||
count += 1; | ||
} | ||
return count; | ||
} | ||
/** | ||
* Returns the line number of the code of the line that called the line() function. | ||
@@ -61,15 +95,6 @@ * This works by throwing and catching an exception and then reading the stack | ||
line(depth=0) { | ||
try { | ||
throw new Error("AwesomeUtils.Module.line() call, ignore this error."); | ||
} | ||
catch (ex) { | ||
let stack = ex.stack.split(/\n/g).slice(2); | ||
depth = Math.min(999,Math.max(0,depth)); | ||
depth = Math.min(stack.length-1,Math.max(0,depth)); | ||
while (depth>0) { | ||
depth -= 1; | ||
stack.shift(); | ||
} | ||
return stack[0].replace(/^.*:(\d+):\d+\)$/,"$1"); | ||
} | ||
let stack = this.stack(depth,depth+1); | ||
return stack[0].line; | ||
} | ||
@@ -90,21 +115,7 @@ | ||
*/ | ||
source(depth=0,removeAnonymous=false) { | ||
try { | ||
throw new Error("AwesomeUtils.Module.line() call, ignore this error."); | ||
} | ||
catch (ex) { | ||
let stack = ex.stack.split(/\n/g).slice(2); | ||
if (removeAnonymous) { | ||
stack = stack.filter((line)=>{ | ||
return !line.match(ANONYMOUS_RE); | ||
}); | ||
} | ||
source(depth=0) { | ||
depth = Math.min(999,Math.max(0,depth)); | ||
depth = Math.min(stack.length-1,Math.max(0,depth)); | ||
while (depth>0) { | ||
depth -= 1; | ||
stack.shift(); | ||
} | ||
return stack[0].match(ANONYMOUS_RE) && "anonymous" || stack[0].replace(/^.*\((.*):\d+:.*$/,"$1"); | ||
} | ||
let stack = this.stack(depth,depth+1); | ||
return stack[0].source; | ||
} | ||
@@ -126,18 +137,62 @@ | ||
sourceAndLine(depth=0) { | ||
try { | ||
throw new Error("AwesomeUtils.Module.line() call, ignore this error."); | ||
} | ||
catch (ex) { | ||
let stack = ex.stack.split(/\n/g).slice(2); | ||
depth = Math.min(999,Math.max(0,depth)); | ||
depth = Math.min(stack.length-1,Math.max(0,depth)); | ||
while (depth>0) { | ||
depth -= 1; | ||
stack.shift(); | ||
} | ||
return stack[0].replace(/^.*\((.*):(\d+):.*$/,"$1:$2"); | ||
} | ||
let stack = this.stack(depth,depth+1); | ||
return stack[0].source+":"+stack[0].line; | ||
} | ||
/** | ||
* Returns the currently running stack trace, as an array of objects (see below) | ||
* that describes where in the current execution stack the application | ||
* currently is. | ||
* | ||
* The returned array is comprised of stack entry objects which | ||
* have the following shape: | ||
* | ||
* ``` | ||
* entry = { | ||
* entry: string - the full stack trace entry string | ||
* method: the method name from the stack trace entry | ||
* source: the filename the method is in | ||
* line: the line number the execution is on | ||
* position: the line position the execution is on | ||
* } | ||
* ``` | ||
* | ||
* @param {Number} [start=0] | ||
* @param {Number} [end=10] | ||
* @return {Array<Object>} | ||
*/ | ||
stack(start=0,end=start+10) { | ||
start = Math.min(999,Math.max(0,start)); | ||
end = Math.min(999,Math.max(0,end)); | ||
let saved = Error.stackTraceLimit; | ||
if (start+end>=saved) Error.stackTraceLimit = start+end+1; | ||
let obj = {}; | ||
Error.captureStackTrace(obj); | ||
Error.stackTraceLimit = saved; | ||
let stack = obj.stack.split(/\n/g).slice(1); | ||
stack = stack.map((entry)=>{ | ||
entry = entry.trim(); | ||
let match = entry.match(STACK_PARSER); | ||
return { | ||
entry: entry, | ||
method: match && match[3] || match && match[10] || "", | ||
source: match && match[4] || match && match[11] || "", | ||
line: match && match[6] && parseInt(match[6]) || 0, | ||
position: match && match[8] && parseInt(match[8]) || 0 | ||
}; | ||
}); | ||
if (start>0) stack = stack.slice(start); | ||
if (end>start) stack = stack.slice(0,end-start); | ||
return stack; | ||
} | ||
} | ||
module.exports = new ModuleUtils(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75623
1840