Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rules.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @fileoverview Defines a storage for rules.
  3. * @author Nicholas C. Zakas
  4. * @author aladdin-add
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. const builtInRules = require("../rules");
  11. //------------------------------------------------------------------------------
  12. // Helpers
  13. //------------------------------------------------------------------------------
  14. /**
  15. * Normalizes a rule module to the new-style API
  16. * @param {(Function|{create: Function})} rule A rule object, which can either be a function
  17. * ("old-style") or an object with a `create` method ("new-style")
  18. * @returns {{create: Function}} A new-style rule.
  19. */
  20. function normalizeRule(rule) {
  21. return typeof rule === "function" ? Object.assign({ create: rule }, rule) : rule;
  22. }
  23. //------------------------------------------------------------------------------
  24. // Public Interface
  25. //------------------------------------------------------------------------------
  26. class Rules {
  27. constructor() {
  28. this._rules = Object.create(null);
  29. }
  30. /**
  31. * Registers a rule module for rule id in storage.
  32. * @param {string} ruleId Rule id (file name).
  33. * @param {Function} ruleModule Rule handler.
  34. * @returns {void}
  35. */
  36. define(ruleId, ruleModule) {
  37. this._rules[ruleId] = normalizeRule(ruleModule);
  38. }
  39. /**
  40. * Access rule handler by id (file name).
  41. * @param {string} ruleId Rule id (file name).
  42. * @returns {{create: Function, schema: JsonSchema[]}}
  43. * A rule. This is normalized to always have the new-style shape with a `create` method.
  44. */
  45. get(ruleId) {
  46. if (typeof this._rules[ruleId] === "string") {
  47. this.define(ruleId, require(this._rules[ruleId]));
  48. }
  49. if (this._rules[ruleId]) {
  50. return this._rules[ruleId];
  51. }
  52. if (builtInRules.has(ruleId)) {
  53. return builtInRules.get(ruleId);
  54. }
  55. return null;
  56. }
  57. *[Symbol.iterator]() {
  58. yield* builtInRules;
  59. for (const ruleId of Object.keys(this._rules)) {
  60. yield [ruleId, this.get(ruleId)];
  61. }
  62. }
  63. }
  64. module.exports = Rules;