Skip to content

Instantly share code, notes, and snippets.

View k1r0s's full-sized avatar

Ciro Iván k1r0s

View GitHub Profile
@k1r0s
k1r0s / multiple-material-dialog-ccc.ts
Last active September 6, 2017 06:52
cross cutting concerns when dealing with material dialog
public selectUser(user: User): void {
try {
if(user.address.city === 'South Elvis') {
throw new Error(`We cannot retrieve info from people settled in South Elvis >.<'`)
}
/* here */
this.dialogRef = this.dialogFactory.open(
UserDialogComponent,
@k1r0s
k1r0s / a-material-dialog.ts
Created September 4, 2017 16:11
common implementation of angular material's dialog
const dialogRef = this.dialogFactory.open(
SomeComponent,
{
opt1: anyOpt,
opt2: anyOpt,
}
)
@k1r0s
k1r0s / dialog-angular-CCC.md
Last active September 15, 2017 17:52
Material's Dialog as a cross cutting concern

In OOP, each method is an action or verb domain related

Cross Cutting Concerns are pieces of code that brings nothing to understand what this action is really doing by mixing infrastructure concerns with domain concerns

  // stuff that matters
  
 this.dialogRef = this.dialogFactory.open( // this stuff only represents "SHOW A DIALOG, DOMAIN SPEAKING"
@k1r0s
k1r0s / writers.component.ts
Last active September 11, 2017 11:40
solving code duplication and hidden patterns in OOP
@Component({
selector: 'writers',
templateUrl: './writers.component.html',
styleUrls: ['./writers.component.css']
})
export class WritersComponent implements LoadingDialog, InitResourceContainer<User>, CacheContainer, DialogHolder, OnInit {
public userList: User[] = []
public dialogRef: MdDialogRef<UserDialogComponent>
public loadingDialogRef: MdDialogRef<any>
@k1r0s
k1r0s / user-posts.component.ts
Created September 11, 2017 11:40
solving code duplication and hidden patterns in OOP Raw
@Component({
selector: 'user-posts',
templateUrl: './user-posts.component.html',
styleUrls: ['./user-posts.component.css']
})
export class UserPostsComponent implements InitResourceContainer<Post>, CacheContainer, LoadingDialog, OnInit {
public loadingDialogRef: MdDialogRef<any>
public servicePath: string
public userPostList: Post[] = []
@k1r0s
k1r0s / writers-select-user.ts
Last active September 15, 2017 18:20
Multiple concerns in the same method
public selectUser(user: User): void {
try { // infrastructure <-- language primitive, expressing a place where an error can happen
// business (if an user from 'South Elvis' was selected)
if(user.address.city === this.forbiddenCity) {
throw new Error(`We cannot retrieve info from people settled in ${user.address.city} >.<'`)
}
// business (openning a dialog)
@k1r0s
k1r0s / writers-select-user-SRP-decorator.ts
Last active September 15, 2017 20:09
ES7 Decorators providing Single Responsibility Principle
@ReturnException // business <-- capture exception
@OpenDialog(UserDialogComponent) // business <-- open a dialog having method's returned value
public selectUser(user: User): User {
// infrastructure <-- this needs to be explicitly placed here
if(user.address.city === this.forbiddenCity) {
// business <-- throw an exception if an user from 'South Elvis' was selected
throw new Error(`We cannot retrieve info from people settled in ${user.address.city} >.<'`)
}
@k1r0s
k1r0s / my-selector.tsx
Last active October 6, 2022 21:52
Preact native <select> implementation with search feature
/**
* <MySelector
* options={exampleArr}
* printBy="name"
* selected={this.selectedOpt}
* onOptionSelected={(opt) =>
* this.mySelectorValueChange(opt)}
* />
*/
import { h, Component } from "preact";
@k1r0s
k1r0s / ArrayPollyfill.js
Last active December 5, 2017 11:11
Add some useful methods to native JavaScript Array
Object.assign(Array.prototype, {
has(val) {
return this.indexOf(val) > -1;
},
first() {
return this[0];
},
last() {
return this[this.length - 1];
}
@k1r0s
k1r0s / advice-merge.spec.js
Created December 6, 2017 23:26
merge some async advices showcase
import { beforeMethod } from "..";
const Delay = secs => meta => setTimeout(meta.commit, secs);
const soMuchDelay = [Delay(1000), Delay(1000), Delay(1000)];
const someBehavior = beforeMethod(...soMuchDelay, Delay(5));
class Car {