semantic-network
Version:
A utility library for manipulating a list of links that form a semantic interface to a network of resources.
102 lines • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkRelConvertUtil = void 0;
var semantic_link_1 = require("semantic-link");
var LinkRelConvertUtil = exports.LinkRelConvertUtil = /** @class */ (function () {
function LinkRelConvertUtil() {
}
/**
* Takes an array of potentially camel-cased strings and only returns those that have a dash in
* the form
*
* @example
* [questionType, type] --> [question-type]
*
* @param {string[]} array
* @return {string[]}
*/
LinkRelConvertUtil.filterCamelToDash = function (array) {
return (array || [])
.filter(function (item) { return LinkRelConvertUtil.NameRegex.test(item); })
.map(function (item) { return LinkRelConvertUtil.replaceToDash(item); });
};
/**
* Takes an array of potentially camel-cased strings and returns all in dash form
*
* @example
* [questionType, type] --> [question-type, type]
*
* @param {string[]} array
* @return {string[]}
*/
LinkRelConvertUtil.camelToDash = function (array) {
if (typeof array === 'string') {
return LinkRelConvertUtil.replaceToDash(array);
}
return array.map(function (item) { return LinkRelConvertUtil.replaceToDash(item); });
};
/**
* Takes a string and returns any dash string as camel-cased string
*
* @example
* question-type --> questionType
* type --> type
*/
LinkRelConvertUtil.dashToCamel = function (str) {
return str.replace(/(-[a-z])/g, function ($1) { return $1.toUpperCase().replace(LinkRelConvertUtil.DASH, LinkRelConvertUtil.EMPTY); });
};
/**
* Takes a string or a Regexp and makes camel cased strings.
*
* @example
*
* test -> test
* /test/ -> test
* /test/g -> test
* /create-form/ -> createForm
* 'create-form' -> createForm
*
* @param rel relationship that will become the field name
* @returns field name
*/
LinkRelConvertUtil.relTypeToCamel = function (rel, includeTitle) {
if (includeTitle === void 0) { includeTitle = false; }
var name = LinkRelConvertUtil.relToCamel(rel);
if (includeTitle && (0, semantic_link_1.instanceOfLinkSelector)(rel) && 'title' in rel) {
var title = rel.title;
if (typeof title === 'string') {
var titleCamelCase = LinkRelConvertUtil.dashToCamel(title);
return name + titleCamelCase.charAt(0).toUpperCase() + titleCamelCase.slice(1);
}
}
return name;
};
LinkRelConvertUtil.relToCamel = function (rel) {
if (!rel) {
// broken or at least log an warning
return '';
}
if ((0, semantic_link_1.instanceOfLinkSelector)(rel)) {
rel = rel.rel;
}
if (typeof rel === 'string') {
// broken. can't cater for 'create-form' - return dashToCamel
return LinkRelConvertUtil.dashToCamel(rel);
}
if (rel instanceof RegExp) {
// remove the regexp aspects eg /test/gi -> test
var str = rel.toString().replace(/\/[gi]*/g, LinkRelConvertUtil.EMPTY);
return LinkRelConvertUtil.dashToCamel(str);
}
// User has passed in a set of link rels and without resource which to pick is indeterminate
throw new Error("Rel type of array not parsable to be converted: '[".concat(rel, "]'"));
};
LinkRelConvertUtil.replaceToDash = function (item) {
return item.replace(LinkRelConvertUtil.NameRegex, function ($1) { return LinkRelConvertUtil.DASH + $1.toLowerCase(); });
};
LinkRelConvertUtil.NameRegex = /([A-Z])/g;
LinkRelConvertUtil.EMPTY = '';
LinkRelConvertUtil.DASH = '-';
return LinkRelConvertUtil;
}());
//# sourceMappingURL=linkRelConvertUtil.js.map