Advertisement
Filkolev

Query Mess - Solution

Mar 24th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.     var regex = /([^=&?]+)=([^=&?]+)/g,
  3.         whitespaceRegex = /(\+|%20)+/g,
  4.         lineIndex,
  5.         currentLine,
  6.         lineObject,
  7.         match,
  8.         key,
  9.         value,
  10.         output,
  11.         i;
  12.  
  13.     for (lineIndex in args) {
  14.         currentLine = args[lineIndex];
  15.         lineObject = {};
  16.  
  17.         match = regex.exec(currentLine);
  18.  
  19.         while (match) {
  20.             key = match[1]
  21.                 .replace(whitespaceRegex, ' ')
  22.                 .trim();
  23.  
  24.             value = match[2]
  25.                 .replace(whitespaceRegex, ' ')
  26.                 .trim();
  27.  
  28.             if (!lineObject[key]) {
  29.                 lineObject[key] = [];
  30.             }
  31.  
  32.             lineObject[key].push(value);
  33.  
  34.             match = regex.exec(currentLine);
  35.         }
  36.  
  37.         output = '';
  38.  
  39.         for (i in lineObject) {
  40.             output += i + '=[' + lineObject[i].join(', ') + ']';
  41.         }
  42.  
  43.         console.log(output);
  44.     }
  45. }
  46.  
  47. //solve([
  48. //    'foo=%20foo&value=+val&foo+=5+%20+203',
  49. //    'foo=poo%20&value=valley&dog=wow+',
  50. //    'url=https://softuni.bg/trainings/coursesinstances/details/1070',
  51. //    'https://softuni.bg/trainings.asp?trainer=nakov&course=oop&course=php'
  52. //]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement