-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecks.js
More file actions
108 lines (94 loc) · 3.05 KB
/
checks.js
File metadata and controls
108 lines (94 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
/**
* Check the contents of a single cell against the rules for that column
* @param {string} cell Contents of a single cell
* @param {Object} colDef Definition of that column from the schema file
* @return {string[]} Array of messages describing problems found, empty if OK
*/
const checkCell = (cell, colDef) => {
const msgs = [];
// Column must actually exist, unless optional (not the same thing as not being empty)
// (If numbered, must be enough columns. If named, must appear in the header.)
if (cell === undefined) {
if (colDef.optional) {
return [];
} else {
return ['no such column'];
}
}
// Column must/mustn't be empty
if (colDef.isEmpty) {
if (cell !== '') {
msgs.push('isn\'t empty when it should be');
}
} else if (colDef.noEmpty) {
if (cell === '') {
msgs.push('is empty when it shouldn\'t be');
}
}
// Column must consist only of 7-bit ASCII printing characters
if (colDef.isAscii) {
const buf = Buffer.from(cell);
for (let i = 0; i < buf.length; i++) {
if (buf[i] < 0x20 || buf[i] > 0x7E) {
msgs.push('contains characters other than 7-bit ASCII printing characters');
break;
}
}
}
// Column must not contain leading or trailing whitespace
if (colDef.isTrim) {
if (cell !== cell.trim()) {
msgs.push('contains leading or trailing whitespace');
}
}
// Column must match a regex (use ^ and $ for complete/exact match)
if (colDef.regex) {
const regex = new RegExp(colDef.regex);
if (!regex.test(cell)) {
msgs.push('does not match regular expression');
}
}
// Column must parse as a finite integer
if (colDef.isInt) {
if (!isFinite(parseInt(cell))) {
msgs.push('is not a finite integer');
}
}
// Column must parse as a finite floating-point number (or an integer)
if (colDef.isFloat) {
if (!isFinite(parseFloat(cell))) {
msgs.push('is not a finite floating-point number');
}
}
// Column must meet a minimum (numeric or string) value
if (typeof colDef.minVal === 'number') {
if (!(parseFloat(cell) >= colDef.minVal)) {
msgs.push('is below minimum numeric value or is not a number');
}
} else if (typeof colDef.minVal === 'string') {
if (!(cell >= colDef.minVal)) {
msgs.push('sorts before minimum string value');
}
}
// Column must meet a maximum (numeric or string) value
if (typeof colDef.maxVal === 'number') {
if (!(parseFloat(cell) <= colDef.maxVal)) {
msgs.push('is above maximum numeric value or is not a number');
}
} else if (typeof colDef.maxVal === 'string') {
if (!(cell <= colDef.maxVal)) {
msgs.push('sorts after maximum string value');
}
}
// Column must meet a minimum string length
if (new String(cell).length < colDef.minLen) {
msgs.push('is too short as a string');
}
// Column must meet a maximum string length
if (new String(cell).length > colDef.maxLen) {
msgs.push('is too long as a string');
}
return msgs;
};
module.exports = { checkCell };