Skip to content

Instantly share code, notes, and snippets.

View armanozak's full-sized avatar

Levent Arman Özak armanozak

View GitHub Profile
@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 / 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 / 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 / demo.component.ts
Created May 3, 2020 11:28
[Strategy Pattern with TypeScript - ABP Angular Implementation] DomInsertionService Usage #blog #typescript
import { DomInsertionService, CONTENT_STRATEGY } from '@abp/ng.core';
@Component({
/* class metadata here */
})
class DemoComponent {
constructor(private domInsertionService: DomInsertionService) {}
ngOnInit() {
const scriptElement = this.domInsertionService.insertContent(
@armanozak
armanozak / content.strategy.ts
Created May 3, 2020 11:30
[Strategy Pattern with TypeScript - ABP Angular Implementation] ContentStrategy #blog #typescript
import {
ContentSecurityStrategy,
CONTENT_SECURITY_STRATEGY,
} from './content-security.strategy';
import { DomStrategy, DOM_STRATEGY } from './dom.strategy';
export abstract class ContentStrategy<T extends HTMLScriptElement | HTMLStyleElement = any> {
constructor(
public content: string,
protected domStrategy: DomStrategy = DOM_STRATEGY.AppendToHead(),
@armanozak
armanozak / dom-insertion.service.ts
Created May 3, 2020 11:31
[Strategy Pattern with TypeScript - ABP Angular Implementation] DomInsertionService #blog #typescript
import { Injectable } from '@angular/core';
import { ContentStrategy } from '../strategies/content.strategy';
import { generateHash } from '../utils';
@Injectable({ providedIn: 'root' })
export class DomInsertionService {
private readonly inserted = new Set<number>();
insertContent<T extends HTMLScriptElement | HTMLStyleElement>(
contentStrategy: ContentStrategy<T>,
@armanozak
armanozak / template.html
Created June 9, 2020 14:29
[Attribute Directives to Avoid Repetition in Angular] Properties passed to ngx-datatable before using directives #blog #angular
<ngx-datatable
[rows]="data$ | async"
[count]="totalCount$ | async"
[loadingIndicator]="list.isLoading$ | async"
[limit]="list.maxResultCount"
[offset]="list.page"
(page)="list.page = $event.offset"
(sort)="sort($event)"
[externalPaging]="true"
[externalSorting]="true"
@armanozak
armanozak / template.html
Created June 9, 2020 14:30
[Attribute Directives to Avoid Repetition in Angular] Properties passed to ngx-datatable after using directives #blog #angular
<ngx-datatable
[rows]="data$ | async"
[count]="totalCount$ | async"
[list]="list"
default
>
<!-- templates here -->
</ngx-datatable>
@armanozak
armanozak / ngx-datatable-list.directive.ts
Created June 9, 2020 14:31
[Attribute Directives to Avoid Repetition in Angular] The Adapter Directive #blog #angular
@Directive({
// tslint:disable-next-line
selector: 'ngx-datatable[list]',
exportAs: 'ngxDatatableList',
})
export class NgxDatatableListDirective implements OnChanges, OnDestroy, OnInit {
private subscription = new Subscription();
@Input() list: ListService;
@armanozak
armanozak / ngx-datatable-default.directive.ts
Created June 9, 2020 14:32
[Attribute Directives to Avoid Repetition in Angular] The Default Properties Directive #blog #angular
@Directive({
// tslint:disable-next-line
selector: 'ngx-datatable[default]',
exportAs: 'ngxDatatableDefault',
})
export class NgxDatatableDefaultDirective {
@Input() class = 'material bordered';
@HostBinding('class')
get classes(): string {