Created
November 30, 2016 16:22
-
-
Save johnny5th/5fe711c54b4458cf37ad265510f8d7b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class MessageStats { | |
constructor (message = null) { | |
// Constraints | |
this.constraints = { | |
byteCount1: "", | |
byteCount2: "", | |
minPlateNumberByteCount: "", | |
maxPlateNumberByteCount: "" | |
}; | |
// Stats | |
this.totalChars; | |
this.messageChars; | |
this.specialChars; | |
this.totalValidChars; | |
this.invalidChars; | |
// Alt Plate | |
this.altPlate = false; | |
// Message | |
if(message) { | |
this.message = message; | |
this.calculateStats(); | |
} | |
} | |
removeAltPlate() { | |
this.altPlate = false; | |
} | |
setAltPlate(altPlate) { | |
if(!altPlate) | |
return false; | |
this.altPlate = altPlate; | |
this.altPlate.totalByteCount = this.altPlate.byteCount1 + this.altPlate.byteCount2; | |
} | |
setMessage(message) { | |
this.message = message; | |
this.calculateStats(); | |
} | |
setConstraints(constraints) { | |
for(let constraint in constraints) { | |
if(typeof this.constraints[constraint] !== 'undefined') { | |
this.constraints[constraint] = constraints[constraint]; | |
} else { | |
console.log('[MessageStats] Not a valid constraint: ', constraint); | |
} | |
} | |
if(constraints.byteCount1 || constraints.byteCount2) { | |
this.constraints.totalByteCount = this.constraints.byteCount1 + this.constraints.byteCount2; | |
} | |
} | |
// Assumes valid plate | |
setConstraintsFromPlate(plate) { | |
this.setConstraints({ | |
byteCount1: plate.byteCount1, | |
byteCount2: plate.byteCount2, | |
minPlateNumberByteCount: plate.minPlateNumberByteCount, | |
maxPlateNumberByteCount: plate.maxPlateNumberByteCount | |
}); | |
} | |
// Set calculated message stats | |
calculateStats() { | |
this.totalChars = this.message.length; | |
this.messageChars = (this.message.match(/([a-z0-9])/g)||[]).length; | |
this.specialChars = (this.message.match(/([ &-\.\*\@])/g)||[]).length; | |
this.totalValidChars = this.messageChars + this.specialChars; | |
this.invalidChars = this.totalChars - this.totalValidChars; | |
} | |
// Whether | |
canAddByte() { | |
let calculatedVals = this.getCalculatedPlateConstraints(); | |
return (calculatedVals.calculatedTotalByteCount > (this.totalChars || 0)); | |
} | |
getCalculatedPlateConstraints() { | |
let calculatedVals = { | |
calculatedByteCount1: this.constraints.byteCount1, | |
calculatedByteCount2: this.constraints.byteCount2, | |
calculatedTotalByteCount: this.constraints.totalByteCount, | |
calculatedMaxPlateNumberByteCount: this.constraints.maxPlateNumberByteCount | |
}; | |
if(this.altPlate.type == "upgrade") { | |
calculatedVals.calculatedByteCount1 = this.altPlate.byteCount1; | |
calculatedVals.calculatedByteCount2 = this.altPlate.byteCount2; | |
calculatedVals.totalByteCount = this.altPlate.totalByteCount; | |
calculatedVals.calculatedMaxPlateNumberByteCount = this.altPlate.maxPlateNumberByteCount; | |
} | |
return calculatedVals; | |
} | |
// Returns an object with isValid bool, altPlateStatus string | |
validate() { | |
let calculatedMaxPlateNumberByteCount = this.constraints.maxPlateNumberByteCount; | |
let calculatedMinPlateNumberByteCount = this.constraints.minPlateNumberByteCount; | |
let calculatedTotalByteCount = this.constraints.totalByteCount; | |
let altPlateStatus = false; | |
if(this.altPlate) { | |
// Message fits in upgrade plate | |
if(this.altPlate.type == "upgrade" | |
&&this.messageChars <= this.altPlate.maxPlateNumberByteCount && this.messageChars > this.constraints.maxPlateNumberByteCount | |
&& this.totalValidChars <= this.altPlate.totalByteCount) { | |
calculatedTotalByteCount = this.altPlate.totalByteCount; | |
calculatedMaxPlateNumberByteCount = this.altPlate.maxPlateNumberByteCount; | |
altPlateStatus = "upgrade"; | |
} | |
// Message fits in downgrade plate | |
if(this.altPlate.type == "downgrade" | |
&& this.messageChars <= this.altPlate.maxPlateNumberByteCount | |
&& this.totalValidChars <= this.altPlate.totalByteCount) { | |
calculatedTotalByteCount = this.altPlate.totalByteCount; | |
calculatedMaxPlateNumberByteCount = this.altPlate.maxPlateNumberByteCount; | |
altPlateStatus = "downgrade"; | |
} | |
} | |
let valid = (this.messageChars >= calculatedMinPlateNumberByteCount | |
&& this.messageChars <= calculatedMaxPlateNumberByteCount | |
&& this.totalValidChars <= calculatedTotalByteCount); | |
return {isValid: valid, altPlateStatus: altPlateStatus}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment