getExtensionFromUri.js 975 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import Uri from "urijs";
  2. import defined from "./defined.js";
  3. import DeveloperError from "./DeveloperError.js";
  4. /**
  5. * Given a URI, returns the extension of the URI.
  6. * @function getExtensionFromUri
  7. *
  8. * @param {string} uri The Uri.
  9. * @returns {string} The extension of the Uri.
  10. *
  11. * @example
  12. * //extension will be "czml";
  13. * const extension = Cesium.getExtensionFromUri('/Gallery/simple.czml?value=true&example=false');
  14. */
  15. function getExtensionFromUri(uri) {
  16. //>>includeStart('debug', pragmas.debug);
  17. if (!defined(uri)) {
  18. throw new DeveloperError("uri is required.");
  19. }
  20. //>>includeEnd('debug');
  21. const uriObject = new Uri(uri);
  22. uriObject.normalize();
  23. let path = uriObject.path();
  24. let index = path.lastIndexOf("/");
  25. if (index !== -1) {
  26. path = path.substr(index + 1);
  27. }
  28. index = path.lastIndexOf(".");
  29. if (index === -1) {
  30. path = "";
  31. } else {
  32. path = path.substr(index + 1);
  33. }
  34. return path;
  35. }
  36. export default getExtensionFromUri;