getBaseUri.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import Uri from "../ThirdParty/Uri.js";
  2. import defined from "./defined.js";
  3. import DeveloperError from "./DeveloperError.js";
  4. /**
  5. * Given a URI, returns the base path of the URI.
  6. * @function
  7. *
  8. * @param {String} uri The Uri.
  9. * @param {Boolean} [includeQuery = false] Whether or not to include the query string and fragment form the uri
  10. * @returns {String} The base path of the Uri.
  11. *
  12. * @example
  13. * // basePath will be "/Gallery/";
  14. * const basePath = Cesium.getBaseUri('/Gallery/simple.czml?value=true&example=false');
  15. *
  16. * // basePath will be "/Gallery/?value=true&example=false";
  17. * const basePath = Cesium.getBaseUri('/Gallery/simple.czml?value=true&example=false', true);
  18. */
  19. function getBaseUri(uri, includeQuery) {
  20. //>>includeStart('debug', pragmas.debug);
  21. if (!defined(uri)) {
  22. throw new DeveloperError("uri is required.");
  23. }
  24. //>>includeEnd('debug');
  25. let basePath = "";
  26. const i = uri.lastIndexOf("/");
  27. if (i !== -1) {
  28. basePath = uri.substring(0, i + 1);
  29. }
  30. if (!includeQuery) {
  31. return basePath;
  32. }
  33. uri = new Uri(uri);
  34. if (uri.query().length !== 0) {
  35. basePath += `?${uri.query()}`;
  36. }
  37. if (uri.fragment().length !== 0) {
  38. basePath += `#${uri.fragment()}`;
  39. }
  40. return basePath;
  41. }
  42. export default getBaseUri;