« 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: 92a964141b3588fb23bf9f4650e9faa01a762609 (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
34
35
36
37
38
39
'use strict'

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

function makeOrderedPlaylistPicker(playlist) {
  // Ordered playlist picker - this plays all the tracks in a playlist in
  // order, after flattening it.

  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) {
  // Shuffle playlist picker - this selects a random track at any index in
  // the playlist, after flattening it.

  const allSongs = flattenPlaylist(playlist)

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

module.exports = {
  makeOrderedPlaylistPicker,
  makeShufflePlaylistPicker
}