Created
January 29, 2013 05:11
-
-
Save oojacoboo/4661969 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| parseUnits = function() { | |
| if(jsonResultsReady == false) return; | |
| if(currentResult && nResults) | |
| if(currentResult >= nResults) return; | |
| //for pager: | |
| var showStart = currentResult + 1, | |
| showEnd = currentResult + itemsPerResponse; | |
| var counter = 0, | |
| unitList = ""; | |
| //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 hasKeyValueMatch(key, value) { | |
| return _.filter(json, function(object) { | |
| return (object.key == value); | |
| }); | |
| } | |
| function filterJson(filterStr) { | |
| return _.filter(json, function(object) { | |
| return (containsMatch(object, filterStr) || | |
| (_.contains(filterStr, object.tags) //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(); | |
| if(keyValueOrSearch.match(keyValueRegex)) { | |
| var keyValue = keyValueOrSearch.split(/:/); | |
| hasKeyValueMatch(keyValue[0], keyValue[1]); | |
| } else { | |
| json = filterJson(keyValueOrSearch); | |
| } | |
| }); | |
| return json; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment