Advertisement
dimipan80

Exams - Query Strings Mess / Link Strings

Dec 23rd, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* The input is passed as array of query strings.
  2.  For each field of the form, the query string contains a pair field=value.
  3.  Within each pair, the field name and value are separated by an equals sign, '='.
  4.  The series of pairs are separated by the ampersand, '&’.
  5.  The question mark is used as a separator and is not part of the query string.
  6.  SPACE is encoded as '+' or "%20". URL query string may contain another URL as value.
  7.  Print at single line each processed string as follows:  key=[value]nextkey=[another  value] … etc.
  8.  Multiple whitespace characters should be reduced to one inside value/key names,
  9.  but there shouldn’t be any whitespaces before/after extracted keys and values.
  10.  If a key already exists, the value is added with comma and whitespace after other values
  11.  of the existing key in current string. */
  12.  
  13. "use strict";
  14.  
  15. function printArrayOfQueryStrings(args) {
  16.     args.forEach(function(input) {
  17.         var currentLineArr = input.split('&').filter(Boolean);
  18.         var rowQueryObj = {};
  19.         for (var i = 0; i < currentLineArr.length; i += 1) {
  20.             currentLineArr[i] = currentLineArr[i].split('?').filter(Boolean);
  21.             if (currentLineArr[i].length > 1) {
  22.                 currentLineArr[i].splice(0, 1);
  23.             }
  24.  
  25.             currentLineArr[i] = currentLineArr[i][0];
  26.  
  27.             var pair = currentLineArr[i].split('=').filter(Boolean);
  28.             var field = pair[0].split(/\+|%20/).filter(Boolean);
  29.             field = field.join(' ');
  30.  
  31.             var value = pair[1].split(/\+|%20/).filter(Boolean);
  32.             value = value.join(' ');
  33.  
  34.             if (!(field in rowQueryObj)) {
  35.                 rowQueryObj[field] = [];
  36.             }
  37.  
  38.             if (rowQueryObj[field].indexOf(value) < 0) {
  39.                 rowQueryObj[field].push(value);
  40.             }
  41.         }
  42.  
  43.         var resultStr = '';
  44.         for (var key in rowQueryObj) {
  45.             if (rowQueryObj.hasOwnProperty(key)) {
  46.                 resultStr += key + '=[' + rowQueryObj[key].join(', ') + ']';
  47.             }
  48.         }
  49.  
  50.         console.log(resultStr);
  51.     });
  52. }
  53.  
  54. printArrayOfQueryStrings(['login=student&password=student']);
  55. printArrayOfQueryStrings(['field=value1&field=value2&field=value3',
  56.     'http://example.com/over/there?name=ferret']);
  57. printArrayOfQueryStrings(['foo=%20foo&value=+val&foo+=5+%20+203',
  58.     'foo=%20poo%20&value=valley&dog=wow+',
  59.     'url=https://softuni.bg/trainings/coursesinstances/details/1070',
  60.     'https://softuni.bg/trainings?trainer=nakov&course=oop&course=php']);
  61. printArrayOfQueryStrings(['surprise=box&surprise=pink+ribbon&flowers=red%20roses',
  62.     'day=birthday&day=friday%20&day=sunny+day']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement