« 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: 014eee8382e3fd8fd68249edbfbae3569d5746ea (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
40
41
42
43
'use strict'

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

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

  const flatGroup = flattenGrouplike(grouplike)
  let index = 0

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

function makeShufflePlaylistPicker(grouplike) {
  // Shuffle playlist picker - this selects a random track at any index in
  // the playlist, after flattening it.

  const flatGroup = flattenGrouplike(grouplike)

  return function() {
    if (flatGroup.items.length) {
      const index = Math.floor(Math.random() * flatGroup.items.length)
      const picked = flatGroup.items[index]
      return picked
    } else {
      return null
    }
  }
}

module.exports = {
  makeOrderedPlaylistPicker,
  makeShufflePlaylistPicker
}