Skip to content

Instantly share code, notes, and snippets.

View touhidrahman's full-sized avatar

Touhid Rahman touhidrahman

View GitHub Profile
@touhidrahman
touhidrahman / objectdiff.ts
Last active October 11, 2018 22:16
Compare two similar/identical javascript objects for differences. Returns the changed properties filled with updated value, and unchanged fields with null.
export class ObjectDiff {
isObject(obj: Object): boolean {
return typeof obj === 'object' && !Array.isArray(obj)
}
isArray(obj: Object): boolean {
return Array.isArray(obj)
}
@touhidrahman
touhidrahman / flatten.ts
Last active October 11, 2018 21:43
Flatten a javascript object in specified structure (see comment block)
/**
* Run file
*
* `tsc flatten.ts && node flatten.js`
*
* Given following object:
{
test: "test1",
same: "same",
nested: {
@touhidrahman
touhidrahman / github-ubuntu.sh
Created October 15, 2018 14:42 — forked from dstroot/github-ubuntu.sh
Setting up github on Ubuntu
#!/bin/bash
###############################################
# To use:
# wget https://raw.github.com/gist/4411254
# chmod 777 github-ubuntu.sh
# ./github-ubuntu.sh
###############################################
echo "*****************************************"
echo " Step 1: Check your keys"
echo "*****************************************"
@touhidrahman
touhidrahman / state-atom.ts
Last active February 5, 2021 00:02
Medium article state management with state atoms
import { BehaviorSubject, Observable } from 'rxjs'
import { distinctUntilChanged } from 'rxjs/operators'
export class StateAtom<T> {
value$: Observable<T>
private store: BehaviorSubject<T>
private value: T
private initialValue: T
@touhidrahman
touhidrahman / view-article-page-state.service.ts
Last active February 6, 2021 16:18
view-article-page-state.service.ts
@Injectable()
export class ViewArticlePageStateService {
private currentState: ViewArticlePageState = initialState
private id: StateAtom<number> = new StateAtom(initialState.id)
private article: StateAtom<Article> = new StateAtom(initialState.article)
private loading: StateAtom<boolean> = new StateAtom(initialState.loading)
private searchTerm: StateAtom<string> = new StateAtom(initialState.searchTerm)
private comments: StateAtom<Comment[]> = new StateAtom(initialState.comments)
private commentsPagination: StateAtom<Params> = new StateAtom(initialState.commentsPagination)
@touhidrahman
touhidrahman / view-article-page-state.ts
Created February 6, 2021 15:31
Medium article state management - initial state
export interface ViewArticlePageState {
id: number
article: Article
comments: Comment[]
searchTerm: string
loading: boolean
commentsPagination: {
_limit: number
_start: number
_sort: string
@touhidrahman
touhidrahman / view-article-page.state.service.v1.ts
Created February 6, 2021 15:46
Medium state managemt article viewpageStateService v1
@Injectable()
export class ViewArticlePageStateService {
private currentState: ViewArticlePageState = initialState
private id: StateAtom<number> = new StateAtom(initialState.id)
private article: StateAtom<Article> = new StateAtom(initialState.article)
private loading: StateAtom<boolean> = new StateAtom(initialState.loading)
private searchTerm: StateAtom<string> = new StateAtom(initialState.searchTerm)
private comments: StateAtom<Comment[]> = new StateAtom(initialState.comments)
private commentsPagination: StateAtom<Params> = new StateAtom(initialState.commentsPagination)
@touhidrahman
touhidrahman / view-article-page.state.service.v3.ts
Last active February 6, 2021 16:07
Medium state managemt article viewpageStateService v3
@Injectable()
export class ViewArticlePageStateService {
// ... class properties declaration, omitted for brevity
constructor(private articleService: ArticleService, private commentService: CommentService) {
this.handleEffects()
}
private handleEffects() {
this.id.value$.pipe(
@touhidrahman
touhidrahman / view-article-page.component.html
Last active February 6, 2021 18:53
Medium article state management component
<ng-container *ngIf="state$ | async as state">
<article *ngIf="state.article as article" class="article">
<h1>{{ article.title }}</h1>
<p>{{ article.body }}</p>
</article>
<section class="comments">
<h1>Comments</h1>
<div *ngIf="state.commentsPagination as pagination" class="pagination">
View
@touhidrahman
touhidrahman / unconventional-angular-hide.directive.ts
Last active April 18, 2021 13:00
Medium article Unconventional Angular Hide Directive
import {Directive, ElementRef, Input, Renderer2} from '@angular/core';
@Directive({ selector: '[appHide]' })
export class HideDirective {
@Input() set appHide(shouldHide: boolean) {
(shouldHide)
? this.renderer2.setStyle(this.elRef.nativeElement, 'visibility', 'hidden')
: this.renderer2.removeStyle(this.elRef.nativeElement, 'visibility');
}