Skip to content

Instantly share code, notes, and snippets.

@SudoPlz
Last active June 6, 2017 20:07
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 SudoPlz/df473d4b9c30c6ad6cf915d4c493a907 to your computer and use it in GitHub Desktop.
Save SudoPlz/df473d4b9c30c6ad6cf915d4c493a907 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. Run it by typing node shouldComponentUpdateCreator.js on your terminal.
screenHasLoaded: React.PropTypes.bool.isRequired,
isPartOfClass: React.PropTypes.bool.isRequired,
appointment: React.PropTypes.instanceOf(Map).isRequired,
// formErrors: React.PropTypes.instanceOf(Map).isRequired,
// timezoneZ: React.PropTypes.string.isRequired,
// isModal: React.PropTypes.bool.isRequired,
isClass: React.PropTypes.bool.isRequired,
classSize: React.PropTypes.number,
h: React.PropTypes.number.isRequired,
w: React.PropTypes.number.isRequired,
orientation: React.PropTypes.string.isRequired,
hasInternet: React.PropTypes.bool.isRequired,
// noOfRecurringApts: React.PropTypes.number,
currency: React.PropTypes.string.isRequired,
// appointmentTypeColor: React.PropTypes.string,
availableAddOns: React.PropTypes.instanceOf(Map),
availableCertificates: React.PropTypes.instanceOf(Map),
isPostingNewAppointment: React.PropTypes.bool.isRequired,
isReschedulingAppointment: React.PropTypes.bool.isRequired,
isCancelingAppointment: React.PropTypes.bool.isRequired,
timeFormatUsable: React.PropTypes.string,
// getBtnHeight: React.PropTypes.func.isRequired,
onRescheduleTap: React.PropTypes.func.isRequired,
onCancelAppointmentTap: React.PropTypes.func.isRequired,
onAppointmentCreateTap: React.PropTypes.func.isRequired,
onEditTap: React.PropTypes.func.isRequired,
isFetchingFuturePastAppts: React.PropTypes.bool.isRequired,
pastAppointment: React.PropTypes.instanceOf(Map),
futureAppointment: React.PropTypes.instanceOf(Map),
shouldComponentUpdate(nextProps) {
return (
nextProps.screenHasLoaded !== this.props.screenHasLoaded
||
nextProps.isPartOfClass !== this.props.isPartOfClass
||
nextProps.appointment !== this.props.appointment
||
nextProps.isClass !== this.props.isClass
||
nextProps.classSize !== this.props.classSize
||
nextProps.h !== this.props.h
||
nextProps.w !== this.props.w
||
nextProps.orientation !== this.props.orientation
||
nextProps.hasInternet !== this.props.hasInternet
||
nextProps.currency !== this.props.currency
||
nextProps.availableAddOns !== this.props.availableAddOns
||
nextProps.availableCertificates !== this.props.availableCertificates
||
nextProps.isPostingNewAppointment !== this.props.isPostingNewAppointment
||
nextProps.isReschedulingAppointment !== this.props.isReschedulingAppointment
||
nextProps.isCancelingAppointment !== this.props.isCancelingAppointment
||
nextProps.timeFormatUsable !== this.props.timeFormatUsable
||
nextProps.isFetchingFuturePastAppts !== this.props.isFetchingFuturePastAppts
||
nextProps.pastAppointment !== this.props.pastAppointment
||
nextProps.futureAppointment !== this.props.futureAppointment
);
}
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