Skip to content

Instantly share code, notes, and snippets.

@lski
lski / StatusCodeError.ts
Created February 12, 2019 15:57
Custom error object that retains the stacktrace. Designed to signify a fetch request was unsuccessful
export class StatusCodeError extends Error {
statusCode: number;
constructor(statusCode: number, message: string, ...params: any[]) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(message);
// Maintains proper stack trace for where our error was thrown (only available on V8)
@lski
lski / delayPromise.ts
Created March 15, 2019 10:31
Simple delayPromise function you can place into the promise chain to delay the promise, useful for mocking
const delayPromise = (time: number) =>
<T>(result) =>
new Promise<T>((response) =>
setTimeout(() => {
response(result);
}, time)
);
@lski
lski / StringExtensions.cs
Created March 15, 2019 10:56
An Extension method to perform a case insensitive equality check on two strings. (Needs adding to Lski.Toolbox)
using System;
namespace Lski.Toolbox.Txt {
public static class StringExtensions
{
/// <summary>
/// Performs a case insenstive equality check on two strings.
/// </summary>
public static bool IsEqual(this string strA, string strB)
@lski
lski / ThrowIf.cs
Created March 15, 2019 11:26
Throw helper methods
using System;
namespace Lski.Exceptions {
/// <summary>
/// Static methods to throw conditionally is te assertion is false.
/// </summary>
/// <remarks>
/// A major advantage to a method like this is reduced IL and assembly code as according to
/// <a href="https://referencesource.microsoft.com/#mscorlib/system/throwhelper.cs">ThrowHelper.cs</a>.
@lski
lski / ThrowHelpers.cs
Last active March 15, 2019 11:36
Throw helper methods
using System;
namespace Lski.Exceptions {
/// <summary>
/// Static methods to throw exceptions with a smaller footprint
/// </summary>
/// <remarks>
/// A major advantage to a method like this is reduced IL and assembly code as according to
/// <a href="https://referencesource.microsoft.com/#mscorlib/system/throwhelper.cs">ThrowHelper.cs</a>.
@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 / 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 / GuidExtensions.cs
Last active April 16, 2019 11:20
A short format GUID/UUID based on Mads Kristensens work
public static class GuidExtensions {
/// <summary>
/// Converts a Guid into a short format useful urls
/// </summary>
/// <remarks>
/// See <see>https://madskristensen.net/blog/A-shorter-and-URL-friendly-GUID</see> for a more detailed explantion.
/// </remarks>
public static string ToShortFormat(this Guid guid) =>
Convert.ToBase64String(guid.ToByteArray())
@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>
@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)
{