Advertisement
jargon

Glow JS 0-01 Associative Array (javascript)

Aug 28th, 2016
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** Glow JS 0-01 Associative Array by Timothy Robert Keal **/
  2.  
  3. function array_dump_this(element, index, array) {
  4.   document.body.innerHTML+=('<font color="green">a[<font color="red">' + index + '</font>]="<font color="red">' + element + '</font>"</font><br>');
  5. }
  6.  
  7. function array_dump(array) {
  8.   array.forEach(array_dump_this)
  9. }
  10.  
  11. function addToArray(array) {
  12.   array.push(prompt("Add items to array or 'q' to stop"));
  13.   if (array[array.length - 1] == 'q') {
  14.     array.pop();
  15.     array_dump(array);
  16.   } else {
  17.     addToArray(array);
  18.   }
  19. }
  20.  
  21. function addslashes(string) {
  22.   return string.replace(/\\/g, '\\\\').
  23.   replace(/\u0008/g, '\\b').
  24.   replace(/\t/g, '\\t').
  25.   replace(/\n/g, '\\n').
  26.   replace(/\f/g, '\\f').
  27.   replace(/\r/g, '\\r').
  28.   replace(/'/g, '\\\'').
  29.   replace(/"/g, '\\"');
  30. }
  31.  
  32. function unicode_toHex(label = '') {
  33.   var ret = '';
  34.   var unicode = '';
  35.   for (var offset = 0; offset < label.length; offset++) {
  36.     unicode = parseInt(label.charCodeAt(offset), 10).toString(16);
  37.     ret += parseInt(unicode.length, 10).toString(16) + unicode;
  38.   }
  39.   return ret;
  40. }
  41.  
  42. function array_op(mode = 'set', array = 'aa', arg_label = '', arg_value = 1) {
  43.   var label = unicode_toHex(arg_label);
  44.   var addr = array;
  45.   var addr_last = '';
  46.   var i = 0;
  47.   for (var offset = 0; offset < label.length; offset++) {
  48.     addr_last = addr;
  49.     addr += '[' + parseInt(label.substring(offset, offset), 16) + ']';
  50.     if (eval(addr + '===undefined')) for (i = 0; i <= 16; i++)
  51.         eval(addr_last + '.push(\'\');');
  52.     }
  53.     if (eval(addr + '[16]===undefined')) for (i = 0; i <= 16; i++)
  54.         eval(addr + '.push(\'\');'); addr += '[16]';
  55.       switch (mode) {
  56.         case 'get':
  57.           return eval(addr);
  58.           break;
  59.         case 'set':
  60.           return eval(addr + '=\'' + addslashes(arg_value) + '\'');
  61.           break;
  62.         case 'inc':
  63.           return eval(addr + '+=' + arg_value);
  64.           break;
  65.         case 'dec':
  66.           return eval(addr + '-=' + arg_value);
  67.         case 'append':
  68.           return eval(addr + '+=\'' + addslashes(arg_value) + '\'');
  69.           break;
  70.         case 'seek':
  71.           return addr;
  72.           break;
  73.         default:
  74.         return;
  75.       }
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement