You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

query-string.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. import $ from 'jquery'
  25. /**
  26. * Parses a URL query string into a JS map
  27. *
  28. * @param {string} queryString query string in the format param1=1234&param2=abcde&param3=xyz
  29. * @return {object.<string, string>} map containing key/values matching the URL parameters
  30. */
  31. export const parse = queryString => {
  32. let pos
  33. let components
  34. const result = {}
  35. let key
  36. if (!queryString) {
  37. return null
  38. }
  39. pos = queryString.indexOf('?')
  40. if (pos >= 0) {
  41. queryString = queryString.substr(pos + 1)
  42. }
  43. const parts = queryString.replace(/\+/g, '%20').split('&')
  44. for (let i = 0; i < parts.length; i++) {
  45. // split on first equal sign
  46. const part = parts[i]
  47. pos = part.indexOf('=')
  48. if (pos >= 0) {
  49. components = [
  50. part.substr(0, pos),
  51. part.substr(pos + 1),
  52. ]
  53. } else {
  54. // key only
  55. components = [part]
  56. }
  57. if (!components.length) {
  58. continue
  59. }
  60. key = decodeURIComponent(components[0])
  61. if (!key) {
  62. continue
  63. }
  64. // if equal sign was there, return string
  65. if (components.length > 1) {
  66. result[key] = decodeURIComponent(components[1])
  67. } else {
  68. // no equal sign => null value
  69. result[key] = null
  70. }
  71. }
  72. return result
  73. }
  74. /**
  75. * Builds a URL query from a JS map.
  76. *
  77. * @param {object.<string, string>} params map containing key/values matching the URL parameters
  78. * @return {string} String containing a URL query (without question) mark
  79. */
  80. export const build = params => {
  81. if (!params) {
  82. return ''
  83. }
  84. return $.map(params, function(value, key) {
  85. let s = encodeURIComponent(key)
  86. if (value !== null && typeof (value) !== 'undefined') {
  87. s += '=' + encodeURIComponent(value)
  88. }
  89. return s
  90. }).join('&')
  91. }