Skip to content

Commit

Permalink
Adding token seprator for better filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjpaz committed Apr 30, 2021
1 parent f89fcf9 commit 52fb968
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const config = require('./config');

const isRNG = (t) => new RegExp(config.redepmtionRandomText).test(t);

const TOKEN_SEPARATOR = "_";

class RomShuffler {
constructor() {
this.state = {};
Expand All @@ -32,7 +34,24 @@ class RomShuffler {

if(index && index !== '' && !isRNG(index)) {
roms = roms
.filter((rom) => rom.toLowerCase().startsWith(index.toLowerCase()));
.filter((rom) => {
const p = rom.split(TOKEN_SEPARATOR);

console.log(p, index);
if(p[0] && p[0] === index) {
return true;
}

if(p[1] && p[1].startsWith(index)) {
return true;
}

return rom.toLowerCase().startsWith(index.toLowerCase());
});

if(roms.length > 1) {
roms = roms.filter((rom) => rom.split(TOKEN_SEPARATOR)[0] === index);
}
} else {
roms = roms
.filter((rom) => rom !== this.state.currentRom)
Expand Down
37 changes: 37 additions & 0 deletions src/swap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,43 @@ describe('swap', () => {
expect(rom).to.eql('1_Foo.nes');
});

it('should return a specific rom if it matches a filter value (hack to fix the numeral code issue', async () => {

const shuffler = new RomShuffler();

shuffler.fetchCurrentRoms = sinon.spy(() => [
'1_Foo.nes',
'11111_Bar.nes',
'11_Bar.nes',
'11_Bar.nes',
'11111111_Bar.nes',
'11_Bar.nes',
'11_Bar.nes',
'111_Bar.nes',
'111_Bar.nes',
'111_Bar.nes',
]);

const rom = await shuffler.shuffle('1');

expect(rom).to.eql('1_Foo.nes');
});

it('should return a specific rom if it matches a filter value (token separator parts)', async () => {

const shuffler = new RomShuffler();

shuffler.fetchCurrentRoms = sinon.spy(() => [
'1_Foo.nes',
'2_Bar.nes'
]);

const rom = await shuffler.shuffle('Foo');

expect(rom).to.eql('1_Foo.nes');
});



it('should be random on null', async () => {

Expand Down

0 comments on commit 52fb968

Please sign in to comment.