url-path
Version:
Adaptation of the WHATWG URL API for absolute paths
105 lines (84 loc) • 4.78 kB
JavaScript
;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _fixBabelExtend = function (O) {
var gPO = O.getPrototypeOf || function (o) {
return o.__proto__;
},
sPO = O.setPrototypeOf || function (o, p) {
o.__proto__ = p;
return o;
},
construct = (typeof Reflect === 'undefined' ? 'undefined' : _typeof(Reflect)) === 'object' ? Reflect.construct : function (Parent, args, Class) {
var Constructor,
a = [null];
a.push.apply(a, args);
Constructor = Parent.bind.apply(Parent, a);
return sPO(new Constructor(), Class.prototype);
};
return function fixBabelExtend(Class) {
var Parent = gPO(Class);
return sPO(Class, sPO(function Super() {
return construct(Parent, arguments, gPO(this).constructor);
}, Parent));
};
}(Object);
var _require = require('./url-api'),
URL = _require.URL;
var URLPath = _fixBabelExtend(function (_URL) {
_inherits(URLPath, _URL);
_createClass(URLPath, null, [{
key: 'bogusify',
value: function bogusify(url) {
if (!url) {
return 'bogus://bogushost/';
} else if (url.startsWith('/')) {
return 'bogus://bogushost' + url;
} else {
return 'bogus://bogushost/' + url;
}
}
}, {
key: 'debogusify',
value: function debogusify(url) {
var bogus = 'bogus://bogushost';
// Confidence check.
if (!url.startsWith(bogus)) {
throw Error('Expected absolute path ' + url + ' to begin with ' + bogus);
}
return url.substring(bogus.length);
}
}]);
function URLPath(url, baseUrl) {
_classCallCheck(this, URLPath);
try {
var _this = _possibleConstructorReturn(this, (URLPath.__proto__ || Object.getPrototypeOf(URLPath)).call(this, url, baseUrl));
return _possibleConstructorReturn(_this);
} catch (e) {}
var _this = _possibleConstructorReturn(this, (URLPath.__proto__ || Object.getPrototypeOf(URLPath)).call(this, url, URLPath.bogusify(baseUrl)));
_this.isBogus = true;
return _this;
}
_createClass(URLPath, [{
key: 'toString',
value: function toString() {
return this.href;
}
}, {
key: 'toJSON',
value: function toJSON() {
return this.href;
}
}, {
key: 'href',
get: function get() {
return this.isBogus ? this.constructor.debogusify(_get(URLPath.prototype.__proto__ || Object.getPrototypeOf(URLPath.prototype), 'href', this)) : _get(URLPath.prototype.__proto__ || Object.getPrototypeOf(URLPath.prototype), 'href', this);
}
}]);
return URLPath;
}(URL));
module.exports = URLPath;