Skip to content

Instantly share code, notes, and snippets.

@lski
lski / sortObjectProps.js
Created March 29, 2020 18:35
Sort Object Properties
/**
* 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 March 17, 2020 13:28
A handler for Node process ending. Good for clean up. By default on Ctrl+C, process.exit() and uncaught exceptions.
/**
* 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 March 17, 2020 12:55
A simple check to see if a file exists, resolving to true or false, but creating the file regardless
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 November 2, 2019 10:49
Create a logger that will only log values if the initial currentLevel is equal to or greater than the initially passed in level
/**
* 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 October 27, 2019 12:02
Converts a list of objects to a Markdown table
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 October 6, 2019 15:46
Wrappers the Okta Auth for React, includes support for hooks
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 October 2, 2019 14:24
A simple localStorage wrapper storing objects, rather than strings
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);
@lski
lski / VsCode.ps1
Created April 22, 2019 09:40
A backup and restore script for VSCode
$root = if($PSScriptRoot) { $PSScriptRoot } else { '.' }
function Backup-VsCode(
[string]
$backupDir = "$root"
) {
if($null -ne ($code = FindVsCodeExe)) {
Extract-VsCodeSettings -backupDir $backupDir
@lski
lski / Launch.cs
Created April 18, 2019 18:47
Launch a web broswer with optional URL on Win/Mac/Linux
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WebBrowser
{
public static class Browser
{
public static Process Launch(string url, string browser = null)
{
@lski
lski / HttpClientWrapper.cs
Created April 18, 2019 11:45
A wrapper for HttpClient to allow for mocking... however do not use, see readme
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace HttpWrapper
{
/// <summary>
/// A concrete implementation of IHttpClient, which can be used easily for mocking
/// </summary>