@platform/cell.schema
Version:
URI and database schemas for the `cell.os`.
128 lines (127 loc) • 5.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Links = void 0;
var tslib_1 = require("tslib");
var common_1 = require("../common");
var Uri_1 = require("../Uri");
var Encoding_Key_1 = require("../Encoding/Encoding.Key");
var Links = (function () {
function Links(args) {
this.prefix = args.prefix;
}
Links.prototype.isKey = function (input) {
return Links.isKey(this.prefix, input);
};
Links.isKey = function (prefix, input) {
input = (input || '').toString().trim();
return input.startsWith(prefix);
};
Links.prototype.total = function (links) {
if (links === void 0) { links = {}; }
return Links.total(this.prefix, links);
};
Links.total = function (prefix, links) {
if (links === void 0) { links = {}; }
return Object.keys(links).reduce(function (acc, next) {
return Links.isKey(prefix, next) ? acc + 1 : acc;
}, 0);
};
Links.prototype.toKey = function (input) {
return Links.toKey(this.prefix, input);
};
Links.toKey = function (prefix, input) {
input = (input || '').trim();
if (!input) {
throw new Error("Link key must have a value.");
}
prefix = (prefix || '').trim().replace(/:*$/, '');
return "".concat(prefix, ":").concat(Encoding_Key_1.KeyEncoding.escape(input));
};
Links.prototype.toList = function (links) {
if (links === void 0) { links = {}; }
return Links.toList(this.prefix, links);
};
Links.toList = function (prefix, links) {
if (links === void 0) { links = {}; }
return Object.keys(links)
.map(function (key) { return ({ key: key, value: links[key] }); })
.filter(function (_a) {
var key = _a.key;
return Links.isKey(prefix, key);
})
.map(function (_a) {
var key = _a.key, value = _a.value;
return ({ key: key, value: value });
});
};
Links.prototype.parseKey = function (linkKey) {
return Links.parseKey(this.prefix, linkKey);
};
Links.parseKey = function (prefix, linkKey) {
var key = (linkKey || '').trim();
var path = key.replace(new RegExp("^".concat(prefix, ":")), '');
path = shouldDecode(path) ? Links.decodeKey(path) : path;
var lastSlash = path.lastIndexOf('/');
var name = lastSlash < 0 ? path : path.substring(lastSlash + 1);
var dir = lastSlash < 0 ? '' : path.substring(0, lastSlash);
var lastPeriod = name.lastIndexOf('.');
var ext = lastPeriod < 0 ? '' : name.substring(lastPeriod + 1);
var res = { prefix: prefix, key: key, path: path, name: name, dir: dir, ext: ext };
return res;
};
Links.prototype.parseValue = function (linkValue) {
return Links.parseValue(linkValue);
};
Links.parseValue = function (linkValue) {
var parts = (linkValue || '')
.trim()
.split('?')
.map(function (part) { return part.trim(); });
var uri = Uri_1.Uri.parse(parts[0] || '').parts;
var query = common_1.QueryString.toObject(parts[1]);
var value = parts.join('?').replace(/\?$/, '');
var res = { value: value, uri: uri, query: query };
return res;
};
Links.prototype.parse = function (linkKey, linkValue) {
return Links.parseLink(this.prefix, linkKey, linkValue);
};
Links.parseLink = function (prefix, linkKey, linkValue) {
var key = Links.parseKey(prefix, linkKey);
var value = Links.parseValue(linkValue);
return tslib_1.__assign(tslib_1.__assign({}, key), value);
};
Links.prototype.find = function (links) {
if (links === void 0) { links = {}; }
return Links.find(this.prefix, links);
};
Links.find = function (prefix, links) {
if (links === void 0) { links = {}; }
return {
byName: function (path) {
path = (path || '').trim().replace(/^\/*/, '');
return Object.keys(links)
.map(function (key) { return ({ key: key, value: links[key] }); })
.filter(function (_a) {
var key = _a.key;
return Links.isKey(prefix, key);
})
.find(function (_a) {
var key = _a.key;
var parsed = Links.parseKey(prefix, key);
return (path || '').includes('*')
? common_1.wildcard.isMatch(parsed.path, path)
: parsed.path === path;
});
},
};
};
Links.encodeKey = Encoding_Key_1.KeyEncoding.escape;
Links.decodeKey = Encoding_Key_1.KeyEncoding.unescape;
Links.create = function (prefix) { return new Links({ prefix: prefix }); };
return Links;
}());
exports.Links = Links;
function shouldDecode(input) {
return input.includes(':');
}