url.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. JSONObject
  5. } from '@phosphor/coreutils';
  6. import * as urlparse
  7. from 'url-parse';
  8. /**
  9. * The namespace for URL-related functions.
  10. */
  11. export
  12. namespace URLExt {
  13. /**
  14. * Parse a url into a URL object.
  15. *
  16. * @param urlString - The URL string to parse.
  17. *
  18. * @returns A URL object.
  19. */
  20. export
  21. function parse(url: string): IUrl {
  22. if (typeof document !== 'undefined') {
  23. let a = document.createElement('a');
  24. a.href = url;
  25. return a;
  26. }
  27. return urlparse(url);
  28. }
  29. /**
  30. * Join a sequence of url components and normalizes as in node `path.join`.
  31. *
  32. * @param parts - The url components.
  33. *
  34. * @returns the joined url.
  35. */
  36. export
  37. function join(...parts: string[]): string {
  38. // Adapted from url-join.
  39. // Copyright (c) 2016 José F. Romaniello, MIT License.
  40. // https://github.com/jfromaniello/url-join/blob/v1.1.0/lib/url-join.js
  41. let str = [].slice.call(parts, 0).join('/');
  42. // make sure protocol is followed by two slashes
  43. str = str.replace(/:\//g, '://');
  44. // remove consecutive slashes
  45. str = str.replace(/([^:\s])\/+/g, '$1/');
  46. // remove trailing slash before parameters or hash
  47. str = str.replace(/\/(\?|&|#[^!])/g, '$1');
  48. // replace ? in parameters with &
  49. str = str.replace(/(\?.+)\?/g, '$1&');
  50. return str;
  51. }
  52. /**
  53. * Encode the components of a multi-segment url.
  54. *
  55. * @param url - The url to encode.
  56. *
  57. * @returns the encoded url.
  58. *
  59. * #### Notes
  60. * Preserves the `'/'` separators.
  61. * Should not include the base url, since all parts are escaped.
  62. */
  63. export
  64. function encodeParts(url: string): string {
  65. return join(...url.split('/').map(encodeURIComponent));
  66. }
  67. /**
  68. * Return a serialized object string suitable for a query.
  69. *
  70. * @param object - The source object.
  71. *
  72. * @returns an encoded url query.
  73. *
  74. * #### Notes
  75. * From [stackoverflow](http://stackoverflow.com/a/30707423).
  76. */
  77. export
  78. function objectToQueryString(value: JSONObject): string {
  79. return '?' + Object.keys(value).map(key =>
  80. encodeURIComponent(key) + '=' + encodeURIComponent(String(value[key]))
  81. ).join('&');
  82. }
  83. /**
  84. * Test whether the url is a local url.
  85. */
  86. export
  87. function isLocal(url: string): boolean {
  88. const { host, protocol } = parse(url);
  89. return protocol !== 'data:' && host === location.host || host === '';
  90. }
  91. /**
  92. * The interface for a URL object
  93. */
  94. export interface IUrl {
  95. /**
  96. * The full URL string that was parsed with both the protocol and host
  97. * components converted to lower-case.
  98. */
  99. href?: string;
  100. /**
  101. * Identifies the URL's lower-cased protocol scheme.
  102. */
  103. protocol?: string;
  104. /**
  105. * The full lower-cased host portion of the URL, including the port if
  106. * specified.
  107. */
  108. host?: string;
  109. /**
  110. * The lower-cased host name portion of the host component without the
  111. * port included.
  112. */
  113. hostname?: string;
  114. /**
  115. * The numeric port portion of the host component.
  116. */
  117. port?: string;
  118. /**
  119. * The entire path section of the URL.
  120. */
  121. pathname?: string;
  122. /**
  123. * The "fragment" portion of the URL including the leading ASCII hash
  124. * `(#)` character
  125. */
  126. hash?: string;
  127. /**
  128. * The search element, including leading question mark (`'?'`), if any,
  129. * of the URL.
  130. */
  131. search?: string;
  132. }
  133. }