Skip to content

Instantly share code, notes, and snippets.

View Gerrit0's full-sized avatar

Gerrit Birkeland Gerrit0

View GitHub Profile
@Gerrit0
Gerrit0 / events.ts
Last active June 29, 2020 03:44
Simple Event emitter - Dual licenced under MIT and Apache 2.0
/**
* Simple, type safe, event emitter class.
*
* @example
* ```ts
* const x = new EventEmitter<{ a: [string] }>()
* x.on('a', a => a.repeat(123)) // ok
* x.on('b', console.log) // error, 'b' is not assignable to 'a'
* const y = new EventEmitter<{ a: [string]; [k: string]: unknown[] }>()
* y.on('a', a => a.repeat(123)) // ok
@Gerrit0
Gerrit0 / grammar.ne
Last active February 14, 2020 18:59
TypeScript type grammar - paste this into https://omrelli.ug/nearley-playground/
@{%
const nth = n => d => d[n]
const intrinsic = d => ({ type: 'intrinsic', value: d[0] })
%}
# I skipped function types and object types, running low on time.
Main -> _ Type0 _ {% d => JSON.stringify(d[1]) %}
# Handle precedence, lowest first
import * as ts from 'typescript'
import { resolve } from 'path'
const options: ts.CompilerOptions = {
declaration: true,
emitDeclarationOnly: true,
allowJs: true, // I treat jsw as js
// ...
}
@Gerrit0
Gerrit0 / notes.md
Last active November 15, 2019 00:19
TypeDoc restructure notes

This is a first attempt at a high level restructure of TypeDoc. There are almost certainly issues with the proposed design that I'm missing or haven't resolved yet. Some of these include

  1. How should users modify the JSON output? #930
  2. A lot of events are used by TypeDoc's current design, this offers a lot of flexibility for plugins to attach to an event and modify an object at any part of its lifecycle, but makes debugging painful since they are also heavily used internally. Which events should be retained, and which should be removed?
  3. Does it make sense for readers to declare their options in the constructor? It is nice to tie the code that uses the option and the code that declares it together, but this could cause problems when using the api programatically.
  4. Should some readers exist by default? (TsConfig, TypeDoc)

Goals in this first pass:

  1. Define the primary interfaces required to write a CLI
/**
* Helper function to find the caret position in a contenteditable element
* Credit: http://stackoverflow.com/questions/3972014/
* @param {HTMLElement} editableDiv
* @return {number}
*/
function getCaretPosition(editableDiv) {
let caretPos = 0;
let sel = window.getSelection();
interface ApiErrorResponse {
cod: number;
message: string;
}
interface ApiDayResponse {
dt: number;
main: {
temp: number;
temp_min: number;
@Gerrit0
Gerrit0 / index.js
Last active December 5, 2017 03:41
requirebin sketch
const { Minimatch } = require("minimatch")
const patterns = [
// Separate test folder
'**/{test,node_modules}/**/*.ts',
// Tests alongside source
'**/{node_modules/**/*,*.{spec,test}}.ts',
// Alongside source, including node_modules
'**/*.{spec,test}.ts',
]
@Gerrit0
Gerrit0 / inbox_signatures.user.js
Created May 24, 2016 17:33
Adds a button to Inbox by Gmail's interface, allowing you to easily insert a signature at the current cursor position
// ==UserScript==
// @name Inbox Signatures
// @namespace http://gerritbirkeland.com/
// @version 1.0
// @description Adds a button to Inbox by Gmail's compose dialog to add a signature.
// @author Gerrit0
// @match https://inbox.google.com/*
// @grant none
// ==/UserScript==
@Gerrit0
Gerrit0 / ajax.js
Last active May 27, 2016 17:34
A micro ajax helper for promises
let ajax = (function() {
/**
* Easily make an XHR request.
*
* @param {string} protocol
* @param {string} url
* @param {object} params -- WARNING. Only accepts shallow objects.
* @return {Promise}
*/
function xhr(protocol, url = '/', params = {}, headers = {}) {