Skip to content

Instantly share code, notes, and snippets.

View RomkeVdMeulen's full-sized avatar

Romke van der Meulen RomkeVdMeulen

View GitHub Profile
export function autocast<T>(type: {new (): T}) {
return function(target: any, key: string) {
makeAutocast(target, key, (newVal: any) => {
if (newVal === undefined) {
return undefined;
}
if (newVal instanceof type) {
return newVal;
}
return Object.assign(new type(), newVal);
@RomkeVdMeulen
RomkeVdMeulen / init.ts
Last active May 10, 2018 17:48
@init / @waitOnInit TypeScript method decorators
const INIT_METHODS = new Map<any, string[]>();
type InitMethodDescriptor = TypedPropertyDescriptor<() => void>
| TypedPropertyDescriptor<() => Promise<void>>;
export function init(target: any, key: string, _descriptor: InitMethodDescriptor) {
if (!INIT_METHODS.has(target)) {
INIT_METHODS.set(target, []);
}
INIT_METHODS.get(target)!.push(key);
@RomkeVdMeulen
RomkeVdMeulen / prop-decorator.ts
Last active June 26, 2020 14:01
How to change instance properties through decorators in TypeScript: http://romkevandermeulen.nl/2018/01/24/typescript-property-decorators.html
function makePropertyMapper<T>(prototype: any, key: string, mapper: (value: any) => T) {
const values = new Map<any, T>();
Object.defineProperty(prototype, key, {
set(firstValue: any) {
Object.defineProperty(this, key, {
get() {
return values.get(this);
},
set(value: any) {
values.set(this, mapper(value));
@RomkeVdMeulen
RomkeVdMeulen / by_marker.d.ts
Last active March 21, 2018 13:08
Add a `by.marker` locator to Protractor to match elements marked with an "e2e" attribute
import {Locator} from "protractor/built/locators";
declare module "protractor/built/locators" {
export interface ProtractorBy {
marker(marker: string, parentElement?: Node): Locator;
}
}
@RomkeVdMeulen
RomkeVdMeulen / tobeinstanceof.ts
Last active August 16, 2017 16:50
Custom matcher for Jasmine > 2.0 to verify if a given value is an instance of the expected type
/* Run this during setup */
beforeAll(() => {
jasmine.addMatchers({
toBeInstanceOf: function() {
return {
compare: function(actual: any, expected: {new (...args: any[]): any}) {
const pass = actual instanceof expected;
return {
pass,
@RomkeVdMeulen
RomkeVdMeulen / serve.sh
Created July 31, 2017 08:11
How to serve content with ExpressJS in 3 quick steps
yarn add express # or 'npm install express'
echo 'var express = require("express");
var app = express();
app.use("/", express.static(__dirname));
app.listen(3001);' > serve.js
node serve.js
@RomkeVdMeulen
RomkeVdMeulen / app.html
Last active April 7, 2017 10:17
Aurelia binding: InlineViewStrategy update issue
<template>
<p>Result:</p>
<compose view.bind="message" containerless></compose>
<hr>
<label for="replaceTimeout1">Ms before first replacement</label>
<input type="number" value.bind="replaceTimeout1" id="replaceTimeout1"/>
<hr>
<label for="replaceTimeout2">Ms before second replacement</label>
<input type="number" value.bind="replaceTimeout2" id="replaceTimeout2"/>
@RomkeVdMeulen
RomkeVdMeulen / pollers.ts
Last active April 10, 2017 11:24
Simple poller library built in TypeScipt
import {BackendService} from "./backend";
export class PollerService {
constructor(private backendService: BackendService) {
}
static readonly INTERVAL = {
SHORT: 1000,
STANDARD: 2000,
@RomkeVdMeulen
RomkeVdMeulen / html_forms.md
Last active April 7, 2017 07:55
Some notes on HTML forms
@RomkeVdMeulen
RomkeVdMeulen / app.html
Last active March 27, 2017 11:41
Aurelia addError() during validation
<template>
<require from="./registration-form"></require>
<registration-form></registration-form>
</template>