-
Notifications
You must be signed in to change notification settings - Fork 0
/
caller.js
64 lines (55 loc) · 2.14 KB
/
caller.js
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
(function (global, factory) {
if (typeof exports === 'object' && typeof module !== 'undefined')
module.exports = factory();
else if (typeof define === 'function' && define.amd)
define(factory)
else {
global.caller = factory();
}
})(this, function () {
var config = {
// for a Ruby on Rails app where `/assets/app/` directory stands for app-specific javascripts
appSources: ['/assets/(app/|application-)']
};
/**
* Rubyish stack trace output powered by Stacktrace-js
* @requires stacktrace-js/stacktrace
* @param silent [Boolean] if false, print full stack trace, otherwise (default) scope to application code (/assets/app/*)
**/
function log (silent) {
var c = toArray(silent, silent !== false ? 0 : 1);
if (!c.length)
return;
var subject = c[0].split('@'),
string = subject[1] + " `" + subject[0] + "' called:\n" +
c.slice(1).map(function (line) {
return ' from ' + line.split('@')[1] + ' in `' + line.split('@')[0] + "'";
}).join('\n');
console.log(string);
}
/**
* Stacktrace cleaner
* @param silent [Boolean] if false, prints full stack trace, otherwise (default) scopes to application code ( /assets/app/* )
* @param offset [Integer] offset in lines. Set it to 1+ when making caller-decorator functions. Default: 0
**/
function toArray (silent, offset) {
if (typeof window.printStackTrace != 'function')
return [];
if (typeof offset == 'undefined')
offset = 0;
var trace = window.printStackTrace();
if (silent !== false) {
var re = new RegExp("://[^/]+(" + config.appSources.join('|') + ").*\\.js\\b");
trace = trace.filter(function (line) {
return line.match(re);
});
} else
trace = trace.slice(4);
return trace.slice(offset);
}
return {
config: config,
log: log,
toArray: toArray
};
})