deemixer/src/utils/utils.js

105 lines
2.3 KiB
JavaScript
Raw Normal View History

/**
* Climbs the DOM until the root is reached, storing every node passed.
* @param {HTMLElement} el
* @return {Array} Contains all the nodes between el and the root
*/
export function generatePath(el) {
if (!el) {
throw new Error('No element passed to the generatePath function!')
}
let path = [el]
while ((el = el.parentNode) && el !== document) {
path.push(el)
}
return path
}
export function isValidURL(text) {
let lowerCaseText = text.toLowerCase()
if (lowerCaseText.startsWith('http')) {
if (
lowerCaseText.indexOf('deezer.com') >= 0 ||
lowerCaseText.indexOf('deezer.page.link') >= 0 ||
lowerCaseText.indexOf('open.spotify.com') >= 0
) {
return true
}
} else if (lowerCaseText.startsWith('spotify:')) {
return true
}
return false
}
2020-04-14 14:48:13 +00:00
export function convertDuration(duration) {
// Convert from seconds only to mm:ss format
let mm, ss
2020-04-09 10:24:49 +00:00
mm = Math.floor(duration / 60)
ss = duration - mm * 60
// Add leading zero if ss < 0
2020-04-09 10:24:49 +00:00
if (ss < 10) {
ss = '0' + ss
2020-04-09 10:24:49 +00:00
}
return mm + ':' + ss
2020-04-09 10:24:49 +00:00
}
export function convertDurationSeparated(duration) {
let hh, mm, ss
2020-04-09 10:24:49 +00:00
mm = Math.floor(duration / 60)
hh = Math.floor(mm / 60)
ss = duration - mm * 60
mm -= hh * 60
2020-04-09 10:24:49 +00:00
return [hh, mm, ss]
}
export function numberWithDots(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.')
}
2020-04-23 19:03:12 +00:00
// On scroll event, returns currentTarget = null
// Probably on other events too
export function debounce(func, wait, immediate) {
2020-04-19 20:02:06 +00:00
var timeout
return function() {
2020-04-19 20:02:06 +00:00
var context = this
var args = arguments
var later = function() {
2020-04-19 20:02:06 +00:00
timeout = null
if (!immediate) func.apply(context, args)
}
var callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
2020-04-09 10:24:49 +00:00
}
/**
* Workaround to copy to the clipboard cross-OS by generating a
* ghost input and copying the passed String
*
* @param {string} text Text to copy
*/
export function copyToClipboard(text) {
const ghostInput = document.createElement('input')
document.body.appendChild(ghostInput)
ghostInput.setAttribute('type', 'text')
ghostInput.setAttribute('value', text)
ghostInput.select()
ghostInput.setSelectionRange(0, 99999)
document.execCommand('copy')
ghostInput.remove()
}
export default {
isValidURL,
convertDuration,
convertDurationSeparated,
numberWithDots,
debounce
}