@gatekeeper_technology/report-utils
Version:
Gatekeeper's pdf/email Utils - shared in NPM
80 lines (79 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sortArrayByField = exports.formatText = exports.getDisplayLabel = exports.getDisplayValue = void 0;
/**
* Returns the display value of single choice integers and single choice
* This could be done better with the "?." property.
*/
function getDisplayValue(object, field) {
if (object == null || field == null)
return null;
if (object[field] === undefined)
return null; // Checking for undefined specifically.
if (object.type == null || object.type.attributes == null)
return null; // Check that object is DB object.
if (object[field] == null)
return ""; // If the value is null, return empty string.
const object_attribute = object.type.attributes[field];
if (object_attribute == null)
return "";
const field_object_props = object_attribute.type;
if (field_object_props == null)
return "";
const is_single_choice = field_object_props.options != null;
if (is_single_choice) {
const field_props = field_object_props.options[object[field]];
if (field_props == null) {
return "";
}
return field_props.label;
}
else {
return object[field];
}
}
exports.getDisplayValue = getDisplayValue;
/**
* This could be done better with the "?." property.
*/
function getDisplayLabel(object, field) {
var _a;
if (((_a = object === null || object === void 0 ? void 0 : object.type) === null || _a === void 0 ? void 0 : _a.attributes) == null || field == null || object[field] == null)
return "";
const object_attribute = object.type.attributes[field];
if (object_attribute == null)
return "";
return object_attribute.label;
}
exports.getDisplayLabel = getDisplayLabel;
function formatText(text, type = "lower") {
if (typeof text !== "string")
return text;
if (!["lower", "higher"].includes(type))
return text;
if (type === "lower") {
return text.trim().toLowerCase().replace(/[\s\/\*]+/g, "_");
}
else {
return text.trim().toUpperCase().replace(/[\s\/\*]+/g, "_");
}
}
exports.formatText = formatText;
function sortArrayByField(object_array, field_name, sort_direction = 1) {
if (!Array.isArray(object_array))
return object_array;
return object_array.sort((a, b) => {
const valueA = a[field_name];
const valueB = b[field_name];
if (valueA > valueB) {
return sort_direction;
}
else if (valueA < valueB) {
return -sort_direction;
}
else {
return 0;
}
});
}
exports.sortArrayByField = sortArrayByField;