Skip to content

Commit 7cc3d02

Browse files
committed
Type our SlotMachine class
Declare some private members inline so that we get automatic type inference from our default sum function.
1 parent acb50bc commit 7cc3d02

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/lib/slotMachine.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
import { sum } from './hashingFunctions';
22

33
class SlotMachine<T> {
4-
private slots: T[];
54
private numSlots: number;
6-
private hashingFn: (items: number[]) => number;
75

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;
128
}
139

1410
pull(string) {
1511
const str = string.replace(/\.(png|jpg|gif|)$/g, '');
1612
const stringArray = str.split('');
17-
return this.slots[this._indexFor(stringArray)];
13+
return this.slots[this.indexFor(stringArray)];
1814
}
1915

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;
2319
return Math.abs(index);
2420
}
2521

26-
_getCharInt(char) {
22+
private getCharInt(char) {
2723
return parseInt(char.charCodeAt(0) || 0, 10);
2824
}
2925
}

0 commit comments

Comments
 (0)