Skip to content

Commit 897dd6d

Browse files
author
Eric Wendelin
committed
UMD wrapper
1 parent 80ade07 commit 897dd6d

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

stack-parser.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
1-
// TODO: AMD/CommonJS/etc wrapper
2-
(function stackParser() {
3-
function StackFrame(functionName, args, srcUrl, lineNumber, columnNumber) {
1+
// TODO: split into new module
2+
(function (root, factory) {
3+
if (typeof define === 'function' && define.amd) {
4+
define([], factory);
5+
} else if (typeof exports === 'object') {
6+
module.exports = factory();
7+
} else {
8+
root.StackFrame = factory();
9+
}
10+
}(this, function () {
11+
return function StackFrame(functionName, args, srcUrl, lineNumber, columnNumber) {
412
this.fn = functionName;
513
this.args = args;
614
this.src = srcUrl;
715
this.line = lineNumber;
816
this.column = columnNumber;
917
}
10-
11-
function StackParser() {
18+
}));
19+
20+
// TODO?: Error.prototype.parseError = function parseError(e) {};
21+
(function (root, factory) {
22+
if (typeof define === 'function' && define.amd) {
23+
define([], factory);
24+
} else if (typeof exports === 'object') {
25+
module.exports = factory();
26+
} else {
27+
root.StackParser = factory();
28+
}
29+
}(this, function () {
30+
return function StackParser() {
1231
this.firefoxSafariStackEntryRegExp = /\S+\:\d+/;
1332
this.chromeIEStackEntryRegExp = /\s+at /;
1433

@@ -35,7 +54,7 @@
3554
" at foo (http://path/to/file.js:20:5)\n" +
3655
" at http://path/to/file.js:24:4"*/
3756

38-
return error.stack.split('\n').splice(1).map(function(line) {
57+
return error.stack.split('\n').splice(1).map(function (line) {
3958
var tokens = line.split(/\s+/).splice(2);
4059
var location = tokens.pop().replace(/[\(\)\s]/g, '').split(':');
4160
var functionName = (!tokens[0] || tokens[0] === 'Anonymous') ? '' : tokens[0];
@@ -49,9 +68,9 @@
4968
"foo@http://path/to/file.js:82\n" +
5069
"[native code]" */
5170

52-
return error.stack.split('\n').filter(function(line) {
71+
return error.stack.split('\n').filter(function (line) {
5372
return !!line.match(this.firefoxSafariStackEntryRegExp);
54-
}.bind(this)).map(function(line) {
73+
}.bind(this)).map(function (line) {
5574
var tokens = line.split('@');
5675
var location = tokens.pop().split(':');
5776
var functionName = tokens.shift() || '';
@@ -61,7 +80,7 @@
6180

6281
this.parseOpera = function parseOpera(e) {
6382
if (!e.stacktrace || (e.message.indexOf('\n') > -1
64-
&& e.message.split('\n').length > e.stacktrace.split('\n').length)) {
83+
&& e.message.split('\n').length > e.stacktrace.split('\n').length)) {
6584
return this.parseOpera9(e);
6685
} else if (!e.stack) {
6786
return this.parseOpera10a(e);
@@ -108,9 +127,9 @@
108127

109128
// Opera 10.65+ Error.stack very similar to FF/Safari
110129
this.parseOpera11 = function parseOpera11(error) {
111-
return error.stack.split('\n').filter(function(line) {
130+
return error.stack.split('\n').filter(function (line) {
112131
return !!line.match(this.firefoxSafariStackEntryRegExp);
113-
}.bind(this)).map(function(line) {
132+
}.bind(this)).map(function (line) {
114133
var tokens = line.split('@');
115134
var location = tokens.pop().split(':');
116135
var functionCall = (tokens.shift() || '');
@@ -120,9 +139,5 @@
120139
});
121140
}
122141
}
142+
}));
123143

124-
// Error.prototype.parseError = function parseError(e) {};
125-
126-
window.StackFrame = StackFrame;
127-
window.StackParser = StackParser;
128-
})();

0 commit comments

Comments
 (0)