validation.js 2.5 KB

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