Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80743 views
1
/***********************************************************************
2
3
A JavaScript tokenizer / parser / beautifier / compressor.
4
https://github.com/mishoo/UglifyJS2
5
6
-------------------------------- (C) ---------------------------------
7
8
Author: Mihai Bazon
9
<[email protected]>
10
http://mihai.bazon.net/blog
11
12
Distributed under the BSD license:
13
14
Copyright 2012 (c) Mihai Bazon <[email protected]>
15
16
Redistribution and use in source and binary forms, with or without
17
modification, are permitted provided that the following conditions
18
are met:
19
20
* Redistributions of source code must retain the above
21
copyright notice, this list of conditions and the following
22
disclaimer.
23
24
* Redistributions in binary form must reproduce the above
25
copyright notice, this list of conditions and the following
26
disclaimer in the documentation and/or other materials
27
provided with the distribution.
28
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
30
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
33
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
34
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
39
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
SUCH DAMAGE.
41
42
***********************************************************************/
43
44
"use strict";
45
46
function array_to_hash(a) {
47
var ret = Object.create(null);
48
for (var i = 0; i < a.length; ++i)
49
ret[a[i]] = true;
50
return ret;
51
};
52
53
function slice(a, start) {
54
return Array.prototype.slice.call(a, start || 0);
55
};
56
57
function characters(str) {
58
return str.split("");
59
};
60
61
function member(name, array) {
62
for (var i = array.length; --i >= 0;)
63
if (array[i] == name)
64
return true;
65
return false;
66
};
67
68
function find_if(func, array) {
69
for (var i = 0, n = array.length; i < n; ++i) {
70
if (func(array[i]))
71
return array[i];
72
}
73
};
74
75
function repeat_string(str, i) {
76
if (i <= 0) return "";
77
if (i == 1) return str;
78
var d = repeat_string(str, i >> 1);
79
d += d;
80
if (i & 1) d += str;
81
return d;
82
};
83
84
function DefaultsError(msg, defs) {
85
Error.call(this, msg);
86
this.msg = msg;
87
this.defs = defs;
88
};
89
DefaultsError.prototype = Object.create(Error.prototype);
90
DefaultsError.prototype.constructor = DefaultsError;
91
92
DefaultsError.croak = function(msg, defs) {
93
throw new DefaultsError(msg, defs);
94
};
95
96
function defaults(args, defs, croak) {
97
if (args === true)
98
args = {};
99
var ret = args || {};
100
if (croak) for (var i in ret) if (ret.hasOwnProperty(i) && !defs.hasOwnProperty(i))
101
DefaultsError.croak("`" + i + "` is not a supported option", defs);
102
for (var i in defs) if (defs.hasOwnProperty(i)) {
103
ret[i] = (args && args.hasOwnProperty(i)) ? args[i] : defs[i];
104
}
105
return ret;
106
};
107
108
function merge(obj, ext) {
109
var count = 0;
110
for (var i in ext) if (ext.hasOwnProperty(i)) {
111
obj[i] = ext[i];
112
count++;
113
}
114
return count;
115
};
116
117
function noop() {};
118
119
var MAP = (function(){
120
function MAP(a, f, backwards) {
121
var ret = [], top = [], i;
122
function doit() {
123
var val = f(a[i], i);
124
var is_last = val instanceof Last;
125
if (is_last) val = val.v;
126
if (val instanceof AtTop) {
127
val = val.v;
128
if (val instanceof Splice) {
129
top.push.apply(top, backwards ? val.v.slice().reverse() : val.v);
130
} else {
131
top.push(val);
132
}
133
}
134
else if (val !== skip) {
135
if (val instanceof Splice) {
136
ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);
137
} else {
138
ret.push(val);
139
}
140
}
141
return is_last;
142
};
143
if (a instanceof Array) {
144
if (backwards) {
145
for (i = a.length; --i >= 0;) if (doit()) break;
146
ret.reverse();
147
top.reverse();
148
} else {
149
for (i = 0; i < a.length; ++i) if (doit()) break;
150
}
151
}
152
else {
153
for (i in a) if (a.hasOwnProperty(i)) if (doit()) break;
154
}
155
return top.concat(ret);
156
};
157
MAP.at_top = function(val) { return new AtTop(val) };
158
MAP.splice = function(val) { return new Splice(val) };
159
MAP.last = function(val) { return new Last(val) };
160
var skip = MAP.skip = {};
161
function AtTop(val) { this.v = val };
162
function Splice(val) { this.v = val };
163
function Last(val) { this.v = val };
164
return MAP;
165
})();
166
167
function push_uniq(array, el) {
168
if (array.indexOf(el) < 0)
169
array.push(el);
170
};
171
172
function string_template(text, props) {
173
return text.replace(/\{(.+?)\}/g, function(str, p){
174
return props[p];
175
});
176
};
177
178
function remove(array, el) {
179
for (var i = array.length; --i >= 0;) {
180
if (array[i] === el) array.splice(i, 1);
181
}
182
};
183
184
function mergeSort(array, cmp) {
185
if (array.length < 2) return array.slice();
186
function merge(a, b) {
187
var r = [], ai = 0, bi = 0, i = 0;
188
while (ai < a.length && bi < b.length) {
189
cmp(a[ai], b[bi]) <= 0
190
? r[i++] = a[ai++]
191
: r[i++] = b[bi++];
192
}
193
if (ai < a.length) r.push.apply(r, a.slice(ai));
194
if (bi < b.length) r.push.apply(r, b.slice(bi));
195
return r;
196
};
197
function _ms(a) {
198
if (a.length <= 1)
199
return a;
200
var m = Math.floor(a.length / 2), left = a.slice(0, m), right = a.slice(m);
201
left = _ms(left);
202
right = _ms(right);
203
return merge(left, right);
204
};
205
return _ms(array);
206
};
207
208
function set_difference(a, b) {
209
return a.filter(function(el){
210
return b.indexOf(el) < 0;
211
});
212
};
213
214
function set_intersection(a, b) {
215
return a.filter(function(el){
216
return b.indexOf(el) >= 0;
217
});
218
};
219
220
// this function is taken from Acorn [1], written by Marijn Haverbeke
221
// [1] https://github.com/marijnh/acorn
222
function makePredicate(words) {
223
if (!(words instanceof Array)) words = words.split(" ");
224
var f = "", cats = [];
225
out: for (var i = 0; i < words.length; ++i) {
226
for (var j = 0; j < cats.length; ++j)
227
if (cats[j][0].length == words[i].length) {
228
cats[j].push(words[i]);
229
continue out;
230
}
231
cats.push([words[i]]);
232
}
233
function compareTo(arr) {
234
if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";";
235
f += "switch(str){";
236
for (var i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":";
237
f += "return true}return false;";
238
}
239
// When there are more than three length categories, an outer
240
// switch first dispatches on the lengths, to save on comparisons.
241
if (cats.length > 3) {
242
cats.sort(function(a, b) {return b.length - a.length;});
243
f += "switch(str.length){";
244
for (var i = 0; i < cats.length; ++i) {
245
var cat = cats[i];
246
f += "case " + cat[0].length + ":";
247
compareTo(cat);
248
}
249
f += "}";
250
// Otherwise, simply generate a flat `switch` statement.
251
} else {
252
compareTo(words);
253
}
254
return new Function("str", f);
255
};
256
257
function all(array, predicate) {
258
for (var i = array.length; --i >= 0;)
259
if (!predicate(array[i]))
260
return false;
261
return true;
262
};
263
264
function Dictionary() {
265
this._values = Object.create(null);
266
this._size = 0;
267
};
268
Dictionary.prototype = {
269
set: function(key, val) {
270
if (!this.has(key)) ++this._size;
271
this._values["$" + key] = val;
272
return this;
273
},
274
add: function(key, val) {
275
if (this.has(key)) {
276
this.get(key).push(val);
277
} else {
278
this.set(key, [ val ]);
279
}
280
return this;
281
},
282
get: function(key) { return this._values["$" + key] },
283
del: function(key) {
284
if (this.has(key)) {
285
--this._size;
286
delete this._values["$" + key];
287
}
288
return this;
289
},
290
has: function(key) { return ("$" + key) in this._values },
291
each: function(f) {
292
for (var i in this._values)
293
f(this._values[i], i.substr(1));
294
},
295
size: function() {
296
return this._size;
297
},
298
map: function(f) {
299
var ret = [];
300
for (var i in this._values)
301
ret.push(f(this._values[i], i.substr(1)));
302
return ret;
303
},
304
toObject: function() { return this._values }
305
};
306
Dictionary.fromObject = function(obj) {
307
var dict = new Dictionary();
308
dict._size = merge(dict._values, obj);
309
return dict;
310
};
311
312