file.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { type_manager } from "./functions.js";
  2. class file {
  3. #name;
  4. #path;
  5. #extension;
  6. constructor(path, windows_like = false) {
  7. if (!type_manager.is_string(path)) {
  8. throw "Path tu the file must be string.";
  9. }
  10. path = path.trim();
  11. if (path.length === 0) {
  12. throw "Path of the file can not be empty.";
  13. }
  14. const separator = windows_like ? "\\" : "/";
  15. const dirs = path.split(separator);
  16. const full_filename = dirs.pop().trim();
  17. const filename_parts = full_filename.split(".");
  18. this.#name = filename_parts.shift();
  19. this.#extension = filename_parts.join(".")
  20. this.#path = dirs.join("/");
  21. }
  22. get full_path() {
  23. if (this.#path.length === 0) {
  24. return this.filename;
  25. }
  26. return this.#path + "/" + this.filename;
  27. }
  28. get filename() {
  29. if (this.#extension.length === 0) {
  30. return this.#name;
  31. }
  32. return this.#name + "." + this.#extension;
  33. }
  34. get name() {
  35. return this.#name;
  36. }
  37. get extension() {
  38. return this.#extension;
  39. }
  40. get path() {
  41. return this.#path;
  42. }
  43. get type() {
  44. return this.#extension;
  45. }
  46. }
  47. export { file };