urlcat-umd

A UMD version of urlcat

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/489919/1458346/urlcat-umd.js

  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.urlcat = factory());
  5. })(this, (function () { 'use strict';
  6.  
  7. const nullOrUndefined = v => v === undefined || v === null;
  8. const removeNullOrUndef = params => Object.entries(params).reduce((result, [key, value]) => {
  9. if (nullOrUndefined(value)) return result;
  10. result[key] = value;
  11. return result
  12. }, {});
  13.  
  14. /**
  15. * Creates a query string from the specified object.
  16. *
  17. * @param {Object} params an object to convert into a query string.
  18. * @param {Object} config configuration to stringify the query params.
  19. *
  20. * @returns {String} Query string.
  21. *
  22. * @example
  23. * ```ts
  24. * query({ id: 42, search: 'foo' })
  25. * // -> 'id=42&search=foo'
  26. * ```
  27. */
  28. const query = (params, config) => {
  29. /* NOTE: Handle quirk of `new UrlSearchParams(params).toString()` in Webkit 602.x.xx
  30. * versions which returns stringified object when params is empty object
  31. */
  32. if (Object.keys(params).length < 1) return '';
  33. return new URLSearchParams(params).toString();
  34. };
  35.  
  36. /**
  37. * Joins two strings using a separator.
  38. * If the separator occurs at the concatenation boundary in either of the strings, it is removed.
  39. * This prevents accidental duplication of the separator.
  40. *
  41. * @param {String} part1 First string.
  42. * @param {String} separator Separator used for joining.
  43. * @param {String} part2 Second string.
  44. *
  45. * @returns {String} Joined string.
  46. *
  47. * @example
  48. * ```ts
  49. * join('first/', '/', '/second')
  50. * // -> 'first/second'
  51. * ```
  52. */
  53. const join = (part1, separator, part2) =>{
  54. const p1 = part1.endsWith(separator)
  55. ? part1.slice(0, -separator.length)
  56. : part1;
  57. const p2 = part2.startsWith(separator)
  58. ? part2.slice(separator.length)
  59. : part2;
  60. return p1 === '' || p2 === ''
  61. ? p1 + p2
  62. : p1 + separator + p2;
  63. };
  64.  
  65. const validatePathParam = (params, key) => {
  66. const allowedTypes = ['boolean', 'string', 'number'];
  67. if (!Object.prototype.hasOwnProperty.call(params, key)) throw new Error(`Missing value for path parameter ${key}.`);
  68. if (!allowedTypes.includes(typeof params[key])) throw new TypeError(`Path parameter ${key} cannot be of type ${typeof params[key]}. Allowed types are: ${allowedTypes.join(', ')}.`);
  69. if (typeof params[key] === 'string' && params[key].trim() === '') throw new Error(`Path parameter ${key} cannot be an empty string.`);
  70. };
  71.  
  72.  
  73. const path = (template, params) => {
  74. const remainingParams = { ...params };
  75. const renderedPath = template.replace(/:[_A-Za-z]+[_A-Za-z0-9]*/g, p => {
  76. const key = p.slice(1);
  77. validatePathParam(params, key);
  78. delete remainingParams[key];
  79. return encodeURIComponent(params[key]);
  80. });
  81. return { renderedPath, remainingParams };
  82. };
  83.  
  84. const joinFullUrl = (renderedPath, baseUrl, pathAndQuery) => renderedPath.length ? join(baseUrl, '/', pathAndQuery) : join(baseUrl, '?', pathAndQuery);
  85.  
  86. const urlcatImpl = (pathTemplate, params, baseUrl, config) => {
  87. const { renderedPath, remainingParams } = path(pathTemplate, params);
  88. const cleanParams = removeNullOrUndef(remainingParams);
  89. const renderedQuery = query(cleanParams);
  90. const pathAndQuery = join(renderedPath, '?', renderedQuery);
  91. return baseUrl ? joinFullUrl(renderedPath, baseUrl, pathAndQuery) : pathAndQuery;
  92. };
  93.  
  94. const urlcat = (baseUrlOrTemplate, pathTemplateOrParams, maybeParams = {}, config = {}) => {
  95. if (typeof pathTemplateOrParams === 'string') {
  96. const baseUrl = baseUrlOrTemplate;
  97. const pathTemplate = pathTemplateOrParams;
  98. const params = maybeParams;
  99. return urlcatImpl(pathTemplate, params, baseUrl);
  100. }
  101. else {
  102. const baseTemplate = baseUrlOrTemplate;
  103. const params = pathTemplateOrParams;
  104. return urlcatImpl(baseTemplate, params, undefined);
  105. }
  106. };
  107.  
  108. return urlcat;
  109.  
  110. }));

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址