react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / 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[3],43host: match[4],44port: match[6],45path: match[7]46};47}48exports.urlParse = urlParse;4950function urlGenerate(aParsedUrl) {51var url = aParsedUrl.scheme + "://";52if (aParsedUrl.auth) {53url += aParsedUrl.auth + "@"54}55if (aParsedUrl.host) {56url += aParsedUrl.host;57}58if (aParsedUrl.port) {59url += ":" + aParsedUrl.port60}61if (aParsedUrl.path) {62url += aParsedUrl.path;63}64return url;65}66exports.urlGenerate = urlGenerate;6768function join(aRoot, aPath) {69var url;7071if (aPath.match(urlRegexp) || aPath.match(dataUrlRegexp)) {72return aPath;73}7475if (aPath.charAt(0) === '/' && (url = urlParse(aRoot))) {76url.path = aPath;77return urlGenerate(url);78}7980return aRoot.replace(/\/$/, '') + '/' + aPath;81}82exports.join = join;8384/**85* Because behavior goes wacky when you set `__proto__` on objects, we86* have to prefix all the strings in our set with an arbitrary character.87*88* See https://github.com/mozilla/source-map/pull/31 and89* https://github.com/mozilla/source-map/issues/3090*91* @param String aStr92*/93function toSetString(aStr) {94return '$' + aStr;95}96exports.toSetString = toSetString;9798function fromSetString(aStr) {99return aStr.substr(1);100}101exports.fromSetString = fromSetString;102103function relative(aRoot, aPath) {104aRoot = aRoot.replace(/\/$/, '');105106var url = urlParse(aRoot);107if (aPath.charAt(0) == "/" && url && url.path == "/") {108return aPath.slice(1);109}110111return aPath.indexOf(aRoot + '/') === 0112? aPath.substr(aRoot.length + 1)113: aPath;114}115exports.relative = relative;116117function strcmp(aStr1, aStr2) {118var s1 = aStr1 || "";119var s2 = aStr2 || "";120return (s1 > s2) - (s1 < s2);121}122123/**124* Comparator between two mappings where the original positions are compared.125*126* Optionally pass in `true` as `onlyCompareGenerated` to consider two127* mappings with the same original source/line/column, but different generated128* line and column the same. Useful when searching for a mapping with a129* stubbed out mapping.130*/131function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) {132var cmp;133134cmp = strcmp(mappingA.source, mappingB.source);135if (cmp) {136return cmp;137}138139cmp = mappingA.originalLine - mappingB.originalLine;140if (cmp) {141return cmp;142}143144cmp = mappingA.originalColumn - mappingB.originalColumn;145if (cmp || onlyCompareOriginal) {146return cmp;147}148149cmp = strcmp(mappingA.name, mappingB.name);150if (cmp) {151return cmp;152}153154cmp = mappingA.generatedLine - mappingB.generatedLine;155if (cmp) {156return cmp;157}158159return mappingA.generatedColumn - mappingB.generatedColumn;160};161exports.compareByOriginalPositions = compareByOriginalPositions;162163/**164* Comparator between two mappings where the generated positions are165* compared.166*167* Optionally pass in `true` as `onlyCompareGenerated` to consider two168* mappings with the same generated line and column, but different169* source/name/original line and column the same. Useful when searching for a170* mapping with a stubbed out mapping.171*/172function compareByGeneratedPositions(mappingA, mappingB, onlyCompareGenerated) {173var cmp;174175cmp = mappingA.generatedLine - mappingB.generatedLine;176if (cmp) {177return cmp;178}179180cmp = mappingA.generatedColumn - mappingB.generatedColumn;181if (cmp || onlyCompareGenerated) {182return cmp;183}184185cmp = strcmp(mappingA.source, mappingB.source);186if (cmp) {187return cmp;188}189190cmp = mappingA.originalLine - mappingB.originalLine;191if (cmp) {192return cmp;193}194195cmp = mappingA.originalColumn - mappingB.originalColumn;196if (cmp) {197return cmp;198}199200return strcmp(mappingA.name, mappingB.name);201};202exports.compareByGeneratedPositions = compareByGeneratedPositions;203204});205206207