validateTableData.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. /**
  6. * @typedef {string} cell
  7. */
  8. /**
  9. * @typedef {cell[]} validateData~column
  10. */
  11. /**
  12. * @param {column[]} rows
  13. * @returns {undefined}
  14. */
  15. exports.default = rows => {
  16. if (!Array.isArray(rows)) {
  17. throw new TypeError('Table data must be an array.');
  18. }
  19. if (rows.length === 0) {
  20. throw new Error('Table must define at least one row.');
  21. }
  22. if (rows[0].length === 0) {
  23. throw new Error('Table must define at least one column.');
  24. }
  25. const columnNumber = rows[0].length;
  26. for (const cells of rows) {
  27. if (!Array.isArray(cells)) {
  28. throw new TypeError('Table row data must be an array.');
  29. }
  30. if (cells.length !== columnNumber) {
  31. throw new Error('Table must have a consistent number of cells.');
  32. }
  33. // @todo Make an exception for newline characters.
  34. // @see https://github.com/gajus/table/issues/9
  35. for (const cell of cells) {
  36. if (/[\u0001-\u001A]/.test(cell)) {
  37. throw new Error('Table data must not contain control characters.');
  38. }
  39. }
  40. }
  41. };