database.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { element } from "./element.js";
  2. import { submission } from "./submission.js";
  3. import { project } from "./project.js";
  4. import { is_string } from "./functions.js";
  5. class database {
  6. #content;
  7. constructor(content) {
  8. if (!(content instanceof project)) {
  9. throw "Content must be instance of an project.";
  10. }
  11. this.#content = content;
  12. }
  13. get content() {
  14. return this.#content;
  15. }
  16. search(input) {
  17. if (!is_string(input)) {
  18. throw "Phrase to search must be string.";
  19. }
  20. const content = this.#content;
  21. const phrase = input.trim().toLowerCase();
  22. const result = [];
  23. content.submissions.forEach(submission => {
  24. if (submission.name.toLowerCase().search(phrase) !== -1 ) {
  25. result.push(submission);
  26. }
  27. submission.elements.forEach(element => {
  28. if (element.name.toLowerCase().search(phrase) !== -1) {
  29. result.push(element);
  30. }
  31. });
  32. });
  33. return result;
  34. }
  35. }
  36. export { database };