match.js 1.5 KB

12345678910111213141516171819202122232425
  1. var Type = require('../src/type');
  2. function match(t, o){ var r = false;
  3. t = t || '';
  4. o = Type.text.is(o)? {'=': o} : o || {}; // {'~', '=', '*', '<', '>', '+', '-', '?', '!'} // ignore case, exactly equal, anything after, lexically larger, lexically lesser, added in, subtacted from, questionable fuzzy match, and ends with.
  5. if(Type.obj.has(o,'~')){ t = t.toLowerCase(); o['='] = (o['='] || o['~']).toLowerCase() }
  6. if(Type.obj.has(o,'=')){ return t === o['='] }
  7. if(Type.obj.has(o,'*')){ if(t.slice(0, o['*'].length) === o['*']){ r = true; t = t.slice(o['*'].length) } else { return false }}
  8. if(Type.obj.has(o,'!')){ if(t.slice(-o['!'].length) === o['!']){ r = true } else { return false }}
  9. if(Type.obj.has(o,'+')){
  10. if(Type.list.map(Type.list.is(o['+'])? o['+'] : [o['+']], function(m){
  11. if(t.indexOf(m) >= 0){ r = true } else { return true }
  12. })){ return false }
  13. }
  14. if(Type.obj.has(o,'-')){
  15. if(Type.list.map(Type.list.is(o['-'])? o['-'] : [o['-']], function(m){
  16. if(t.indexOf(m) < 0){ r = true } else { return true }
  17. })){ return false }
  18. }
  19. if(Type.obj.has(o,'>')){ if(t > o['>']){ r = true } else { return false }}
  20. if(Type.obj.has(o,'<')){ if(t < o['<']){ r = true } else { return false }}
  21. function fuzzy(t,f){ var n = -1, i = 0, c; for(;c = f[i++];){ if(!~(n = t.indexOf(c, n+1))){ return false }} return true } // via http://stackoverflow.com/questions/9206013/javascript-fuzzy-search
  22. if(Type.obj.has(o,'?')){ if(fuzzy(t, o['?'])){ r = true } else { return false }} // change name!
  23. return r;
  24. }
  25. module.exports = match;