UNPKG

@difizen/mana-common

Version:

252 lines (245 loc) 9.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.URI = void 0; var _path = require("./path"); var _uri = require("./vscode-uri/uri"); function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var URI = exports.URI = /*#__PURE__*/function () { function URI() { var uri = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var options = arguments.length > 1 ? arguments[1] : undefined; _classCallCheck(this, URI); _defineProperty(this, "codeUri", void 0); _defineProperty(this, "_path", void 0); if (uri instanceof _uri.URI) { this.codeUri = uri; } else { this.codeUri = _uri.URI.parse(uri, false, options); } } _createClass(URI, [{ key: "parent", get: function get() { if (this.path.isRoot) { return this; } return URI.withPath(this, this.path.dir); } }, { key: "relative", value: function relative(uri) { if (this.authority !== uri.authority || this.scheme !== uri.scheme) { return undefined; } return _path.Path.relative(this.path, uri.path); } /** * return a new URI replacing the current with its normalized path, resolving '..' and '.' segments */ }, { key: "normalizePath", value: function normalizePath() { return URI.withPath(this, _path.Path.normalize(this.path)); } }, { key: "displayName", get: function get() { var base = this.path.base; if (base) { return base; } if (this.path.isRoot) { return this.path.toString(); } return ''; } }, { key: "scheme", get: function get() { return this.codeUri.scheme; } }, { key: "authority", get: function get() { return this.codeUri.authority; } }, { key: "path", get: function get() { if (this._path === undefined) { this._path = new _path.Path(this.codeUri.path); } return this._path; } }, { key: "query", get: function get() { return this.codeUri.query; } }, { key: "fragment", get: function get() { return this.codeUri.fragment; } }, { key: "toString", value: function toString(skipEncoding) { return this.codeUri.toString(skipEncoding); } }, { key: "toJSON", value: function toJSON() { return _objectSpread(_objectSpread({}, this.codeUri.toJSON()), {}, { scheme: this.codeUri.scheme }); } }, { key: "equals", value: function equals(uri) { var caseSensitive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (!this.sameOrigin(uri)) { return false; } return caseSensitive ? this.toString() === uri.toString() : this.toString().toLowerCase() === uri.toString().toLowerCase(); } }, { key: "includes", value: function includes(uri) { var caseSensitive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (!this.sameOrigin(uri)) { return false; } var left = this.path; var right = uri.path; if (!caseSensitive) { left = new _path.Path(left.toString().toLowerCase()); right = new _path.Path(right.toString().toLowerCase()); } return left.includes(right); } }, { key: "sameOrigin", value: function sameOrigin(uri) { return this.authority === uri.authority && this.scheme === uri.scheme; } }, { key: "getParsedQuery", value: function getParsedQuery() { var queryString = this.query; var query = {}; var pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&'); for (var i = 0; i < pairs.length; i++) { var pair = pairs[i].split('='); query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || ''); } return query; } }], [{ key: "resolve", value: function resolve(uri, path) { return URI.withPath(uri, _path.Path.join(uri.path, path.toString())); } /** * return a new URI replacing the current with the given scheme */ }, { key: "withScheme", value: function withScheme(uri, scheme) { var newCodeUri = _uri.URI.from(_objectSpread(_objectSpread({}, uri.toJSON()), {}, { scheme: scheme })); return new URI(newCodeUri); } /** * return a new URI replacing the current with the given authority */ }, { key: "withAuthority", value: function withAuthority(uri) { var authority = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; var newCodeUri = _uri.URI.from(_objectSpread(_objectSpread({}, uri.toJSON()), {}, { authority: authority })); return new URI(newCodeUri); } /** * return a new URI replacing the current with the given path */ }, { key: "withPath", value: function withPath(uri) { var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; var newCodeUri = _uri.URI.from(_objectSpread(_objectSpread({}, uri.toJSON()), {}, { path: path.toString() })); return new URI(newCodeUri); } /** * return a new URI replacing the current with the given query */ }, { key: "withQuery", value: function withQuery(uri) { var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; var newCodeUri = _uri.URI.from(_objectSpread(_objectSpread({}, uri.toJSON()), {}, { query: query })); return new URI(newCodeUri); } /** * return a new URI replacing the current with the given fragment */ }, { key: "withFragment", value: function withFragment(uri) { var fragment = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; var newCodeUri = _uri.URI.from(_objectSpread(_objectSpread({}, uri.toJSON()), {}, { fragment: fragment })); return new URI(newCodeUri); } }, { key: "sameOrigin", value: function sameOrigin(uriA, uriB) { return uriA.authority === uriB.authority && uriA.scheme === uriB.scheme; } }, { key: "isEqualOrParent", value: function isEqualOrParent(uriA, uriB) { var caseSensitive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; if (!URI.sameOrigin(uriA, uriB)) { return false; } var left = uriA.path; var right = uriB.path; if (!caseSensitive) { left = new _path.Path(left.toString().toLowerCase()); right = new _path.Path(right.toString().toLowerCase()); } return !!_path.Path.relative(left, right); } }, { key: "stringifyQuery", value: function stringifyQuery(query) { var values = []; Object.keys(query).forEach(function (key) { var value = encodeURIComponent(query[key]); if (value !== undefined) { values.push(encodeURIComponent(key) + '=' + value); } }); return values.join('&'); } }]); return URI; }();