diff --git a/bower.json b/bower.json index 26f8519..561a8f5 100644 --- a/bower.json +++ b/bower.json @@ -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.3.4", + "version": "0.4.0", "license": "Apache License, Version 2.0", "readmeFilename": "README.md", "repository": { diff --git a/package.json b/package.json index 4807089..14ac665 100644 --- a/package.json +++ b/package.json @@ -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.3.4", + "version": "0.4.0", "author": "Brian Reavis ", "main": "./sifter.js", "repository": { diff --git a/sifter.js b/sifter.js index a3268f8..b54ce0c 100644 --- a/sifter.js +++ b/sifter.js @@ -221,7 +221,7 @@ * @param {object} result * @return {mixed} */ - get_field = function(name, result) { + get_field = function(name, result) { if (name === '$score') return result.score; return self.items[result.id][name]; }; @@ -391,8 +391,8 @@ if (typeof a === 'number' && typeof b === 'number') { return a > b ? 1 : (a < b ? -1 : 0); } - a = String(a || '').toLowerCase(); - b = String(b || '').toLowerCase(); + a = asciifold(String(a || '')); + b = asciifold(String(b || '')); if (a > b) return 1; if (b > a) return -1; return 0; @@ -425,21 +425,44 @@ }; var DIACRITICS = { - 'a': '[aÀÁÂÃÄÅàáâãäåĀā]', + 'a': '[aÀÁÂÃÄÅàáâãäåĀāąĄ]', 'c': '[cÇçćĆčČ]', 'd': '[dđĐďĎ]', - 'e': '[eÈÉÊËèéêëěĚĒē]', + 'e': '[eÈÉÊËèéêëěĚĒēęĘ]', 'i': '[iÌÍÎÏìíîïĪī]', - 'n': '[nÑñňŇ]', + 'l': '[lłŁ]', + 'n': '[nÑñňŇńŃ]', 'o': '[oÒÓÔÕÕÖØòóôõöøŌō]', 'r': '[rřŘ]', - 's': '[sŠš]', + 's': '[sŠšśŚ]', 't': '[tťŤ]', 'u': '[uÙÚÛÜùúûüůŮŪū]', 'y': '[yŸÿýÝ]', - 'z': '[zŽž]' + 'z': '[zŽžżŻźŹ]' }; + var asciifold = (function() { + var i, n, k, chunk; + var foreignletters = ''; + var lookup = {}; + for (k in DIACRITICS) { + if (DIACRITICS.hasOwnProperty(k)) { + chunk = DIACRITICS[k].substring(2, DIACRITICS[k].length - 1); + foreignletters += chunk; + for (i = 0, n = chunk.length; i < n; i++) { + lookup[chunk.charAt(i)] = k; + } + } + } + var regexp = new RegExp('[' + foreignletters + ']', 'g'); + return function(str) { + return str.replace(regexp, function(foreignletter) { + return lookup[foreignletter]; + }).toLowerCase(); + }; + })(); + + // export // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sifter.min.js b/sifter.min.js index a85e3d4..4a07269 100644 --- a/sifter.min.js +++ b/sifter.min.js @@ -1,2 +1,2 @@ /*! sifter.js | https://github.com/brianreavis/sifter.js | Apache License (v2) */ -(function(r,t){if(typeof define==="function"&&define.amd){define(t)}else if(typeof exports==="object"){module.exports=t()}else{r.Sifter=t()}})(this,function(){var r=function(r,t){this.items=r;this.settings=t||{diacritics:true}};r.prototype.tokenize=function(r){r=n(String(r||"").toLowerCase());if(!r||!r.length)return[];var t,e,o,s;var u=[];var a=r.split(/ +/);for(t=0,e=a.length;t0){o.items.push({score:i,id:e})}})}else{e.iterator(e.items,function(r,t){o.items.push({score:1,id:t})})}s=e.getSortFunction(o,t);if(s)o.items.sort(s);o.total=o.items.length;if(typeof t.limit==="number"){o.items=o.items.slice(0,t.limit)}return o};var t=function(r,t){if(typeof r==="number"&&typeof t==="number"){return r>t?1:rt)return 1;if(t>r)return-1;return 0};var e=function(r,t){var e,n,i,o;for(e=1,n=arguments.length;e0){o.items.push({score:i,id:e})}})}else{e.iterator(e.items,function(r,t){o.items.push({score:1,id:t})})}u=e.getSortFunction(o,t);if(u)o.items.sort(u);o.total=o.items.length;if(typeof t.limit==="number"){o.items=o.items.slice(0,t.limit)}return o};var t=function(r,t){if(typeof r==="number"&&typeof t==="number"){return r>t?1:rt)return 1;if(t>r)return-1;return 0};var e=function(r,t){var e,n,i,o;for(e=1,n=arguments.length;e