Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. function test(REGEX_RE, filter, mutation, expected) {
  2. if(Array.isArray(mutation)) {
  3. return mutation.forEach(m => test(REGEX_RE, filter, m, expected));
  4. }
  5.  
  6.  
  7. const exec = REGEX_RE.exec(filter);
  8. try {
  9. const reg = new RegExp(exec[1], exec[2])
  10. const success = reg.test(mutation) === expected ? `\x1b[32msuccess\x1b[0m` : `\x1b[31mfailed \x1b[0m`;
  11. console.log('\t', success, filter, reg, mutation, expected);
  12. } catch(e) {
  13. console.log('\t', `\x1b[31mfailed \x1b[0m`, filter, 'Could not compile', exec.slice(1));
  14. }
  15. }
  16.  
  17. function testfor(REGEX_RE) {
  18. console.log(REGEX_RE);
  19. test(REGEX_RE, '/^cou.*/', [
  20. 'coucou', 'couteau', 'couperet'
  21. ], true);
  22.  
  23. test(REGEX_RE, '/^cou.*/', [
  24. 'toto', 'piscou'
  25. ], false);
  26.  
  27. test(REGEX_RE, '/^cou.*/i', [
  28. 'CoucOu', 'cOuteau', 'cOuperet'
  29. ], true);
  30.  
  31. test(REGEX_RE, '/^co\\/u.*/', [
  32. 'co/ucou', 'co/uteau', 'co/uperet'
  33. ], true);
  34.  
  35. test(REGEX_RE, '/^co\\/u.*/', [
  36. 'coucou', 'couteau', 'couperet'
  37. ], false);
  38.  
  39. test(REGEX_RE, '/^co\\/u.*/i', [
  40. 'cO/ucou', 'cO/uteau', 'cO/uperet'
  41. ], true);
  42.  
  43. test(REGEX_RE, '/^co\\/u|toto.*/', [
  44. 'toto', 'totocoucou'
  45. ], true);
  46.  
  47. test(REGEX_RE, '/^co/u|toto.*/', [
  48. 'toto', 'totocoucou'
  49. ], false);
  50. }
  51.  
  52. testfor(/^\/(.*?)\/(\w*)/); // Initial regexp, should fail in several cases
  53. testfor(/^\/((?:(?:.*?)(?:\\\/)?)*?)\/(\w*)/); // works for all test cases
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement