Reputation: 21
I'm working on a project in Node.js (Foundry Virtual Table Top), and I have two arrays. First is a list of entries, the second is the same list, but with indexing numbers prefixed to it. I need to match the two, based on the name, and update the first list to include the same numbering.
Example of 1st list:
Example of 2nd list:
So I want "Early Life" to be updated to "1.3. Early Life"
I have the base of it done, along with the regex I need, but I'm not sure how to actually compare, match, and then update based on the partial matches. Any help would be greatly appreciated, I have very little experience with javascript.
/** Takes a compendium of journals and matches then replaces names to those of an imported list prepended with index numbers
* New naming convention == "#.##... TEXT"
* Eg. "Special Abilities" -> "2.01.2. Special Abilities"
*
* 1. put the new name list in an array
* 2. iterate through the compendium, get index or contents
* 3. compare the entries name value vs new list(array), regex'd to remove the #s and . prior to the name, and set an update array with the new name
* 4. update the compendium
*/
//read text file of new names
let f = await fetch("/test_list.txt")
let txt = await f.text()
// Convert to array, split by line
let updatedNames = txt.toString().split("\n");
// Compendium to Update
const compendiumLabel = "Gamemastery";
(async ()=>{
const p = game.packs.entries.find(e => e.metadata.label === compendiumLabel)
// Array of current journal entries (filtering out Compendium Folders)
const currentNames = p.index.filter(x => x.name != '#[CF_tempEntity]').map(i=>{
return { name : i.name };
});
// Compare currentNames to updatedNames (regex'd to remove starting numbers and .)
// regex that excludes the numbering "#.#.#. " from updatedNames
let regex = new RegExp('[^(\d+.)+\s].*', 'g')
// update compendium
})();
Upvotes: 0
Views: 125
Reputation: 13802
Maybe something like this:
const notIndexed = `Early Life
Circumstances of Birth
Family
Region`.split('\n');
const indexed = `1.3. Early Life
1.3.1. Circumstances of Birth
1.3.2. Family
1.3.3. Region`.split('\n');
const regexp = /^((?:\d.)+) +(.+)/;
const mapping = new Map(
indexed.map((line) => {
const [, index, title] = line.match(regexp);
return [title, index];
})
);
const reIndexed = notIndexed.map(
(line) => {
if (mapping.has(line)) return `${mapping.get(line)} ${line}`;
return line;
}
).join('\n');
console.log(reIndexed);
Upvotes: 1