Skip to content

Instantly share code, notes, and snippets.

View WouterSpaak's full-sized avatar

Wouter Spaak WouterSpaak

  • The People Group
  • Nijmegen, The Netherlands
  • 15:36 (UTC +02:00)
View GitHub Profile
/**
* LSystem implementation.
* @see https://jsantell.com/l-systems.
*/
class LSystem<T> {
constructor(
private readonly productions: Map<T, T>,
private readonly splitter: (serialized: T) => T[],
private readonly serializer: (collection: T[]) => T
) { }
type Nil = null | undefined;
class Maybe<T> {
private wrappedValue: T | Nil;
constructor(value: T | Nil) {
this.wrappedValue = value;
}
map<U>(fn: (value: T) => U): Maybe<U> {
if (!this.wrappedValue) {
import { HostListener } from '@angular/core';
import { Subject, UnaryFunction, Observable } from 'rxjs';
export function ObservableHostListener<T, R = never>(
eventName: string,
args?: string[],
operator?: UnaryFunction<Observable<T>, Observable<R>>
): PropertyDecorator {
// Calling HostListener will return a decorator, ready to
import { HostListener } from '@angular/core';
import { Subject, UnaryFunction, Observable } from 'rxjs';
export function ObservableHostListener(): PropertyDecorator {
return (target: any, key: string) => {
// The stuff we did earlier ...
// Patch ngOnDestroy so we can correctly complete our subject.
patchOnDestroy(target, subject);
// Creating an internal subject that will recieve values from
// Angular calling the shadowed method with new values.
const subject = new Subject<any>();
// We need a super secret string to monkey patch a new method onto
// the target class.
const newKey = `__keyForObservableHostListener__${key}`;
Object.defineProperties(target, {
[newKey]: {
import { HostListener } from '@angular/core';
import { UnaryFunction, Observable } from 'rxjs';
export function ObservableHostListener(
eventName: string,
args?: string[],
operator?: UnaryFunction<Observable<any>, Observable<any>>
): PropertyDecorator {
// Calling HostListener will return a decorator, ready to
@ObservableHostListener<MouseEvent, Coordinate>(
'window:click',
['$event'],
pipe(
map(({ clientX, clientY }) => {
return { x: clientX, y: clientY };
})
)
)
coordinateStream$: Observable<Coordinate>;
@Directive({
selector: '[mouseClick]'
})
export class MouseClickDirective implements OnInit {
private readonly clicks$ = new Subject<MouseEvent>();
private readonly someStream$: Observable<any>;
@HostListener('click', ['$event']) handleClick(click: MouseEvent) {
this.clicks$.next(click);
}
@Directive({
selector: '[mouseClick]'
})
export class MouseClickDirective {
@HostListener('click', ['$event']) handleClick(click: MouseEvent) {
console.log(`Clicked at X: ${click.clientX} and Y: ${click.clientY}`);
}
}
class Filter<T> {
static greaterThan(threshold: number, orEqualTo: boolean = false) {
return (input: number) => orEqualTo ? input >= threshold : input > threshold;
}
static equalTo(compare: number | string) {
return (input: number | string) => input === compare;
}
constructor(private readonly collection: Array<T>) { }