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.

utils.js 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.validate = validate;
  6. exports.typeIs = typeIs;
  7. exports.validateType = validateType;
  8. exports.validateOptional = validateOptional;
  9. exports.validateOptionalType = validateOptionalType;
  10. exports.arrayOf = arrayOf;
  11. exports.arrayOfType = arrayOfType;
  12. exports.validateArrayOfType = validateArrayOfType;
  13. exports.assertEach = assertEach;
  14. exports.assertOneOf = assertOneOf;
  15. exports.assertNodeType = assertNodeType;
  16. exports.assertNodeOrValueType = assertNodeOrValueType;
  17. exports.assertValueType = assertValueType;
  18. exports.assertShape = assertShape;
  19. exports.assertOptionalChainStart = assertOptionalChainStart;
  20. exports.chain = chain;
  21. exports.default = defineType;
  22. exports.NODE_PARENT_VALIDATIONS = exports.DEPRECATED_KEYS = exports.BUILDER_KEYS = exports.NODE_FIELDS = exports.FLIPPED_ALIAS_KEYS = exports.ALIAS_KEYS = exports.VISITOR_KEYS = void 0;
  23. var _is = require("../validators/is");
  24. var _validate = require("../validators/validate");
  25. const VISITOR_KEYS = {};
  26. exports.VISITOR_KEYS = VISITOR_KEYS;
  27. const ALIAS_KEYS = {};
  28. exports.ALIAS_KEYS = ALIAS_KEYS;
  29. const FLIPPED_ALIAS_KEYS = {};
  30. exports.FLIPPED_ALIAS_KEYS = FLIPPED_ALIAS_KEYS;
  31. const NODE_FIELDS = {};
  32. exports.NODE_FIELDS = NODE_FIELDS;
  33. const BUILDER_KEYS = {};
  34. exports.BUILDER_KEYS = BUILDER_KEYS;
  35. const DEPRECATED_KEYS = {};
  36. exports.DEPRECATED_KEYS = DEPRECATED_KEYS;
  37. const NODE_PARENT_VALIDATIONS = {};
  38. exports.NODE_PARENT_VALIDATIONS = NODE_PARENT_VALIDATIONS;
  39. function getType(val) {
  40. if (Array.isArray(val)) {
  41. return "array";
  42. } else if (val === null) {
  43. return "null";
  44. } else {
  45. return typeof val;
  46. }
  47. }
  48. function validate(validate) {
  49. return {
  50. validate
  51. };
  52. }
  53. function typeIs(typeName) {
  54. return typeof typeName === "string" ? assertNodeType(typeName) : assertNodeType(...typeName);
  55. }
  56. function validateType(typeName) {
  57. return validate(typeIs(typeName));
  58. }
  59. function validateOptional(validate) {
  60. return {
  61. validate,
  62. optional: true
  63. };
  64. }
  65. function validateOptionalType(typeName) {
  66. return {
  67. validate: typeIs(typeName),
  68. optional: true
  69. };
  70. }
  71. function arrayOf(elementType) {
  72. return chain(assertValueType("array"), assertEach(elementType));
  73. }
  74. function arrayOfType(typeName) {
  75. return arrayOf(typeIs(typeName));
  76. }
  77. function validateArrayOfType(typeName) {
  78. return validate(arrayOfType(typeName));
  79. }
  80. function assertEach(callback) {
  81. function validator(node, key, val) {
  82. if (!Array.isArray(val)) return;
  83. for (let i = 0; i < val.length; i++) {
  84. const subkey = `${key}[${i}]`;
  85. const v = val[i];
  86. callback(node, subkey, v);
  87. if (process.env.BABEL_TYPES_8_BREAKING) (0, _validate.validateChild)(node, subkey, v);
  88. }
  89. }
  90. validator.each = callback;
  91. return validator;
  92. }
  93. function assertOneOf(...values) {
  94. function validate(node, key, val) {
  95. if (values.indexOf(val) < 0) {
  96. throw new TypeError(`Property ${key} expected value to be one of ${JSON.stringify(values)} but got ${JSON.stringify(val)}`);
  97. }
  98. }
  99. validate.oneOf = values;
  100. return validate;
  101. }
  102. function assertNodeType(...types) {
  103. function validate(node, key, val) {
  104. for (const type of types) {
  105. if ((0, _is.default)(type, val)) {
  106. (0, _validate.validateChild)(node, key, val);
  107. return;
  108. }
  109. }
  110. throw new TypeError(`Property ${key} of ${node.type} expected node to be of a type ${JSON.stringify(types)} but instead got ${JSON.stringify(val == null ? void 0 : val.type)}`);
  111. }
  112. validate.oneOfNodeTypes = types;
  113. return validate;
  114. }
  115. function assertNodeOrValueType(...types) {
  116. function validate(node, key, val) {
  117. for (const type of types) {
  118. if (getType(val) === type || (0, _is.default)(type, val)) {
  119. (0, _validate.validateChild)(node, key, val);
  120. return;
  121. }
  122. }
  123. throw new TypeError(`Property ${key} of ${node.type} expected node to be of a type ${JSON.stringify(types)} but instead got ${JSON.stringify(val == null ? void 0 : val.type)}`);
  124. }
  125. validate.oneOfNodeOrValueTypes = types;
  126. return validate;
  127. }
  128. function assertValueType(type) {
  129. function validate(node, key, val) {
  130. const valid = getType(val) === type;
  131. if (!valid) {
  132. throw new TypeError(`Property ${key} expected type of ${type} but got ${getType(val)}`);
  133. }
  134. }
  135. validate.type = type;
  136. return validate;
  137. }
  138. function assertShape(shape) {
  139. function validate(node, key, val) {
  140. const errors = [];
  141. for (const property of Object.keys(shape)) {
  142. try {
  143. (0, _validate.validateField)(node, property, val[property], shape[property]);
  144. } catch (error) {
  145. if (error instanceof TypeError) {
  146. errors.push(error.message);
  147. continue;
  148. }
  149. throw error;
  150. }
  151. }
  152. if (errors.length) {
  153. throw new TypeError(`Property ${key} of ${node.type} expected to have the following:\n${errors.join("\n")}`);
  154. }
  155. }
  156. validate.shapeOf = shape;
  157. return validate;
  158. }
  159. function assertOptionalChainStart() {
  160. function validate(node) {
  161. var _current;
  162. let current = node;
  163. while (node) {
  164. const {
  165. type
  166. } = current;
  167. if (type === "OptionalCallExpression") {
  168. if (current.optional) return;
  169. current = current.callee;
  170. continue;
  171. }
  172. if (type === "OptionalMemberExpression") {
  173. if (current.optional) return;
  174. current = current.object;
  175. continue;
  176. }
  177. break;
  178. }
  179. throw new TypeError(`Non-optional ${node.type} must chain from an optional OptionalMemberExpression or OptionalCallExpression. Found chain from ${(_current = current) == null ? void 0 : _current.type}`);
  180. }
  181. return validate;
  182. }
  183. function chain(...fns) {
  184. function validate(...args) {
  185. for (const fn of fns) {
  186. fn(...args);
  187. }
  188. }
  189. validate.chainOf = fns;
  190. if (fns.length >= 2 && "type" in fns[0] && fns[0].type === "array" && !("each" in fns[1])) {
  191. throw new Error(`An assertValueType("array") validator can only be followed by an assertEach(...) validator.`);
  192. }
  193. return validate;
  194. }
  195. const validTypeOpts = ["aliases", "builder", "deprecatedAlias", "fields", "inherits", "visitor", "validate"];
  196. const validFieldKeys = ["default", "optional", "validate"];
  197. function defineType(type, opts = {}) {
  198. const inherits = opts.inherits && store[opts.inherits] || {};
  199. let fields = opts.fields;
  200. if (!fields) {
  201. fields = {};
  202. if (inherits.fields) {
  203. const keys = Object.getOwnPropertyNames(inherits.fields);
  204. for (const key of keys) {
  205. const field = inherits.fields[key];
  206. const def = field.default;
  207. if (Array.isArray(def) ? def.length > 0 : def && typeof def === "object") {
  208. throw new Error("field defaults can only be primitives or empty arrays currently");
  209. }
  210. fields[key] = {
  211. default: Array.isArray(def) ? [] : def,
  212. optional: field.optional,
  213. validate: field.validate
  214. };
  215. }
  216. }
  217. }
  218. const visitor = opts.visitor || inherits.visitor || [];
  219. const aliases = opts.aliases || inherits.aliases || [];
  220. const builder = opts.builder || inherits.builder || opts.visitor || [];
  221. for (const k of Object.keys(opts)) {
  222. if (validTypeOpts.indexOf(k) === -1) {
  223. throw new Error(`Unknown type option "${k}" on ${type}`);
  224. }
  225. }
  226. if (opts.deprecatedAlias) {
  227. DEPRECATED_KEYS[opts.deprecatedAlias] = type;
  228. }
  229. for (const key of visitor.concat(builder)) {
  230. fields[key] = fields[key] || {};
  231. }
  232. for (const key of Object.keys(fields)) {
  233. const field = fields[key];
  234. if (field.default !== undefined && builder.indexOf(key) === -1) {
  235. field.optional = true;
  236. }
  237. if (field.default === undefined) {
  238. field.default = null;
  239. } else if (!field.validate && field.default != null) {
  240. field.validate = assertValueType(getType(field.default));
  241. }
  242. for (const k of Object.keys(field)) {
  243. if (validFieldKeys.indexOf(k) === -1) {
  244. throw new Error(`Unknown field key "${k}" on ${type}.${key}`);
  245. }
  246. }
  247. }
  248. VISITOR_KEYS[type] = opts.visitor = visitor;
  249. BUILDER_KEYS[type] = opts.builder = builder;
  250. NODE_FIELDS[type] = opts.fields = fields;
  251. ALIAS_KEYS[type] = opts.aliases = aliases;
  252. aliases.forEach(alias => {
  253. FLIPPED_ALIAS_KEYS[alias] = FLIPPED_ALIAS_KEYS[alias] || [];
  254. FLIPPED_ALIAS_KEYS[alias].push(type);
  255. });
  256. if (opts.validate) {
  257. NODE_PARENT_VALIDATIONS[type] = opts.validate;
  258. }
  259. store[type] = opts;
  260. }
  261. const store = {};