Эх сурвалжийг харах

Work on database, add elements and file class to handle content.

Cixo 11 сар өмнө
parent
commit
9eeed7cfbf

+ 41 - 0
assets/element_builder.js

@@ -0,0 +1,41 @@
+import { type_manager } from "./functions.js";
+import { element } from "./element.js";
+
+class element_builder {
+    #name;
+    #description;
+    #pictures;
+    #meshes;
+    #solids;
+    #order;
+
+    constructor() {
+        this.#order = [];
+        this.#solids = [];
+        this.#meshes = [];
+        this.#pictures = [];
+        this.#name = undefined;
+        this.#description = undefined;
+    }
+
+    set name(target) {
+        if (!type_manager.is_string(target)) {
+            throw "Element name must be an string.";
+        }
+
+        this.#name = target;
+    }
+
+    set description(target) {
+        if (!type_manager.is_string(target)) {
+            throw "Description of the element must be an string.";
+        }
+
+        this.#description = target;
+    }
+
+    add_solid(target) { 
+    }
+}
+
+export { element_builder };

+ 62 - 0
assets/file.js

@@ -0,0 +1,62 @@
+import { type_manager } from "./functions.js";
+
+class file {
+    #name;
+    #path;
+    #extension;
+
+    constructor(path, windows_like = false) {
+        if (!type_manager.is_string(path)) {
+            throw "Path tu the file must be string.";
+        }
+
+        path = path.trim();
+
+        if (path.length === 0) {
+            throw "Path of the file can not be empty.";
+        }
+        
+        const separator = windows_like ? "\\" : "/";
+        const dirs = path.split(separator);
+        const full_filename = dirs.pop().trim();
+        const filename_parts = full_filename.split(".");
+        
+        this.#name = filename_parts.shift();
+        this.#extension = filename_parts.join(".")
+        this.#path = dirs.join("/");
+    }
+
+    get full_path() {
+        if (this.#path.length === 0) {
+            return this.filename;
+        }
+
+        return this.#path + "/" + this.filename;
+    }
+
+    get filename() {
+        if (this.#extension.length === 0) {
+            return this.#name;
+        }
+
+        return this.#name + "." + this.#extension;
+    }
+
+    get name() {
+        return this.#name;
+    }
+
+    get extension() {
+        return this.#extension;
+    }
+
+    get path() {
+        return this.#path;
+    }
+
+    get type() {
+        return this.#extension;
+    }
+} 
+
+export { file };

+ 20 - 1
tests/database.js

@@ -11,7 +11,26 @@ const sample = {
             "description": "This is first test submission.",
             "picture": "thumbnail_1.png",
             "elements": {
-                "what": {},
+                "what": {
+                    "description": "This is element named 'What'.",
+                    "pictures": [
+                        "what_1.png",
+                        "what_2.png",
+                        "what_3.png"
+                    ],
+                    "meshes": [
+                        "mesh_a.stl",
+                        "mesh_b.stl"
+                    ],
+                    "solids": [
+                        "solid_a.step",
+                        "solid_b.step"
+                    ],
+                    "order": [
+                        "https://allegro.pl/show/item_a",
+                        "https://e-bay.com/x/y"
+                    ]
+                },
                 "is": {},
                 "it": {}
             }

+ 19 - 0
tests/file.js

@@ -0,0 +1,19 @@
+import { file } from "../assets/file.js";
+
+const dump = (path) => {
+    const target = new file(path);
+
+    console.log("Input: " + path);
+    console.log("Full path: " + target.full_path);
+    console.log("Name: " + target.name);
+    console.log("Type: " + target.extension);
+    console.log("Path: " + target.path);
+    console.log("");
+}
+
+dump("name.txt");
+dump("./where_is_the/file.tar.gz");
+dump("/OwO/UwU/z.c");
+dump("test");
+
+