All files / src/lib util.js

100% Statements 21/21
100% Branches 25/25
100% Functions 8/8
100% Lines 21/21
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    498x     498x 1x     498x 13x     485x         18x   18x 1x     18x         8x 2x           2x 2x         8x         333x           47x 47x 47x 282x   47x                        
// Extract the string contained in a cell object.
function getCellValue(cell) {
  let value = cell.f || cell.v || cell;
 
  // Extract cell value while avoiding array values.
  if (value instanceof Array) {
    value = value.join('');
  }
 
  if (typeof value === 'object') {
    return '';
  }
 
  return `${value}`.replace(/^\s+|\s+$/, '');
}
 
// Extract a DOM element from a possible jQuery blob.
function extractElement(blob) {
  let el = blob;
 
  if (typeof el === 'object' && el.jquery && el.length) {
    el = el[0];
  }
 
  return (el && el.nodeType && el.nodeType === 1) ? el : null;
}
 
// Append HTML output to DOM.
function append(target, html) {
  if (target && target.insertAdjacentHTML) {
    target.insertAdjacentHTML('beforeEnd', html);
  }
}
 
// Return true if the DOM element has the specified class.
function hasClass(el, className) {
  const classes = ` ${el.className} `;
  return classes.indexOf(` ${className} `) !== -1;
}
 
// Return true if the DOM element is a table.
function isTable(el) {
  return el && el.tagName === 'TABLE';
}
 
// Wrap a string in tag.
function wrapTag(str, tag) {
  return `<${tag}>${str}</${tag}>`;
}
 
// Default row template: Output a row object as an HTML table row. Use "td"
// for table body row, "th" for table header rows.
function toHTML(row) {
  const tag = row.num ? 'td' : 'th';
  let html = '';
  Object.keys(row.cells).forEach((key) => {
    html += wrapTag(row.cells[key], tag);
  });
  return wrapTag(html, 'tr');
}
 
export {
  append,
  extractElement,
  getCellValue,
  hasClass,
  isTable,
  toHTML,
  wrapTag,
};