This file contains 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
function dynamicSort(property) { | |
var sortOrder = 1; | |
if(property[0] === "-") { | |
sortOrder = -1; | |
property = property.substr(1); | |
} | |
return function (a,b) { | |
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; | |
return result * sortOrder; | |
} |
This file contains 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
//Start out with a playlist with songs from multiple albums in no particular order | |
[{albumId: A, trackname: "Dream Maker", trackNumber: 3}, | |
{albumId: C, trackname: "Mess", trackNumber: 1}, | |
{albumId: B, trackname: "Dont", trackNumber: 9}, | |
{albumId: W, trackname: "Me", trackNumber: 5}, | |
{albumId: A, trackname: "Heartbreaker", trackNumber: 2}, | |
{albumId: W, trackname: "With", trackNumber: 2}, | |
{albumId: C, trackname: "Around", trackNumber: 6}, | |
{albumId: A, trackname: "Love Taker", trackNumber: 6}] |
This file contains 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
function convertPlaylistToObjectByProperty(simplifiedPlaylist, propertyName){ | |
//use to group large array of tracks into smaller arrays of tracks from same album | |
//this lets us easily sort tracks for a single album, then do a for..in loop | |
//over each albumId as an object key and combine the arrays back together | |
let playlistObject = {}; | |
//eslint-disable-next-line | |
simplifiedPlaylist.map((track) => { | |
if (!playlistObject[track[propertyName]]) { | |
playlistObject[track[propertyName]] = []; | |
} |
This file contains 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
async function identifyAlbumsByArtistId(artistId, returnSimpleArray=true) { | |
//returns an array of albumIds that appear in the playlist, without duplicates | |
const arrayOfAlbumObjects = await spotifyApi.getArtistAlbums(artistId) | |
let albumIds = arrayOfAlbumObjects.items.map((albumObj) => { | |
if (returnSimpleArray) { | |
return albumObj.id | |
} else { | |
return ({albumId: albumObj.id, albumName: albumObj.name, albumUri: albumObj.uri}) | |
} | |
}) |
This file contains 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
function shuffleArray(input) { | |
for (let i = input.length-1; i >=0; i--) { | |
const randomIndex = Math.floor(Math.random()*(i+1)); | |
const itemAtIndex = input[randomIndex]; | |
input[randomIndex] = input[i]; | |
input[i] = itemAtIndex; | |
} | |
return input; | |
} |
This file contains 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
function identifyAlbumsInPlaylist(simplifiedPlaylist, returnSimpleArray=true) { | |
//returns an array of albumIds that appear in the playlist, without duplicates | |
let albumIds = simplifiedPlaylist.map((track) => { | |
if (returnSimpleArray) { | |
return track.albumId | |
} else { | |
return ({albumId: track.albumId, albumName: track.albumName, albumUri: track.albumUri}) | |
} | |
}) | |
albumIds = uniq(albumIds); |
This file contains 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
async function getSimpleAlbumTracks(albumId, albumName, albumUri){ | |
//track_number is what track number a song is on the album | |
try { | |
const tracks = await spotifyApi.getAlbumTracks(albumId); | |
const simpleTracks = tracks.items.map((track) => { | |
const artist = track.artists[0]; | |
return { | |
trackId: track.id, | |
trackName: track.name, | |
trackUri: track.uri, |
This file contains 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
async function getSimplePlaylistTracks(playlistId){ | |
//track_number is what track number a song is on the album | |
try { | |
const tracks = await spotifyApi.getPlaylistTracks(playlistId); | |
//getPlaylistTracks has a bunch of meta data about the playlist we don't need | |
//once again items is the property we really want. It's an array of tracks | |
const simpleTracks = tracks.items.map((trackObject) => { | |
const track = trackObject.track; | |
const album = trackObject.track.album; | |
const artist = trackObject.track.artists[0] |
This file contains 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
export async function getUserPlaylists() { | |
//returns an array of objects with playlist name (like "Favorite Smashing Pumpkins jamz") | |
//and the id of the playlist. Use this to feed the playlists selection list | |
try { | |
const playlistsResponse = await spotifyApi.getUserPlaylists(); | |
//playlistsResponse.items are the actual playlist objects | |
const playlists = playlistsResponse.items.map((playlistObject) => { | |
const {id, name} = playlistObject; | |
return {id: id, playlistName: name} | |
}) |
This file contains 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
export function setAccessToken(accessToken) { | |
//since using spotifyApi as helper library you can set the access code once | |
//you get it and then not have to include it in every request | |
spotifyApi.setAccessToken(accessToken); | |
} |