Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

url-util.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. cordova.define("cordova-plugin-advanced-http.url-util", function(require, exports, module) {
  2. module.exports = function init(jsUtil) {
  3. return {
  4. parseUrl: parseUrl,
  5. appendQueryParamsString: appendQueryParamsString,
  6. serializeQueryParams: serializeQueryParams
  7. }
  8. function parseUrl(url) {
  9. var match = url.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
  10. return match && {
  11. protocol: match[1],
  12. host: match[2],
  13. hostname: match[3],
  14. port: match[4] || '',
  15. pathname: match[5],
  16. search: match[6],
  17. hash: match[7]
  18. }
  19. }
  20. function appendQueryParamsString(url, params) {
  21. if (!url.length || !params.length) {
  22. return url;
  23. }
  24. var parsed = parseUrl(url);
  25. return parsed.protocol
  26. + '//'
  27. + parsed.host
  28. + parsed.pathname
  29. + (parsed.search.length ? parsed.search + '&' + params : '?' + params)
  30. + parsed.hash;
  31. }
  32. function serializeQueryParams(params, encode) {
  33. return serializeObject('', params, encode);
  34. }
  35. function serializeObject(parentKey, object, encode) {
  36. var parts = [];
  37. for (var key in object) {
  38. if (!object.hasOwnProperty(key)) {
  39. continue;
  40. }
  41. var identifier = parentKey.length ? parentKey + '[' + key + ']' : key;
  42. if (jsUtil.getTypeOf(object[key]) === 'Array') {
  43. parts.push(serializeArray(identifier, object[key], encode));
  44. continue;
  45. } else if (jsUtil.getTypeOf(object[key]) === 'Object') {
  46. parts.push(serializeObject(identifier, object[key], encode));
  47. continue;
  48. }
  49. parts.push(serializeIdentifier(parentKey, key, encode) + '=' + serializeValue(object[key], encode));
  50. }
  51. return parts.join('&');
  52. }
  53. function serializeArray(parentKey, array, encode) {
  54. var parts = [];
  55. for (var i = 0; i < array.length; ++i) {
  56. if (jsUtil.getTypeOf(array[i]) === 'Array') {
  57. parts.push(serializeArray(parentKey + '[]', array[i], encode));
  58. continue;
  59. } else if (jsUtil.getTypeOf(array[i]) === 'Object') {
  60. parts.push(serializeObject(parentKey + '[]' + array[i], encode));
  61. continue;
  62. }
  63. parts.push(serializeIdentifier(parentKey, '', encode) + '=' + serializeValue(array[i], encode));
  64. }
  65. return parts.join('&');
  66. }
  67. function serializeIdentifier(parentKey, key, encode) {
  68. if (!parentKey.length) {
  69. return encode ? encodeURIComponent(key) : key;
  70. }
  71. if (encode) {
  72. return encodeURIComponent(parentKey) + '[' + encodeURIComponent(key) + ']';
  73. } else {
  74. return parentKey + '[' + key + ']';
  75. }
  76. }
  77. function serializeValue(value, encode) {
  78. if (encode) {
  79. return encodeURIComponent(value);
  80. } else {
  81. return value;
  82. }
  83. }
  84. };
  85. });