Skip to content

Instantly share code, notes, and snippets.

View alexsasharegan's full-sized avatar

Alex Regan alexsasharegan

View GitHub Profile
@alexsasharegan
alexsasharegan / WrappedError.ts
Created January 3, 2020 17:51
Wrap errors using a linked list. Error burritos 🌯
export class WrappedError<ErrKind = any> extends Error implements Iterable<WrappedError | ErrKind> {
previous: ErrKind | null = null;
guid: Guid;
constructor(message: string, previous?: ErrKind) {
super(message);
// We update the error's name to distinguish it from the base Error.
this.name = this.constructor.name;
this.guid = `<${this.name}:empty>`;
// We add our reference to the original error value (if provided).
@alexsasharegan
alexsasharegan / Bits.js
Last active December 8, 2018 01:22
Bitwise manipulations wrapped in a class with semantically meaningful methods.
export class Bits {
static get Nil() {
return 0;
}
static get All() {
// Flip the sign bit out of all bits engaged.
return ~0 ^ (1 << 31);
}
@alexsasharegan
alexsasharegan / pattern-matching.ts
Created August 15, 2018 20:08
A guide to pseudo pattern matching in TypeScript.
/**
* Step 1:
* Create a string enum. This can be done with a TS enum type,
* or a sum type (union of strings).
*/
// enum style
enum TokenType {
Text = "Text",
UserMention = "UserMention",
" Specify a directory for plugins
" - For Neovim: ~/.local/share/nvim/plugged
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')
Plug 'sheerun/vim-polyglot'
Plug 'itchyny/lightline.vim'
Plug 'joshdick/onedark.vim'
call plug#end()
@alexsasharegan
alexsasharegan / url.ts
Last active November 20, 2017 19:45
Encode and decode urls.
/**
* RegExpURL matches URLs containing a query string.
* - [0] full match
* - [1] base url
* - [2] query string
* - [3] hash (without #)
*/
const RegExpURL = /^(.*)\?([^#]*)(?:#(.*))?$/
/**
* RegExpListKey matches a URL key/value pair that uses array syntax (`key[]=value`).
@brandontrowe
brandontrowe / rxjs.swipeLeft.js
Created August 8, 2017 01:11
RxJS Swipe Left Observable
const tolerance = 200; // swipe distance in pixels
const swipeLeft$ = Rx.Observable.fromEvent(document, "touchstart")
// Switch to listen to touchmove to determine position
.switchMap(startEvent =>
Rx.Observable.fromEvent(document, "touchmove")
// Listen until "touchend" is fired
.takeUntil(Rx.Observable.fromEvent(document, "touchend"))
// Output the pageX location
.map(event => event.touches[0].pageX)
// Accumulate the pageX difference from the start of the touch
@Jamp
Jamp / build_mysql.sh
Last active November 14, 2023 17:19 — forked from shichao-an/build_mysql.sh
Build and install MySQL 5.1 from source on Ubuntu 16.04
#!/bin/bash
# Run as root
set -e
apt-get update
apt-get install -y build-essential
apt-get install -y libncurses5-dev
useradd mysql
@joyrexus
joyrexus / README.md
Last active February 24, 2024 15:16
collapsible markdown

collapsible markdown?

CLICK ME

yes, even hidden code blocks!

print("hello world!")
@clemlatz
clemlatz / self-signed-ssl-certificate.md
Last active April 22, 2024 12:30
Setup a self-signed SSL certificate with Nginx (server and browser)

1. Configure server: Nginx

Create the certificate:

$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt

Create a strong Diffie-Hellman group:

$ sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
@alexsasharegan
alexsasharegan / promisify.js
Last active August 21, 2017 16:16
Promisify Node async functions dynamically. (curries when provided no additional arguments)
function promisify(func, ...args) {
if (arguments.length === 1) {
// Curry with the decremented arity
return promisify.bind(null, func)
}
return new Promise((resolve, reject) => {
func(...args, (error, ...cbArgs) => {
if (error) {
return reject(error)