« get me outta code hell

pickers.js « src - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/pickers.js
blob: 236f9ea9a328e5477b91dedee9cd43ab067e8275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict'

const { flattenPlaylist } = require('./playlist-utils')

function makeOrderedPlaylistPicker(playlist) {
  const allSongs = flattenPlaylist(playlist)
  let index = 0

  return function() {
    if (index < allSongs.length) {
      const picked = allSongs[index]
      index++
      return picked
    } else {
      return null
    }
  }
}

function makeShufflePlaylistPicker(playlist) {
  const allSongs = flattenPlaylist(playlist)

  return function() {
    const index = Math.floor(Math.random() * allSongs.length)
    const picked = allSongs[index]
    return picked
  }
}

module.exports = {
  makeOrderedPlaylistPicker,
  makeShufflePlaylistPicker
}