Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 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
this.msg = msg;
86
this.defs = defs;
87
};
88
89
function defaults(args, defs, croak) {
90
if (args === true)
91
args = {};
92
var ret = args || {};
93
if (croak) for (var i in ret) if (ret.hasOwnProperty(i) && !defs.hasOwnProperty(i))
94
throw new DefaultsError("`" + i + "` is not a supported option", defs);
95
for (var i in defs) if (defs.hasOwnProperty(i)) {
96
ret[i] = (args && args.hasOwnProperty(i)) ? args[i] : defs[i];
97
}
98
return ret;
99
};
100
101
function merge(obj, ext) {
102
for (var i in ext) if (ext.hasOwnProperty(i)) {
103
obj[i] = ext[i];
104
}
105
return obj;
106
};
107
108
function noop() {};
109
110
var MAP = (function(){
111
function MAP(a, f, backwards) {
112
var ret = [], top = [], i;
113
function doit() {
114
var val = f(a[i], i);
115
var is_last = val instanceof Last;
116
if (is_last) val = val.v;
117
if (val instanceof AtTop) {
118
val = val.v;
119
if (val instanceof Splice) {
120
top.push.apply(top, backwards ? val.v.slice().reverse() : val.v);
121
} else {
122
top.push(val);
123
}
124
}
125
else if (val !== skip) {
126
if (val instanceof Splice) {
127
ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);
128
} else {
129
ret.push(val);
130
}
131
}
132
return is_last;
133
};
134
if (a instanceof Array) {
135
if (backwards) {
136
for (i = a.length; --i >= 0;) if (doit()) break;
137
ret.reverse();
138
top.reverse();
139
} else {
140
for (i = 0; i < a.length; ++i) if (doit()) break;
141
}
142
}
143
else {
144
for (i in a) if (a.hasOwnProperty(i)) if (doit()) break;
145
}
146
return top.concat(ret);
147
};
148
MAP.at_top = function(val) { return new AtTop(val) };
149
MAP.splice = function(val) { return new Splice(val) };
150
MAP.last = function(val) { return new Last(val) };
151
var skip = MAP.skip = {};
152
function AtTop(val) { this.v = val };
153
function Splice(val) { this.v = val };
154
function Last(val) { this.v = val };
155
return MAP;
156
})();
157
158
function push_uniq(array, el) {
159
if (array.indexOf(el) < 0)
160
array.push(el);
161
};
162
163
function string_template(text, props) {
164
return text.replace(/\{(.+?)\}/g, function(str, p){
165
return props[p];
166
});
167
};
168
169
function remove(array, el) {
170
for (var i = array.length; --i >= 0;) {
171
if (array[i] === el) array.splice(i, 1);
172
}
173
};
174
175
function mergeSort(array, cmp) {
176
if (array.length < 2) return array.slice();
177
function merge(a, b) {
178
var r = [], ai = 0, bi = 0, i = 0;
179
while (ai < a.length && bi < b.length) {
180
cmp(a[ai], b[bi]) <= 0
181
? r[i++] = a[ai++]
182
: r[i++] = b[bi++];
183
}
184
if (ai < a.length) r.push.apply(r, a.slice(ai));
185
if (bi < b.length) r.push.apply(r, b.slice(bi));
186
return r;
187
};
188
function _ms(a) {
189
if (a.length <= 1)
190
return a;
191
var m = Math.floor(a.length / 2), left = a.slice(0, m), right = a.slice(m);
192
left = _ms(left);
193
right = _ms(right);
194
return merge(left, right);
195
};
196
return _ms(array);
197
};
198
199
function set_difference(a, b) {
200
return a.filter(function(el){
201
return b.indexOf(el) < 0;
202
});
203
};
204
205
function set_intersection(a, b) {
206
return a.filter(function(el){
207
return b.indexOf(el) >= 0;
208
});
209
};
210
211
// this function is taken from Acorn [1], written by Marijn Haverbeke
212
// [1] https://github.com/marijnh/acorn
213
function makePredicate(words) {
214
if (!(words instanceof Array)) words = words.split(" ");
215
var f = "", cats = [];
216
out: for (var i = 0; i < words.length; ++i) {
217
for (var j = 0; j < cats.length; ++j)
218
if (cats[j][0].length == words[i].length) {
219
cats[j].push(words[i]);
220
continue out;
221
}
222
cats.push([words[i]]);
223
}
224
function compareTo(arr) {
225
if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";";
226
f += "switch(str){";
227
for (var i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":";
228
f += "return true}return false;";
229
}
230
// When there are more than three length categories, an outer
231
// switch first dispatches on the lengths, to save on comparisons.
232
if (cats.length > 3) {
233
cats.sort(function(a, b) {return b.length - a.length;});
234
f += "switch(str.length){";
235
for (var i = 0; i < cats.length; ++i) {
236
var cat = cats[i];
237
f += "case " + cat[0].length + ":";
238
compareTo(cat);
239
}
240
f += "}";
241
// Otherwise, simply generate a flat `switch` statement.
242
} else {
243
compareTo(words);
244
}
245
return new Function("str", f);
246
};
247
248
function all(array, predicate) {
249
for (var i = array.length; --i >= 0;)
250
if (!predicate(array[i]))
251
return false;
252
return true;
253
};
254
255
function Dictionary() {
256
this._values = Object.create(null);
257
this._size = 0;
258
};
259
Dictionary.prototype = {
260
set: function(key, val) {
261
if (!this.has(key)) ++this._size;
262
this._values["$" + key] = val;
263
return this;
264
},
265
add: function(key, val) {
266
if (this.has(key)) {
267
this.get(key).push(val);
268
} else {
269
this.set(key, [ val ]);
270
}
271
return this;
272
},
273
get: function(key) { return this._values["$" + key] },
274
del: function(key) {
275
if (this.has(key)) {
276
--this._size;
277
delete this._values["$" + key];
278
}
279
return this;
280
},
281
has: function(key) { return ("$" + key) in this._values },
282
each: function(f) {
283
for (var i in this._values)
284
f(this._values[i], i.substr(1));
285
},
286
size: function() {
287
return this._size;
288
},
289
map: function(f) {
290
var ret = [];
291
for (var i in this._values)
292
ret.push(f(this._values[i], i.substr(1)));
293
return ret;
294
}
295
};
296
297