Skip to content

Instantly share code, notes, and snippets.

View armanozak's full-sized avatar

Levent Arman Özak armanozak

View GitHub Profile
@armanozak
armanozak / avengers.ts
Created May 3, 2020 11:23
[Strategy Pattern with TypeScript] After Implementation #blog #typescript
abstract class Hero {
constructor(public name: string, public weapon?: string) {}
abstract attack(): void;
}
class BlastingHero extends Hero {
attack() {
console.log(`${this.name} blasted ${this.weapon}.`);
}
@armanozak
armanozak / avengers.ts
Created May 3, 2020 11:22
[Strategy Pattern with TypeScript] Before Implementation #blog #typescript
class Hero {
constructor(public name: string, public weapon?: string) {}
}
class Avengers {
private ensemble: Hero[] = [];
private blast(hero: Hero) {
console.log(`${hero.name} blasted ${hero.weapon}`);
}
@armanozak
armanozak / deep-truth.js
Created January 6, 2020 13:29
[Strategy Pattern & Higher Order Functions] How to Switch Algorithms at Run-Time #javascript #tips
function createDeepTruthChecker(useStrategy = Object.keys) {
return function check(obj) {
return !!obj && (
typeof obj === 'object'
? useStrategy(obj).every(key => check(obj[key]))
: true
);
};
}
@armanozak
armanozak / deep-boolean.ts
Created January 1, 2020 21:03
[Callable Constructors in TypeScript] How to Merge Ambient Class Declaration with Function Declaration #typescript #tips
// requires TypeScript v3.6+
export { DeepBoolean };
type Mutable<T> = {
-readonly [P in keyof T]: T[P];
};
declare class DeepBoolean<T = any> {
constructor(source: T);
@armanozak
armanozak / keybindings.json
Last active December 16, 2020 11:07
[Disabling TS Lint, The Easy Way] VS Code Snippet + Key Binding for Adding TS Lint Rule Flags #typescript #vscode #tip
// /User/keybindings.json
[
{
"key": "shift+alt+t",
"command": "editor.action.insertSnippet",
"when": "resourceLangId == typescript && editorTextFocus && !editorReadonly",
"args": {
"langId": "typescript",
"name": "Disable TSLint"
@armanozak
armanozak / card-button.component.ts
Last active November 10, 2019 09:56
Adaptive Components - Using Properties on Parent Component in Angular #blog
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { AbstractCard } from './abstract-card';
import { CardHorizontalComponent } from './card-horizontal.component';
@Component({
selector: 'app-card-button',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<button class="btn" [ngClass]="classes" (click)="parent.action()">
<ng-content></ng-content>
@armanozak
armanozak / card-title.component.ts
Created November 5, 2019 11:16
Adaptive Components - Using :host-context Selector to Set a Style Based on Context #blog
import { ChangeDetectionStrategy, Component } from '@angular/core';
@Component({
selector: 'app-card-title',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<h5 class="card-title">
<ng-content></ng-content>
</h5>
`,
@armanozak
armanozak / abstract-card.ts
Created November 5, 2019 10:25
Adaptive Components - Card Components and How They Are Consumed #blog
import { ContentChild, ElementRef, Input } from '@angular/core';
import { CardTitleComponent } from './card-title.component';
export abstract class AbstractCard {
@Input() action = () => {};
@Input() color = 'primary';
}
@armanozak
armanozak / card-image.component.ts
Last active November 8, 2019 08:05
Adaptive Components - Not-So-Dumb Card Image Component #blog
import {
ChangeDetectionStrategy,
Component,
Input,
Optional,
} from '@angular/core';
import { CardVerticalComponent } from './card-vertical.component';
@Component({
selector: 'app-card-image',
@armanozak
armanozak / card-image.component.ts
Last active November 4, 2019 12:22
Adaptive Components - Dumb Card Image Component #blog
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
@Component({
selector: 'app-card-image',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<img [alt]="alt" [src]="src" [ngClass]="classes" (load)="ready = true" />
<span *ngIf="ready && attribution">
Image by <a [href]="attribution.href">{{ attribution.text }}</a>
</span>