Skip to content

Instantly share code, notes, and snippets.

View tanepiper's full-sized avatar

Tane Piper tanepiper

View GitHub Profile
@tanepiper
tanepiper / validation.ts
Created February 10, 2020 14:54 — forked from busypeoples/validation.ts
Validation in TS (version 2)
type ValidationResult<T, U> = Partial<{ [Key in keyof T]: U }>;
type Validation<T, U> = (fields: T) => ValidationResult<T, U>;
const validate = <T, U = boolean | string>(
validations: Validation<T, U>[],
fields: T
): ValidationResult<T, U> =>
validations
.map(validation => validation(fields))
.reduce((acc, a) => Object.assign(acc, a), {});
@tanepiper
tanepiper / tuples.ts
Created September 21, 2018 14:31 — forked from hmil/tuples.ts
Well-typed variable-length tuples in TypeScript
/**
* Arbitrary-length tuples
* =======================
*
* Working with tuples of unknown length in TypeScript is a pain. Most library authors fall back on enumerating all possible
* tuple lengths up to a threshold (see an example here https://github.com/pelotom/runtypes/blob/fe19290d375c8818d2c52243ddc2911c8369db37/src/types/tuple.ts )
*
* In this gist, I'm attempting to leverage recursion to provide support for arbitrary length tuples. This has the potential
* to make some kinds of declarative APIs nicer and enhance type inference in some cases.
* This example shows how to take a variable-length tuple as an input, transform each of its types and use the resulting
@tanepiper
tanepiper / tuple-utils.ts
Created September 12, 2018 11:23 — forked from KSXGitHub/tuple-utils.ts
Tuple generic
export namespace TupleUtils {
/**
* Get type of first element
* @example `First<[0, 1, 2]>` → `0`
*/
export type First<Tuple extends [any, ...any[]]> = Tuple[0]
/**
* Get type of last element

npm:install:api

Run task npm:install:app after this

cd env/api && npm install --silent

npm:install:app

@tanepiper
tanepiper / ng-serve-docker-nginx-proxy.md
Created September 5, 2018 23:48 — forked from markstuart/ng-serve-docker-nginx-proxy.md
Running Angular CLI in local dev behind docker nginx reverse proxy

Setup

Angular app talking to multiple microservices using Cookie authentication on ajax requests. Cookies are not passed over cross domain ajax requests, so we need to be able to have the Angular app and microservices running on same-domain in local dev as well as in production.

We have a Docker nginx proxy running on localhost:8000:

  • proxying to multiple microservices on localhost:8000/api/<microservice>
  • proxying other requests to Angular CLI (ng serve) on en0 inet (192.168.1.5 for instance) port 4200
  • using a python script to get the correct en0 IP and write it to the nginx config at proxy startup
@tanepiper
tanepiper / TypeInference.ts
Created April 12, 2018 07:54
Type inference in typescript
const foo = function <T>(a: T, b: T[]): any {
return { a, b }
}
foo(6, 'di')
foo(6, [9])
foo<string>(6, 'di')
foo<string>('foo', 'di')
foo<string>('foo', ['di'])
foo<object>({}, [{}]) 
function handleKill1() {
setInterval(createModal, 1000);
}
const open = document.querySelector(".open");
function createModal() {
...
}
open.addEventListener("click", createModal);
document.addEventListener("keyup", event => {
event.preventDefault();
@tanepiper
tanepiper / AnnoyingButton.js
Created January 31, 2018 17:44
Creates an annoying button it's hard to close with a mouse
function createModal() {
// ...
const button = document.createElement("button");
button.innerText = "Close Me";
function moveModal() {
button.setAttribute("disabled", true);
button.innerText = 'LOL';
lastX = newModal.style.top || 0;
lastY = newModal.style.left || 0;
@tanepiper
tanepiper / mm-keyboard.html
Created December 19, 2017 00:15
Example of how to declare a keyboard with Music Markup
<mm-keyboard oscillator-type="sawtooth">
<mm-key class-name="white b" frequency="174.614" time="1"></mm-key>
<mm-key class-name="black as" frequency="184.997" time="1"></mm-key>
<mm-key class-name="white a" frequency="195.998" time="1"></mm-key>
<mm-key class-name="black gs" frequency="200.000" time="1"></mm-key>
<mm-key class-name="white g" frequency="207.652" time="1"></mm-key>
<mm-key class-name="black fs" frequency="233.082" time="1"></mm-key>
<mm-key class-name="white f" frequency="246.942" time="1"></mm-key>
<mm-key class-name="white e" frequency="261.626" time="1"></mm-key> <!-- Middle c -->
<mm-key class-name="black ds" frequency="277.183" time="1"></mm-key>