Skip to content

Instantly share code, notes, and snippets.

View carlhannes's full-sized avatar

Hannes W carlhannes

View GitHub Profile
@carlhannes
carlhannes / batch-promise.js
Last active July 12, 2022 06:18
Batch promises in javascript
/**
* Same as Promise.all(items.map(item => task(item))), but it waits for
* the first {batchSize} promises to finish before starting the next batch.
*
* Usage:
* const results = await batchPromise(STUFF_TO_DO, 5, async (ITEM_TO_DO) => {
* console.log('Fetching', ITEM_TO_DO.url);
* return await fetch(ITEM_TO_DO.url);
* });
*
@carlhannes
carlhannes / groupby.js
Last active September 25, 2022 08:06
groupby.js
const groupBy = (array, fn, last = false) => {
const grouped = [];
for (let index = 0; index < array.length; index += 1) {
const element = array[index];
const key = fn(element);
const foundIndex = grouped.findIndex((v) => v.key === key);
if (foundIndex === -1) {
grouped.push({ key, value: element });
} else if (last) {
@carlhannes
carlhannes / sj-booking-fix.js
Last active August 17, 2024 10:39
sj-booking-fix.js
// Paste the code below into your webbrowser console and press "enter"
// To open the console you can press "F12" or "Ctrl + Shift + J" for most browsers.
// Read more here: https://appuals.com/open-browser-console/
// Instructions video on my twitter: https://twitter.com/_carlhannes/status/1590441813445599232
// The code re-tries fetching data if it gets status 429, which is the error that the SJ page has
// It does this together with an exponential back-off delay which is common to use with microservices of this type
// Because of these re-tries and the delay, the overall load of the website and the servers will be lower,
// since it does not need to re-fetch requests that actually succeed. Read more on my twitter if you're interested:
// https://twitter.com/_carlhannes/status/1590605735314206721
@carlhannes
carlhannes / cachalot-v2-prototype.ts
Last active February 23, 2023 21:10
On-disk simple node.js cache function using fs promises, for things like calling an external API and re-using its data for multiple pages in a Next.js getStaticProps call
/* eslint-disable no-await-in-loop */
import * as fs from 'fs/promises';
import * as path from 'path';
const checkIfExist = async (p: string) => fs.access(p).then(() => true).catch(() => false);
function msg(...args: any[]) {
console.info('🐋:', ...args);
}
#!/bin/bash
# Check if the user is root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Create user and usergroup
groupadd -f boringproxy
@carlhannes
carlhannes / setup-eslint-airbnb.sh
Last active March 7, 2024 13:31
setup-eslint-airbnb.sh
#!/bin/bash
#
# wget -q -O - https://gist.github.com/carlhannes/0a8c827f826d330cfc07b36365e5158e/raw/a4d4ad961d4ba9637d7c7f92c1537e6437a289d9/setup-eslint-airbnb.sh | bash
#
# Step 0: Determine ESLint package and configuration based on project dependencies
PACKAGE_JSON="package.json"
ESLINT_PACKAGE="eslint-config-airbnb-base"
@carlhannes
carlhannes / hdd-spindown.sh
Created December 25, 2023 12:12
hdd-spindown.sh
#!/bin/bash
# 1. Check and install hdparm if not installed
if ! which hdparm > /dev/null; then
echo "hdparm not found! Installing..."
apt-get update
apt-get install -y hdparm
fi
# 2. List HDDs