PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/jsuri-1.1.1.js

https://gitlab.com/yenny.prathivi/My-Wallet
JavaScript | 473 lines | 333 code | 88 blank | 52 comment | 68 complexity | be6ff56935ab063a6a62728410632d44 MD5 | raw file
  1. /*!
  2. * jsUri v1.1.1
  3. * https://github.com/derek-watson/jsUri
  4. *
  5. * Copyright 2011, Derek Watson
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * Includes parseUri regular expressions
  10. * http://blog.stevenlevithan.com/archives/parseuri
  11. * Copyright 2007, Steven Levithan
  12. * Released under the MIT license.
  13. *
  14. * Date: Mon Nov 14 20:06:34 2011 -0800
  15. */
  16. var Query = function (queryString) {
  17. // query string parsing, parameter manipulation and stringification
  18. 'use strict';
  19. var // parseQuery(q) parses the uri query string and returns a multi-dimensional array of the components
  20. parseQuery = function (q) {
  21. var arr = [], i, ps, p, keyval;
  22. if (typeof (q) === 'undefined' || q === null || q === '') {
  23. return arr;
  24. }
  25. if (q.indexOf('?') === 0) {
  26. q = q.substring(1);
  27. }
  28. ps = q.toString().split(/[&;]/);
  29. for (i = 0; i < ps.length; i++) {
  30. p = ps[i];
  31. keyval = p.split('=');
  32. arr.push([keyval[0], keyval[1]]);
  33. }
  34. return arr;
  35. },
  36. params = parseQuery(queryString),
  37. // toString() returns a string representation of the internal state of the object
  38. toString = function () {
  39. var s = '', i, param;
  40. for (i = 0; i < params.length; i++) {
  41. param = params[i];
  42. if (s.length > 0) {
  43. s += '&';
  44. }
  45. s += param.join('=');
  46. }
  47. return s.length > 0 ? '?' + s : s;
  48. },
  49. decode = function (s) {
  50. s = decodeURIComponent(s);
  51. s = s.replace('+', ' ');
  52. return s;
  53. },
  54. // getParamValues(key) returns the first query param value found for the key 'key'
  55. getParamValue = function (key) {
  56. var param, i;
  57. for (i = 0; i < params.length; i++) {
  58. param = params[i];
  59. if (decode(key) === decode(param[0])) {
  60. return param[1];
  61. }
  62. }
  63. },
  64. // getParamValues(key) returns an array of query param values for the key 'key'
  65. getParamValues = function (key) {
  66. var arr = [], i, param;
  67. for (i = 0; i < params.length; i++) {
  68. param = params[i];
  69. if (decode(key) === decode(param[0])) {
  70. arr.push(param[1]);
  71. }
  72. }
  73. return arr;
  74. },
  75. // deleteParam(key) removes all instances of parameters named (key)
  76. // deleteParam(key, val) removes all instances where the value matches (val)
  77. deleteParam = function (key, val) {
  78. var arr = [], i, param, keyMatchesFilter, valMatchesFilter;
  79. for (i = 0; i < params.length; i++) {
  80. param = params[i];
  81. keyMatchesFilter = decode(param[0]) === decode(key);
  82. valMatchesFilter = decode(param[1]) === decode(val);
  83. if ((arguments.length === 1 && !keyMatchesFilter) || (arguments.length === 2 && !keyMatchesFilter && !valMatchesFilter)) {
  84. arr.push(param);
  85. }
  86. }
  87. params = arr;
  88. return this;
  89. },
  90. // addParam(key, val) Adds an element to the end of the list of query parameters
  91. // addParam(key, val, index) adds the param at the specified position (index)
  92. addParam = function (key, val, index) {
  93. if (arguments.length === 3 && index !== -1) {
  94. index = Math.min(index, params.length);
  95. params.splice(index, 0, [key, val]);
  96. } else if (arguments.length > 0) {
  97. params.push([key, val]);
  98. }
  99. return this;
  100. },
  101. // replaceParam(key, newVal) deletes all instances of params named (key) and replaces them with the new single value
  102. // replaceParam(key, newVal, oldVal) deletes only instances of params named (key) with the value (val) and replaces them with the new single value
  103. // this function attempts to preserve query param ordering
  104. replaceParam = function (key, newVal, oldVal) {
  105. var index = -1, i, param;
  106. if (arguments.length === 3) {
  107. for (i = 0; i < params.length; i++) {
  108. param = params[i];
  109. if (decode(param[0]) === decode(key) && decodeURIComponent(param[1]) === decode(oldVal)) {
  110. index = i;
  111. break;
  112. }
  113. }
  114. deleteParam(key, oldVal).addParam(key, newVal, index);
  115. } else {
  116. for (i = 0; i < params.length; i++) {
  117. param = params[i];
  118. if (decode(param[0]) === decode(key)) {
  119. index = i;
  120. break;
  121. }
  122. }
  123. deleteParam(key);
  124. addParam(key, newVal, index);
  125. }
  126. return this;
  127. };
  128. // public api
  129. return {
  130. getParamValue: getParamValue,
  131. getParamValues: getParamValues,
  132. deleteParam: deleteParam,
  133. addParam: addParam,
  134. replaceParam: replaceParam,
  135. toString: toString
  136. };
  137. };
  138. var Uri = function (uriString) {
  139. // uri string parsing, attribute manipulation and stringification
  140. 'use strict';
  141. /*global Query: true */
  142. /*jslint regexp: false, plusplus: false */
  143. var strictMode = false,
  144. // parseUri(str) parses the supplied uri and returns an object containing its components
  145. parseUri = function (str) {
  146. /*jslint unparam: true */
  147. var parsers = {
  148. strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
  149. loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
  150. },
  151. keys = ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
  152. q = {
  153. name: "queryKey",
  154. parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  155. },
  156. m = parsers[strictMode ? "strict" : "loose"].exec(str),
  157. uri = {},
  158. i = 14;
  159. while (i--) {
  160. uri[keys[i]] = m[i] || "";
  161. }
  162. uri[q.name] = {};
  163. uri[keys[12]].replace(q.parser, function ($0, $1, $2) {
  164. if ($1) {
  165. uri[q.name][$1] = $2;
  166. }
  167. });
  168. return uri;
  169. },
  170. uriParts = parseUri(uriString || ''),
  171. queryObj = new Query(uriParts.query),
  172. /*
  173. Basic get/set functions for all properties
  174. */
  175. protocol = function (val) {
  176. if (typeof val !== 'undefined') {
  177. uriParts.protocol = val;
  178. }
  179. return uriParts.protocol;
  180. },
  181. hasAuthorityPrefixUserPref = null,
  182. // hasAuthorityPrefix: if there is no protocol, the leading // can be enabled or disabled
  183. hasAuthorityPrefix = function (val) {
  184. if (typeof val !== 'undefined') {
  185. hasAuthorityPrefixUserPref = val;
  186. }
  187. if (hasAuthorityPrefixUserPref === null) {
  188. return (uriParts.source.indexOf('//') !== -1);
  189. } else {
  190. return hasAuthorityPrefixUserPref;
  191. }
  192. },
  193. userInfo = function (val) {
  194. if (typeof val !== 'undefined') {
  195. uriParts.userInfo = val;
  196. }
  197. return uriParts.userInfo;
  198. },
  199. host = function (val) {
  200. if (typeof val !== 'undefined') {
  201. uriParts.host = val;
  202. }
  203. return uriParts.host;
  204. },
  205. port = function (val) {
  206. if (typeof val !== 'undefined') {
  207. uriParts.port = val;
  208. }
  209. return uriParts.port;
  210. },
  211. path = function (val) {
  212. if (typeof val !== 'undefined') {
  213. uriParts.path = val;
  214. }
  215. return uriParts.path;
  216. },
  217. query = function (val) {
  218. if (typeof val !== 'undefined') {
  219. queryObj = new Query(val);
  220. }
  221. return queryObj;
  222. },
  223. anchor = function (val) {
  224. if (typeof val !== 'undefined') {
  225. uriParts.anchor = val;
  226. }
  227. return uriParts.anchor;
  228. },
  229. /*
  230. Fluent setters for Uri uri properties
  231. */
  232. setProtocol = function (val) {
  233. protocol(val);
  234. return this;
  235. },
  236. setHasAuthorityPrefix = function (val) {
  237. hasAuthorityPrefix(val);
  238. return this;
  239. },
  240. setUserInfo = function (val) {
  241. userInfo(val);
  242. return this;
  243. },
  244. setHost = function (val) {
  245. host(val);
  246. return this;
  247. },
  248. setPort = function (val) {
  249. port(val);
  250. return this;
  251. },
  252. setPath = function (val) {
  253. path(val);
  254. return this;
  255. },
  256. setQuery = function (val) {
  257. query(val);
  258. return this;
  259. },
  260. setAnchor = function (val) {
  261. anchor(val);
  262. return this;
  263. },
  264. /*
  265. Query method wrappers
  266. */
  267. getQueryParamValue = function (key) {
  268. return query().getParamValue(key);
  269. },
  270. getQueryParamValues = function (key) {
  271. return query().getParamValues(key);
  272. },
  273. deleteQueryParam = function (key, val) {
  274. if (arguments.length === 2) {
  275. query().deleteParam(key, val);
  276. } else {
  277. query().deleteParam(key);
  278. }
  279. return this;
  280. },
  281. addQueryParam = function (key, val, index) {
  282. if (arguments.length === 3) {
  283. query().addParam(key, val, index);
  284. } else {
  285. query().addParam(key, val);
  286. }
  287. return this;
  288. },
  289. replaceQueryParam = function (key, newVal, oldVal) {
  290. if (arguments.length === 3) {
  291. query().replaceParam(key, newVal, oldVal);
  292. } else {
  293. query().replaceParam(key, newVal);
  294. }
  295. return this;
  296. },
  297. /*
  298. Serialization
  299. */
  300. // toString() stringifies the current state of the uri
  301. toString = function () {
  302. var s = '',
  303. is = function (s) {
  304. return (s !== null && s !== '');
  305. };
  306. if (is(protocol())) {
  307. s += protocol();
  308. if (protocol().indexOf(':') !== protocol().length - 1) {
  309. s += ':';
  310. }
  311. s += '//';
  312. } else {
  313. if (hasAuthorityPrefix() && is(host())) {
  314. s += '//';
  315. }
  316. }
  317. if (is(userInfo()) && is(host())) {
  318. s += userInfo();
  319. if (userInfo().indexOf('@') !== userInfo().length - 1) {
  320. s += '@';
  321. }
  322. }
  323. if (is(host())) {
  324. s += host();
  325. if (is(port())) {
  326. s += ':' + port();
  327. }
  328. }
  329. if (is(path())) {
  330. s += path();
  331. } else {
  332. if (is(host()) && (is(query().toString()) || is(anchor()))) {
  333. s += '/';
  334. }
  335. }
  336. if (is(query().toString())) {
  337. if (query().toString().indexOf('?') !== 0) {
  338. s += '?';
  339. }
  340. s += query().toString();
  341. }
  342. if (is(anchor())) {
  343. if (anchor().indexOf('#') !== 0) {
  344. s += '#';
  345. }
  346. s += anchor();
  347. }
  348. return s;
  349. },
  350. /*
  351. Cloning
  352. */
  353. // clone() returns a new, identical Uri instance
  354. clone = function () {
  355. return new Uri(toString());
  356. };
  357. // public api
  358. return {
  359. protocol: protocol,
  360. hasAuthorityPrefix: hasAuthorityPrefix,
  361. userInfo: userInfo,
  362. host: host,
  363. port: port,
  364. path: path,
  365. query: query,
  366. anchor: anchor,
  367. setProtocol: setProtocol,
  368. setHasAuthorityPrefix: setHasAuthorityPrefix,
  369. setUserInfo: setUserInfo,
  370. setHost: setHost,
  371. setPort: setPort,
  372. setPath: setPath,
  373. setQuery: setQuery,
  374. setAnchor: setAnchor,
  375. getQueryParamValue: getQueryParamValue,
  376. getQueryParamValues: getQueryParamValues,
  377. deleteQueryParam: deleteQueryParam,
  378. addQueryParam: addQueryParam,
  379. replaceQueryParam: replaceQueryParam,
  380. toString: toString,
  381. clone: clone
  382. };
  383. };
  384. /* add compatibility for users of jsUri <= 1.1.1 */
  385. var jsUri = Uri;