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.

validation.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.extractValidTemplateHeadings =
  6. exports.validateTemplateTableArguments =
  7. exports.validateArrayTable =
  8. void 0;
  9. function _chalk() {
  10. const data = _interopRequireDefault(require('chalk'));
  11. _chalk = function () {
  12. return data;
  13. };
  14. return data;
  15. }
  16. function _prettyFormat() {
  17. const data = require('pretty-format');
  18. _prettyFormat = function () {
  19. return data;
  20. };
  21. return data;
  22. }
  23. function _interopRequireDefault(obj) {
  24. return obj && obj.__esModule ? obj : {default: obj};
  25. }
  26. /**
  27. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  28. *
  29. * This source code is licensed under the MIT license found in the
  30. * LICENSE file in the root directory of this source tree.
  31. *
  32. */
  33. const EXPECTED_COLOR = _chalk().default.green;
  34. const RECEIVED_COLOR = _chalk().default.red;
  35. const validateArrayTable = table => {
  36. if (!Array.isArray(table)) {
  37. throw new Error(
  38. '`.each` must be called with an Array or Tagged Template Literal.\n\n' +
  39. `Instead was called with: ${(0, _prettyFormat().format)(table, {
  40. maxDepth: 1,
  41. min: true
  42. })}\n`
  43. );
  44. }
  45. if (isTaggedTemplateLiteral(table)) {
  46. if (isEmptyString(table[0])) {
  47. throw new Error(
  48. 'Error: `.each` called with an empty Tagged Template Literal of table data.\n'
  49. );
  50. }
  51. throw new Error(
  52. 'Error: `.each` called with a Tagged Template Literal with no data, remember to interpolate with ${expression} syntax.\n'
  53. );
  54. }
  55. if (isEmptyTable(table)) {
  56. throw new Error(
  57. 'Error: `.each` called with an empty Array of table data.\n'
  58. );
  59. }
  60. };
  61. exports.validateArrayTable = validateArrayTable;
  62. const isTaggedTemplateLiteral = array => array.raw !== undefined;
  63. const isEmptyTable = table => table.length === 0;
  64. const isEmptyString = str => typeof str === 'string' && str.trim() === '';
  65. const validateTemplateTableArguments = (headings, data) => {
  66. const missingData = data.length % headings.length;
  67. if (missingData > 0) {
  68. throw new Error(
  69. 'Not enough arguments supplied for given headings:\n' +
  70. EXPECTED_COLOR(headings.join(' | ')) +
  71. '\n\n' +
  72. 'Received:\n' +
  73. RECEIVED_COLOR((0, _prettyFormat().format)(data)) +
  74. '\n\n' +
  75. `Missing ${RECEIVED_COLOR(missingData.toString())} ${pluralize(
  76. 'argument',
  77. missingData
  78. )}`
  79. );
  80. }
  81. };
  82. exports.validateTemplateTableArguments = validateTemplateTableArguments;
  83. const pluralize = (word, count) => word + (count === 1 ? '' : 's');
  84. const START_OF_LINE = '^';
  85. const NEWLINE = '\\n';
  86. const HEADING = '\\s*[^\\s]+\\s*';
  87. const PIPE = '\\|';
  88. const REPEATABLE_HEADING = `(${PIPE}${HEADING})*`;
  89. const HEADINGS_FORMAT = new RegExp(
  90. START_OF_LINE + NEWLINE + HEADING + REPEATABLE_HEADING + NEWLINE,
  91. 'g'
  92. );
  93. const extractValidTemplateHeadings = headings => {
  94. const matches = headings.match(HEADINGS_FORMAT);
  95. if (matches === null) {
  96. throw new Error(
  97. 'Table headings do not conform to expected format:\n\n' +
  98. EXPECTED_COLOR('heading1 | headingN') +
  99. '\n\n' +
  100. 'Received:\n\n' +
  101. RECEIVED_COLOR((0, _prettyFormat().format)(headings))
  102. );
  103. }
  104. return matches[0];
  105. };
  106. exports.extractValidTemplateHeadings = extractValidTemplateHeadings;