Skip to content

Instantly share code, notes, and snippets.

@SudoPlz
Created June 6, 2017 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SudoPlz/7a0158a9d02c37b93eced0d0c4dc38f2 to your computer and use it in GitHub Desktop.
Save SudoPlz/7a0158a9d02c37b93eced0d0c4dc38f2 to your computer and use it in GitHub Desktop.
That js file reads a `file.in` input file that contains prop types declarations and outputs an `file.out` file that will act as our shouldComponentUpdate react method.
var fs = require('fs');
var lineReader = require('readline').createInterface({
input: fs.createReadStream('file.in'),
output: process.stdout,
terminal: false
});
var fd = fs.openSync('file.out', 'w');
fs.write(fd, 'shouldComponentUpdate(nextProps) {\n return (\n');
var lineIt = 0;
lineReader.on('line', function (line) {
if (!line || /^\s*[\/][\/]/.test(line) || line.search(".func") !== -1) {
// ^ starting at the beginning of the line
// \s* look for zero or more whitespace characters
// [\/] and then a forward slash /
// [\/] and then another forward slash /
// it's a comment so
lineIt ++; // increase the line cnt
// and move on
if (!line) {
console.log('Found blank row on line: '+lineIt);
} else if (line.search(".func") !== -1) {
console.log('Found function proptype on line: '+lineIt);
} else {
console.log('Found comment on line: '+lineIt);
}
} else {
var lineSplit = line.split(':');
if (lineSplit) {
var item = lineSplit[0];
// console.log('Line from file:', line);
var lineChangingStr = '';
if (lineIt !== 0) {
lineChangingStr = ' ||\n';
}
fs.write(fd, lineChangingStr+' nextProps.'+item+' !== this.props.'+item+'\n');
lineIt ++;
}
}
});
lineReader.on('close', function () {
fs.write(fd, ' );\n}');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment