Skip to content

Instantly share code, notes, and snippets.

@lski
lski / upsert.ts
Created March 9, 2022 10:05
Simple upsert function
/**
* Updates or adds an item to a new array.
*
* @param arr The array to check if item exists
* @param item The item to add/insert
* @param comparer Run against each item in the array to see if it matches an existing item
* @returns A new array with the item replaced if found, or added to the end if not found.
*/
export const upsert = <T,>(arr: T[], item: T, comparer: (existingItem: T, newItem: T, index: number) => boolean = (item1, item2) => item1 === item2): T[] => {
const output = [];
export const useRenderCount = () => {
const count = useRef(0);
count.current++;
return count.current;
};
@lski
lski / useLoadData.ts
Created February 7, 2021 12:13
A basic load data React hook that handles using fetch to get data
import { useEffect, DependencyList, useState } from 'react';
/**
* `useLoadData` wraps loading data via fetch in as a hook.
*
* Supports handling a loading and error state for the current component and handles
* cleanup using a useEffect via an AbortSignal to prevent updates happening on
* unmounted components. To control how often it runs, use the dependancy list parameter,
* see useEffect for more details.
*
@lski
lski / replace-branch.sh
Last active December 11, 2020 12:28
Replace an entire branch with new code, commit all and push it. Useful for generated github pages.
#!/bin/bash
# Echo usage to console
usage() {
cat << EOF
usage: replace-branch.sh [options] repo-url
options:
-s
The source folder (default: './build')
-b
@lski
lski / change_port.sh
Created December 8, 2020 14:49
Changes the port in nginx in the file. Only for simple replacements.
PORT=${1:-80}
FILE=${2:-/etc/nginx/conf.d/default.conf}
# Replace the port number, retaining whitespace
sed -i 's|\([\s]*listen\) *[0-9]*;|\1 '$PORT';|g' "$FILE"
@lski
lski / nordvpn.service
Last active December 4, 2020 14:02
A script with several command to control a connection to a NordVPN server via the OpenVPN client (as Wireguard casuing issues)
[Unit]
Description=NordVPN Auto Start
After=multi-user.target
After=network-online.target
[Service]
User=root
Type=forking
ExecStart=/home/pi/nordvpn.sh open uk
@lski
lski / AddJsonToPreview.cmd
Created October 24, 2020 13:30
Command to make a text file show in the preview pane, in this case .json files
reg add HKLM\SOFTWARE\Classes\.json /v PerceivedType /t REG_SZ /d text
@lski
lski / debounce.js
Created June 11, 2020 10:51
A simple debounce higher order function
/**
* Creates a new function that will run the callback a maximum of once at the end of the timeout period.
* Useful for using with event listeners that fire often. The opposite of throttling.
*
* @param delay The length of time in ms before firing the callbcak
* @param callback The callback to fire once the time has passed
*/
export function debounce(delay, callback) {
let _args;
let _running = false;
@lski
lski / createStorage.js
Last active June 11, 2020 10:37
Creates a persistent store for data, meaning the key is encapsulated and the data is parsed to/from a string for you.
"use strict";
export const createStorage = (key, scope = localStorage) => {
return {
get() {
const stored = scope.getItem(key);
if (stored === null) {
return null;
}
return JSON.parse(stored);
},
@lski
lski / open_terminal.sh
Last active May 9, 2020 10:06
A way of opening the WT terminal from a WSL terminal in the same directory
# Opens the current folder in the Windows Terminal
function term() {
# Open's terminal
function __runTerm() {
# Using the wslpath tool to convert the current directory to windows format
# which is required by wt. Also using printf to escape the characters so they arent expanded
local var=$(printf "%q" "$(wslpath -w $(pwd))")
# Run wt using powershell command