@platform/cell.schema
Version:
URI and database schemas for the `cell.os`.
67 lines (66 loc) • 2.22 kB
JavaScript
import { queryString } from '../common';
import { Uri } from '../Uri';
import { Links } from '../Links';
const prefix = 'ref';
const ref = Links.create(prefix);
export class RefLinks {
static total(links = {}) {
return ref.total(links);
}
static toKey(linkName) {
return ref.toKey(linkName);
}
static parseKey(linkKey) {
return ref.parseKey(linkKey);
}
static parseValue(linkValue) {
if (!RefLinks.is.refValue(linkValue)) {
throw new Error(`Cannot parse '${linkValue}' as it is not a supported URI type.`);
}
const res = ref.parseValue(linkValue);
const toString = (options = {}) => {
const { hash } = res.query;
const query = queryString
.build({ allowNil: false })
.add('hash', options.hash === null ? null : options.hash || hash)
.toString();
return `${res.uri.toString()}${query}`;
};
return Object.assign(Object.assign({}, res), { toString });
}
static parse(linkKey, linkValue) {
const key = RefLinks.parseKey(linkKey);
const value = RefLinks.parseValue(linkValue);
const toString = value.toString;
return Object.assign(Object.assign(Object.assign({}, key), value), { toString });
}
static toValue(uri, options = {}) {
const query = queryString
.build({ allowNil: false })
.add('hash', options.hash)
.toString();
return `${uri.toString()}${query}`;
}
static toList(links = {}) {
return ref.toList(links).map(({ key, value }) => RefLinks.parse(key, value));
}
static find(links = {}) {
return {
byName(path) {
const match = ref.find(links).byName(path);
return match ? RefLinks.parse(match.key, match.value) : undefined;
},
};
}
}
RefLinks.prefix = prefix;
RefLinks.is = {
refKey(input) {
return ref.isKey(input);
},
refValue(input) {
input = (input || '').toString().trim();
const uri = Uri.parse(input);
return ['NS', 'CELL', 'ROW', 'COLUMN'].includes(uri.type);
},
};