File tree Expand file tree Collapse file tree 1 file changed +7
-11
lines changed Expand file tree Collapse file tree 1 file changed +7
-11
lines changed Original file line number Diff line number Diff line change 1
1
import { sum } from './hashingFunctions' ;
2
2
3
3
class SlotMachine < T > {
4
- private slots : T [ ] ;
5
4
private numSlots : number ;
6
- private hashingFn : ( items : number [ ] ) => number ;
7
5
8
- constructor ( slots : T [ ] , hashingFn ?) {
9
- this . slots = slots ;
10
- this . numSlots = this . slots . length ;
11
- this . hashingFn = hashingFn || sum ;
6
+ constructor ( private slots : T [ ] , private hash = sum ) {
7
+ this . numSlots = slots . length ;
12
8
}
13
9
14
10
pull ( string ) {
15
11
const str = string . replace ( / \. ( p n g | j p g | g i f | ) $ / g, '' ) ;
16
12
const stringArray = str . split ( '' ) ;
17
- return this . slots [ this . _indexFor ( stringArray ) ] ;
13
+ return this . slots [ this . indexFor ( stringArray ) ] ;
18
14
}
19
15
20
- _indexFor ( array ) {
21
- const intArray = array . map ( this . _getCharInt ) ;
22
- const index = ( this . hashingFn ( intArray ) + intArray . length ) % this . numSlots ;
16
+ private indexFor ( array ) {
17
+ const intArray = array . map ( this . getCharInt ) ;
18
+ const index = ( this . hash ( intArray ) + intArray . length ) % this . numSlots ;
23
19
return Math . abs ( index ) ;
24
20
}
25
21
26
- _getCharInt ( char ) {
22
+ private getCharInt ( char ) {
27
23
return parseInt ( char . charCodeAt ( 0 ) || 0 , 10 ) ;
28
24
}
29
25
}
You can’t perform that action at this time.
0 commit comments