All files / babel-plugin-hyperscript-to-jsx/src utils.js

100% Statements 26/26
92% Branches 23/25
100% Functions 5/5
100% Lines 25/25
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 472x   2x 207x 104x   103x 103x 103x 103x 22x   103x 79x   103x 2x   103x 103x   103x   122x   98x     122x 103x 103x 103x 103x         193x         2x        
const extractClassNamesAndId = require("string-extract-class-names");
 
const getTagAndClassNamesAndId = string => {
  if (/^[A-Za-z0-9]+$/.test(string)) {
    return { tag: string };
  } else {
    const dotIndex = string.indexOf(".");
    const hashIndex = string.indexOf("#");
    let index = string.length;
    if (dotIndex === -1) {
      index = hashIndex;
    }
    if (hashIndex === -1) {
      index = dotIndex;
    }
    if (dotIndex !== -1 && hashIndex !== -1) {
      index = dotIndex > hashIndex ? hashIndex : dotIndex;
    }
    const tag = index !== 0 ? string.substr(0, index) : "div";
    const classesAndIds = extractClassNamesAndId(string);
 
    const className = classesAndIds
      .filter(entry => {
        return entry.startsWith(".");
      })
      .map(clazz => clazz.substr(1, clazz.length))
      .join(" ");
 
    const firstId = classesAndIds.filter(entry => entry.startsWith("#"))[0];
    const id = firstId ? firstId.substr(1, firstId.length) : undefined;
    let result = id ? { tag, id } : { tag };
    result = className ? { ...result, className } : result;
    return result;
  }
};
 
function getOption({ opts }, name, defaultValue = true) {
  return opts[name] === undefined || opts[name] === null
    ? defaultValue
    : opts[name];
}
 
module.exports = {
  getOption,
  getTagAndClassNamesAndId
};