1
/* -*- Mode: js; js-indent-level: 2; -*- */
3
* Copyright 2011 Mozilla Foundation and contributors
4
* Licensed under the New BSD license. See LICENSE or:
5
* http://opensource.org/licenses/BSD-3-Clause
7
if (typeof define !== 'function') {
8
var define = require('amdefine')(module);
10
define(function (require, exports, module) {
13
* This is a helper function for getting values from parameter/options
16
* @param args The object we are extracting values from
17
* @param name The name of the property we are getting.
18
* @param defaultValue An optional value to return if the property is missing
19
* from the object. If this is not specified and the property is missing, an
20
* error will be thrown.
22
function getArg(aArgs, aName, aDefaultValue) {
25
} else if (arguments.length === 3) {
28
throw new Error('"' + aName + '" is a required argument.');
31
exports.getArg = getArg;
33
var urlRegexp = /([\w+\-.]+):\/\/((\w+:\w+)@)?([\w.]+)?(:(\d+))?(\S+)?/;
35
function urlParse(aUrl) {
36
var match = aUrl.match(urlRegexp);
48
exports.urlParse = urlParse;
50
function urlGenerate(aParsedUrl) {
51
var url = aParsedUrl.scheme + "://";
52
if (aParsedUrl.auth) {
53
url += aParsedUrl.auth + "@"
55
if (aParsedUrl.host) {
56
url += aParsedUrl.host;
58
if (aParsedUrl.port) {
59
url += ":" + aParsedUrl.port
61
if (aParsedUrl.path) {
62
url += aParsedUrl.path;
66
exports.urlGenerate = urlGenerate;
68
function join(aRoot, aPath) {
71
if (aPath.match(urlRegexp)) {
75
if (aPath.charAt(0) === '/' && (url = urlParse(aRoot))) {
77
return urlGenerate(url);
80
return aRoot.replace(/\/$/, '') + '/' + aPath;
85
* Because behavior goes wacky when you set `__proto__` on objects, we
86
* have to prefix all the strings in our set with an arbitrary character.
88
* See https://github.com/mozilla/source-map/pull/31 and
89
* https://github.com/mozilla/source-map/issues/30
93
function toSetString(aStr) {
96
exports.toSetString = toSetString;
98
function fromSetString(aStr) {
99
return aStr.substr(1);
101
exports.fromSetString = fromSetString;
103
function relative(aRoot, aPath) {
104
aRoot = aRoot.replace(/\/$/, '');
106
var url = urlParse(aRoot);
107
if (aPath.charAt(0) == "/" && url && url.path == "/") {
108
return aPath.slice(1);
111
return aPath.indexOf(aRoot + '/') === 0
112
? aPath.substr(aRoot.length + 1)
115
exports.relative = relative;