Skip to content

Instantly share code, notes, and snippets.

View erikvullings's full-sized avatar

Erik Vullings erikvullings

View GitHub Profile
@erikvullings
erikvullings / message-bus-service.ts
Created August 15, 2018 07:31
Message bus service, or pub/sub service, loosely based on postal.js, but created in TypeScript.
export type ICallback<T> = (data: T, envelope: IEnvelope<T>) => void;
/** Message enveloppe */
export interface IEnvelope<T> {
/* Uses DEFAULT_CHANNEL if no channel is provided */
channel?: string;
/** Topic name */
topic: string;
/** Payload */
data?: T;
@erikvullings
erikvullings / guid.ts
Created July 30, 2018 10:45
Create a new GUID or UUID in TypeScript
/**
* Create a GUID
* @see https://stackoverflow.com/a/2117523/319711
*
* @returns RFC4122 version 4 compliant GUID
*/
export const uuid4 = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
// tslint:disable-next-line:no-bitwise
const r = (Math.random() * 16) | 0;
@erikvullings
erikvullings / deep-copy.ts
Last active October 5, 2023 13:27
Deep copy or clone in TypeScript
/**
* Deep copy function for TypeScript.
* @param T Generic type of target/copied value.
* @param target Target value to be copied.
* @see Source project, ts-deepcopy https://github.com/ykdr2017/ts-deepcopy
* @see Code pen https://codepen.io/erikvullings/pen/ejyBYg
*/
export const deepCopy = <T>(target: T): T => {
if (target === null) {
return target;
@erikvullings
erikvullings / deep-copy.ts
Created July 30, 2018 10:40
Deep copy or clone in TypeScript
/**
* Deep copy function for TypeScript.
* @param T Generic type of target/copied value.
* @param target Target value to be copied.
* @see Source project, ts-deepcopy https://github.com/ykdr2017/ts-deepcopy
*/
export const deepCopy = <T>(target: T): T => {
if (target === null) {
return target;
}
@erikvullings
erikvullings / utils.ts
Last active January 19, 2019 11:46
TypeScript utilities: GUID and deepCopy (clone)
/**
* Create a GUID
* @see https://stackoverflow.com/a/2117523/319711
*
* @returns RFC4122 version 4 compliant GUID
*/
export const uuid4 = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
// tslint:disable-next-line:no-bitwise
const r = (Math.random() * 16) | 0;
@erikvullings
erikvullings / cachify.ts
Created May 31, 2018 08:00
Created a cached version of any function with a single argument.
/**
* Create a cached version of a function.
*
* @example cachedCalcArea = cachify(calcArea);
* @param fn Function whose results you want to cache
* @see TypeScript version of https://dev.to/kepta/javascript-underdogs-part-1---the-weakmap-4jih
*/
export const cachify = <T extends object, R>(fn: (arg: T) => R) => {
const cache = new WeakMap(); // Use Map() when dealing with non-object arguments
return (arg: T) => {
@erikvullings
erikvullings / geo.ts
Last active May 29, 2018 15:49
Convert RD (rijksdriehoek) to WGS84 and back.
/**
* Convert RD (Rijksdriehoek) coordinates to WGS84 and vice versa.
* @see https://thomasv.nl/2014/03/rd-naar-gps/
*/
const projectionBetweenRdWgs84 = () => {
const x0 = 155000;
const y0 = 463000;
const f0 = 52.15517440; // f => phi
const l0 = 5.38720621; // l => lambda
@erikvullings
erikvullings / walk.ts
Last active April 14, 2022 14:23
Recursively walk a directory in TypeScript
import fs from 'fs';
import path from 'path';
/**
* Recursively walk a directory asynchronously and obtain all file names (with full path).
*
* @param dir Folder name you want to recursively process
* @param done Callback function, returns all files with full path.
* @param filter Optional filter to specify which files to include,
* e.g. for json files: (f: string) => /.json$/.test(f)
@erikvullings
erikvullings / docker-compose.yml
Created September 13, 2017 10:16
Docker-compose file for Kafka
---
version: '2'
services:
zookeeper:
image: confluentinc/cp-zookeeper
hostname: zookeeper
extra_hosts:
- "moby:127.0.0.1"
ports:
- "2181:2181"
@erikvullings
erikvullings / gist:8e8d5ae026748681de8ddde90d835cde
Created August 11, 2017 09:30
Starting a new phoenix application
# Phoenix, by default, uses a Postgres database, so start one via docker
docker pull postgres
# Expose it on port 8432
docker run -p 8432:5432 --name hello-postgres -e POSTGRES_PASSWORD=postgres -d postgres
# Install Erlang, Elixir, and now Phoenix
mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
# Create a new Phoenix app
mix phx.new hello
# Edit config/dev.exs and update your hostname. Add a port (default is 5432, but I've mapped it to 8432)