All files / platform/core/src/services/HangingProtocolService/lib displayConstraint.js

0% Statements 0/33
0% Branches 0/18
0% Functions 0/5
0% Lines 0/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                                                                                                                                             
const attributeCache = Object.create(null);
const REGEXP = /^\([x0-9a-f]+\)/;
 
const humanize = text => {
  let humanized = text.replace(/([A-Z])/g, ' $1'); // insert a space before all caps
 
  humanized = humanized.replace(/^./, str => {
    // uppercase the first character
    return str.toUpperCase();
  });
 
  return humanized;
};
 
/**
 * Get the text of an attribute for a given attribute
 * @param  {String} attributeId The attribute ID
 * @param  {Array} attributes   Array of attributes objects with id and text properties
 * @return {String}             If found return the attribute text or an empty string otherwise
 */
const getAttributeText = (attributeId, attributes) => {
  // If the attribute is already in the cache, return it
  if (attributeId in attributeCache) {
    return attributeCache[attributeId];
  }
 
  // Find the attribute with given attributeId
  const attribute = attributes.find(attribute => attribute.id === attributeId);
 
  let attributeText;
 
  // If attribute was found get its text and save it on the cache
  if (attribute) {
    attributeText = attribute.text.replace(REGEXP, '');
    attributeCache[attributeId] = attributeText;
  }
 
  return attributeText || '';
};
 
function displayConstraint(attributeId, constraint, attributes) {
  if (!constraint || !attributeId) {
    return;
  }
 
  const validatorType = Object.keys(constraint)[0];
  if (!validatorType) {
    return;
  }
 
  const validator = Object.keys(constraint[validatorType])[0];
  if (!validator) {
    return;
  }
 
  const value = constraint[validatorType][validator];
  if (value === void 0) {
    return;
  }
 
  let comparator = validator;
  if (validator === 'value') {
    comparator = validatorType;
  }
 
  const attributeText = getAttributeText(attributeId, attributes);
  const constraintText = attributeText + ' ' + humanize(comparator).toLowerCase() + ' ' + value;
 
  return constraintText;
}