-
Notifications
You must be signed in to change notification settings - Fork 8
/
magic-globals.js
70 lines (61 loc) · 1.93 KB
/
magic-globals.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
65
66
67
68
69
/**
* MAGIC GLOBALS
* https://github.com/gavinengel/magic-globals
*/
/** begin setting magic properties into global (required for other functions) */
Object.defineProperty(global, '__stack', {
get: function(){
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack){ return stack; };
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
}
});
/** returns line number when placing this in your code: __line */
Object.defineProperty(global, '__line', {
get: function(){
return __stack[1].getLineNumber();
}
});
/** return filename (without directory path or file extension) when placing this in your code: __file */
Object.defineProperty(global, '__file', {
get: function(){
var file_pieces = __stack[1].getFileName().split(/[\\\/]/).slice(-1)[0].split('.');
return file_pieces.slice(0, file_pieces.length - 1).join('.');
}
});
/** return file extension (without preceding period) when placing this in your code: __ext */
Object.defineProperty(global, '__ext', {
get: function(){
return __stack[1].getFileName().split('.').slice(-1)[0];
}
});
/**
* return current function
* @source https://gist.github.com/lordvlad/ec81834ddff73aaa1ab0
*/
Object.defineProperty(global, '__func', {
get: function(){
return arguments.callee.caller && arguments.callee.caller.name || '(anonymous)';
}
});
/** return base path of project */
Object.defineProperty(global, '__base', {
get: function(){
return process.cwd();
}
});
/** returns filename, a colon, and line number when placing this in your code: __fili */
Object.defineProperty(global, '__fili', {
get: function(){
filid = ':'
if ( typeof GLOBAL.__filid !== 'undefined' && GLOBAL.__filid )
{
filid = GLOBAL.__filid;
}
return __stack[1].getFileName() + filid + __stack[1].getLineNumber();
}
});