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