mirror of
https://github.com/Kornstalx/5etools-mirror-2.github.io.git
synced 2025-10-28 20:45:35 -05:00
v1.199.3
This commit is contained in:
@@ -1,11 +1,57 @@
|
||||
import "../js/parser.js";
|
||||
import "../js/utils.js";
|
||||
import * as ut from "../node/util.js";
|
||||
import {BLOCKLIST_SOURCES_PAGES} from "./util-test.js";
|
||||
|
||||
function getErrorMonotonicallyIncreasing ({walker, filename, json, source}) {
|
||||
const stack = [];
|
||||
let pagePrev = -1;
|
||||
const errorsFile = [];
|
||||
|
||||
walker.walk(
|
||||
json,
|
||||
{
|
||||
object: (obj) => {
|
||||
if (obj.page == null || typeof obj.page !== "number") return;
|
||||
if (obj.page < pagePrev) {
|
||||
const id = obj.id || [...stack].reverse().find(it => it.id)?.id;
|
||||
const name = obj.name || [...stack].reverse().find(it => it.name)?.name;
|
||||
errorsFile.push(`Previous page ${pagePrev} > ${obj.page}${id || name ? ` (at or near ${[id ? `id "${id}"` : "", name ? `name "${name}"` : ""].filter(Boolean).join("; ")})` : ""}`);
|
||||
}
|
||||
pagePrev = obj.page;
|
||||
},
|
||||
},
|
||||
null,
|
||||
stack,
|
||||
);
|
||||
|
||||
if (!errorsFile.length) return null;
|
||||
|
||||
return `Page numbers were not monotonically increasing in "${filename}":\n${errorsFile.map(err => `\t${err}\n`).join("")}\n`;
|
||||
}
|
||||
|
||||
// TODO(Future) could e.g. objects; count pages; if ratio below threshold then error
|
||||
function getErrorNoPages ({walker, filename, json, source}) {
|
||||
if (BLOCKLIST_SOURCES_PAGES.has(source)) return null;
|
||||
|
||||
let hasPage = false;
|
||||
walker.walk(
|
||||
json,
|
||||
{
|
||||
object: (obj) => {
|
||||
if (obj.page != null && (typeof obj.page !== "number" || obj.page !== 0)) return hasPage = true;
|
||||
},
|
||||
},
|
||||
);
|
||||
if (hasPage) return null;
|
||||
|
||||
return `No page numbers found in "${filename}"\n`;
|
||||
}
|
||||
|
||||
async function main () {
|
||||
console.log(`##### Validating adventure/book page numbers #####`);
|
||||
|
||||
const walker = MiscUtil.getWalker({isNoModification: true});
|
||||
const walker = MiscUtil.getWalker({isNoModification: true, isBreakOnReturn: true});
|
||||
|
||||
const errors = [];
|
||||
|
||||
@@ -13,34 +59,18 @@ async function main () {
|
||||
{filename: "adventures.json", prop: "adventure", dir: "adventure"},
|
||||
{filename: "books.json", prop: "book", dir: "book"},
|
||||
].flatMap(({filename, prop, dir}) => ut.readJson(`./data/${filename}`)[prop]
|
||||
.map(({id}) => `./data/${dir}/${dir}-${id.toLowerCase()}.json`))
|
||||
.forEach(filename => {
|
||||
const stack = [];
|
||||
let pagePrev = -1;
|
||||
const errorsFile = [];
|
||||
.map(({id, source}) => ({
|
||||
filename: `./data/${dir}/${dir}-${id.toLowerCase()}.json`,
|
||||
source,
|
||||
})))
|
||||
.forEach(({filename, source}) => {
|
||||
const json = ut.readJson(filename);
|
||||
|
||||
walker.walk(
|
||||
ut.readJson(filename),
|
||||
{
|
||||
object: (obj) => {
|
||||
if (obj.page == null || typeof obj.page !== "number") return obj;
|
||||
if (obj.page < pagePrev) {
|
||||
const id = obj.id || [...stack].reverse().find(it => it.id)?.id;
|
||||
const name = obj.name || [...stack].reverse().find(it => it.name)?.name;
|
||||
errorsFile.push(`Previous page ${pagePrev} > ${obj.page}${id || name ? ` (at or near ${[id ? `id "${id}"` : "", name ? `name "${name}"` : ""].filter(Boolean).join("; ")})` : ""}`);
|
||||
}
|
||||
pagePrev = obj.page;
|
||||
const errorMonotonicallyIncreasing = getErrorMonotonicallyIncreasing({walker, filename, json, source});
|
||||
if (errorMonotonicallyIncreasing) errors.push(errorMonotonicallyIncreasing);
|
||||
|
||||
return obj;
|
||||
},
|
||||
},
|
||||
null,
|
||||
stack,
|
||||
);
|
||||
|
||||
if (!errorsFile.length) return;
|
||||
|
||||
errors.push(`Page numbers were not monotonically increasing in "${filename}":\n${errorsFile.map(err => `\t${err}\n`).join("")}\n`);
|
||||
const errorNoPages = getErrorNoPages({walker, filename, json, source});
|
||||
if (errorNoPages) errors.push(errorNoPages);
|
||||
});
|
||||
|
||||
if (errors.length) {
|
||||
|
||||
Reference in New Issue
Block a user