semantic-network
Version:
A utility library for manipulating a list of links that form a semantic interface to a network of resources.
185 lines • 8.92 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RepresentationUtil = void 0;
var semantic_link_1 = require("semantic-link");
var sparseRepresentationFactory_1 = require("../representation/sparseRepresentationFactory");
var linkRelation_1 = require("../linkRelation");
var anylogger_1 = __importDefault(require("anylogger"));
var namedRepresentationFactory_1 = require("../representation/namedRepresentationFactory");
var instanceOfCollection_1 = require("./instanceOf/instanceOfCollection");
var log = (0, anylogger_1.default)('RepresentationUtil');
var RepresentationUtil = /** @class */ (function () {
function RepresentationUtil() {
}
/**
* Return the list of keys as a typed array from a representation.
*
* see https://fettblog.eu/typescript-better-object-keys/
* see https://stackoverflow.com/questions/52856496/typescript-object-keys-return-string
*
* @param representation representation object
* @returns array of all the field property keys
*/
RepresentationUtil.properties = function (representation) {
return Object.keys(representation)
.filter(function (x) { return x !== 'links'; });
};
RepresentationUtil.getProperty = function (o, propertyName) {
return o[propertyName];
};
/**
* Finds (by OR) a resource item in a collection identified through a found link relation or resource attribute
* that matches an item in the collection items.
*
* It looks for items:
*
* 1. matching link relation (default: Self) by uri
* 2. field attribute (default: name ({@link TrackedRepresentationFactory.mappedTitleAttributeName}) on a resource by string
* 3. link selector
*
*/
RepresentationUtil.findInCollection = function (collection, options) {
if (!collection || !(0, instanceOfCollection_1.instanceOfCollection)(collection)) {
log.error("find resource in collection: failed \u2014 not an instance of collection '".concat(semantic_link_1.LinkUtil.getUri(collection, linkRelation_1.LinkRelation.Self), "'"));
return undefined;
}
var _a = __assign({}, options), _b = _a.rel, rel = _b === void 0 ? undefined : _b, _c = _a.where, where = _c === void 0 ? undefined : _c;
var resourceIdentifier;
if (typeof where === 'string' /* Uri */) {
// treat predicate as Uri
resourceIdentifier = where;
}
else if ((0, semantic_link_1.instanceOfLinkedRepresentation)(where)) {
var uri = semantic_link_1.LinkUtil.getUri(where, rel || linkRelation_1.LinkRelation.Self);
if (uri) {
resourceIdentifier = uri;
}
else {
log.debug('find resource in collection: not found — no \'where\' and \'rel\' options that combine to create resource identifier on \'%s\'', rel || linkRelation_1.LinkRelation.Self, semantic_link_1.LinkUtil.getUri(collection, linkRelation_1.LinkRelation.Self));
return undefined;
}
}
else if ((0, semantic_link_1.instanceOfLinkSelector)(where)) {
var uri = semantic_link_1.LinkUtil.getUri(collection, where);
if (uri) {
resourceIdentifier = uri;
}
else {
log.debug('find resource in collection: not found — no \'where\' and link selector \'%o\' options that combine to create resource identifier on \'%s\'', where, semantic_link_1.LinkUtil.getUri(collection, linkRelation_1.LinkRelation.Self));
return undefined;
}
}
else if (Array.isArray(where)) {
log.debug('find resource in collection: array cannot be assigned to where');
}
else if (!where) {
log.debug('find resource in collection: where not used as \'%s\'', where);
}
else {
log.debug('find resource in collection: unknown where');
}
// attribute look up strategy. Used for fallback strategy 2.
// TODO: allow for multiple link relations in underlying function
var name = namedRepresentationFactory_1.NamedRepresentationFactory.defaultNameStrategy(rel);
// title will only exist where a resource is passed in AND there is a mapped title. Used for fallback strategy 3.
var mappedTitleAttributeName = sparseRepresentationFactory_1.SparseRepresentationFactory.defaultMappedTitleAttributeName;
/** internal helper function to return comparable string from the property of a resource */
function getResourceTitle(obj, prop) {
var _a;
if (prop === void 0) { prop = mappedTitleAttributeName; }
return (_a = obj === null || obj === void 0 ? void 0 : obj[prop]) === null || _a === void 0 ? void 0 : _a.toLowerCase();
}
var resourceTitle = getResourceTitle(where);
log.debug('find resource in collection: \'%s\' \'%s\'', name, resourceTitle);
// go through the collection and match the URI against either a link relation or attribute
return collection
.items
.find(function (item) {
// strategy 1 & 4: Self link of item matches
return semantic_link_1.LinkUtil.getUri(item, rel || linkRelation_1.LinkRelation.Self) === resourceIdentifier ||
// strategy 2: the attribute on the resource is a uri that matches
(name && getResourceTitle(item, name) === resourceIdentifier) ||
// strategy 3: fallback to mapped title values matching (not uris but titles)
(resourceTitle && getResourceTitle(item) === resourceTitle);
});
};
RepresentationUtil.fields = function (representation) {
return Object.keys(representation)
.filter(function (x) { return x !== 'links'; });
};
/**
* Removes the item from the collection by matching its Self link. If not found, it returns undefined.
*/
RepresentationUtil.removeItemFromCollection = function (collection, item) {
var resourceUri = semantic_link_1.LinkUtil.getUri(item, linkRelation_1.LinkRelation.Self);
var indexOfItemToBeRemoved = collection.items.findIndex(function (item) { return semantic_link_1.LinkUtil.getUri(item, linkRelation_1.LinkRelation.Self) === resourceUri; });
if (indexOfItemToBeRemoved >= 0) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
var _a = __read(collection.items.splice(indexOfItemToBeRemoved, 1), 2), head = _a[0], _1 = _a[1];
return head;
}
return undefined;
};
/**
* Removes the item from the collection by matching its Self link. If not found, returns undefined.
*/
RepresentationUtil.addItemToCollection = function (collection, item) {
if (collection.items) {
collection.items.splice(collection.items.length, 0, item);
}
else {
log.warn('Collection adding new items array, reactive bindings may fail');
collection.items = [item];
}
return collection;
};
/**
* Returns the first item from a collection
*
* @obsolete this should never be used but rather look for the 'current' on links and return resource
*/
RepresentationUtil.current = function (collection) {
if (!collection) {
return undefined;
}
if ((0, instanceOfCollection_1.instanceOfCollection)(collection)) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
var _a = __read(collection.items, 1), head = _a[0];
return head;
}
return undefined;
};
return RepresentationUtil;
}());
exports.RepresentationUtil = RepresentationUtil;
//# sourceMappingURL=representationUtil.js.map