Ohm-Management - Projektarbeit B-ME
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.

validateTableData.js.flow 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. // @todo Make an exception for newline characters.
  30. // @see https://github.com/gajus/table/issues/9
  31. for (const cell of cells) {
  32. // eslint-disable-next-line no-control-regex
  33. if (/[\u0001-\u001A]/.test(cell)) {
  34. throw new Error('Table data must not contain control characters.');
  35. }
  36. }
  37. }
  38. };