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.

config-ops.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @fileoverview Config file operations. This file must be usable in the browser,
  3. * so no Node-specific code can be here.
  4. * @author Nicholas C. Zakas
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Private
  9. //------------------------------------------------------------------------------
  10. const RULE_SEVERITY_STRINGS = ["off", "warn", "error"],
  11. RULE_SEVERITY = RULE_SEVERITY_STRINGS.reduce((map, value, index) => {
  12. map[value] = index;
  13. return map;
  14. }, {}),
  15. VALID_SEVERITIES = [0, 1, 2, "off", "warn", "error"];
  16. //------------------------------------------------------------------------------
  17. // Public Interface
  18. //------------------------------------------------------------------------------
  19. module.exports = {
  20. /**
  21. * Normalizes the severity value of a rule's configuration to a number
  22. * @param {(number|string|[number, ...*]|[string, ...*])} ruleConfig A rule's configuration value, generally
  23. * received from the user. A valid config value is either 0, 1, 2, the string "off" (treated the same as 0),
  24. * the string "warn" (treated the same as 1), the string "error" (treated the same as 2), or an array
  25. * whose first element is one of the above values. Strings are matched case-insensitively.
  26. * @returns {(0|1|2)} The numeric severity value if the config value was valid, otherwise 0.
  27. */
  28. getRuleSeverity(ruleConfig) {
  29. const severityValue = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig;
  30. if (severityValue === 0 || severityValue === 1 || severityValue === 2) {
  31. return severityValue;
  32. }
  33. if (typeof severityValue === "string") {
  34. return RULE_SEVERITY[severityValue.toLowerCase()] || 0;
  35. }
  36. return 0;
  37. },
  38. /**
  39. * Converts old-style severity settings (0, 1, 2) into new-style
  40. * severity settings (off, warn, error) for all rules. Assumption is that severity
  41. * values have already been validated as correct.
  42. * @param {Object} config The config object to normalize.
  43. * @returns {void}
  44. */
  45. normalizeToStrings(config) {
  46. if (config.rules) {
  47. Object.keys(config.rules).forEach(ruleId => {
  48. const ruleConfig = config.rules[ruleId];
  49. if (typeof ruleConfig === "number") {
  50. config.rules[ruleId] = RULE_SEVERITY_STRINGS[ruleConfig] || RULE_SEVERITY_STRINGS[0];
  51. } else if (Array.isArray(ruleConfig) && typeof ruleConfig[0] === "number") {
  52. ruleConfig[0] = RULE_SEVERITY_STRINGS[ruleConfig[0]] || RULE_SEVERITY_STRINGS[0];
  53. }
  54. });
  55. }
  56. },
  57. /**
  58. * Determines if the severity for the given rule configuration represents an error.
  59. * @param {int|string|Array} ruleConfig The configuration for an individual rule.
  60. * @returns {boolean} True if the rule represents an error, false if not.
  61. */
  62. isErrorSeverity(ruleConfig) {
  63. return module.exports.getRuleSeverity(ruleConfig) === 2;
  64. },
  65. /**
  66. * Checks whether a given config has valid severity or not.
  67. * @param {number|string|Array} ruleConfig The configuration for an individual rule.
  68. * @returns {boolean} `true` if the configuration has valid severity.
  69. */
  70. isValidSeverity(ruleConfig) {
  71. let severity = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig;
  72. if (typeof severity === "string") {
  73. severity = severity.toLowerCase();
  74. }
  75. return VALID_SEVERITIES.indexOf(severity) !== -1;
  76. },
  77. /**
  78. * Checks whether every rule of a given config has valid severity or not.
  79. * @param {Object} config The configuration for rules.
  80. * @returns {boolean} `true` if the configuration has valid severity.
  81. */
  82. isEverySeverityValid(config) {
  83. return Object.keys(config).every(ruleId => this.isValidSeverity(config[ruleId]));
  84. },
  85. /**
  86. * Normalizes a value for a global in a config
  87. * @param {(boolean|string|null)} configuredValue The value given for a global in configuration or in
  88. * a global directive comment
  89. * @returns {("readable"|"writeable"|"off")} The value normalized as a string
  90. * @throws Error if global value is invalid
  91. */
  92. normalizeConfigGlobal(configuredValue) {
  93. switch (configuredValue) {
  94. case "off":
  95. return "off";
  96. case true:
  97. case "true":
  98. case "writeable":
  99. case "writable":
  100. return "writable";
  101. case null:
  102. case false:
  103. case "false":
  104. case "readable":
  105. case "readonly":
  106. return "readonly";
  107. default:
  108. throw new Error(`'${configuredValue}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`);
  109. }
  110. }
  111. };