Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
/* -*- Mode: js; js-indent-level: 2; -*- */
2
/*
3
* Copyright 2011 Mozilla Foundation and contributors
4
* Licensed under the New BSD license. See LICENSE or:
5
* http://opensource.org/licenses/BSD-3-Clause
6
*/
7
if (typeof define !== 'function') {
8
var define = require('amdefine')(module, require);
9
}
10
define(function (require, exports, module) {
11
12
/**
13
* This is a helper function for getting values from parameter/options
14
* objects.
15
*
16
* @param args The object we are extracting values from
17
* @param name The name of the property we are getting.
18
* @param defaultValue An optional value to return if the property is missing
19
* from the object. If this is not specified and the property is missing, an
20
* error will be thrown.
21
*/
22
function getArg(aArgs, aName, aDefaultValue) {
23
if (aName in aArgs) {
24
return aArgs[aName];
25
} else if (arguments.length === 3) {
26
return aDefaultValue;
27
} else {
28
throw new Error('"' + aName + '" is a required argument.');
29
}
30
}
31
exports.getArg = getArg;
32
33
var urlRegexp = /([\w+\-.]+):\/\/((\w+:\w+)@)?([\w.]+)?(:(\d+))?(\S+)?/;
34
var dataUrlRegexp = /^data:.+\,.+/;
35
36
function urlParse(aUrl) {
37
var match = aUrl.match(urlRegexp);
38
if (!match) {
39
return null;
40
}
41
return {
42
scheme: match[1],
43
auth: match[3],
44
host: match[4],
45
port: match[6],
46
path: match[7]
47
};
48
}
49
exports.urlParse = urlParse;
50
51
function urlGenerate(aParsedUrl) {
52
var url = aParsedUrl.scheme + "://";
53
if (aParsedUrl.auth) {
54
url += aParsedUrl.auth + "@"
55
}
56
if (aParsedUrl.host) {
57
url += aParsedUrl.host;
58
}
59
if (aParsedUrl.port) {
60
url += ":" + aParsedUrl.port
61
}
62
if (aParsedUrl.path) {
63
url += aParsedUrl.path;
64
}
65
return url;
66
}
67
exports.urlGenerate = urlGenerate;
68
69
function join(aRoot, aPath) {
70
var url;
71
72
if (aPath.match(urlRegexp) || aPath.match(dataUrlRegexp)) {
73
return aPath;
74
}
75
76
if (aPath.charAt(0) === '/' && (url = urlParse(aRoot))) {
77
url.path = aPath;
78
return urlGenerate(url);
79
}
80
81
return aRoot.replace(/\/$/, '') + '/' + aPath;
82
}
83
exports.join = join;
84
85
/**
86
* Because behavior goes wacky when you set `__proto__` on objects, we
87
* have to prefix all the strings in our set with an arbitrary character.
88
*
89
* See https://github.com/mozilla/source-map/pull/31 and
90
* https://github.com/mozilla/source-map/issues/30
91
*
92
* @param String aStr
93
*/
94
function toSetString(aStr) {
95
return '$' + aStr;
96
}
97
exports.toSetString = toSetString;
98
99
function fromSetString(aStr) {
100
return aStr.substr(1);
101
}
102
exports.fromSetString = fromSetString;
103
104
function relative(aRoot, aPath) {
105
aRoot = aRoot.replace(/\/$/, '');
106
107
var url = urlParse(aRoot);
108
if (aPath.charAt(0) == "/" && url && url.path == "/") {
109
return aPath.slice(1);
110
}
111
112
return aPath.indexOf(aRoot + '/') === 0
113
? aPath.substr(aRoot.length + 1)
114
: aPath;
115
}
116
exports.relative = relative;
117
118
function strcmp(aStr1, aStr2) {
119
var s1 = aStr1 || "";
120
var s2 = aStr2 || "";
121
return (s1 > s2) - (s1 < s2);
122
}
123
124
/**
125
* Comparator between two mappings where the original positions are compared.
126
*
127
* Optionally pass in `true` as `onlyCompareGenerated` to consider two
128
* mappings with the same original source/line/column, but different generated
129
* line and column the same. Useful when searching for a mapping with a
130
* stubbed out mapping.
131
*/
132
function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) {
133
var cmp;
134
135
cmp = strcmp(mappingA.source, mappingB.source);
136
if (cmp) {
137
return cmp;
138
}
139
140
cmp = mappingA.originalLine - mappingB.originalLine;
141
if (cmp) {
142
return cmp;
143
}
144
145
cmp = mappingA.originalColumn - mappingB.originalColumn;
146
if (cmp || onlyCompareOriginal) {
147
return cmp;
148
}
149
150
cmp = strcmp(mappingA.name, mappingB.name);
151
if (cmp) {
152
return cmp;
153
}
154
155
cmp = mappingA.generatedLine - mappingB.generatedLine;
156
if (cmp) {
157
return cmp;
158
}
159
160
return mappingA.generatedColumn - mappingB.generatedColumn;
161
};
162
exports.compareByOriginalPositions = compareByOriginalPositions;
163
164
/**
165
* Comparator between two mappings where the generated positions are
166
* compared.
167
*
168
* Optionally pass in `true` as `onlyCompareGenerated` to consider two
169
* mappings with the same generated line and column, but different
170
* source/name/original line and column the same. Useful when searching for a
171
* mapping with a stubbed out mapping.
172
*/
173
function compareByGeneratedPositions(mappingA, mappingB, onlyCompareGenerated) {
174
var cmp;
175
176
cmp = mappingA.generatedLine - mappingB.generatedLine;
177
if (cmp) {
178
return cmp;
179
}
180
181
cmp = mappingA.generatedColumn - mappingB.generatedColumn;
182
if (cmp || onlyCompareGenerated) {
183
return cmp;
184
}
185
186
cmp = strcmp(mappingA.source, mappingB.source);
187
if (cmp) {
188
return cmp;
189
}
190
191
cmp = mappingA.originalLine - mappingB.originalLine;
192
if (cmp) {
193
return cmp;
194
}
195
196
cmp = mappingA.originalColumn - mappingB.originalColumn;
197
if (cmp) {
198
return cmp;
199
}
200
201
return strcmp(mappingA.name, mappingB.name);
202
};
203
exports.compareByGeneratedPositions = compareByGeneratedPositions;
204
205
});
206
207