react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / node_modules / source-map / lib / source-map / util.js
80559 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) {145if (aRoot === "") {146aRoot = ".";147}148if (aPath === "") {149aPath = ".";150}151var aPathUrl = urlParse(aPath);152var aRootUrl = urlParse(aRoot);153if (aRootUrl) {154aRoot = aRootUrl.path || '/';155}156157// `join(foo, '//www.example.org')`158if (aPathUrl && !aPathUrl.scheme) {159if (aRootUrl) {160aPathUrl.scheme = aRootUrl.scheme;161}162return urlGenerate(aPathUrl);163}164165if (aPathUrl || aPath.match(dataUrlRegexp)) {166return aPath;167}168169// `join('http://', 'www.example.com')`170if (aRootUrl && !aRootUrl.host && !aRootUrl.path) {171aRootUrl.host = aPath;172return urlGenerate(aRootUrl);173}174175var joined = aPath.charAt(0) === '/'176? aPath177: normalize(aRoot.replace(/\/+$/, '') + '/' + aPath);178179if (aRootUrl) {180aRootUrl.path = joined;181return urlGenerate(aRootUrl);182}183return joined;184}185exports.join = join;186187/**188* Make a path relative to a URL or another path.189*190* @param aRoot The root path or URL.191* @param aPath The path or URL to be made relative to aRoot.192*/193function relative(aRoot, aPath) {194if (aRoot === "") {195aRoot = ".";196}197198aRoot = aRoot.replace(/\/$/, '');199200// XXX: It is possible to remove this block, and the tests still pass!201var url = urlParse(aRoot);202if (aPath.charAt(0) == "/" && url && url.path == "/") {203return aPath.slice(1);204}205206return aPath.indexOf(aRoot + '/') === 0207? aPath.substr(aRoot.length + 1)208: aPath;209}210exports.relative = relative;211212/**213* Because behavior goes wacky when you set `__proto__` on objects, we214* have to prefix all the strings in our set with an arbitrary character.215*216* See https://github.com/mozilla/source-map/pull/31 and217* https://github.com/mozilla/source-map/issues/30218*219* @param String aStr220*/221function toSetString(aStr) {222return '$' + aStr;223}224exports.toSetString = toSetString;225226function fromSetString(aStr) {227return aStr.substr(1);228}229exports.fromSetString = fromSetString;230231function strcmp(aStr1, aStr2) {232var s1 = aStr1 || "";233var s2 = aStr2 || "";234return (s1 > s2) - (s1 < s2);235}236237/**238* Comparator between two mappings where the original positions are compared.239*240* Optionally pass in `true` as `onlyCompareGenerated` to consider two241* mappings with the same original source/line/column, but different generated242* line and column the same. Useful when searching for a mapping with a243* stubbed out mapping.244*/245function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) {246var cmp;247248cmp = strcmp(mappingA.source, mappingB.source);249if (cmp) {250return cmp;251}252253cmp = mappingA.originalLine - mappingB.originalLine;254if (cmp) {255return cmp;256}257258cmp = mappingA.originalColumn - mappingB.originalColumn;259if (cmp || onlyCompareOriginal) {260return cmp;261}262263cmp = strcmp(mappingA.name, mappingB.name);264if (cmp) {265return cmp;266}267268cmp = mappingA.generatedLine - mappingB.generatedLine;269if (cmp) {270return cmp;271}272273return mappingA.generatedColumn - mappingB.generatedColumn;274};275exports.compareByOriginalPositions = compareByOriginalPositions;276277/**278* Comparator between two mappings where the generated positions are279* compared.280*281* Optionally pass in `true` as `onlyCompareGenerated` to consider two282* mappings with the same generated line and column, but different283* source/name/original line and column the same. Useful when searching for a284* mapping with a stubbed out mapping.285*/286function compareByGeneratedPositions(mappingA, mappingB, onlyCompareGenerated) {287var cmp;288289cmp = mappingA.generatedLine - mappingB.generatedLine;290if (cmp) {291return cmp;292}293294cmp = mappingA.generatedColumn - mappingB.generatedColumn;295if (cmp || onlyCompareGenerated) {296return cmp;297}298299cmp = strcmp(mappingA.source, mappingB.source);300if (cmp) {301return cmp;302}303304cmp = mappingA.originalLine - mappingB.originalLine;305if (cmp) {306return cmp;307}308309cmp = mappingA.originalColumn - mappingB.originalColumn;310if (cmp) {311return cmp;312}313314return strcmp(mappingA.name, mappingB.name);315};316exports.compareByGeneratedPositions = compareByGeneratedPositions;317318});319320321