Skip to content

Instantly share code, notes, and snippets.

@lski
lski / createElement.js
Last active January 9, 2024 22:05
Shorthand function for document.createElement, with added functionality... very similar to Reacts createElement :/ lol
/**
* Shorthand function for document.createElement. Accepts and appends attributes to the newly created element and appends child element, if any.
* @param tagName The name of the element eg. a or script
* @param attributes Any additional attributes to add to the element after creating. Eg. href
* @param children Any child elements you want appended on creating this element, obviously they could be createElement calls as well
*/
export function createElement(tagName, attributes, children) {
const ele = attributes === (void 0)
? document.createElement(tagName)
: Object.assign(document.createElement(tagName), attributes);
@lski
lski / XXTea.cs
Created December 24, 2014 13:29
A c# implementation of XXTea encryption algorithm based on the javascript version by Chris Veness
using System;
using System.Text;
/// <summary>
/// A class for encrypting and decrypting a string into base64 format which makes it safe for transfer
/// between applications.
///
/// Reference:
/// Based upon the javascript implementation of xxtea by: Chris Veness
/// www.movable-type.co.uk/tea-block.html
@lski
lski / array.reduce-polyfill.js
Last active April 28, 2022 09:16
Array.reduce polyfill created by MDN, made available to use with NPM
// Production steps of ECMA-262, Edition 5, 15.4.4.21
// Reference: http://es5.github.io/#x15.4.4.21
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(callback /*, initialValue*/) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.reduce called on null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
@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 / wslpath.ps1
Created April 6, 2019 13:25
wslpath wrapper for using directly in powershell
<#
.SYNOPSIS
Converts windows path into a linux path and vice versa.
.DESCRIPTION
Converts windows path into a linux path and vice versa. Use WSL under the hood, so needs to be installed.
See docs wslpath docs for more information.
.PARAMETER path
The path to convert
@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