the jquery effect #2743

Closed
LeonFedotov opened this Issue Feb 28, 2014 · 8 comments

Comments

Projects
None yet
5 participants

When searching for jquery or any other library for that matter, backbone is another example, you get a long list of plugins before you actually find jquery.js
So I thought - maybe it would be smart to sort the results in such a way where it will always show "exact" match first and all the others sorted alphabetically.
thoughts?

Bump. I actually came here to post this same issue and was glad to see someone else sees this as a problem.

Something like this for the sort should suffice:

function criterion(query){
  return function(a,b){
    var aMatchStart = a.toLowerCase().indexOf(query.toLowerCase()) === 0;
    var bMatchStart = b.toLowerCase().indexOf(query.toLowerCase()) === 0;
    if(a.toLowerCase()===query) return -1;
    if(b.toLowerCase()===query) return 1;
    if(aMatchStart){
      // if both match, just use plain string compare
      if(bMatchStart) return (a<b)?-1:1;
      // if only A matches, then sort that one first
      return -1;
    }else{
      // if only B matches, then sort that one first
      if(bMatchStart) return 1;
      // if neither string matches, use plain string compare
      return (a<b)?-1:1;
    }

  }
}

So then you can sort like so:

var libs = [
    'ICanHaz.js',
    'anythingslider',
    'jQuery-Geolocation',
    'jQuery.dotdotdot',
    'jEditable.js',
    'jquery',
    'owl-carousel',
    'superfish',
    'tooltipster'];
libs.sort(criterion('jquery'))

This returns

["jquery", 
 "jQuery-Geolocation", 
 "jQuery.dotdotdot", 
 "ICanHaz.js", 
 "anythingslider", 
 "jEditable.js", 
 "owl-carousel", 
 "superfish", 
 "tooltipster"]

My solution probably isn't optimal but I thought I'd offer it as a starting point.

My thoughts were somewhere around sorting, then /^jquery$/i cut it out of the list, and prepend it.
Looking at the source the list of libs are loaded from the server in a certain order and then just toggled visibility with jquery selectors, this means that it is not such an easy task to accomplish but its possible.

Owner

ryankirkman commented Feb 28, 2014

Great suggestions, thanks guys :)

I've got a fix in mind for this. I just need to block off the time to
implement it.

On Fri, Feb 28, 2014 at 9:54 AM, Leon Fedotov notifications@github.comwrote:

My thoughts were somewhere around sorting, then /^jquery$/i cut it out of
the list, and prepend it.
Looking at the source the list of libs are loaded from the server in a
certain order and then just toggled visibility with jquery selectors, this
means that it is not such an easy task to accomplish but its possible.

Reply to this email directly or view it on GitHubhttps://github.com/cdnjs/cdnjs/issues/2743#issuecomment-36376555
.

Great that would be awesome :)

Contributor

nitriques commented Apr 22, 2014

+1 on this.

Owner

ryankirkman commented Apr 22, 2014

This is now fixed (we've had it in for a week or two): http://cdnjs.com/

The fix was made possible by: http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance for those interested in the technical aspect

Owner

ryankirkman commented Apr 22, 2014

E.g. try searching for jquery - it's now the top result

Contributor

nitriques commented Apr 22, 2014

Great use of the Levenshtein distance. +1 on this x 100

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment