Last active
August 29, 2015 14:11
-
-
Save panicdragon/379fa8701bbc7144c6b1 to your computer and use it in GitHub Desktop.
Star Class
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 class Star { | |
| /** | |
| * コンストラクタ | |
| * @param starNum スターの数 | |
| * @param starTotalLength スター総数 | |
| * @param starAll スター詳細 | |
| * @param starAverage スター総合の平均値 | |
| */ | |
| constructor( | |
| public starNum?: number, | |
| public starTotalLength?: number, | |
| public starAll?: string[], | |
| public starAverage?: number | |
| ) { | |
| if (starAll) { | |
| if (!starTotalLength) { | |
| this.starTotalLength = this.getTotalLength(starAll); | |
| } | |
| this.starAverage = this.getStarAverage(starAll); | |
| this.getStarAverageList(this.starAverage); | |
| } | |
| } | |
| static fromJson(json: string): Star { | |
| var data = JSON.parse(json); | |
| return Star.fromData(data); | |
| } | |
| static fromData(data: any): Star { | |
| return new Star( | |
| data.star_num, | |
| data.star_total_length, | |
| data.star_all, | |
| 0 | |
| ); | |
| } | |
| /** | |
| * スターの総数を返す | |
| * @param starAll | |
| * @returns {number} | |
| */ | |
| public getTotalLength(starAll: string[]): number { | |
| var totalLength: number = 0; | |
| _.each(starAll, function(val: number) { | |
| totalLength += parseInt(val, 10); | |
| }); | |
| return totalLength; | |
| } | |
| /** | |
| * スターの平均値を返す | |
| * @param starAll | |
| * @returns {number} | |
| */ | |
| public getStarAverage(starAll: string[]): number { | |
| var totalNum: number = 0; | |
| _.each(starAll, function(val: number, key: any) { | |
| totalNum += parseInt(val * key, 10); | |
| }); | |
| return Math.round(((totalNum / this.starTotalLength) * 10)) / 10; | |
| } | |
| /** | |
| * スターの平均値を ng-repeatで表示するためのリストを返す | |
| * @param num | |
| * @returns {string[]} | |
| */ | |
| public getStarAverageList(num: number): string[] { | |
| if (num !== parseInt(num, 10)) { | |
| if (parseInt((String(num).split('.')[1]), 10) > 4) { | |
| this.starNum = Math.floor(num) + 0.5; | |
| } else { | |
| this.starNum = Math.floor(num); | |
| } | |
| } | |
| return this.getStarIconList(); | |
| } | |
| /** | |
| * スターを ng-repeatで表示するためのリストを返す | |
| * @returns {string[]} | |
| */ | |
| public getStarIconList(): string[] { | |
| var ret = ['', '', '', '', ''], i: number; | |
| for (i = 0; i < 5; i++) { | |
| if (this.starNum <= i && (this.starNum - i) !== 0.5) { | |
| ret[i] = 'off'; | |
| } else if (this.starNum - i === 0.5) { | |
| ret[i] = 'half'; | |
| } | |
| } | |
| return ret; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment