database.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { element } from "./element.js";
  2. import { submission } from "./submission.js";
  3. class database {
  4. #submissions;
  5. constructor() {
  6. this.clean();
  7. }
  8. search(input) {
  9. const phrase = input.trim();
  10. const found = [];
  11. this.submissions.forEach(name => {
  12. const current = this.#submissions[name];
  13. if (name.search(phrase) !== -1) {
  14. found.push(current);
  15. }
  16. current.elemenets.forEach(name => {
  17. if (name.search(phrase) !== -1) {
  18. found.push(current.get(name));
  19. }
  20. });
  21. });
  22. return found;
  23. }
  24. clean() {
  25. this.#submissions = {};
  26. }
  27. add(container) {
  28. if (!(container instanceof submission)) {
  29. throw "Trying to add something which is not an submission.";
  30. }
  31. if (container.name in this.#submissions) {
  32. throw "Trying to add submission which already exists.";
  33. }
  34. this.#submissions[container.name] = container;
  35. }
  36. get submissions() {
  37. return Object.keys(this.#submissions);
  38. }
  39. get(name) {
  40. if (typeof(name) !== "string") {
  41. throw "Submission name must be an string.";
  42. }
  43. if (!(name in this.#submissions)) {
  44. throw "Trying to get submission which not exists.";
  45. }
  46. return this.#submissions[name];
  47. }
  48. }
  49. export { database };