Skip to content

Instantly share code, notes, and snippets.

View dscheerens's full-sized avatar

Daan Scheerens dscheerens

View GitHub Profile
@dscheerens
dscheerens / cached.decorator.ts
Created June 16, 2023 08:18
TypeScript decorator to apply caching on functions
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface CachedDecoratorOptions<FunctionArguments extends any[]> {
keyGenerator?: CacheKeyGenerator<FunctionArguments>;
cache?: CacheFactory;
}
export type CacheKeyGenerator<T extends any[]> = (...args: T) => unknown; // eslint-disable-line @typescript-eslint/no-explicit-any
export interface Cache {
get(cacheKey: unknown): unknown;
@dscheerens
dscheerens / ng-init.decorator.ts
Created October 21, 2020 19:31
Initializer decorator for Angular components (fully type-safe!)
import { Subscription } from 'rxjs';
const GLOBAL_COMPONENT_SUBSCRIPTIONS = new WeakMap<any, Set<Subscription>>();
export function NgInit<
TargetType extends { constructor: Function, ngOnInit?(): void, ngOnDestroy?(): void },
PropertyKey extends string,
PropertyContainer extends { [k in PropertyKey]: () => (void | Subscription) },
FunctionType extends PropertyContainer[PropertyKey]
>(
@dscheerens
dscheerens / memoize.decorator.ts
Last active September 30, 2021 22:13
TypeScript memoize decorator for get accessor functions
const GLOBAL_MEMOIZATION_MAP = new WeakMap<object, Map<string, unknown>>();
// tslint:disable-next-line:ban-types
export function Memoize<T extends { constructor: Function }>(
target: T,
propertyKey: string,
descriptor: PropertyDescriptor,
): PropertyDescriptor {
const originalGet = descriptor.get; // tslint:disable-line: no-unbound-method
@dscheerens
dscheerens / define-translation-keys.ts
Created May 6, 2020 20:11
Utility function to define type safe translation keys
const TRANSLATION_KEY_PLACEHOLDER = Symbol('TRANSLATION_KEY_PLACEHOLDER');
type TranslationKeyPlaceholder = typeof TRANSLATION_KEY_PLACEHOLDER;
export interface TranslationStructure {
[key: string]: TranslationStructure | TranslationKeyPlaceholder;
}
export type TranslationKeysOf<T extends TranslationStructure> = {
[P in keyof T]: T[P] extends TranslationStructure ? TranslationKeysOf<T[P]> : string
};
@dscheerens
dscheerens / injection-token-factory-builder.ts
Created August 15, 2019 05:47
Builder pattern applied to InjectionToken factory functions
// ========================================
// app-module.ts
// ========================================
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { NumbersModule } from './numbers';
@NgModule({
@dscheerens
dscheerens / observe-property.ts
Last active March 22, 2022 12:07
Property observe function
import { BehaviorSubject, Observable } from 'rxjs';
/**
* Observes the specified property and returns a stream that emits all values which are assigned to the property. When subscribing to the
* resulting stream it will always first emit the current value of the property, followed by all new values that are assigned to it.
*
* @param target Object containing the property.
* @param key Key of the property that is to be observed.
* @returns A stream of all values that are assigned to the specified property, starting with the current value of the property.
*/