Skip to content

Commit

Permalink
Import: Fix sets prettifier
Browse files Browse the repository at this point in the history
`setName` can contain illegal JSON values such as `"` so we
`JSON.stringify` the name to make sure its valid JSON.
  • Loading branch information
thejetou committed Nov 16, 2023
1 parent 8ac6192 commit 85902ce
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function cpdir(src, dest) {
// Minify the sets so we save bandwith
if (src === 'src/js/data/sets') {
const brkOpenIdx = contents.indexOf('{');
contents = `${contents.slice(0, brkOpenIdx)} = ${JSON.stringify(JSON.parse(contents.slice(brkOpenIdx, -2)))};`
contents = `${contents.slice(0, brkOpenIdx)}${JSON.stringify(JSON.parse(contents.slice(brkOpenIdx, -2)))};`
}
fs.writeFileSync(path.join(dest, file), contents);
} else {
Expand Down
35 changes: 18 additions & 17 deletions import/src/set-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,16 @@ async function importGen(
statsIgnore[specieName].add(formatID);
const item = gen.items.get(pset.item) ?? ModdedDex.forGen(gen.num).items.get(pset.item);
const copyTo = similarFormes(pset, format, specie, item);
// Quintuple loop... yikes
// Quintuple loop... yikes
for (const forme of copyTo.formes) {
if (!statsIgnore[forme]) statsIgnore[forme] = new Set();
statsIgnore[forme].add(formatID);
if (!calcSets[forme]) calcSets[forme] = {};
if (!statsIgnore[forme]) statsIgnore[forme] = new Set();
statsIgnore[forme].add(formatID);
if (!calcSets[forme]) calcSets[forme] = {};
if (copyTo.abilityChange) {
const formeSpecie = getSpecie(gen, forme);
calcSets[forme][setName] = {...calcSet, ability: formeSpecie.abilities[0]};
} else {
calcSets[forme][setName] = calcSet;
} else {
calcSets[forme][setName] = calcSet;
}
}
}
Expand Down Expand Up @@ -467,15 +467,15 @@ async function importGen(
const item = gen.items.get(pset.item) ?? ModdedDex.forGen(gen.num).items.get(pset.item);
const copyTo = similarFormes(pset, format, specie, item);
for (const forme of copyTo.formes) {
if (statsIgnore[forme]?.has(formatID)) {
continue;
}
if (!calcSets[forme]) calcSets[forme] = {};
if (statsIgnore[forme]?.has(formatID)) {
continue;
}
if (!calcSets[forme]) calcSets[forme] = {};
if (copyTo.abilityChange) {
const formeSpecie = getSpecie(gen, forme);
calcSets[forme][setName] = {...calcSet, ability: formeSpecie.abilities[0]};
} else {
calcSets[forme][setName] = calcSet;
} else {
calcSets[forme][setName] = calcSet;
}
}
}
Expand All @@ -485,21 +485,22 @@ async function importGen(

// JSON.stringify's pretty option takes too much space since it gives every element
// a separate newline so we write our custom stringify which makes it pretty but still
// take a reasonable amount of space. The build script minfies it so its OK
// take a reasonable amount of space. The build script minfies it so this has no effect
// on the bundle size
function stringifyCalcSets(calcSets: {[specie: string]: {[name: string]: CalcSet }}) {
let buf = [];
const space = (n = 2) => " ".repeat(n);
const buf = [];
const space = (n = 2) => ' '.repeat(n);
for (const [specie, sets] of Object.entries(calcSets)) {
buf.push(`${space()}"${specie}": {`);
const setsBuf = [];
for (const [setName, set] of Object.entries(sets)) {
setsBuf.push(`${space(4)}"${setName}": ${JSON.stringify(set)}`);
setsBuf.push(`${space(4)}${JSON.stringify(setName)}: ${JSON.stringify(set)}`);
}
buf.push(setsBuf.join(',\n'));
buf.push(`${space()}},`);
}
// Slice the last comma so we are valid JSON
return buf.join(`\n`).slice(0, -1);
return buf.join('\n').slice(0, -1);
}

(async () => {
Expand Down

0 comments on commit 85902ce

Please sign in to comment.