blob: 56a3e19024c473bedbf947baf34b77f235206cc2 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8/**
9 * @typedef {string} cell
10 */
11
12/**
13 * @typedef {cell[]} validateData~column
14 */
15
16/**
17 * @param {column[]} rows
18 * @returns {undefined}
19 */
20const validateTableData = rows => {
21 if (!Array.isArray(rows)) {
22 throw new TypeError('Table data must be an array.');
23 }
24
25 if (rows.length === 0) {
26 throw new Error('Table must define at least one row.');
27 }
28
29 if (rows[0].length === 0) {
30 throw new Error('Table must define at least one column.');
31 }
32
33 const columnNumber = rows[0].length;
Yang Guo4fd355c2019-09-19 10:59:03 +020034
Tim van der Lippeb97da6b2021-02-12 14:32:53 +000035 for (const cells of rows) {
36 if (!Array.isArray(cells)) {
37 throw new TypeError('Table row data must be an array.');
Yang Guo4fd355c2019-09-19 10:59:03 +020038 }
Tim van der Lippeb97da6b2021-02-12 14:32:53 +000039
40 if (cells.length !== columnNumber) {
41 throw new Error('Table must have a consistent number of cells.');
42 }
43
44 for (const cell of cells) {
45 // eslint-disable-next-line no-control-regex
46 if (/[\u0001-\u0006\u0008\u0009\u000B-\u001A]/.test(cell)) {
47 throw new Error('Table data must not contain control characters.');
Yang Guo4fd355c2019-09-19 10:59:03 +020048 }
49 }
50 }
51};
52
53var _default = validateTableData;
54exports.default = _default;
55//# sourceMappingURL=validateTableData.js.map