Example #1
0
File: rand.js Project: moos/wordpos
    function collector(key, nextKey, index, startsWith, num, callback, buffer) {
      var lines = buffer.toString().split('\n'),
        matches = lines.map(function (line) {
          return line.substring(0, line.indexOf(' '));
        });
      //console.log(' got lines for key ', key, lines.length);

      // we got bunch of matches for key - now search within for startsWith
      if (startsWith !== key) {
        // binary search for startsWith within set of matches
        var ind = _.sortedIndex(matches, startsWith);
        if (ind >= lines.length || matches[ind].indexOf(startsWith) === -1) {
          callback && callback([], startsWith);
          resolve([]);
          return;
        }

        var trie = new Trie();
        trie.addStrings(matches);
        //console.log('Trie > ', trie.matchesWithPrefix( startsWith ));
        matches = trie.keysWithPrefix(startsWith);
      }

      var words = _.sample(matches, num);
      callback && callback(words, startsWith);
      resolve(words);
    }
Example #2
0
 _.indexOf = function(array, item, isSorted) {
   if (array == null) return -1;
   var i, l;
   if (isSorted) {
     i = _.sortedIndex(array, item);
     return array[i] === item ? i : -1;
   }
   if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
   for (i = 0, l = array.length; i < l; i++) if (i in array && array[i] === item) return i;
   return -1;
 };
Example #3
0
ChatRoom.prototype.user_connect = function (user) {
  var index         = _.sortedIndex(users, user, iterator('fn', 'ln')),
      u_last_active = (this.last_active = (new Date).getTime()) / 1000;
  console.log('index is ' + index);
  if (sids[aid]) {
    // if user is already currently connected, i.e. opens chat in a new tab
    users[index]['la'] = u_last_active;
    users[index]['si'].push(user['si']);
    _.uniq(users[index]['si']);
  }
  user['la'] = u_last_active;
  user['si'] = [user[si]];
  users.splice(index, 0, user);
};
Example #4
0
 var maxLoopsAtSameTime = 500;//to prevent endless loops.
 function insertByTime(array, item) {
     var insertionIndex = _.sortedIndex(array, item, 'time');
     array.splice(insertionIndex, 0, item);
 }