Skip to content

Instantly share code, notes, and snippets.

@roshane
Last active April 11, 2023 12:55
Show Gist options
  • Save roshane/9f27a442b9c38fde81e3544e9c7c5e41 to your computer and use it in GitHub Desktop.
Save roshane/9f27a442b9c38fde81e3544e9c7c5e41 to your computer and use it in GitHub Desktop.
javascript cheats
//flatten array (if `flat` is missing)
const flatten = (input) => {
return input.reduce((a,c)=>{
return util.isArray(c) ? a.concat(flatten(c)) : a.concat(c);
},[]);
}
//module exports shortcut
module.exports.foo = ()=> 'foo'
//console function to extract youtube videos by title filter
function selectURL(text){
return Array.from(document.querySelectorAll('h3.ytd-playlist-video-renderer')).filter(item=>{
const label = item.getAttribute('aria-label');
console.log('item',label);
return label.indexOf(text)>-1;
}).flatMap(item =>{
const label = item.getAttribute('aria-label');
return Array.from(item.childNodes).filter(it=>it['href'])
.map(it=>{
return {
title:it['title'],
href: it['href']
}
});
});
}
//download as CSV
function downloadAsCSV(data, titles){
let csvContent = titles.join(',')+'\r\n';
data.forEach(data => {
let row = titles.map(t=>data[t]).join(',');
csvContent += row + '\r\n';
})
let downLink = document.createElement('a');
let blob = new Blob(["\ufeff", csvContent]);
let urlObject = URL.createObjectURL(blob);
downLink.setAttribute('href', urlObject);
downLink.setAttribute('download', 'customer.csv');
document.body.appendChild(downLink);
downLink.click();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment