This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
) { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ObservableHostListener<MouseEvent, Coordinate>( | |
'window:click', | |
['$event'], | |
pipe( | |
map(({ clientX, clientY }) => { | |
return { x: clientX, y: clientY }; | |
}) | |
) | |
) | |
coordinateStream$: Observable<Coordinate>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Directive({ | |
selector: '[mouseClick]' | |
}) | |
export class MouseClickDirective { | |
@HostListener('click', ['$event']) handleClick(click: MouseEvent) { | |
console.log(`Clicked at X: ${click.clientX} and Y: ${click.clientY}`); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>) { } |
NewerOlder