Created
May 14, 2017 12:39
-
-
Save nachocab/5c056ef978351096bf72087cde608e6b to your computer and use it in GitHub Desktop.
JavaScript Value Objects using stampits (from http://crushlovely.com/journal/7-patterns-to-refactor-javascript-applications-value-objects/)
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
const stampit = require('stampit') | |
Grade = stampit({ | |
init(percentage) { | |
this.percentage = percentage | |
this.grades = [ | |
{letter: 'A', minimumPercentage: 0.9, passing: true}, | |
{letter: 'B', minimumPercentage: 0.8, passing: true}, | |
{letter: 'C', minimumPercentage: 0.7, passing: true}, | |
{letter: 'D', minimumPercentage: 0.6, passing: true}, | |
{letter: 'F', minimumPercentage: 0, passing: false} | |
] | |
this.grade = this.grade(percentage) | |
}, | |
methods: { | |
passingGradeLetters() { | |
return this.grades.filter(x => x.passing).map(x => x.letter) | |
}, | |
grade(percentage) { | |
debugger | |
return this.grades.find(x => percentage >= x.minimumPercentage) | |
}, | |
letterGrade() { | |
return this.grade.letter | |
}, | |
isPassing() { | |
return this.grade.passing | |
}, | |
isImprovementFrom(grade) { | |
return this.isBetterThan(grade) | |
}, | |
isBetterThan(grade) { | |
return this.percentage > grade.percentage | |
}, | |
valueOf() { | |
return this.percentage | |
} | |
} | |
}) | |
module.exports = Grade |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment