uri.min.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. (function (global) {
  2. var re = {
  3. starts_with_slashes: /^\/+/,
  4. ends_with_slashes: /\/+$/,
  5. pluses: /\+/g,
  6. query_separator: /[&;]/,
  7. uri_parser: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@\/]*)(?::([^:@]*))?)?@)?(\[[0-9a-fA-F:.]+\]|[^:\/?#]*)(?::(\d+|(?=:)))?(:)?)((((?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
  8. };
  9. if (!Array.prototype.forEach) {
  10. Array.prototype.forEach = function (callback, thisArg) {
  11. var T, k;
  12. if (this == null) {
  13. throw new TypeError(' this is null or not defined');
  14. }
  15. var O = Object(this);
  16. var len = O.length >>> 0;
  17. if (typeof callback !== "function") {
  18. throw new TypeError(callback + ' is not a function');
  19. }
  20. if (arguments.length > 1) {
  21. T = thisArg
  22. }
  23. k = 0;
  24. while (k < len) {
  25. var kValue;
  26. if (k in O) {
  27. kValue = O[k];
  28. callback.call(T, kValue, k, O)
  29. }
  30. k++
  31. }
  32. }
  33. }
  34. function decode(s) {
  35. if (s) {
  36. s = s.toString().replace(re.pluses, '%20');
  37. s = decodeURIComponent(s)
  38. }
  39. return s
  40. }
  41. function parseUri(str) {
  42. var parser = re.uri_parser;
  43. var parserKeys = ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "isColonUri", "relative", "path", "directory", "file", "query", "anchor"];
  44. var m = parser.exec(str || '');
  45. var parts = {};
  46. parserKeys.forEach(function (key, i) {
  47. parts[key] = m[i] || ''
  48. });
  49. return parts
  50. }
  51. function parseQuery(str) {
  52. var i, ps, p, n, k, v, l;
  53. var pairs = [];
  54. if (typeof (str) === 'undefined' || str === null || str === '') {
  55. return pairs
  56. }
  57. if (str.indexOf('?') === 0) {
  58. str = str.substring(1)
  59. }
  60. ps = str.toString().split(re.query_separator);
  61. for (i = 0, l = ps.length; i < l; i++) {
  62. p = ps[i];
  63. n = p.indexOf('=');
  64. if (n !== 0) {
  65. k = decode(p.substring(0, n));
  66. v = decode(p.substring(n + 1));
  67. pairs.push(n === -1 ? [p, null] : [k, v])
  68. }
  69. }
  70. return pairs
  71. }
  72. function Uri(str) {
  73. this.uriParts = parseUri(str);
  74. this.queryPairs = parseQuery(this.uriParts.query);
  75. this.hasAuthorityPrefixUserPref = null
  76. }
  77. ['protocol', 'userInfo', 'host', 'port', 'path', 'anchor'].forEach(function (key) {
  78. Uri.prototype[key] = function (val) {
  79. if (typeof val !== 'undefined') {
  80. this.uriParts[key] = val
  81. }
  82. return this.uriParts[key]
  83. }
  84. });
  85. Uri.prototype.hasAuthorityPrefix = function (val) {
  86. if (typeof val !== 'undefined') {
  87. this.hasAuthorityPrefixUserPref = val
  88. }
  89. if (this.hasAuthorityPrefixUserPref === null) {
  90. return (this.uriParts.source.indexOf('//') !== -1)
  91. } else {
  92. return this.hasAuthorityPrefixUserPref
  93. }
  94. };
  95. Uri.prototype.isColonUri = function (val) {
  96. if (typeof val !== 'undefined') {
  97. this.uriParts.isColonUri = !!val
  98. } else {
  99. return !!this.uriParts.isColonUri
  100. }
  101. };
  102. Uri.prototype.query = function (val) {
  103. var s = '', i, param, l;
  104. if (typeof val !== 'undefined') {
  105. this.queryPairs = parseQuery(val)
  106. }
  107. for (i = 0, l = this.queryPairs.length; i < l; i++) {
  108. param = this.queryPairs[i];
  109. if (s.length > 0) {
  110. s += '&'
  111. }
  112. if (param[1] === null) {
  113. s += param[0]
  114. } else {
  115. s += param[0];
  116. s += '=';
  117. if (typeof param[1] !== 'undefined') {
  118. s += encodeURIComponent(param[1])
  119. }
  120. }
  121. }
  122. return s.length > 0 ? '?' + s : s
  123. };
  124. Uri.prototype.getQueryParamValue = function (key) {
  125. var param, i, l;
  126. for (i = 0, l = this.queryPairs.length; i < l; i++) {
  127. param = this.queryPairs[i];
  128. if (key === param[0]) {
  129. return param[1]
  130. }
  131. }
  132. };
  133. Uri.prototype.getQueryParamValues = function (key) {
  134. var arr = [], i, param, l;
  135. for (i = 0, l = this.queryPairs.length; i < l; i++) {
  136. param = this.queryPairs[i];
  137. if (key === param[0]) {
  138. arr.push(param[1])
  139. }
  140. }
  141. return arr
  142. };
  143. Uri.prototype.deleteQueryParam = function (key, val) {
  144. var arr = [], i, param, keyMatchesFilter, valMatchesFilter, l;
  145. for (i = 0, l = this.queryPairs.length; i < l; i++) {
  146. param = this.queryPairs[i];
  147. keyMatchesFilter = decode(param[0]) === decode(key);
  148. valMatchesFilter = param[1] === val;
  149. if ((arguments.length === 1 && !keyMatchesFilter) || (arguments.length === 2 && (!keyMatchesFilter || !valMatchesFilter))) {
  150. arr.push(param)
  151. }
  152. }
  153. this.queryPairs = arr;
  154. return this
  155. };
  156. Uri.prototype.addQueryParam = function (key, val, index) {
  157. if (arguments.length === 3 && index !== -1) {
  158. index = Math.min(index, this.queryPairs.length);
  159. this.queryPairs.splice(index, 0, [key, val])
  160. } else if (arguments.length > 0) {
  161. this.queryPairs.push([key, val])
  162. }
  163. return this
  164. };
  165. Uri.prototype.hasQueryParam = function (key) {
  166. var i, len = this.queryPairs.length;
  167. for (i = 0; i < len; i++) {
  168. if (this.queryPairs[i][0] == key) return true
  169. }
  170. return false
  171. };
  172. Uri.prototype.replaceQueryParam = function (key, newVal, oldVal) {
  173. var index = -1, len = this.queryPairs.length, i, param;
  174. if (arguments.length === 3) {
  175. for (i = 0; i < len; i++) {
  176. param = this.queryPairs[i];
  177. if (decode(param[0]) === decode(key) && decodeURIComponent(param[1]) === decode(oldVal)) {
  178. index = i;
  179. break
  180. }
  181. }
  182. if (index >= 0) {
  183. this.deleteQueryParam(key, decode(oldVal)).addQueryParam(key, newVal, index)
  184. }
  185. } else {
  186. for (i = 0; i < len; i++) {
  187. param = this.queryPairs[i];
  188. if (decode(param[0]) === decode(key)) {
  189. index = i;
  190. break
  191. }
  192. }
  193. this.deleteQueryParam(key);
  194. this.addQueryParam(key, newVal, index)
  195. }
  196. return this
  197. };
  198. ['protocol', 'hasAuthorityPrefix', 'isColonUri', 'userInfo', 'host', 'port', 'path', 'query', 'anchor'].forEach(function (key) {
  199. var method = 'set' + key.charAt(0).toUpperCase() + key.slice(1);
  200. Uri.prototype[method] = function (val) {
  201. this[key](val);
  202. return this
  203. }
  204. });
  205. Uri.prototype.scheme = function () {
  206. var s = '';
  207. if (this.protocol()) {
  208. s += this.protocol();
  209. if (this.protocol().indexOf(':') !== this.protocol().length - 1) {
  210. s += ':'
  211. }
  212. s += '//'
  213. } else {
  214. if (this.hasAuthorityPrefix() && this.host()) {
  215. s += '//'
  216. }
  217. }
  218. return s
  219. };
  220. Uri.prototype.origin = function () {
  221. var s = this.scheme();
  222. if (this.userInfo() && this.host()) {
  223. s += this.userInfo();
  224. if (this.userInfo().indexOf('@') !== this.userInfo().length - 1) {
  225. s += '@'
  226. }
  227. }
  228. if (this.host()) {
  229. s += this.host();
  230. if (this.port() || (this.path() && this.path().substr(0, 1).match(/[0-9]/))) {
  231. s += ':' + this.port()
  232. }
  233. }
  234. return s
  235. };
  236. Uri.prototype.addTrailingSlash = function () {
  237. var path = this.path() || '';
  238. if (path.substr(-1) !== '/') {
  239. this.path(path + '/')
  240. }
  241. return this
  242. };
  243. Uri.prototype.toString = function () {
  244. var path, s = this.origin();
  245. if (this.isColonUri()) {
  246. if (this.path()) {
  247. s += ':' + this.path()
  248. }
  249. } else if (this.path()) {
  250. path = this.path();
  251. if (!(re.ends_with_slashes.test(s) || re.starts_with_slashes.test(path))) {
  252. s += '/'
  253. } else {
  254. if (s) {
  255. s.replace(re.ends_with_slashes, '/')
  256. }
  257. path = path.replace(re.starts_with_slashes, '/')
  258. }
  259. s += path
  260. } else {
  261. if (this.host() && (this.query().toString() || this.anchor())) {
  262. s += '/'
  263. }
  264. }
  265. if (this.query().toString()) {
  266. s += this.query().toString()
  267. }
  268. if (this.anchor()) {
  269. if (this.anchor().indexOf('#') !== 0) {
  270. s += '#'
  271. }
  272. s += this.anchor()
  273. }
  274. return s
  275. };
  276. Uri.prototype.clone = function () {
  277. return new Uri(this.toString())
  278. };
  279. if (typeof define === 'function' && define.amd) {
  280. define(function () {
  281. return Uri
  282. })
  283. } else if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
  284. module.exports = Uri
  285. } else {
  286. globalThis.Uri = Uri
  287. }
  288. }(this));