Skip to content

Instantly share code, notes, and snippets.

@nickscript0
nickscript0 / mpg-to-Lp100km-bookmarklet.js
Last active December 20, 2019 13:26
Bookmarklet: Finds all strings that match '<number> mpg' and suffixes them with the l/100km conversion
javascript: (function () {
function convert(currentElement) {
if (!currentElement.textContent) return;
/*
* Match mpg strings with negative lookahead for the l/100km conversion string
* to avoid duplicating the conversion when run multiple times.
* E.g. "N.N mpg" not followed by " (N.N l/100km)"
*/
const mpgRegex = /(\d*(\.\d+)?)(\s)*mpg(?!\s\(\d+\.\d+\sl\/100km\))/gi;
const currentText = currentElement.textContent;
@nickscript0
nickscript0 / raspberry_pi_initial_setup.md
Last active May 16, 2022 00:20
Raspberry Pi 3 - Initial Setup with Raspbian

Initial Raspberry Pi 3 Setup with Raspbian

Flash Raspbian and connect to network via ethernet

  1. Download the latest Lite version from https://www.raspberrypi.org/software/operating-systems/
  2. Use the "balenaEtcher" app (https://www.balena.io/etcher/) to flash the iso to a microSD card
  3. Copy a blank file named ssh to the boot partition (the root folder of the microSD card)
  4. Power up the Raspberry PI, find it's IP in your router's connected devices, and ssh pi@<ip> (password: raspberry)

Initial Hardware and Raspbian configuration

  1. sudo raspi-config
  2. System Options > Change user password
@nickscript0
nickscript0 / public-ip.sh
Created December 24, 2018 17:07
Fastest way to find your Public IP in the terminal (OpenDNS query)
dig myip.opendns.com @208.67.222.222 +short
@nickscript0
nickscript0 / typescript-zip.ts
Created December 17, 2018 14:38
Typed zip functions in TypeScript
// zip for 2 arrays
function zip2<A, B>(a: A[], b: B[]): Array<[A, B]> {
return a.map((_, c) => [a, b].map(row => row[c])) as Array<[A, B]>;
}
// zip for any number of arrays
type Zip<T extends unknown[][]> = { [I in keyof T]: T[I] extends (infer U)[] ? U : never }[];
function zip<T extends unknown[][]>(...args: T): Zip<T> {
return <Zip<T>><unknown>(args[0].map((_, c) => args.map(row => row[c])));
}
@nickscript0
nickscript0 / songs_to_csv.js
Created December 15, 2018 19:47
Improvement to export_google_music.js gist
// After running this guy's Google Play Music export https://gist.github.com/dcalacci/7f8853174797c0c56c49
// His export to CSV function doesn't format the lines properly
// Paste the following function in the console and run 'songsToCsv(all_songs)'
function songsToCsv(data) {
const out = [];
const seen = new Set();
const keys = [
'artist', 'album', 'title', 'duration', 'playcount', 'rating'
];
@nickscript0
nickscript0 / date-fns-migrate.js
Last active October 2, 2021 02:33
How to migrate from moment.js to date-fns (as the package is way smaller)
/**
* date-fns v2 is still in alpha, but has built-in Typescript support so best to use it:
* Docs: https://date-fns.org/v2.0.0-alpha.16/docs/Getting-Started
*
* npm install --save date-fns@next
*/
// Useful functions to import
import { formatDistanceStrict, isAfter, isBefore, format, subDays, isSameDay } from 'date-fns';
@nickscript0
nickscript0 / kill_sticky_headers.js
Created August 13, 2018 13:45
Kill Sticky Headers - Updated Version
// Updated the original to include the new 'sticky' style in addition to 'fixed' https://alisdair.mcdiarmid.org/kill-sticky-headers/
// Note to run this as a Chrome bookmark paste it in the URL field, prefixed with: javascript:
(function () {
var i, elements = document.querySelectorAll('body *'); for (i = 0; i < elements.length; i++) {
if (["sticky", "fixed"].includes(getComputedStyle(elements[i]).position)) {
elements[i].parentNode.removeChild(elements[i]);
}
}
}
)();
@nickscript0
nickscript0 / vsc_user_settings.json
Created February 20, 2018 23:18
My Visual Studio Code User Settings
// Place your settings in this file to overwrite the default settings
{
"window.zoomLevel": 0,
"typescript.referencesCodeLens.enabled": false,
"editor.cursorBlinking": "solid",
"editor.minimap.enabled": false,
"workbench.colorCustomizations": {
"editor.lineHighlightBackground": "#585a6941"
}
}
@nickscript0
nickscript0 / typescript_cheatsheet.md
Last active April 29, 2018 03:58
Typescript Cheatsheet
@nickscript0
nickscript0 / promisified_node_fs.ts
Created February 17, 2018 18:45
Typescript Promisified nodejs fs functions
// promisified fs.writeFile
export function writeFile(filename, data): Promise<void> {
return new Promise<void>((resolve, reject) =>
fs.writeFile(filename, data, (err) => (err) ? reject(err) : resolve())
);
}
// promisified fs.readFile
function readFile(filename): Promise<string> {
return new Promise<string>((resolve, reject) =>