Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / default-property-function.js
Created September 24, 2019 17:10
Adding default properties to an array of objects
const pokemon = [
{
name: 'Charmander',
type: 'Fire'
},
{ name: 'Squirtle', type: 'Water', abilities: ['Torrent', 'Rain Dish'] },
{
name: 'Bulbasur',
type: 'Plant'
}
@NyaGarcia
NyaGarcia / nested-object-clone.js
Last active September 27, 2019 15:42
Cloning an object with nested array, with the spread operator
const pokemon = {
name: 'Squirtle',
type: 'Water',
abilities: ['Torrent', 'Rain Dish']
};
const squirtleClone = { ...pokemon, abilities: [...pokemon.abilities] };
pokemon.name = 'Charmander';
pokemon.abilities.push('Surf');
@NyaGarcia
NyaGarcia / rest-parameter-remove.js
Created October 4, 2019 17:19
Removing object properties with the rest parameter
const pokemon = {
useless: 'useless property',
id: 1,
name: 'Squirtle',
type: 'Water'
};
// Removing the useless property:
({ useless, ...newPokemon } = pokemon);
console.log(newPokemon); // Result: { id: 1, name: 'Squirtle', type: 'Water' }
@NyaGarcia
NyaGarcia / rest-parameter-remove-many.js
Last active October 4, 2019 17:32
Removing several object properties with the rest parameter
const pokemon = {
useless: 'Useless property',
id: 1,
name: 'Squirtle',
useless2: 'Another useless prop',
type: 'Water'
};
// Removing both useless and useless2 properties:
({ id, useless, useless2, ...newPokemon } = pokemon);
@NyaGarcia
NyaGarcia / dynamic-remove-property.js
Created October 6, 2019 16:05
Removing an object property dynamically with the rest parameter - ES6 version
const pokemon = {
useless: 'useless property',
id: 1,
name: 'Squirtle',
type: 'Water'
};
const remove = (property, { [property]: value, ...newObject }) => newObject;
console.log(remove('useless', pokemon)); // Result: { id: 1, name: 'Squirtle', type: 'Water' }
function getPokemon(type) {
let pokemon;
switch (type) {
case 'Water':
pokemon = 'Squirtle';
break;
case 'Fire':
pokemon = 'Charmander';
break;
case 'Plant':
@NyaGarcia
NyaGarcia / replace-switch-map.js
Last active October 8, 2019 17:55
Using a Map to replace a switch statement
const pokemon = new Map([
['Water', 'Squirtle'],
['Fire', 'Charmander'],
['Plant', 'Bulbasur'],
['Electric', 'Pikachu']
]);
function getPokemon(type) {
return pokemon.get(type) || 'Mew';
}
@NyaGarcia
NyaGarcia / complex-conditional.js
Last active October 9, 2019 16:51
A function with a complex conditional statement
function checkGameStatus() {
if (
remaining === 0 ||
(remaining === 1 && remainingPlayers === 1) ||
remainingPlayers === 0
) {
quitGame();
}
}
@NyaGarcia
NyaGarcia / extracting-conditional.js
Created October 9, 2019 17:03
Extracting a conditional to a function
function isGameLost() {
return (
remaining === 0 ||
(remaining === 1 && remainingPlayers === 1) ||
remainingPlayers === 0
);
}
// Our function is now much easier to understand:
function checkGameStatus() {
@NyaGarcia
NyaGarcia / fixing-duplication.js
Last active October 10, 2019 13:03
Further refactoring duplicating code
function getNews(type) {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, type);
}
function getNewsContent(newsList, type) {
const news = [];
for (let i = newsList.length - 1; i >= 0; i--) {
if (newsList[i].type === type) {
news.push(newsList[i].content);