Skip to content

Instantly share code, notes, and snippets.

View NFantela's full-sized avatar

Nikša Fantela NFantela

  • Rijeka, Croatia
View GitHub Profile
@NFantela
NFantela / const-assertions-in-typescript.ts
Created February 26, 2021 11:33
Narrowing down types with const assertion demo
type Genres = 'comedy' | 'horror';
type Book = { title: string, genre : Genres};
const firstBook = {
title : "My first book",
genre: 'comedy'
}
const secondBook:Book = {
title : "My fisrt book",
@NFantela
NFantela / typescript-template-literals-example.ts
Last active January 26, 2021 13:05
Using Typescript template literals to create custom router params type checker
type ExtractRouteParams<T extends string> =
string extends T
? Record<string, string>
// Match empty strings
: T extends ''
? ['Please provide a template']
// Match whitespace
: T extends `${infer _Start} ${infer _Rest}`
@NFantela
NFantela / array-recursive.component.ts
Last active October 27, 2020 09:34
Recursion in Angular components
@Component({
selector: "array-recursive-c",
template: `
<ng-container *ngIf="isArrayHere; else showJustItem">
<!-- iterate on the current level of nested array-->
<array-recursive-c
*ngFor="let item of value"
[value]="item"
></array-recursive-c>
@NFantela
NFantela / delay-first-emmision-by.ts
Created October 14, 2020 12:30
Create custom RXJS operator to delay first emmision fron any observable.
import { MonoTypeOperatorFunction, Observable, Observer, Operator, TeardownLogic } from 'rxjs';
import { delay, take, switchMapTo } from 'rxjs/operators';
// defining operator as class must implement call method
class DelayFirstEmmisionOperator<T> implements Operator<T, T> {
constructor(private delayInMs:number) { }
call(observer: Observer<T>, source: Observable<T>): TeardownLogic {
return source.pipe(
delay(this.delayInMs),
@NFantela
NFantela / curry-function.ts
Created September 3, 2020 08:16
Javascript Currying and Pipes
function curry(fn:Function){
return function(...args:any[]){
// fn.length is number of arguments
if(args.length >= fn.length){
return fn.apply(null, args); // just call fn
}
// bind a new function with passed arguments
return fn.bind(null, ...args);
}
}
@NFantela
NFantela / network-app.component.ts
Last active August 17, 2020 10:45
Observable based network preloading service
@Component({
selector: 'app-root',
template: `
<h3>Router outlet </h3>
<button type="button" (click)="preloadIt('network-demo')">Preload it</button>
<button type="button" (click)="preloadIt('network-second')">Preload second</button>
<router-outlet></router-outlet>
`,
styleUrls: ['./app.component.scss']
})
@NFantela
NFantela / local-storage.service.ts
Last active July 23, 2020 09:48
Local storage as observable service
@Injectable()
export class LocalStorageManagerService extends Observable<Map<string, string>>
implements OnDestroy {
private readonly _entireStorage$:BehaviorSubject<Map<string, string>>
= new BehaviorSubject(this._allStorage());
constructor(@Inject(LOCAL_STORAGE) private _localStorage: Storage){
super( subscriber => {
@NFantela
NFantela / same-selector.usage.html
Last active July 16, 2020 09:29
Same selectors on directives
<!-- the only difference is "someInput" -->
<some-directive someInput></some-directive>
<some-directive></some-directive>
@NFantela
NFantela / custom-rxjs-operator.ts
Last active June 19, 2020 10:24
Playing with different ways of RxJS Observables creation and usage
// defining operator as class must implement call method
class LogOperator<T> implements Operator<T, T> {
constructor(private _additionalMessage?:string) { }
call(observer: Observer<T>, source: Observable<T>): TeardownLogic {
return source.pipe(
tap(val => console.log(`${this._additionalMessage ? this._additionalMessage : 'Logging: '} ${val}`))
).subscribe(observer);
}
}
@NFantela
NFantela / my-store.module.ts
Created June 16, 2020 10:09
Ngrx Store - state, store and module
export interface StoreConfig<T, V extends Action = Action> {
initialState?: InitialState<T>;
reducerFactory?: ActionReducerFactory<T, V>;
metaReducers?: MetaReducer<T, V>[];
}
export interface RootStoreConfig<T, V extends Action = Action>
extends StoreConfig<T, V> {
runtimeChecks?: Partial<RuntimeChecks>;
}