Permalink
Browse files

Preserves uniqueness of case-insensitive usernames, capitalization of…

… last-used username, and handles Unicode characters.
  • Loading branch information...
1 parent ac8f98d commit 35ff61987bf537578df2763c92b40499bdedcfa6 @ljharb ljharb committed Sep 23, 2012
Showing with 18 additions and 2 deletions.
  1. +18 −2 lib/factoidserv/index.js
View
@@ -9,8 +9,24 @@ var FactoidServer = module.exports = function(filename) {
this.db = new JSONSaver(filename);
};
+var caseInsensitiveDeDupingInsert = function (array, value) {
+ var regex = new RegExp('^' + value + '$', 'i');
+ var found = array.some(function (item, index) {
+ if (regex.test(item)) {
+ array[index] = value;
+ return true;
+ });
+ });
+ if (!found) {
+ array.push(value);
+ }
+};
+
FactoidServer.prototype.learn = function(key, value, username) {
+ if (!username) {
+ username = '';
+ }
var db = this.db.object.factoids;
@@ -24,8 +40,8 @@ FactoidServer.prototype.learn = function(key, value, username) {
return this.learn(db[key].alias, value, username);
}
creator = db[key].creator;
- editors = db[key].editors || [];
- editors.push(username);
+ editors = db[key].editors || editors;
+ caseInsensitiveDeDupingInsert(editors, username);
popularity = db[key].popularity || 0;
}

0 comments on commit 35ff619

Please sign in to comment.