Skip to content

Instantly share code, notes, and snippets.

View carlhannes's full-sized avatar

Hannes W carlhannes

View GitHub Profile
@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
@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"
#!/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 / 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);
}
@carlhannes
carlhannes / sj-booking-fix.js
Last active July 11, 2023 10:02
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 / 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 / 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 / picassorenderer.jsx
Last active May 11, 2022 07:04
React picasso.js rendering area
import React, { useEffect } from 'react';
import picasso from 'picasso.js';
const debounce = (func, wait, immediate = false) => {
let timeout;
return function debouncedFn(...args) {
return new Promise((resolve) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
@carlhannes
carlhannes / json-path-resolver.js
Last active February 23, 2022 12:53
JSON Path Resolver in JavaScript
/**
* Resolves the value at the given JSON path
*
* @param {String} path Path with / for children
* @param {Object} obj Object to use when searching
* @return {Object} Result at the end of the path
*
* @example
* let path = "/path/to/paradise";
* let obj = {
@carlhannes
carlhannes / debounce.ts
Last active March 11, 2024 11:07
ES6 Async Debounce JavaScript & TypeScript Helper Functions
// ES6 Async version of the "classic" JavaScript Debounce function.
// Works both with and without promises, so you can replace your existing
// debounce helper function with this one (and it will behave the same).
// The only difference is that this one returns a promise, so you can use
// it with async/await.
//
// I've converted this into a TypeScript module, and added a few more
// features to it, such as the ability to cancel the debounce, and also
// execute the function immediately, using the `doImmediately` method.
//