Skip to content

Instantly share code, notes, and snippets.

@lski
lski / debounce.js
Created Jun 11, 2020
A simple debounce higher order function
View debounce.js
/**
* 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 Jun 11, 2020
Creates a persistent store for data, meaning the key is encapsulated and the data is parsed to/from a string for you.
View createStorage.js
"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
A way of opening the WT terminal from a WSL terminal in the same directory
View open_terminal.sh
# 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
@lski
lski / sortObjectProps.js
Created Mar 29, 2020
Sort Object Properties
View sortObjectProps.js
/**
* Does a simple (single level) sort on the properties in an object.
* @returns {Map}
*/
function sortObjectProps(json) {
return Object.keys(json)
.sort((a, b) => a >= b)
.reduce((accumulator, item) => {
accumulator.set(item, json[item]);
@lski
lski / onProcessExit.js
Created Mar 17, 2020
A handler for Node process ending. Good for clean up. By default on Ctrl+C, process.exit() and uncaught exceptions.
View onProcessExit.js
/**
* Handles cleanup after basic close events, if the handler returns nothing
*
* @param {Function} handler
* @param {Array<string>} [events=['SIGINT', 'uncaughtException', 'exit']]
*/
export const onProcessExit = (
handler,
events = ['SIGINT', 'uncaughtException', 'exit']
) => {
@lski
lski / fileExistsAndCreate.js
Created Mar 17, 2020
A simple check to see if a file exists, resolving to true or false, but creating the file regardless
View fileExistsAndCreate.js
import fs from 'fs';
/**
* Accepts a full file path and checks whether the file exists returning the answer
* If it doesnt exist it will also attempt to create the file.
*
* @param {string} filepath The file to check and create.
*/
export const fileExistsAndCreate = filepath => {
return new Promise((resolve, reject) => {
@lski
lski / createLogger.js
Last active Nov 2, 2019
Create a logger that will only log values if the initial currentLevel is equal to or greater than the initially passed in level
View createLogger.js
/**
* Creates a logger that optional runs depending on the current level set for logging in the application
*
* Levels:
* 0 = debug
* 1 = trace (alias for verbose)
* 2 = info
* 3 = warn
* 4 = error
* 5+ = none
@lski
lski / toMarkdownTable.js
Created Oct 27, 2019
Converts a list of objects to a Markdown table
View toMarkdownTable.js
export const toMarkdownTable = (keys = null) => (list) => {
if (list == null) throw new Error('The list needs to be an array');
if (list.length === 0) return '';
keys = keys || Object.keys(list[0]);
if (keys.length === 0) return '';
@lski
lski / AutheticationProvider.jsx
Created Oct 6, 2019
Wrappers the Okta Auth for React, includes support for hooks
View AutheticationProvider.jsx
import React, { createContext, useContext, useMemo, useState, useEffect } from "react";
import { Route, withRouter } from "react-router-dom";
import { Auth } from "@okta/okta-react";
import { useExpensiveRef } from '../features/useExpensiveRef';
import { Redirect } from "react-router";
import { getLocationStorage } from "../utils/localStorage";
import { log } from '../utils/log';
const settings = {
issuer: process.env.REACT_APP_OKTA_ISSUER,
@lski
lski / localStorage.js
Created Oct 2, 2019
A simple localStorage wrapper storing objects, rather than strings
View localStorage.js
function setLocationStorage(key, value) {
localStorage.setItem(key, JSON.stringify(value));
return value;
}
function getLocationStorage(key, clear = false) {
const value = localStorage.getItem(key);
if(clear === true) {
removeLocationStorage(key);
You can’t perform that action at this time.