Skip to content

Instantly share code, notes, and snippets.

@draeton
Last active October 8, 2015 12:48
Show Gist options
  • Save draeton/3334359 to your computer and use it in GitHub Desktop.
Save draeton/3334359 to your computer and use it in GitHub Desktop.
Perfect Albums Queue
var Queue = function (list) {
this.list = [].concat(list);
};
Queue.prototype = {
constructor: Queue,
random: function () {
var length = this.list.length;
var index = -1;
var item;
while (length && !(item = this.list[index])) {
index = Math.round(Math.random() * length);
}
return item;
}
};
var Albums = function (file, callback) {
this.callback = callback;
this.albums = [];
this.queue = null;
this.zeroMessage = "So sad, too bad, no favs :(";
this.onLoad = this.onLoad.bind(this);
this.load(file);
};
Albums.prototype = {
constructor: Albums,
load: function (file) {
var fs = require("fs");
var path = require("path");
fs.readFile(path.resolve(__dirname, file), this.onLoad);
},
onLoad: function (err, data) {
if (err) throw err;
var text = String(data).trim();
if (data && text.length) {
this.albums = data && text.split("\n");
this.queue = new Queue(this.albums);
}
this.callback && this.callback(this);
},
random: function () {
var album = this.queue.random();
return album || this.zeroMessage;
}
};
var albums = new Albums("albums.txt", function (albums) {
console.log("Play... ", albums.random());
});
node album.js
Aaliyah - Aaliyah
Adele - 21
Air - Virgin Suicides
Alanis Morissette - Jagged Little Pill
Amy Winehouse - Back to Black
Arcade Fire - Funeral
Aretha Franklin - The Very Best of Aretha Franklin
Beach Boys - Pet Sounds
Bebel Gilberto - Bebel Gilberto
Bjork - Homogenic
Carole King - Tapestry
Coldplay - A Rush of Blood to the Head
Coldplay - Parachutes
David Gray - White Ladder
Death Cab for Cutie - Plans
Diana Krall - The Look of Love
Diana Ross & The Supremes - The Ultimate Collection
Ed Sheeran - +
Erykah Badu - Mama's Gun
Fiona Apple - Tidal
Fleet Foxes - Fleet Foxes
Fleetwood Mac - The Dance
George Michael - Faith
Harry Connick Jr. - When Harry Met Sally Soundtrack
Incubus - Make Yourself
Janelle Monae - The Archandroid
Jay-Z - The Black Album
Jewel - Pieces of You
Jill Scott - Who Is Jill Scott?
Jimmy Eat World - Bleed American
John Mayer - Room for Squares
Justice - Cross
Justin Timberlake - Justified
Kelly Clarkson - Breakaway
LCD Soundsystem - Sound of Silver
Lauryn Hill - The Miseducation of Lauryn Hill
Mariah Carey - Mariah Carey
Natalie Imbruglia - White Lillies Island
Nick Drake - Pink Moon
Nirvana - In Utero
Phoenix - Wolfgang Amadeus Phoenix
Poe - Haunted
Radiohead - In Rainbows
Radiohead - OK Computer
Rent
Starsailor - Love Is Here
Stevie Wonder - Songs in the Key of Life
Sufjan Stevens - Come On Feel The Illinoise!
Tears for Fears - Tears Roll Down
Third Eye Blind - Third Eye Blind
Thom Yorke - The Eraser
Tool - Lateralus
Travis - The Invisible Band
U2 - The Joshua Tree
Vampire Weekend - Vampire Weekend
Wilco - Yankee Hotel Foxtrot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment