UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

158 lines (157 loc) 3.92 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); function createURI(options) { let s = ""; if ((options.authority || options.scheme) && (options.host || options.hostpath)) { throw new Error("Can't have 'scheme' or 'authority' and 'host' or 'hostpath' option"); } if (options.host && options.hostpath) { throw new Error("Can't have 'host' and 'hostpath' option"); } if (options.path && options.hostpath) { throw new Error("Can't have 'path' and 'hostpath' option"); } if (options.scheme) { s += `${options.scheme}:`; } if (options.authority) { s += `//${options.authority}`; } if (options.host) { s += options.host; } if (options.path) { s += options.path; } if (options.hostpath) { s += options.hostpath; } if (options.query) { s += `?${options.query}`; } if (options.fragment) { s += `#${options.fragment}`; } return s; } const re = /^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/; class URI { /** * Create a new URI instance. * * @param {string} uri - URI string. */ constructor(uri) { /** * The scheme. (e.g. http). * * @type {string} */ __publicField(this, "scheme"); /** * The authority. (e.g. `www.example.com`). * * @type {string} */ __publicField(this, "authority"); /** * The path. (e.g. /users/example). * * @type {string} */ __publicField(this, "path"); /** * The query, the section after a ?. (e.g. search=value). * * @type {string} */ __publicField(this, "query"); /** * The fragment, the section after a #. * * @type {string} */ __publicField(this, "fragment"); const result = uri.match(re); this.scheme = result[2]; this.authority = result[4]; this.path = result[5]; this.query = result[7]; this.fragment = result[9]; } /** * Convert URI back to string. * * @returns {string} The URI as a string. */ toString() { let s = ""; if (this.scheme) { s += `${this.scheme}:`; } if (this.authority) { s += `//${this.authority}`; } s += this.path; if (this.query) { s += `?${this.query}`; } if (this.fragment) { s += `#${this.fragment}`; } return s; } /** * Returns the query parameters as an Object. * * @returns {object} The URI's query parameters converted to an Object. * @example * const s = "http://example.com?a=1&b=2&c=3"; * const uri = new pc.URI(s); * const q = uri.getQuery(); * console.log(q.a); // logs "1" * console.log(q.b); // logs "2" * console.log(q.c); // logs "3" */ getQuery() { const result = {}; if (this.query) { const queryParams = decodeURIComponent(this.query).split("&"); for (const queryParam of queryParams) { const pair = queryParam.split("="); result[pair[0]] = pair[1]; } } return result; } /** * Set the query section of the URI from an Object. * * @param {object} params - Key-Value pairs to encode into the query string. * @example * const s = "http://example.com"; * const uri = new pc.URI(s); * uri.setQuery({ * "a": 1, * "b": 2 * }); * console.log(uri.toString()); // logs "http://example.com?a=1&b=2" */ setQuery(params) { let q = ""; for (const key in params) { if (params.hasOwnProperty(key)) { if (q !== "") { q += "&"; } q += `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`; } } this.query = q; } } export { URI, createURI };