« get me outta code hell

general-util.js - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/general-util.js
blob: abe23995caf5a21349d386bed5117934bdc948e5 (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
module.exports.commandExists = async function(command) {
  return false
}

function downloadPlaylistFromURL(url) {
  return fetch(url).then(res => res.text())
}

module.exports.downloadPlaylistFromOptionValue = function(arg) {
  // TODO: Verify things!
  return downloadPlaylistFromURL(arg)
}

module.exports.shuffleArray = function(array) {
  // Shuffles the items in an array. Returns a new array (does not modify the
  // passed array). Super-interesting post on how this algorithm works:
  // https://bost.ocks.org/mike/shuffle/

  const workingArray = array.slice(0)

  let m = array.length

  while (m) {
    let i = Math.floor(Math.random() * m)
    m--

    // Stupid lol; avoids the need of a temporary variable!
    Object.assign(workingArray, {
      [m]: workingArray[i],
      [i]: workingArray[m]
    })
  }

  return workingArray
}