Last active
December 21, 2015 01:05
-
-
Save aichholzer/4e87c221b7facea7efb4 to your computer and use it in GitHub Desktop.
Shuffle & match a list of names for whatever occasion.
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
'use strict'; | |
class Shufflr { | |
constructor () { | |
process.argv.splice(0, 2); | |
this.people = process.argv.sort(); | |
this.selectedPeople = []; | |
this.results = {}; | |
} | |
names (index) { | |
index = index || 0; | |
let availablePeople = this.people.filter(x => this.selectedPeople.indexOf(x) == -1); | |
let receiver = availablePeople[Math.round(Math.random() * (availablePeople.length - 1))]; | |
if (this.people[index] === receiver) { | |
this.selectedPeople = []; | |
return this.names(0); | |
} | |
this.selectedPeople.push(receiver); | |
this.results[this.people[index]] = receiver; | |
index += 1; | |
if (index < this.people.length) { | |
return this.names(index); | |
} | |
return this.results; | |
} | |
} | |
let shuffle = new Shufflr(); | |
console.log(shuffle.names()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use:
node shufflr.js [name ]
Example:
node shufflr.js peter paul marry jane andrew
Will output something like:
Enjoy!
:)