Skip to content

Instantly share code, notes, and snippets.

@oojacoboo
Created January 29, 2013 04:43
Show Gist options
  • Select an option

  • Save oojacoboo/4661871 to your computer and use it in GitHub Desktop.

Select an option

Save oojacoboo/4661871 to your computer and use it in GitHub Desktop.
//query our json
var filterUnits = function(units) {
var json = _.clone(units),
searchPattern = null,
keyValueRegex = /(\w*:\w+)/g,
splitSearch = _.without(searchQuery.split(keyValueRegex), "");
function match(s) {
return (s) ? s.match(searchPattern) : false;
}
function containsMatch(object, filter) {
searchPattern = new RegExp('(' + RegExp.escape(filter) + ')', "ig");
return _.chain(object)
.crush()
.reject(_.isUndefined)
.any(match).value();
}
function filterJson(filterStr) {
_.filter(json, function(object) {
return (containsMatch(object, filterStr) ||
($.inArray(filterStr, object.tags) > -1 //check units tag array, -1 is false
|| (filterStr == "Pending Tenants" && object.pendingTenantsCount > 0)
|| (filterStr == "Lacking Management Fee" && object.lackingManagementFee == 1)
|| filterStr.length === 0)
);
});
}
_.each(splitSearch, function(keyValueOrSearch) {
keyValueOrSearch = keyValueOrSearch.trim();
log(keyValueOrSearch);
// if(keyValueOrSearch.match(keyValueRegex))
// var keyValue = keyValueOrSearch.split(/:/);
filterJson(keyValueOrSearch);
});
return json;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment