Search code examples
javascriptjquerysearchplaylist

Find Song That Matches Search (Artist and/or Track Name)


I'm working on a search function that finds the song where the search matches the artist name and the track name. If this topic interests you and have an ideal method of solving this your input would be greatly appreciated. This is more of a logic question than a programming knowledge question.

Here is my current code.

searchPlaylist: function(a,b){
    var retArr = [];
    if (typeof a === 'string') a = eval(a);
    $.each(b, function(i,v){
        if (typeof v['id'] !== "string" || typeof v['artist'] !== "string" || typeof v['track'] !== "string") return;
        var artist = v['artist'].replace(/[^a-zA-Z 0-9]+/g,'').replace('   ',' ').replace('  ',' ');
        var track = v['track'].replace(/[^a-zA-Z 0-9]+/g,'').replace('   ',' ').replace('  ',' ');
        if (artist.match(a) || track.match(a)) retArr.push(i);
    });
    aC.searchLength = retArr.length;
    return retArr;
}

And here is how I call the function.

doSearch: function(){
    var val = $.trim($("#sB").val().replace(/[^a-zA-Z 0-9]+/g,'').replace('   ',' ').replace('  ',' '));
    if (1 < val.length && val != search) {
        aC.search = val;
        var rlength = aC.searchLength;
        aC.searchResults = aC.searchPlaylist("/" + aC.search + "/i", aC.playlist);
        if (aC.searchLength > 0) $("#sresultcount").text(aC.searchLength);
        if (rlength != aC.searchLength) {
            aC.loadPlaylist($.grep(aC.playlist, function(v,i){
                return $.inArray(i,aC.searchResults) > -1;
            }));
        }           
    } else if (0 == val.length) {
        aC.search = "";
        aC.searchLength = 0;
        $("#sresultcount").empty();
        aC.loadPlaylist(aC.playlist);
    }
}

The aC.searchPlaylist function is passed a RegEx statement for the search and the second argument is the variable of the array where the playlist is stored.

Here is the structure of the playlist array.

aC.playlist = [
    {"id":"bd6ve0ydHVo","artist":"0SM","track":"The Landing feat. Alex G - Original Mix","img":"http://i.ytimg.com/vi/bd6ve0ydHVo/default.jpg","duration":322},
    {"id":"KIijaPllLNI","artist":"2 Chainz","track":"No Lie - Explicit Version","img":"http://i.ytimg.com/vi/KIijaPllLNI/default.jpg","duration":240},
    {"id":"esBlVulbkQQ","artist":"2 LIVE CREW","track":"We Want Some P--sy","img":"http://i.ytimg.com/vi/esBlVulbkQQ/default.jpg","duration":170},
    {"id":"5sc_nQiuDN0","artist":"2 LIVE CREW","track":"Face Down A-- Up","img":"http://i.ytimg.com/vi/5sc_nQiuDN0/default.jpg","duration":115},
    {"id":"42vxicGNumM","artist":"2 LIVE CREW","track":"Me So Horny","img":"http://i.ytimg.com/vi/42vxicGNumM/default.jpg","duration":284},
    {"id":"42boE4fc5X4","artist":"2 LIVE CREW","track":"Hoochie Mama","img":"http://i.ytimg.com/vi/42boE4fc5X4/default.jpg","duration":180},
    {"id":"sNRa1M39RRY","artist":"2Pac","track":"I Ain't Mad At Cha","img":"http://i.ytimg.com/vi/sNRa1M39RRY/default.jpg","duration":233},
    {"id":"2cjv7hEAytU","artist":"2Pac","track":"Me Against The World","img":"http://i.ytimg.com/vi/2cjv7hEAytU/default.jpg","duration":283},
    {"id":"8p9jSRxguAA","artist":"2Pac","track":"Ambitionz Az A Ridah","img":"http://i.ytimg.com/vi/8p9jSRxguAA/default.jpg","duration":276},
    {"id":"W69SSLfRJho","artist":"2Pac","track":"Life Goes On","img":"http://i.ytimg.com/vi/W69SSLfRJho/default.jpg","duration":302},
    {"id":"W6S7dAsIzIU","artist":"2Pac","track":"All Eyez On Me","img":"http://i.ytimg.com/vi/W6S7dAsIzIU/default.jpg","duration":318},
    {"id":"khkx7yXzGhc","artist":"2Pac","track":"2 Of Amerikaz Most Wanted - (Explicit)","img":"http://i.ytimg.com/vi/khkx7yXzGhc/default.jpg","duration":316}
];

Here is what I'm thinking the results should be.

The search term could be one of the following:

live crew - want some OR live crew want some

I generally prefer the second option because I remove special characters so the hyphen in the first example would get removed.

As you can tell the dilemma once you split the string up into an array based on spaces how do we search in artist or track name. (I don't think it's a good idea to search all available combinations.)

HnS Music Discovery

Thanks in advance. Your opinion will be appreciated.


Solution

  • searchPlaylist: function(a,b){
        var retArr = [], search = a.split(' ');
        $.each(b, function(i,v){
            if (typeof v['id'] !== "string" || typeof v['artist'] !== "string" || typeof v['track'] !== "string") return;
            var artist = v['artist'].replace(/[^a-zA-Z 0-9]+/g,'').replace('   ',' ').replace('  ',' ');
            var track = v['track'].replace(/[^a-zA-Z 0-9]+/g,'').replace('   ',' ').replace('  ',' ');
            var song = artist + " " + track;
            var rg = new RegExp('^(?=.*?'+search.join(")(?=.*?")+')',"i");
            if (song.match(rg)) retArr.push(i);
        });
        aC.searchLength = retArr.length;
        return retArr;
    }