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.

stringify-queryparams.ts 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. // Copyright Joyent, Inc. and other Node contributors.
  21. //
  22. // Permission is hereby granted, free of charge, to any person obtaining a
  23. // copy of this software and associated documentation files (the
  24. // "Software"), to deal in the Software without restriction, including
  25. // without limitation the rights to use, copy, modify, merge, publish,
  26. // distribute, sublicense, and/or sell copies of the Software, and to permit
  27. // persons to whom the Software is furnished to do so, subject to the
  28. // following conditions:
  29. //
  30. // The above copyright notice and this permission notice shall be included
  31. // in all copies or substantial portions of the Software.
  32. //
  33. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  34. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  35. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  36. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  37. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  38. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  39. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  40. function stringifyPrimitive(v: any) {
  41. switch (typeof v) {
  42. case 'string':
  43. return v;
  44. case 'boolean':
  45. return v ? 'true' : 'false';
  46. case 'number':
  47. return isFinite(v) ? v : '';
  48. default:
  49. return '';
  50. }
  51. }
  52. export function stringify(obj: any, sep?: any, eq?: any, name?: any) {
  53. sep = sep || '&';
  54. eq = eq || '=';
  55. if (obj === null) {
  56. obj = undefined;
  57. }
  58. if (typeof obj === 'object') {
  59. return Object.keys(obj)
  60. .map(k => {
  61. const ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
  62. if (Array.isArray(obj[k])) {
  63. return obj[k]
  64. .map((v: any) => {
  65. return ks + encodeURIComponent(stringifyPrimitive(v));
  66. })
  67. .join(sep);
  68. }
  69. return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
  70. })
  71. .filter(Boolean)
  72. .join(sep);
  73. }
  74. if (!name) {
  75. return '';
  76. }
  77. return (
  78. encodeURIComponent(stringifyPrimitive(name)) + eq + encodeURIComponent(stringifyPrimitive(obj))
  79. );
  80. }