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.

44 lines
993 B

4 years ago
  1. /**
  2. * @typedef {string} cell
  3. */
  4. /**
  5. * @typedef {cell[]} validateData~column
  6. */
  7. /**
  8. * @param {column[]} rows
  9. * @returns {undefined}
  10. */
  11. export default (rows) => {
  12. if (!Array.isArray(rows)) {
  13. throw new TypeError('Table data must be an array.');
  14. }
  15. if (rows.length === 0) {
  16. throw new Error('Table must define at least one row.');
  17. }
  18. if (rows[0].length === 0) {
  19. throw new Error('Table must define at least one column.');
  20. }
  21. const columnNumber = rows[0].length;
  22. for (const cells of rows) {
  23. if (!Array.isArray(cells)) {
  24. throw new TypeError('Table row data must be an array.');
  25. }
  26. if (cells.length !== columnNumber) {
  27. throw new Error('Table must have a consistent number of cells.');
  28. }
  29. for (const cell of cells) {
  30. // eslint-disable-next-line no-control-regex
  31. if (/[\u0001-\u0006\u0008-\u0009\u000B-\u001A]/.test(cell)) {
  32. throw new Error('Table data must not contain control characters.');
  33. }
  34. }
  35. }
  36. };