Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / extracting-function-logic.js
Last active October 10, 2019 16:00
Extracting logic from a complex function
function startProgram() {
if (!window.indexedDB) {
throw new Error("Browser doesn't support indexedDB");
}
initDatabase();
setListeners();
printEmployeeList();
}
@NyaGarcia
NyaGarcia / non-boolean-shorthands.js
Last active November 6, 2019 12:21
Conditionals with boolean variables without using shorthands
if (isWildPokemon === true) {
usePokeball();
}
if (isPokemon === true && isSaved === false) {
save(pokemon);
}
@NyaGarcia
NyaGarcia / boolean-shorthands.js
Last active November 6, 2019 12:20
Conditionals with boolean shorthands
if (isWildPokemon) {
usePokeball();
}
if (isPokemon && !isSaved) {
save(pokemon);
}
@NyaGarcia
NyaGarcia / return-true-false.js
Created November 7, 2019 16:31
Two functions that return true; return false;
function isWildPokemon(pokemon) {
if (pokemon.isWild) {
return true;
} else {
return false;
}
}
function isSaved(pokemonId) {
if (this.savedPokemon.some(({ id }) => id === pokemonId)) {
@NyaGarcia
NyaGarcia / fix-return-true-false.js
Created November 7, 2019 16:50
Directly returning boolean values to avoid return true return false
function isWildPokemon(pokemon) {
return pokemon.isWild;
}
function isSaved(pokemonId) {
return this.savedPokemon.some(({ id }) => id === pokemonId);
}
@NyaGarcia
NyaGarcia / if-else-if.js
Created November 7, 2019 17:38
Verbose if else if statement
function attack(pokemon) {
if (pokemon.type === 'water') {
useWaterAttack();
} else if (pokemon.type === 'fire') {
useFireAttack();
} else if (pokemon.type === 'ice') {
useIceAttack();
} else if (pokemon.type === 'electric') {
useElectricAttack();
} else {
@NyaGarcia
NyaGarcia / tsconfig.json
Created February 2, 2020 20:10
ts-paths configuration
{
"compilerOptions": {
...
"baseUrl": "src",
"paths": {
"@models/*": ["models/*"],
"@shared": ["shared/*"]
}
},
}
@NyaGarcia
NyaGarcia / nested-subscribes.js
Last active March 22, 2020 20:10
Nested observable subscribes
getTrainer().subscribe(trainer =>
getStarterPokemon(trainer).subscribe(pokemon =>
// Do stuff with pokemon
)
);
@NyaGarcia
NyaGarcia / switchMap.js
Last active March 22, 2020 20:10
Fixing nested subscribes with the switchMap operator
getTrainer()
.pipe(
switchMap(trainer => getStarterPokemon(trainer))
)
.subscribe(pokemon => {
// Do stuff with pokemon
});
@NyaGarcia
NyaGarcia / unsubscribe.ts
Created February 29, 2020 12:03
Manually unsubscribing from an observable subscription
pokemonSubscription = pokemon$.subscribe(pokemon => {
// Do something with pokemon
});
pokemonSubscription.unsubscribe();