Permalink
Browse files

BUGFIX: server side hack to work better with accented characters

  • Loading branch information...
jonenst committed May 10, 2012
1 parent c1047da commit 83c348cfd887bdb8d043467d758768ab85a65e11
@@ -231,13 +231,35 @@ function increment_string_key(str) {
return str+'\ufff0';
}
+//Copy paste from search/lists/intersect-search.js
+//TODO find a solution
function search_nonmatch_field(str) {
if (str == "nom")
return "prenom"
else
return "nom";
}
+function remove_accents(str) {
+ var changes = [
+ { base: "a", letters: /[àáâãäå]/g },
+ { base: "e", letters: /["éèêë"]/g },
+ { base: "i", letters: /[ìíîï]/g },
+ { base: "o", letters: /["òóôõö"]/g },
+ { base: "u", letters: /[ùúûü]/g },
+ { base: "y", letters: /[ýÿ]/g },
+ { base: "oe", letters: /œ/g },
+ { base: "ae", letters: /æ/g },
+ { base: "c", letters: /ç/g },
+ { base: "n", letters: /ñ/g }
+ ];
+ for(var i=0; i<changes.length; i++) {
+ str = str.replace(changes[i].letters, changes[i].base);
+ }
+ return str;
+}
+
+
function cat_from_year(year, is_woman) {
var date = new Date();
var age = date.getYear() - year;
@@ -3,20 +3,17 @@ function(cb, wtf, request) {
var split = request.trim().split(" ");
var key = split[0];
- app.db.view("search/contestants-search", {
+ var opts = {
+ my_limit: 10,
startkey: key,
- limit: 10,
- endkey: increment_string_key(key),
+ endkey: increment_string_key(key)
+ };
+ if (split.length>1) {
+ opts.term = split[1];
+ }
+ app.db.list("search/intersect-search", "contestants-search", opts, {
success: function(data) {
- var res;
- if (split.length > 1) {
- var regexp = new RegExp(split[1], "i");
- res = _.filter(data.rows, function(row) {
- return regexp.test(row.value[search_nonmatch_field(row.value.match)]);
- });
- } else {
- res = data.rows;
- }
+ var res = data.rows;
res.request = request;
cb(res);
}});
@@ -9,7 +9,7 @@
<li data-bib="{{dossard}}"
data-name="{{nom}}"
data-firstname="{{prenom}}">
- <p><b>{{nom}}</b> {{prenom}}&nbsp: dossard {{dossard}}</p>
+ <p>{{nom}} {{prenom}}&nbsp: dossard {{dossard}}</p>
</li>
{{/names}}
</ol>
@@ -5,30 +5,38 @@ function() {
source: function(request, response) {
var split = request.term.trim().split(" ");
var term = split[0];
- app.db.view("search/contestants-search", {
- limit: 10,
+ var opts = {
+ my_limit: 10,
startkey: term,
- endkey: increment_string_key(term),
+ endkey: increment_string_key(term)
+ };
+ if (split.length>1) {
+ opts.term = split[1];
+ }
+ app.db.list("search/intersect-search", "contestants-search", opts, {
success: function(data) {
- var regexp = new RegExp("("+term+")","i");
- var regexp2 = new RegExp("( "+split[1]+")","i");
+ var regexp = new RegExp(remove_accents(term),"i");
+ var regexp2;
+ if (split.length>1) {
+ regexp2 = new RegExp(remove_accents(split[1]),"i");
+ }
response(data.rows.map(function(row) {
- var value = row.value[row.value.match] + " " +
- row.value[search_nonmatch_field(row.value.match)];
- var label = value.replace(regexp, "<strong>$1</strong>");
+ var firstvalue = row.value[row.value.match];
+ var secondvalue= row.value[search_nonmatch_field(row.value.match)];
+ var value = firstvalue + " " + secondvalue;
+ var label = "<strong>" + firstvalue.substr(0, split[0].length) +
+ "</strong>" + firstvalue.substr(split[0].length);
if (split.length>1) {
- var label2 = label.replace(regexp2, "<strong>$1</strong>");
- if (label2 != label) {
- label = label2;
- } else {
- return {};
- }
+ label = label + " " + "<strong>" + secondvalue.substr(0, split[1].length) + "</strong>" +
+ secondvalue.substr(split[1].length);
+ } else {
+ label = label + " " + secondvalue;
}
return {
label: label,
value: value
};
- }).filter(function(data) { return data.value != undefined; }));
+ }));
}
});
}
@@ -0,0 +1,55 @@
+function(head, req) {
+
+ function search_nonmatch_field(str) {
+ if (str == "nom")
+ return "prenom"
+ else
+ return "nom";
+ }
+
+ function remove_accents(str) {
+ var changes = [
+ { base: "a", letters: /[àáâãäå]/g },
+ { base: "e", letters: /["éèêë"]/g },
+ { base: "i", letters: /[ìíîï]/g },
+ { base: "o", letters: /["òóôõö"]/g },
+ { base: "u", letters: /[ùúûü]/g },
+ { base: "y", letters: /[ýÿ]/g },
+ { base: "oe", letters: /œ/g },
+ { base: "ae", letters: /æ/g },
+ { base: "c", letters: /ç/g },
+ { base: "n", letters: /ñ/g }
+ ];
+ for(var i=0; i<changes.length; i++) {
+ str = str.replace(changes[i].letters, changes[i].base);
+ }
+ return str;
+ }
+
+ start({
+ "headers": {
+ "Content-Type": "text/plain"
+ }
+ });
+
+ var other_term = req.query.term;
+ var limit = req.query.my_limit;
+ var regexp;
+ if (other_term != undefined) {
+ regexp = new RegExp(remove_accents(other_term), "i");
+ }
+ var row;
+ var arr = [];
+ var i = 0;
+ while(row = getRow()) {
+ if (regexp == undefined || regexp.test(remove_accents(row.value[search_nonmatch_field(row.value.match)]))) {
+ arr.push(row);
+ i++;
+ if (limit && i==limit)
+ break;
+ }
+ }
+
+ var res = { rows: arr };
+ return JSON.stringify(res);
+}

0 comments on commit 83c348c

Please sign in to comment.