@@ -5,6 +5,12 @@ David Quesenberry
55SpaceInvadersHighScoreService.js
66*/
77import { StorageService } from '../../../src/engine/persistence/index.js' ;
8+ import {
9+ sanitizeScore ,
10+ sanitizeInitials ,
11+ sanitizeRow ,
12+ sortRows ,
13+ } from '../../../src/shared/utils/highScoreUtils.js' ;
814
915const DEFAULT_KEY = 'toolboxaid:games:space-invaders:high-score-table' ;
1016const DEFAULT_ROWS = [
@@ -15,29 +21,6 @@ const DEFAULT_ROWS = [
1521 { initials : 'BOT' , score : 500 } ,
1622] ;
1723
18- function sanitizeScore ( value ) {
19- if ( ! Number . isFinite ( value ) ) {
20- return 0 ;
21- }
22- return Math . max ( 0 , Math . trunc ( value ) ) ;
23- }
24-
25- function sanitizeInitials ( value ) {
26- const letters = String ( value ?? '' ) . toUpperCase ( ) . replace ( / [ ^ A - Z ] / g, '' ) ;
27- return ( letters || 'AAA' ) . slice ( 0 , 3 ) . padEnd ( 3 , 'A' ) ;
28- }
29-
30- function sanitizeRow ( row ) {
31- return {
32- initials : sanitizeInitials ( row ?. initials ) ,
33- score : sanitizeScore ( row ?. score ) ,
34- } ;
35- }
36-
37- function sortRows ( rows ) {
38- return [ ...rows ] . sort ( ( a , b ) => b . score - a . score ) ;
39- }
40-
4124export default class SpaceInvadersHighScoreService {
4225 constructor ( { key = DEFAULT_KEY , tableSize = 5 , storage = null } = { } ) {
4326 this . key = key ;
0 commit comments