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

url-util.js 2.6KB

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