JSFiddle - React, Tailwind, and code Playground

by pierian_design

JavaScript

var str = 'David';
var toAdd = {
    'av': 'Q', 'vi': 'W', 'i': 'A', 'avi': 'S'
};
var find = Object.keys(toAdd).sort(function(a, b) {
  return b.length - a.length;
});

console.log('777777777', find);

function getEntries(haystack, needle) {
    let pos = 0; // Position Ref
    let result = []; // Final output of all index's.
    let hayStackLower = haystack.toLowerCase();
    let needleLower = needle.toLowerCase();
    
    // Loop to check all occurrences 
    while (hayStackLower.indexOf(needleLower, pos) != -1) {
      result.push(hayStackLower.indexOf(needleLower , pos));
      pos = hayStackLower.indexOf(needleLower , pos) + 1;
    }

    return result;
}

function getCoords(str, whatFind) {
    return whatFind.reduce((aAcc, sFind) => {
        let re = getEntries(str, sFind);
        
        console.log('11111111111111', re);
        
        re.forEach((entry, i) => {
            let isAlreadyIn = aAcc.findIndex(e => { //findLastIndex ???
            
/*
            		if (sFind === 'avi') {
                	console.log(' ');
                  
                  console.log(sFind);
                  console.log(entry);
                  console.log(JSON.parse(JSON.stringify(e)));
                  console.log(JSON.parse(JSON.stringify(aAcc)));
                  
                  console.log(' ');
                }
*/
            
                if (
                  entry >= e.pos && entry <= e.pos + e.key.length - 1 ||
                  entry + sFind.length - 1 >= e.pos && entry + sFind.length - 1 <= e.pos + e.key.length - 1
                ) {
                	return true;
                } else {
                	return false;
                }
            });
            
            let oSubstr = { key: sFind, pos: /*re[i]*/ entry};
            
            if (isAlreadyIn !== -1) {
              if (aAcc[isAlreadyIn].key.length < oSubstr.key.length) {
              	aAcc[isAlreadyIn] = oSubstr;
              }
            } else {
           ...