MecLib GitHub issue no. 35: Make cleanupName() more robust

MecLib GitHub issue no. 35: Make cleanupName() more robust

by aaronamran

JavaScript

/* function cleanupName(str) {
  // 
  console.log('original string input is here: ' + str);
  let strList = str.split(/\s+|\);
  let out =""
  console.log("here is strList: " + strList)
  
  strList.forEach(function(st) {
    console.log("here is st: " + st);
    var pos = st.search("_") 
    if (st.length>1 && pos>1) { 
      // remove underscores at wrong places
      st = st.replace(/_/g, '')
      pos = -1}
    if (isNaN(st[0]) == true && st.length>1 && pos===-1) { st = st.substring(0, 1) + "_" + st.substring(1);} // insert an underscore if string is longer than one character
    else if (isNaN(st[0]) == false && st.length>1 && pos===-1) { st = st.substring(0, 1) + "*" + st.substring(1);}
    out = out+st+"*"
  });
  out = out.slice(0, -1); // renove trailing space
  return out
} */

function cleanupName(str) {
  console.log('original string input is here: ' + str);
  let strList = str.split(/\s+|\*/);
  let out = ""
  console.log("here is strList: " + strList)

  strList.forEach(function(st) {
    console.log("here is st: " + st);
    let pos = st.search("_")
    if (st.length > 1 && pos > 1) {
      // remove underscores at wrong places
      st = st.replace(/_/g, '')
      pos = -1
    }
    if (isNaN(st[0]) == true && st.length > 1 && pos === -1) {
      st = st.substring(0, 1) + "_" + st.substring(1);
    } // insert an underscore if string is longer than one character
    else if (isNaN(st[0]) == false && st.length > 1 && pos === -1) {
      st = st.substring(0, 1) + "*" + st.substring(1);
    }
    // should output have * or just empty space? since toTEX() replaces * with empty spaces
    out = out + st + "*"
  });
  out = out.slice(0, -1); // renove trailing space
  return out
}

// should output have * or just empty space? since toTEX() replaces * with empty spaces

console.log(cleanupName("Fa"))
console.log(cleanupName("Fax"))
console.log(cleanupName("Fa_x"))
console.log(cleanupName("F_b"))
console.log(cleanupName("Fa_x...