Skip to content

Instantly share code, notes, and snippets.

View NickHodges's full-sized avatar

Nick Hodges NickHodges

View GitHub Profile
class Athlete {
constructor(private name: string) {}
}
const MixinDecathlete = ThrowsFar(RunsFast(JumpsHigh(Athlete)));
const mixinDecathlete = new MixinDecathlete('Rafer Johnson');
mixinDecathlete.runFast();
mixinDecathlete.throwFar();
mixinDecathlete.jumpHigh();
function RunsFast<TBase extends Constructor>(Base: TBase) {
return class extends Base {
runFast() {
console.log('Runs fast');
}
};
}
function ThrowsFar<TBase extends Constructor>(Base: TBase) {
return class extends Base {
type Constructor<T = {}> = new (...args: any[]) => T;
class Decathlete implements RunsFast, JumpsHigh, ThrowsFar {
runsFast!: () => void;
jumpsHigh!: () => void;
throwsFar!: () => void;
}
const aDecathlete = new Decathlete();
console.log('Decathlete:');
aDecathlete.runsFast();
aDecathlete.jumpsHigh();
aDecathlete.throwsFar();
applyMixins(Decathlete, [runsFast, jumpsHigh, throwsFar]);
class Decathlete implements runsFast, jumpsHigh, throwsFar {
runsFast: () => void;
jumpsHigh: () => void;
throwsFar: () => void;
}
function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
});
});
class runsFast {
runsFast() {
console.log('Runs fast');
}
}
class jumpsHigh {
jumpsHigh() {
console.log('Jumps high');
}
@NickHodges
NickHodges / EncryptSomething.ts
Last active February 24, 2020 23:54
EncryptSomething
function EncryptSomething(aSuperSecretStuff: any[], aIWantToBeSafe: Boolean) {
var encryptor: IEncrypt;
if (aIWantToBeSafe) {
encryptor = new superDuperPowerfulEncryption();
} else {
encryptor = new whoCaresHowSafe();
}
encryptor.Encrypt(aSuperSecretStuff);
}