• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const head = arr => (arr && arr.length ? arr[0] : undefined); head([1, 2, 3]); // 1 head([]); // undefined head(null); // undefined head(undefined); // undefined
• Nov 16, 2023 •LeifMessinger
0 likes • 7 views
function filePath(file){ let folders = file.getParents(); const parents = []; function makePathString(folderArray){ let path = ""; folderArray.forEach((folder)=>{ path += "/" + folder.getName(); }); return path; } while (folders.hasNext()) { const folder = folders.next(); //This should hopefully remove that folder from the iterator parents.unshift(folder); } return makePathString(parents); } function myFunction() { const myEmail = Session.getEffectiveUser().getEmail(); Logger.log("My email is " + myEmail); // Log the name of every file in the user's Drive. var fileIterator = DriveApp.getFiles(); const files = []; //List of [File, size] entries const filesMaxSize = 10; while (fileIterator.hasNext()) { var file = fileIterator.next(); const owner = file.getOwner(); //Only files I own if((!(owner)) || (!(owner.getEmail)) || owner.getEmail() != myEmail){ continue; } const entry = [file, file.getSize()]; function slideUp(arr, index){ //Let's keep sliding it up so we don't have to sort it at the end. for(let i = index; i > 0; --i){ //Stops at 1 const nextFile = arr[i-1]; if(nextFile[1] > entry[1]){ break; }else{ //Swap with the next file to slide the file up const temp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = temp; } } } if(files.length < filesMaxSize){ files.push(entry); slideUp(files, files.length - 1); }else{ if(entry[1] > files[files.length - 1][1]){ //If it's bigger than the smallest file in the list. files[files.length - 1] = entry; //Replace the smallest file slideUp(files, files.length - 1); //Slide it up } } } for(let i = 0; i < filesMaxSize; ++i){ const file = files[i][0]; const size = files[i][1]; Logger.log(size + "\t" + filePath(file) + "/" + file.getName() + "\t" + file.getOwner().getName()); } }
• Feb 11, 2021 •LeifMessinger
0 likes • 2 views
function spam(times = 1,log = true){ for(let i = 0; i < times; i++){ $.get("backend-search.php", {term: (" "+Date.now())}).done(function(data){ if(log) console.log(data); }); } } spam(100000);
• Jan 17, 2021 •C S
const getSearchTerm = delimiter => { let searchTerm = ""; for (let i = 1; i < commands.length - 1; i++) searchTerm = searchTerm + commands[i] + delimiter; searchTerm += commands[commands.length - 1]; return searchTerm; };
0 likes • 1 view
class LinkedList { constructor() { this.nodes = []; } get size() { return this.nodes.length; } get head() { return this.size ? this.nodes[0] : null; } get tail() { return this.size ? this.nodes[this.size - 1] : null; } insertAt(index, value) { const previousNode = this.nodes[index - 1] || null; const nextNode = this.nodes[index] || null; const node = { value, next: nextNode }; if (previousNode) previousNode.next = node; this.nodes.splice(index, 0, node); } insertFirst(value) { this.insertAt(0, value); } insertLast(value) { this.insertAt(this.size, value); } getAt(index) { return this.nodes[index]; } removeAt(index) { const previousNode = this.nodes[index - 1]; const nextNode = this.nodes[index + 1] || null; if (previousNode) previousNode.next = nextNode; return this.nodes.splice(index, 1); } clear() { this.nodes = []; } reverse() { this.nodes = this.nodes.reduce( (acc, { value }) => [{ value, next: acc[0] || null }, ...acc], [] ); } *[Symbol.iterator]() { yield* this.nodes; } } const list = new LinkedList(); list.insertFirst(1); list.insertFirst(2); list.insertFirst(3); list.insertLast(4); list.insertAt(3, 5); list.size; // 5 list.head.value; // 3 list.head.next.value; // 2 list.tail.value; // 4 [...list.map(e => e.value)]; // [3, 2, 1, 5, 4] list.removeAt(1); // 2 list.getAt(1).value; // 1 list.head.next.value; // 1 [...list.map(e => e.value)]; // [3, 1, 5, 4] list.reverse(); [...list.map(e => e.value)]; // [4, 5, 1, 3] list.clear(); list.size; // 0
• Oct 15, 2023 •LeifMessinger
//Disclaimer: I do not condone mass web scraping of ESPN websites, and this is just theoretical code that hasn't been used. //Scrape player stats off of https://www.espn.com/nfl/team/roster/_/name/phi/philadelphia-eagles //Example player JS object /* { "shortName": "S. Opeta", "name": "Sua Opeta", "href": "http://www.espn.com/nfl/player/_/id/3121009/sua-opeta", "uid": "s:20~l:28~a:3121009", "guid": "dec19157-e984-f981-3724-498281328c97", "id": "3121009", "height": "6' 4\"", "weight": "305 lbs", "age": 27, "position": "G", "jersey": "78", "birthDate": "08/15/96", "headshot": "https://a.espncdn.com/i/headshots/nfl/players/full/3121009.png", "lastName": "Sua Opeta", "experience": 4, "college": "Weber State" } */ //The page needs to be focused, so you have to put this code in a bookmarklet and click the page before clicking it. Allegedly. navigator.clipboard.writeText(JSON.stringify( window["__espnfitt__"].page.content.roster.groups.flatMap((group)=>{ return group.athletes.map((athlete)=>{ //We can assume all football players are above 100 lbs and below 1000 lbs. athlete.weight = parseInt(athlete.weight.substring(0,3)); if(athlete.experience == "R") athlete.experience = 0; else athlete.experience = parseInt(athlete.experience); //We can assume players are at least 1 foot, or under 10 feet tall. athlete.inches = 12 * parseInt(athlete.height.substring(0, 1)); //Add feet in inches athlete.inches += parseInt(athlete.height.substring(2).replaceAll("\"", "").trim()); //Add remaining inches const monthDayYear = athlete.birthDate.split("/"); athlete.birthMonth = parseInt(monthDayYear[0]); athlete.birthDay = parseInt(monthDayYear[1]); athlete.birthYear = parseInt(monthDayYear[2]); //The only really useful stuff we get from this is Height, weight, left handedness, age, position, and birthday return athlete; }); }) , null, "\t")).then(null, ()=>{alert("That failed.")}); //Changes all the team links on this page https://www.espn.com/nfl/stats/team to the team's roster for easier scraping $$("table > tbody > tr > td > div > div > a").forEach((elm)=>{ elm.setAttribute("href", elm.getAttribute("href").replace("team/", "team/roster/")); });