Skip to content

Instantly share code, notes, and snippets.

View armanozak's full-sized avatar

Levent Arman Özak armanozak

View GitHub Profile
@armanozak
armanozak / sequence.js
Created August 8, 2020 16:17
[Creating an Array of Sequential Numbers] A Performance Comparison of Different Methods #javascript #tip
const log = console.log;
/* #1 Array.prototype.fill [0.7s/10M ops] */
log(Array(10).fill(0).map((_, i) => i));
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
/* #2 Array.apply & Array.prototype.map [1.9s/10M ops] */
log(Array.apply(null, Array(10)).map((_, i) => i));
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@armanozak
armanozak / bigint.js
Last active August 6, 2020 05:43
[BigInt] How BigInt Works #tip #javascript
let maxSafeInteger = 2 ** 53 - 1;
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(maxSafeInteger); // 9007199254740991
console.log(maxSafeInteger + 1); // 9007199254740992
console.log(maxSafeInteger + 2); // 9007199254740992 😯
let bigInteger = 2n ** 53n - 1n;
maxSafeInteger = BigInt(Number.MAX_SAFE_INTEGER);
@armanozak
armanozak / promise.js
Created August 4, 2020 15:54
[Promise.all vs Promise.any vs Promise.race] How to Combine Promises #tip #javascript
const apiUrl = 'https://jsonplaceholder.typicode.com';
const createUserEndpoint = userId => apiUrl + '/users/' + userId;
const createTodosEndpoint = userId => createUserEndpoint(userId) + '/todos';
const createAltTodosEndpoint = userId => apiUrl + '/todos?userId=' + userId;
const createTimeout = seconds => new Promise(
(_, reject) => setTimeout(() => reject('TIMEOUT'), seconds * 1000)
);
const fetchJSON = endpoint => fetch(endpoint).then(res => res.json());
@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 {
@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 / 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 / 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 / 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 / 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 / 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(