Created
August 1, 2011 12:42
-
-
Save dennishall/1118053 to your computer and use it in GitHub Desktop.
Include/Exclude zipcode ranges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('ul.models li.model').each(function(i, node) { | |
var li = $(node); | |
// <li data-excludes="88995, 90000-99999"> | |
var includes = li.attr('data-includes').replace(' ', '').split(','); | |
var excludes = li.attr('data-excludes').replace(' ', '').split(','); | |
if(!includes.length){ | |
includes = [[0, 99999]]; | |
} else { | |
// normalize | |
$.each(includes, function(i, item){ | |
item = item.split('-'); | |
if(item.length == 1){ | |
item = [item[0], item[0]]; | |
} | |
includes[i] = [1*item[0], 1*item[1]]; | |
}); | |
} | |
includes.sort(function(a, b){ | |
return a[0] - b[0]; | |
}); | |
$.each(excludes, function(i, excludedZip) { | |
var excludeRange = excludedZip.split('-'); | |
if (excludeRange.length == 1) { | |
excludeRange = [excludeRange[0], excludeRange[0]]; | |
} | |
excludeRange = [1*excludeRange[0], 1*excludeRange[1]]; | |
// we have a range: excludeRange[0] - excludeRange[1] | |
$.each(includes, function(i, includeRange){ | |
if(excludeRange[1] < includeRange[1]){ | |
var newIncludeRange = [excludeRange[1] + 1, includeRange[1]]; | |
includeRange[1] = excludeRange[0] + 1; | |
if(i){ | |
includes[i-1][1] = includeRange[1] - 1; | |
} | |
} | |
}); | |
}); | |
li.css('display', 'none'); | |
$.each(includes, function(i, range) { | |
var isIncluded = userZip >= range[0] && userZip <= range[1]; | |
if(isIncluded){ | |
li.css({ | |
'display' : 'block' | |
}); | |
} | |
}); | |
}); // end $('ul.models li.model').each(...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment