UNPKG

@platform/cell.schema

Version:

URI and database schemas for the `cell.os`.

71 lines (70 loc) 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var common_1 = require("../common"); var Url = (function () { function Url(args) { this._ = { query: {}, querystring: '', }; this.origin = (args.origin || '').trim().replace(/\/*$/, ''); this.path = "/" + (args.path || '').trim().replace(/^\/*/, ''); this._.query = args.query || {}; this._.querystring = typeof args.querystring === 'string' ? args.querystring.trim() : ''; } Object.defineProperty(Url.prototype, "querystring", { get: function () { var text = (this._.querystring || '').replace(/^\?*/, ''); var query = this._.query || {}; var res = ''; var format = function (value) { value = typeof value === 'string' ? value.trim() : value; return value; }; var append = function (key, value) { res = res ? res + "&" : res; res = value === undefined ? "" + res + key : "" + res + key + "=" + value; }; Object.keys(query).forEach(function (key) { var value = query[key]; if (typeof value !== 'function' && value !== undefined && value !== '') { if (Array.isArray(value)) { var values = value.map(function (value) { return format(value); }); common_1.R.uniq(values).forEach(function (value) { return append(key, value); }); } else { append(key, format(value)); } } }); if (text) { res = res ? res + "&" + text : text; } return res ? "?" + res : ''; }, enumerable: true, configurable: true }); Url.prototype.query = function (input) { var querystring = this._.querystring || ''; var query = this._.query || {}; if (typeof input === 'object') { query = tslib_1.__assign(tslib_1.__assign({}, query), input); } return new Url({ origin: this.origin, path: this.path, query: query, querystring: querystring, }); }; Url.prototype.toString = function (options) { if (options === void 0) { options = {}; } var origin = common_1.defaultValue(options.origin, true); var path = "" + this.path + this.querystring; return origin ? "" + this.origin + path : path; }; return Url; }()); exports.Url = Url;