Search code examples
coldfusionlucee

islistinlist function for coldfusion 11


I have this function and running flawless on coldfusion 2016 and lucee but wen i run in cf11, it fails.

i am trying to understand how can i make it work with cf11

private any function isListInList(list1, list2, returnBoolean = true){
        var rv = false; 
        var matches = [];
        listToArray(list1).each(
            function(element) { 
                if(listToArray(list2).containsNoCase(element)){
                    rv = true;
                    arrayAppend(matches, element);
                }
            }
        );
        return arguments.returnBoolean ? rv : matches;
    }

Solution

  • This version of your function will compile and run on CF11

    private any function isListInList(list1, list2, returnBoolean = true){
        var rv = false; 
        var matches = [];
        var arr1 = listToArray(list1);
        var arr2 = listToArray(list2);
        arr1.each(
            function(element) { 
                if(arr2.findNoCase(element)){
                    rv = true;
                    arrayAppend(matches, element);
                }
            }
        );
        return arguments.returnBoolean ? rv : matches;
    }
    

    This version that uses a for() loop will be optimized for large arrays since it will short circuit the loop when it finds a match and you just want the boolean back.

    private any function isListInList(list1, list2, returnBoolean = true){
        var arr1 = listToArray(list1);
        var arr2 = listToArray(list2);
        var rv = false; 
        var matches = [];
        for( var element in arr1 )  {
            if(arr2.findNoCase(element)){
                if( returnBoolean ) {
                    return true;
                } else {
                    arrayAppend(matches, element);
                }
            }
        }
        return arguments.returnBoolean ? false : matches;
    }