react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / uglify-js / lib / utils.js
80743 views/***********************************************************************12A JavaScript tokenizer / parser / beautifier / compressor.3https://github.com/mishoo/UglifyJS245-------------------------------- (C) ---------------------------------67Author: Mihai Bazon8<[email protected]>9http://mihai.bazon.net/blog1011Distributed under the BSD license:1213Copyright 2012 (c) Mihai Bazon <[email protected]>1415Redistribution and use in source and binary forms, with or without16modification, are permitted provided that the following conditions17are met:1819* Redistributions of source code must retain the above20copyright notice, this list of conditions and the following21disclaimer.2223* Redistributions in binary form must reproduce the above24copyright notice, this list of conditions and the following25disclaimer in the documentation and/or other materials26provided with the distribution.2728THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY29EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE30IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR31PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE32LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,33OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,34PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR35PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY36THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR37TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF38THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF39SUCH DAMAGE.4041***********************************************************************/4243"use strict";4445function array_to_hash(a) {46var ret = Object.create(null);47for (var i = 0; i < a.length; ++i)48ret[a[i]] = true;49return ret;50};5152function slice(a, start) {53return Array.prototype.slice.call(a, start || 0);54};5556function characters(str) {57return str.split("");58};5960function member(name, array) {61for (var i = array.length; --i >= 0;)62if (array[i] == name)63return true;64return false;65};6667function find_if(func, array) {68for (var i = 0, n = array.length; i < n; ++i) {69if (func(array[i]))70return array[i];71}72};7374function repeat_string(str, i) {75if (i <= 0) return "";76if (i == 1) return str;77var d = repeat_string(str, i >> 1);78d += d;79if (i & 1) d += str;80return d;81};8283function DefaultsError(msg, defs) {84Error.call(this, msg);85this.msg = msg;86this.defs = defs;87};88DefaultsError.prototype = Object.create(Error.prototype);89DefaultsError.prototype.constructor = DefaultsError;9091DefaultsError.croak = function(msg, defs) {92throw new DefaultsError(msg, defs);93};9495function defaults(args, defs, croak) {96if (args === true)97args = {};98var ret = args || {};99if (croak) for (var i in ret) if (ret.hasOwnProperty(i) && !defs.hasOwnProperty(i))100DefaultsError.croak("`" + i + "` is not a supported option", defs);101for (var i in defs) if (defs.hasOwnProperty(i)) {102ret[i] = (args && args.hasOwnProperty(i)) ? args[i] : defs[i];103}104return ret;105};106107function merge(obj, ext) {108var count = 0;109for (var i in ext) if (ext.hasOwnProperty(i)) {110obj[i] = ext[i];111count++;112}113return count;114};115116function noop() {};117118var MAP = (function(){119function MAP(a, f, backwards) {120var ret = [], top = [], i;121function doit() {122var val = f(a[i], i);123var is_last = val instanceof Last;124if (is_last) val = val.v;125if (val instanceof AtTop) {126val = val.v;127if (val instanceof Splice) {128top.push.apply(top, backwards ? val.v.slice().reverse() : val.v);129} else {130top.push(val);131}132}133else if (val !== skip) {134if (val instanceof Splice) {135ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);136} else {137ret.push(val);138}139}140return is_last;141};142if (a instanceof Array) {143if (backwards) {144for (i = a.length; --i >= 0;) if (doit()) break;145ret.reverse();146top.reverse();147} else {148for (i = 0; i < a.length; ++i) if (doit()) break;149}150}151else {152for (i in a) if (a.hasOwnProperty(i)) if (doit()) break;153}154return top.concat(ret);155};156MAP.at_top = function(val) { return new AtTop(val) };157MAP.splice = function(val) { return new Splice(val) };158MAP.last = function(val) { return new Last(val) };159var skip = MAP.skip = {};160function AtTop(val) { this.v = val };161function Splice(val) { this.v = val };162function Last(val) { this.v = val };163return MAP;164})();165166function push_uniq(array, el) {167if (array.indexOf(el) < 0)168array.push(el);169};170171function string_template(text, props) {172return text.replace(/\{(.+?)\}/g, function(str, p){173return props[p];174});175};176177function remove(array, el) {178for (var i = array.length; --i >= 0;) {179if (array[i] === el) array.splice(i, 1);180}181};182183function mergeSort(array, cmp) {184if (array.length < 2) return array.slice();185function merge(a, b) {186var r = [], ai = 0, bi = 0, i = 0;187while (ai < a.length && bi < b.length) {188cmp(a[ai], b[bi]) <= 0189? r[i++] = a[ai++]190: r[i++] = b[bi++];191}192if (ai < a.length) r.push.apply(r, a.slice(ai));193if (bi < b.length) r.push.apply(r, b.slice(bi));194return r;195};196function _ms(a) {197if (a.length <= 1)198return a;199var m = Math.floor(a.length / 2), left = a.slice(0, m), right = a.slice(m);200left = _ms(left);201right = _ms(right);202return merge(left, right);203};204return _ms(array);205};206207function set_difference(a, b) {208return a.filter(function(el){209return b.indexOf(el) < 0;210});211};212213function set_intersection(a, b) {214return a.filter(function(el){215return b.indexOf(el) >= 0;216});217};218219// this function is taken from Acorn [1], written by Marijn Haverbeke220// [1] https://github.com/marijnh/acorn221function makePredicate(words) {222if (!(words instanceof Array)) words = words.split(" ");223var f = "", cats = [];224out: for (var i = 0; i < words.length; ++i) {225for (var j = 0; j < cats.length; ++j)226if (cats[j][0].length == words[i].length) {227cats[j].push(words[i]);228continue out;229}230cats.push([words[i]]);231}232function compareTo(arr) {233if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";";234f += "switch(str){";235for (var i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":";236f += "return true}return false;";237}238// When there are more than three length categories, an outer239// switch first dispatches on the lengths, to save on comparisons.240if (cats.length > 3) {241cats.sort(function(a, b) {return b.length - a.length;});242f += "switch(str.length){";243for (var i = 0; i < cats.length; ++i) {244var cat = cats[i];245f += "case " + cat[0].length + ":";246compareTo(cat);247}248f += "}";249// Otherwise, simply generate a flat `switch` statement.250} else {251compareTo(words);252}253return new Function("str", f);254};255256function all(array, predicate) {257for (var i = array.length; --i >= 0;)258if (!predicate(array[i]))259return false;260return true;261};262263function Dictionary() {264this._values = Object.create(null);265this._size = 0;266};267Dictionary.prototype = {268set: function(key, val) {269if (!this.has(key)) ++this._size;270this._values["$" + key] = val;271return this;272},273add: function(key, val) {274if (this.has(key)) {275this.get(key).push(val);276} else {277this.set(key, [ val ]);278}279return this;280},281get: function(key) { return this._values["$" + key] },282del: function(key) {283if (this.has(key)) {284--this._size;285delete this._values["$" + key];286}287return this;288},289has: function(key) { return ("$" + key) in this._values },290each: function(f) {291for (var i in this._values)292f(this._values[i], i.substr(1));293},294size: function() {295return this._size;296},297map: function(f) {298var ret = [];299for (var i in this._values)300ret.push(f(this._values[i], i.substr(1)));301return ret;302},303toObject: function() { return this._values }304};305Dictionary.fromObject = function(obj) {306var dict = new Dictionary();307dict._size = merge(dict._values, obj);308return dict;309};310311312