UNPKG

@lyra/base

Version:

Lyra plugin containing the base components and roles for a Lyra configuration

102 lines (84 loc) 3.15 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); // Location implementation which keeps overlapping props in sync, e.g. host / hostname, and search / query exports.configure = configure; var _url = require('url'); var _url2 = _interopRequireDefault(_url); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function configure({ qsImpl } = { qsImpl: require('querystring') }) { class Location { get search() { const stringified = qsImpl.stringify(this.query || {}); return stringified.length > 1 ? `?${stringified}` : null; } set search(newSearch) { this.query = qsImpl.parse(newSearch.replace(/^\?/, '')); } get host() { return this.port ? `${this.hostname}:${this.port}` : this.hostname; } get path() { return this.pathname + (this.search || ''); } set path(newPath) { const parsed = _url2.default.parse(newPath, false, false); Object.assign(this, { pathname: parsed.pathname, query: qsImpl.parse(parsed.search.substring(1)) }); } set host(newVal) { var _newVal$split = newVal.split(':'), _newVal$split2 = _slicedToArray(_newVal$split, 2); const hostname = _newVal$split2[0], port = _newVal$split2[1]; Object.assign(this, { hostname: hostname, port: port }); } get href() { return _url2.default.format(this); } set href(newHref) { const parsed = _url2.default.parse(newHref, false, false); Object.assign(this, { protocol: parsed.protocol, slashes: parsed.slashes, hostname: parsed.hostname, pathname: parsed.pathname, port: parsed.port, auth: parsed.auth, query: qsImpl.parse((parsed.search || '').substring(1)), hash: parsed.hash }); } clone() { Object.assign(new Location(), this); } extend(properties) { return Object.assign(new Location(), this, properties); } } return { parse(urlToParse) { return Object.assign(new Location(), { href: urlToParse }); }, stringify(u) { return _url2.default.format({ protocol: u.protocol, slashes: u.slashes, hostname: u.hostname, pathname: u.pathname, port: u.port, search: u.search, hash: u.hash }); } }; } exports.default = configure();