parser.js 971 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { type_manager } from "./functions.js";
  2. class parser {
  3. #locale;
  4. #content;
  5. constructor(content, locale) {
  6. if (!(content instanceof Object)) {
  7. throw "Content to create parser from must be an object.";
  8. }
  9. if (type_manager.is_string(locale)) {
  10. throw "Locale of the content to show errors must be string.";
  11. }
  12. this.#content = content;
  13. this.#locale = locale;
  14. }
  15. exists(target) {
  16. if (!type_manager.is_string(target)) {
  17. throw "Name of the element to parse must be an string.";
  18. }
  19. return this.#content.hasOwnProperty(target);
  20. }
  21. get(target) {
  22. if (!this.exists(target)) {
  23. throw "Can not get " + target + " from " + this.#locale + ".";
  24. }
  25. return this.#content[target];
  26. }
  27. get_parser(target) {
  28. return new self(this.get(target), this.#locale + "." + target);
  29. }
  30. }
  31. export { parser };