Skip to content

Instantly share code, notes, and snippets.

@bryangingechen
Last active August 22, 2020 09:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryangingechen/90e9c5cc453c16014b431e0b8bcd90c6 to your computer and use it in GitHub Desktop.
Save bryangingechen/90e9c5cc453c16014b431e0b8bcd90c6 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const [fileName, outputFile] = process.argv.slice(2);
if (fileName && outputFile) {
const nodes = JSON.parse(fs.readFileSync(fileName, 'utf8'));
if (!Array.isArray(nodes))
return console.log(`${fileName} could not be parsed into an Array`);
if (nodes.some(d => Object.keys(d).some(e => !['id', 'value', 'pinned'].includes(e))))
return console.log(`the objects in ${fileName} may only have 'id', 'version' and 'pinned' properties`);
if (nodes.some(d => d.hasOwnProperty('value') && typeof d.value !== 'string') ||
nodes.some(d => d.hasOwnProperty('pinned') && typeof d.pinned !== 'boolean'))
return console.log(`the 'value' and 'pinnned' values in ${fileName}, if present, must be Strings and Booleans, respectively`);
const newNodes = nodes.reduce((acc, node) => {
// mutates the elements of nodes, so don't use nodes again after this!
node.id = acc.length;
acc.push(node);
return acc;
}, []);
console.log(`${newNodes.length} cells, can be reuploaded as version ${newNodes.length+1}`);
console.log(`writing to ${outputFile}`);
fs.writeFileSync(outputFile, JSON.stringify(newNodes), {flag:'wx'});
} else {
console.log(`run with:
node reindex-nbjson.js inputFile outputFile`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment