Skip to main content

retryPromise.js

0 likes • Oct 20, 2023 • 12 views
JavaScript
Loading...

More JavaScript Posts

next cpu

0 likes • Nov 18, 2022 • 1 view
JavaScript
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
const { exec } = require("child_process");
exec("sensors|grep "high"|grep "Core"|cut -d "+" -f2|cut -d "." -f1|sort -nr|sed -n 1p", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
export default function Home() {
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<p className={styles.description}>
Get started by editing{' '}
<code className={styles.code}>pages/index.js</code>
</p>
<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h2>Documentation &rarr;</h2>
<p>Find in-depth information about Next.js features and API.</p>
</a>
<a href="https://nextjs.org/learn" className={styles.card}>
<h2>Learn &rarr;</h2>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/canary/examples"
className={styles.card}
>
<h2>Examples &rarr;</h2>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h2>Deploy &rarr;</h2>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span className={styles.logo}>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
</footer>
</div>
)
}

unzipWith() function

0 likes • Nov 19, 2022 • 0 views
JavaScript
const unzipWith = (arr, fn) =>
arr
.reduce(
(acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
Array.from({
length: Math.max(...arr.map(x => x.length))
}).map(x => [])
)
.map(val => fn(...val));
unzipWith(
[
[1, 10, 100],
[2, 20, 200],
],
(...args) => args.reduce((acc, v) => acc + v, 0)
);
// [3, 30, 300]

kNearestNeighbors

0 likes • Nov 19, 2022 • 5 views
JavaScript
const kNearestNeighbors = (data, labels, point, k = 3) => {
const kNearest = data
.map((el, i) => ({
dist: Math.hypot(...Object.keys(el).map(key => point[key] - el[key])),
label: labels[i]
}))
.sort((a, b) => a.dist - b.dist)
.slice(0, k);
return kNearest.reduce(
(acc, { label }, i) => {
acc.classCounts[label] =
Object.keys(acc.classCounts).indexOf(label) !== -1
? acc.classCounts[label] + 1
: 1;
if (acc.classCounts[label] > acc.topClassCount) {
acc.topClassCount = acc.classCounts[label];
acc.topClass = label;
}
return acc;
},
{
classCounts: {},
topClass: kNearest[0].label,
topClassCount: 0
}
).topClass;
};
const data = [[0, 0], [0, 1], [1, 3], [2, 0]];
const labels = [0, 1, 1, 0];
kNearestNeighbors(data, labels, [1, 2], 2); // 1
kNearestNeighbors(data, labels, [1, 0], 2); // 0

ESPN Web Scrape

0 likes • Oct 15, 2023 • 6 views
JavaScript
//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/"));
});

compact object

0 likes • Nov 19, 2022 • 0 views
JavaScript
const compactObject = val => {
const data = Array.isArray(val) ? val.filter(Boolean) : val;
return Object.keys(data).reduce(
(acc, key) => {
const value = data[key];
if (Boolean(value))
acc[key] = typeof value === 'object' ? compactObject(value) : value;
return acc;
},
Array.isArray(val) ? [] : {}
);
};
const obj = {
a: null,
b: false,
c: true,
d: 0,
e: 1,
f: '',
g: 'a',
h: [null, false, '', true, 1, 'a'],
i: { j: 0, k: false, l: 'a' }
};
compactObject(obj);
// { c: true, e: 1, g: 'a', h: [ true, 1, 'a' ], i: { l: 'a' } }

Temperature Converter

0 likes • Nov 19, 2022 • 0 views
JavaScript
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;
// Examples
celsiusToFahrenheit(15); // 59
celsiusToFahrenheit(0); // 32
celsiusToFahrenheit(-20); // -4
fahrenheitToCelsius(59); // 15
fahrenheitToCelsius(32); // 0