arvgta / ajaxify

Ajaxify - The Ajax Plugin
https://4nf.org/
274 stars 124 forks source link

Create new OO class "Hints" #184

Closed arvgta closed 4 years ago

arvgta commented 4 years ago

Please consider this function, which currently helps handle all variants of "hints" employed by the user:

function _searchHints(txt, hints) { //search for text in given hints, which may be array of strings or comma separated string
    if (!txt || !hints) return; //validate both are given - otherwise quick return
    if (typeof hints === "string") hints = hints.split(", "); //from here on only an array is allowed

    for (var i = 0; i < hints.length; i++) //search hints array
        if (txt.iO(hints[i])) return true; //if single hint found within passed text - return true
}

...which is far from ideal, mostly because the internal array is built on the fly each time.

Instead, I would like to replace this helper function with an object-oriented and thus persistent little class, that has two phases:

  1. Initialisation: Create the internal array - only once
  2. Search hints: Determine, whether the URL passed is addressed by any hints specified in the previous step by the user

I've made this draft:

function Hints(hints) {  var myHints = (typeof hints === 'string' && hints.length > 0) ? hints.split(", ") : false; //hints are passed as a comma separated string
    this.find = function(txt) {
        if (!txt || !myHints) return; //validate both are given - otherwise quick return
        return myHints.some( function(h) { return txt.iO(h) } ); // iterate through items, on first positive match return true
    };
}

...which has been successfully tested against:

Please see changes, where calling code has also been updated...


Closing, because several tests were performed successfully. Will re-open if any test fails...