Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function zsay(s) {
  2.   function z(token) {
  3.     switch (token[0]) {
  4.     case 'zext':
  5.         return zify(token[1]).toLowerCase();
  6.     case 'zeep':
  7.         return token[1];
  8.     }
  9.   }
  10.   return zeparate(s).map(z).reduce((a,b) => a + b);
  11. }
  12.  
  13. function zeparate(s) {
  14.   function group(c) {
  15.     return 'abcedefghijklmnopqrstuvwxyzABCEDEFGHIJKLMNOPQRSTUVWXYZ0123456789[]'.indexOf(c) != -1;
  16.   }
  17.   var body = s;
  18.   var res = [];
  19.   var ztype = undefined;
  20.   var val = undefined;
  21.   while (body) {
  22.     if (group(body[0])) {
  23.       ztype = 'zext';
  24.       val = span(group, body);
  25.     } else {
  26.       ztype = 'zeep';
  27.       val = span((c) => !group(c), body);
  28.     }
  29.     body = val[1];
  30.     res.push([ztype, val[0]]);
  31.   }
  32.   return res;
  33. }
  34.  
  35. function span(test, m) {
  36.   var border = 0;
  37.   for (var i = 0; i < m.length; i++) {
  38.     if (!test(m[i]))
  39.       break;
  40.     border++;
  41.   }
  42.   return [m.substring(0, border), m.substring(border, m.length)];
  43. }
  44.  
  45. function zify(s) {
  46.   var body = s;
  47.   while (true) {
  48.     var len = body.length;
  49.     if (len < 4) {
  50.       return 'z'.repeat(4 - len) + body;
  51.     } else if (len == 4) {
  52.       return 'z' + body.substring(1, body.length);
  53.     } else if (hasVowel(body)) {
  54.       body = dropVowel(body);
  55.     } else {
  56.       body = body.substring(1, body.length);
  57.     }
  58.   }
  59. }
  60.  
  61. function hasVowel(s) {
  62.   for (var i = 0; i < s.length; i++)
  63.     if (isVowel(s[i]))
  64.       return true;
  65.   return false;
  66. }
  67.  
  68. function dropVowel(s) {
  69.   for (var i = 0; i < s.length; i++)
  70.     if (isVowel(s[i]))
  71.       return s.replace(new RegExp(s[i], ''), '');
  72.   return s;
  73. }
  74.  
  75. function isVowel(c) {
  76.   return 'aeiouAEIOU'.indexOf(c) != -1;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement