All files / engine/Source/Core getBaseUri.js

100% Statements 14/14
100% Branches 10/10
100% Functions 1/1
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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