5etools-utils 0.13.42 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/TestData.js +54 -47
- package/package.json +2 -1
package/lib/TestData.js
CHANGED
|
@@ -4,12 +4,12 @@ import {ObjectWalker} from "./ObjectWalker.js";
|
|
|
4
4
|
|
|
5
5
|
/** Runs multiple handlers on each file, to avoid re-reading each file for each handler */
|
|
6
6
|
class _ParsedJsonChecker {
|
|
7
|
-
static
|
|
7
|
+
static _FILE_HANDLERS = [];
|
|
8
8
|
|
|
9
9
|
static _PRIMITIVE_HANDLERS = {};
|
|
10
10
|
|
|
11
|
-
static registerFileHandler (
|
|
12
|
-
|
|
11
|
+
static registerFileHandler (fileHandler) {
|
|
12
|
+
this._FILE_HANDLERS.push(fileHandler);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
static addPrimitiveHandler (primitiveType, handler) {
|
|
@@ -19,20 +19,22 @@ class _ParsedJsonChecker {
|
|
|
19
19
|
|
|
20
20
|
/* -------------------------------------------- */
|
|
21
21
|
|
|
22
|
-
static
|
|
23
|
-
this.
|
|
22
|
+
static async pRun (filePath, {fnIsIgnoredFile, fnIsIgnoredDirectory} = {}) {
|
|
23
|
+
await this._pFileRecurse({
|
|
24
24
|
filePath,
|
|
25
25
|
fnIsIgnoredFile,
|
|
26
26
|
fnIsIgnoredDirectory,
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
static
|
|
30
|
+
static async _pFileRecurse ({filePath, fnIsIgnoredFile, fnIsIgnoredDirectory}) {
|
|
31
31
|
if (fs.lstatSync(filePath).isDirectory()) {
|
|
32
32
|
if (fnIsIgnoredDirectory && fnIsIgnoredDirectory(filePath)) return;
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
for (const nxt of fs.readdirSync(filePath)) {
|
|
35
|
+
await this._pFileRecurse({filePath: `${filePath}/${nxt}`, fnIsIgnoredFile, fnIsIgnoredDirectory});
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
if (!filePath.endsWith(".json") || (fnIsIgnoredFile && fnIsIgnoredFile(filePath))) return;
|
|
@@ -45,50 +47,55 @@ class _ParsedJsonChecker {
|
|
|
45
47
|
primitiveHandlers: this._PRIMITIVE_HANDLERS,
|
|
46
48
|
});
|
|
47
49
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
for (const dataTester of this._FILE_HANDLERS) {
|
|
51
|
+
dataTester.handleFile(filePath, contents);
|
|
52
|
+
await dataTester.pHandleFile(filePath, contents);
|
|
53
|
+
dataTester.addMessageBoundary();
|
|
54
|
+
}
|
|
50
55
|
}
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
class DataTesterBase {
|
|
54
|
-
|
|
59
|
+
_message = "";
|
|
55
60
|
|
|
56
|
-
|
|
57
|
-
this.
|
|
61
|
+
_addMessage (str) {
|
|
62
|
+
this._message += str;
|
|
58
63
|
}
|
|
59
64
|
|
|
60
|
-
|
|
61
|
-
if (!this.
|
|
62
|
-
if (this.
|
|
63
|
-
this.
|
|
64
|
-
this.
|
|
65
|
+
addMessageBoundary () {
|
|
66
|
+
if (!this._message.trim()) return;
|
|
67
|
+
if (this._message.slice(-5) === "\n---\n") return;
|
|
68
|
+
this._message = this._message.trimEnd();
|
|
69
|
+
this._message += `\n---\n`;
|
|
65
70
|
}
|
|
66
71
|
|
|
67
|
-
|
|
72
|
+
getMessage () { return this._message; }
|
|
68
73
|
|
|
69
74
|
/* -------------------------------------------- */
|
|
70
75
|
|
|
71
|
-
|
|
76
|
+
async pRun () { /* Implement as required */ }
|
|
72
77
|
|
|
73
|
-
|
|
78
|
+
async pPostRun () { /* Implement as required */ }
|
|
74
79
|
|
|
75
80
|
/* -------------------------------------------- */
|
|
76
81
|
|
|
77
|
-
|
|
82
|
+
registerParsedFileCheckers (parsedJsonChecker) { /* Implement as required */ }
|
|
78
83
|
|
|
79
|
-
|
|
84
|
+
registerParsedPrimitiveHandlers (parsedJsonChecker) { /* Implement as required */ }
|
|
80
85
|
|
|
81
86
|
/* -------------------------------------------- */
|
|
82
87
|
|
|
83
|
-
|
|
88
|
+
handleFile (filePath, contents) { /* Implement as required */ }
|
|
89
|
+
|
|
90
|
+
async pHandleFile (filePath, contents) { /* Implement as required */ }
|
|
84
91
|
}
|
|
85
92
|
|
|
86
93
|
class BraceCheck extends DataTesterBase {
|
|
87
|
-
|
|
94
|
+
registerParsedPrimitiveHandlers (parsedJsonChecker) {
|
|
88
95
|
parsedJsonChecker.addPrimitiveHandler("string", this._checkString.bind(this));
|
|
89
96
|
}
|
|
90
97
|
|
|
91
|
-
|
|
98
|
+
_checkString (str, {filePath}) {
|
|
92
99
|
let total = 0;
|
|
93
100
|
for (let i = 0; i < str.length; ++i) {
|
|
94
101
|
const c = str[i];
|
|
@@ -110,23 +117,23 @@ class BraceCheck extends DataTesterBase {
|
|
|
110
117
|
class EscapeCharacterCheck extends DataTesterBase {
|
|
111
118
|
static _CHARS = 16;
|
|
112
119
|
|
|
113
|
-
|
|
120
|
+
_errors = [];
|
|
114
121
|
|
|
115
|
-
|
|
122
|
+
registerParsedFileCheckers (parsedJsonChecker) {
|
|
116
123
|
parsedJsonChecker.registerFileHandler(this);
|
|
117
124
|
}
|
|
118
125
|
|
|
119
|
-
|
|
126
|
+
_checkString (str) {
|
|
120
127
|
let re = /([\n\t\r])/g;
|
|
121
128
|
let m;
|
|
122
129
|
while ((m = re.exec(str))) {
|
|
123
|
-
const startIx = Math.max(m.index - this._CHARS, 0);
|
|
124
|
-
const endIx = Math.min(m.index + this._CHARS, str.length);
|
|
130
|
+
const startIx = Math.max(m.index - this.constructor._CHARS, 0);
|
|
131
|
+
const endIx = Math.min(m.index + this.constructor._CHARS, str.length);
|
|
125
132
|
this._errors.push(`...${str.substring(startIx, endIx)}...`.replace(/[\n\t\r]/g, (...m) => m[0] === "\n" ? "***\\n***" : m[0] === "\t" ? "***\\t***" : "***\\r***"));
|
|
126
133
|
}
|
|
127
134
|
}
|
|
128
135
|
|
|
129
|
-
|
|
136
|
+
handleFile (file, contents) {
|
|
130
137
|
this._errors = [];
|
|
131
138
|
ObjectWalker.walk({
|
|
132
139
|
obj: contents,
|
|
@@ -145,40 +152,40 @@ class EscapeCharacterCheck extends DataTesterBase {
|
|
|
145
152
|
class DataTester {
|
|
146
153
|
static register (
|
|
147
154
|
{
|
|
148
|
-
|
|
155
|
+
dataTesters,
|
|
149
156
|
},
|
|
150
157
|
) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
158
|
+
dataTesters.forEach(dataTester => {
|
|
159
|
+
dataTester.registerParsedPrimitiveHandlers(_ParsedJsonChecker);
|
|
160
|
+
dataTester.registerParsedFileCheckers(_ParsedJsonChecker);
|
|
154
161
|
});
|
|
155
162
|
}
|
|
156
163
|
|
|
157
164
|
static async pRun (
|
|
158
165
|
filePath,
|
|
159
|
-
|
|
166
|
+
dataTesters,
|
|
160
167
|
{
|
|
161
168
|
fnIsIgnoredFile,
|
|
162
169
|
fnIsIgnoredDirectory,
|
|
163
170
|
} = {},
|
|
164
171
|
) {
|
|
165
|
-
_ParsedJsonChecker.
|
|
172
|
+
await _ParsedJsonChecker.pRun(filePath, {fnIsIgnoredFile, fnIsIgnoredDirectory});
|
|
166
173
|
|
|
167
|
-
for (const
|
|
168
|
-
await
|
|
174
|
+
for (const dataTester of dataTesters) {
|
|
175
|
+
await dataTester.pRun();
|
|
169
176
|
}
|
|
170
177
|
|
|
171
|
-
for (const
|
|
172
|
-
await
|
|
178
|
+
for (const dataTester of dataTesters) {
|
|
179
|
+
await dataTester.pPostRun();
|
|
173
180
|
}
|
|
174
181
|
}
|
|
175
182
|
|
|
176
|
-
static getLogReport (
|
|
183
|
+
static getLogReport (dataTesters) {
|
|
177
184
|
let outMessage = "";
|
|
178
|
-
for (const
|
|
179
|
-
const pt =
|
|
180
|
-
if (pt) outMessage += `Error messages for ${
|
|
181
|
-
else console.log(`##### ${
|
|
185
|
+
for (const dataTester of dataTesters) {
|
|
186
|
+
const pt = dataTester.getMessage();
|
|
187
|
+
if (pt) outMessage += `Error messages for ${dataTester.constructor.name}:\n\n${pt}\n`;
|
|
188
|
+
else console.log(`##### ${dataTester.constructor.name} passed! #####`);
|
|
182
189
|
}
|
|
183
190
|
|
|
184
191
|
if (outMessage) console.error(outMessage);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "5etools-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.1",
|
|
4
4
|
"description": "Shared utilities for the 5etools ecosystem.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/Api.js",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"build": "node node/compile-schemas.js",
|
|
24
24
|
"test": "npm run lint:js && npm run test:js",
|
|
25
25
|
"test:js": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
|
|
26
|
+
"lint": "npm run lint:js",
|
|
26
27
|
"lint:js": "eslint lib node test --fix",
|
|
27
28
|
"preversion": "npm t"
|
|
28
29
|
},
|