react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / uglify-js / node_modules / source-map / lib / source-map / util.js
80766 views/* -*- Mode: js; js-indent-level: 2; -*- */1/*2* Copyright 2011 Mozilla Foundation and contributors3* Licensed under the New BSD license. See LICENSE or:4* http://opensource.org/licenses/BSD-3-Clause5*/6if (typeof define !== 'function') {7var define = require('amdefine')(module, require);8}9define(function (require, exports, module) {1011/**12* This is a helper function for getting values from parameter/options13* objects.14*15* @param args The object we are extracting values from16* @param name The name of the property we are getting.17* @param defaultValue An optional value to return if the property is missing18* from the object. If this is not specified and the property is missing, an19* error will be thrown.20*/21function getArg(aArgs, aName, aDefaultValue) {22if (aName in aArgs) {23return aArgs[aName];24} else if (arguments.length === 3) {25return aDefaultValue;26} else {27throw new Error('"' + aName + '" is a required argument.');28}29}30exports.getArg = getArg;3132var urlRegexp = /^(?:([\w+\-.]+):)?\/\/(?:(\w+:\w+)@)?([\w.]*)(?::(\d+))?(\S*)$/;33var dataUrlRegexp = /^data:.+\,.+$/;3435function urlParse(aUrl) {36var match = aUrl.match(urlRegexp);37if (!match) {38return null;39}40return {41scheme: match[1],42auth: match[2],43host: match[3],44port: match[4],45path: match[5]46};47}48exports.urlParse = urlParse;4950function urlGenerate(aParsedUrl) {51var url = '';52if (aParsedUrl.scheme) {53url += aParsedUrl.scheme + ':';54}55url += '//';56if (aParsedUrl.auth) {57url += aParsedUrl.auth + '@';58}59if (aParsedUrl.host) {60url += aParsedUrl.host;61}62if (aParsedUrl.port) {63url += ":" + aParsedUrl.port64}65if (aParsedUrl.path) {66url += aParsedUrl.path;67}68return url;69}70exports.urlGenerate = urlGenerate;7172/**73* Normalizes a path, or the path portion of a URL:74*75* - Replaces consequtive slashes with one slash.76* - Removes unnecessary '.' parts.77* - Removes unnecessary '<dir>/..' parts.78*79* Based on code in the Node.js 'path' core module.80*81* @param aPath The path or url to normalize.82*/83function normalize(aPath) {84var path = aPath;85var url = urlParse(aPath);86if (url) {87if (!url.path) {88return aPath;89}90path = url.path;91}92var isAbsolute = (path.charAt(0) === '/');9394var parts = path.split(/\/+/);95for (var part, up = 0, i = parts.length - 1; i >= 0; i--) {96part = parts[i];97if (part === '.') {98parts.splice(i, 1);99} else if (part === '..') {100up++;101} else if (up > 0) {102if (part === '') {103// The first part is blank if the path is absolute. Trying to go104// above the root is a no-op. Therefore we can remove all '..' parts105// directly after the root.106parts.splice(i + 1, up);107up = 0;108} else {109parts.splice(i, 2);110up--;111}112}113}114path = parts.join('/');115116if (path === '') {117path = isAbsolute ? '/' : '.';118}119120if (url) {121url.path = path;122return urlGenerate(url);123}124return path;125}126exports.normalize = normalize;127128/**129* Joins two paths/URLs.130*131* @param aRoot The root path or URL.132* @param aPath The path or URL to be joined with the root.133*134* - If aPath is a URL or a data URI, aPath is returned, unless aPath is a135* scheme-relative URL: Then the scheme of aRoot, if any, is prepended136* first.137* - Otherwise aPath is a path. If aRoot is a URL, then its path portion138* is updated with the result and aRoot is returned. Otherwise the result139* is returned.140* - If aPath is absolute, the result is aPath.141* - Otherwise the two paths are joined with a slash.142* - Joining for example 'http://' and 'www.example.com' is also supported.143*/144function join(aRoot, aPath) {145var aPathUrl = urlParse(aPath);146var aRootUrl = urlParse(aRoot);147if (aRootUrl) {148aRoot = aRootUrl.path || '/';149}150151// `join(foo, '//www.example.org')`152if (aPathUrl && !aPathUrl.scheme) {153if (aRootUrl) {154aPathUrl.scheme = aRootUrl.scheme;155}156return urlGenerate(aPathUrl);157}158159if (aPathUrl || aPath.match(dataUrlRegexp)) {160return aPath;161}162163// `join('http://', 'www.example.com')`164if (aRootUrl && !aRootUrl.host && !aRootUrl.path) {165aRootUrl.host = aPath;166return urlGenerate(aRootUrl);167}168169var joined = aPath.charAt(0) === '/'170? aPath171: normalize(aRoot.replace(/\/+$/, '') + '/' + aPath);172173if (aRootUrl) {174aRootUrl.path = joined;175return urlGenerate(aRootUrl);176}177return joined;178}179exports.join = join;180181/**182* Because behavior goes wacky when you set `__proto__` on objects, we183* have to prefix all the strings in our set with an arbitrary character.184*185* See https://github.com/mozilla/source-map/pull/31 and186* https://github.com/mozilla/source-map/issues/30187*188* @param String aStr189*/190function toSetString(aStr) {191return '$' + aStr;192}193exports.toSetString = toSetString;194195function fromSetString(aStr) {196return aStr.substr(1);197}198exports.fromSetString = fromSetString;199200function relative(aRoot, aPath) {201aRoot = aRoot.replace(/\/$/, '');202203var url = urlParse(aRoot);204if (aPath.charAt(0) == "/" && url && url.path == "/") {205return aPath.slice(1);206}207208return aPath.indexOf(aRoot + '/') === 0209? aPath.substr(aRoot.length + 1)210: aPath;211}212exports.relative = relative;213214function strcmp(aStr1, aStr2) {215var s1 = aStr1 || "";216var s2 = aStr2 || "";217return (s1 > s2) - (s1 < s2);218}219220/**221* Comparator between two mappings where the original positions are compared.222*223* Optionally pass in `true` as `onlyCompareGenerated` to consider two224* mappings with the same original source/line/column, but different generated225* line and column the same. Useful when searching for a mapping with a226* stubbed out mapping.227*/228function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) {229var cmp;230231cmp = strcmp(mappingA.source, mappingB.source);232if (cmp) {233return cmp;234}235236cmp = mappingA.originalLine - mappingB.originalLine;237if (cmp) {238return cmp;239}240241cmp = mappingA.originalColumn - mappingB.originalColumn;242if (cmp || onlyCompareOriginal) {243return cmp;244}245246cmp = strcmp(mappingA.name, mappingB.name);247if (cmp) {248return cmp;249}250251cmp = mappingA.generatedLine - mappingB.generatedLine;252if (cmp) {253return cmp;254}255256return mappingA.generatedColumn - mappingB.generatedColumn;257};258exports.compareByOriginalPositions = compareByOriginalPositions;259260/**261* Comparator between two mappings where the generated positions are262* compared.263*264* Optionally pass in `true` as `onlyCompareGenerated` to consider two265* mappings with the same generated line and column, but different266* source/name/original line and column the same. Useful when searching for a267* mapping with a stubbed out mapping.268*/269function compareByGeneratedPositions(mappingA, mappingB, onlyCompareGenerated) {270var cmp;271272cmp = mappingA.generatedLine - mappingB.generatedLine;273if (cmp) {274return cmp;275}276277cmp = mappingA.generatedColumn - mappingB.generatedColumn;278if (cmp || onlyCompareGenerated) {279return cmp;280}281282cmp = strcmp(mappingA.source, mappingB.source);283if (cmp) {284return cmp;285}286287cmp = mappingA.originalLine - mappingB.originalLine;288if (cmp) {289return cmp;290}291292cmp = mappingA.originalColumn - mappingB.originalColumn;293if (cmp) {294return cmp;295}296297return strcmp(mappingA.name, mappingB.name);298};299exports.compareByGeneratedPositions = compareByGeneratedPositions;300301});302303304