ArtGateOne / dot2apcmini_old

this code is for control dot2 (DMX) console or software with Akai APC mini (midi controller) required installed nodejs v 14.15.0
2 stars 1 forks source link

Autocolor with not exact Names #3

Open hfuerst opened 11 months ago

hfuerst commented 11 months ago

Hi, great work!

I did a quick and dirty hack for Searching not exact Names. I hope you can implement it (maybe in better Code) in default functions:

// Colornames: Double Words before Single Words, and 2 Letters at the end !!! (White = 3, Warm = 8, Rose = 4) var colornames = ['Black', 'Warm', 'White', 'Deep Red', 'Red', 'Orange', 'Yellow', 'Fern Green', 'Sea Green', 'Green', 'Cyan', 'Lavender', 'Blue', 'Violet', 'Magenta', 'Pink', 'CTO', 'CTB', 'Grey', 'Gr', 'Sw', 'Ws', 'Wm', 'Rt', 'Or', 'Ge', 'Gn', 'Tk', 'Cy', 'Bl', 'Vi', 'Ma', 'Pk']; var colorcodes = [ 0, 8, 3, 121, 5, 9, 13, 17, 25, 21, 33, 41, 45, 49, 53, 57, 108, 93, 1, 1, 0, 3, 8, 5, 9, 13, 21, 33, 33, 45, 49, 53, 57];

// ---------------------------------------------

////////////////////////////////////////////////////////////////////////////////////////////////////////////// // HF edit

// Original / function findColorIndex(colorName) { //const names = ['Black', 'White', 'Red', 'Orange', 'Yellow', 'Fern Green', 'Green', 'Sea Green', 'Cyan', 'Lavender', 'Blue', 'Violet', 'Magenta', 'Pink', 'CTO', 'CTB', 'Grey', 'Deep Red']; //const index = names.indexOf(colorName); const index = colornames.indexOf(colorName); return index; } /

function findColorIndex(colorName) { //const names = ['Black', 'White', 'Red*', 'Orange', 'Yellow', 'Fern Green', 'Green', 'Sea Green', 'Cyan', 'Lavender', 'Blue', 'Violet', 'Magenta', 'Pink', 'CTO', 'CTB', 'Grey', 'Deep Red']; //const index = names.indexOf(colorName);

//---------------------------------------------------------------
// Original
//const index = colornames.indexOf(colorName);

//------------------------------------
// nocase only
/*
const index = arr.findIndex(
    item => colorName.toLowerCase() === colornames.toLowerCase()
);
return index;
*/

//------------------------------------
// nocase and Wildcard
var buttonLabel = colorName.toLowerCase();
var index = -1; 
for (n = 0; n < colornames.length; n++) {
    var Search = colornames[n].toLowerCase();
    //------------------------
    if (Search.length > 2 ){
            if (matchRuleExpl(buttonLabel, "*" + Search + "*")){
                //
                index = n;
                break;
            }
    }
    //------------------------
    else {
        if (buttonLabel.length > 3){
            if (matchRuleExpl(buttonLabel, "*" + Search + "*")){
                if (matchRuleExpl(buttonLabel, "* " + Search)){
                    //
                    index = n;
                    break;
                };
                if (matchRuleExpl(buttonLabel, Search + " *")){
                    //
                    index = n;
                    break;
                };
            };
        }
        else{
            if (matchRuleExpl(buttonLabel, Search)){
                //
                index = n;
                break;
            };
        }
    }
    //------------------------

    /*
    //debug
    if (colorName == "Mitte Zentriert"){
        console.log('');
        console.log('n............: '+n);
        //console.log('');
        console.log('colorName....: '+colorName);
        console.log('buttonLabel.: '+buttonLabel);
        //console.log('');
        console.log('colornames[n]: '+colornames[n]);
        console.log('Search.......: '+Search);
        console.log('index........: '+index+'\n\n');
        alert('stop');
    }
    */

}

return index;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

// HF edit // found at // https://stackoverflow.com/questions/26246601/wildcard-string-comparison-in-javascript

function matchRuleExpl(str, rule) { // for this solution to work on any string, no matter what characters it has var escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|[]\/\])/g, "\$1");

// "."  => Find a single character, except newline or line terminator
// ".*" => Matches any string that contains zero or more characters
rule = rule.split("*").map(escapeRegex).join(".*");

// "^"  => Matches any string with the following at the beginning of it
// "$"  => Matches any string with that in front at the end of it
rule = "^" + rule + "$"

//Create a regular expression object for matching string
var regex = new RegExp(rule);

//Returns true if it finds a match, otherwise it returns false
return regex.test(str);

} / Examples alert( "1. " + matchRuleShort("bird123", "bird") + "\n" + "2. " + matchRuleShort("123bird", "bird") + "\n" + "3. " + matchRuleShort("123bird123", "bird") + "\n" + "4. " + matchRuleShort("bird123bird", "birdbird") + "\n" + "5. " + matchRuleShort("123bird123bird123", "birdbird") + "\n" + "6. " + matchRuleShort("s[pe]c 3 re$ex 6 cha^rs", "s[pe]cre$excha^rs") + "\n" + "7. " + matchRuleShort("should not match", "should noooot match") + "\n" ); */