lib/Tokenizer.js
// splits a list of characters and converts to a predicate hash
splits a list of characters and converts to a predicate hash
splits a list of characters and converts to a predicate hash
function toPredicate(str) {
    var words = str.split("");
    var f = "",
      i,
      cats = [];
    out: for (i = 0; i < words.length; ++i) {
      for (var j = 0; j < cats.length; ++j)
        if (cats[j][0].length == words[i].length) {
          cats[j].push(words[i]);
          continue out;
        }
      cats.push([words[i]]);
    }
    if (cats.length > 3) {
      cats.sort(function(a, b) {
        return b.length - a.length;
      });
      f += "switch(str.length){";
      for (i = 0; i < cats.length; ++i) {
        var cat = cats[i];
        f += "case " + cat[0].length + ":";
        compareTo(cat);
      }
      f += "}";
    } else {
      compareTo(words);
    }
    return new Function("str", f);
    function compareTo(arr) {
      if (arr.length == 1) {
        return (f += "return str === " + JSON.stringify(arr[0]) + ";");
      }
      f += "switch(str){";
      for (var i = 0; i < arr.length; ++i) {
        f += "case " + JSON.stringify(arr[i]) + ":";
      }
      f += "return true}return false;";
    }
  }
function toPredicate ( str ) { var words = str . split ( "" ) ; var f = "" , i , cats = [ ] ; out : for ( i = 0 ; i < words . length ; ++ i ) { for ( var j = 0 ; j < cats . length ; ++ j ) if ( cats [ j ] [ 0 ] . length == words [ i ] . length ) { cats [ j ] . push ( words [ i ] ) ; continue out ; } cats . push ( [ words [ i ] ] ) ; } if ( cats . length > 3 ) { cats . sort ( function ( a , b ) { return b . length - a . length ; } ) ; f += "switch(str.length){" ; for ( i = 0 ; i < cats . length ; ++ i ) { var cat = cats [ i ] ; f += "case " + cat [ 0 ] . length + ":" ; compareTo ( cat ) ; } f += "}" ; } else { compareTo ( words ) ; } return new Function ( "str" , f ) ; function compareTo ( arr ) { if ( arr . length == 1 ) { return ( f += "return str === " + JSON . stringify ( arr [ 0 ] ) + ";" ) ; } f += "switch(str){" ; for ( var i = 0 ; i < arr . length ; ++ i ) { f += "case " + JSON . stringify ( arr [ i ] ) + ":" ; } f += "return true}return false;" ; } }
splits a list of characters and converts to a predicate hash
splits a list of characters and converts to a predicate hash
// adapted from https://github.com/marijnh/acorn