Skip to content

Commit

Permalink
Released 0.5.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Mar 7, 2016
1 parent ff4a53f commit 964c46f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sifter",
"keywords": ["search","filter","sift","data","results","match","sort","autocomplete"],
"description": "A library for textually searching arrays and hashes of objects by property (or multiple properties). Designed specifically for autocomplete.",
"version": "0.4.5",
"version": "0.5.0",
"license": "Apache License, Version 2.0",
"readmeFilename": "README.md",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"autocomplete"
],
"description": "A library for textually searching arrays and hashes of objects by property (or multiple properties). Designed specifically for autocomplete.",
"version": "0.4.5",
"version": "0.5.0",
"license": "Apache-2.0",
"author": "Brian Reavis <brian@thirdroute.com>",
"main": "./sifter.js",
Expand Down
24 changes: 20 additions & 4 deletions sifter.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@
* @returns {function}
*/
Sifter.prototype.getScoreFunction = function(search, options) {
var self, fields, tokens, token_count;
var self, fields, tokens, token_count, nesting;

self = this;
search = self.prepareSearch(search, options);
tokens = search.tokens;
fields = search.options.fields;
token_count = tokens.length;
nesting = search.options.nesting;

/**
* Calculates how close of a match the
Expand Down Expand Up @@ -157,12 +158,12 @@
}
if (field_count === 1) {
return function(token, data) {
return scoreValue(data[fields[0]], token);
return scoreValue(getattr(data, fields[0], nesting), token);
};
}
return function(token, data) {
for (var i = 0, sum = 0; i < field_count; i++) {
sum += scoreValue(data[fields[i]], token);
sum += scoreValue(getattr(data, fields[i], nesting), token);
}
return sum / field_count;
};
Expand Down Expand Up @@ -223,7 +224,7 @@
*/
get_field = function(name, result) {
if (name === '$score') return result.score;
return self.items[result.id][name];
return getattr(self.items[result.id], name, options.nesting);
};

// parse options
Expand Down Expand Up @@ -412,6 +413,21 @@
return a;
};

/**
* A property getter resolving dot-notation
* @param {Object} obj The root object to fetch property on
* @param {String} name The optionally dotted property name to fetch
* @param {Boolean} nesting Handle nesting or not
* @return {Object} The resolved property value
*/
var getattr = function(obj, name, nesting) {
if (!obj || !name) return;
if (!nesting) return obj[name];
var names = name.split(".");
while(names.length && (obj = obj[names.shift()]));
return obj;
};

var trim = function(str) {
return (str + '').replace(/^\s+|\s+$|/g, '');
};
Expand Down
2 changes: 1 addition & 1 deletion sifter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 964c46f

Please sign in to comment.