UNPKG

siren-types

Version:

A bunch of Siren types defined in TypeScript plus some useful functions

67 lines (66 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isEmbeddedRepr = exports.isEmbeddedLink = exports.collectSelves = exports.isA = void 0; /** Shorthand function */ function isA(e, className, ...additionalClasses) { const names = [className, ...additionalClasses]; return names.some(name => e.class.indexOf(name) >= 0); } exports.isA = isA; /** * Fetch (all) self relations. Only the href is included here because these * might come from a Link, EmbeddedLink or EmbeddedEntity (very unlikely). * @param s */ function collectSelves(e) { let ret = []; if (e.links) { for (let i = 0; i < e.links.length; i++) { // istanbul ignore else if (e.links[i].rel.indexOf("self") >= 0) { ret.push(e.links[i].href); } } } if (e.entities) { for (let i = 0; i < e.entities.length; i++) { const entity = e.entities[i]; if (isEmbeddedLink(entity)) { // istanbul ignore else if (entity.rel.indexOf("self") >= 0) { ret.push(entity.href); } } else { // From the spec: // // Root entities and sub-entities that are embedded representations // SHOULD contain a links collection with at least one item contain // a rel value of self and an href attribute with a value of the // entity's URI. // istanbul ignore else if (entity.rel.indexOf("self") >= 0) { ret = [...ret, ...collectSelves(entity)]; } } } } return ret; } exports.collectSelves = collectSelves; /** * If you use this function in a conditional statement, the TypeScript compiler * will know to narrow the type of the argument based on the result. */ function isEmbeddedLink(s) { return s.hasOwnProperty("href"); } exports.isEmbeddedLink = isEmbeddedLink; /** * If you use this function in a conditional statement, the TypeScript compiler * will know to narrow the type of the argument based on the result. */ function isEmbeddedRepr(s) { return !s.hasOwnProperty("href"); } exports.isEmbeddedRepr = isEmbeddedRepr;