Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / rest-parameter-destructuring.js
Created September 23, 2019 15:57
Destructuring with the rest parameter
const pokemon = {
id: 1,
name: 'Squirtle',
type: 'Water'
};
const { id, ...rest } = pokemon;
console.log(rest); //Result: { name: 'Squirtle', type: 'Water' }
@NyaGarcia
NyaGarcia / default-property.js
Last active September 23, 2019 16:43
Showing how the existing abilities property isn't modified
const pokemon = {
name: 'Squirtle',
type: 'Water',
abilities: ['Torrent', 'Rain Dish']
};
const { abilities = [], ...rest } = pokemon;
const fullSquirtle = { ...rest, abilities };
console.log(rest); //Result: { name: 'Squirtle', type: 'Water' }
@NyaGarcia
NyaGarcia / default-values-destructuring.js
Created September 24, 2019 16:00
Assigning default values to destructured variables
const pokemon = {
id: 1,
name: 'Squirtle'
};
const { type, name } = pokemon;
console.log(name); //Result: Squirtle
console.log(type); //Result: undefined
//Assigning default value to the type variable
@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 / 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
Last active January 22, 2020 13:28
Removing an object property dynamically with the rest paramter
const pokemon = {
useless: 'useless property',
id: 1,
name: 'Squirtle',
type: 'Water'
};
function removeProperty(property, object) {
({ [property]: value, ...newObject } = object);
return newObject;
@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' }
@NyaGarcia
NyaGarcia / nested-if.js
Last active March 2, 2021 17:39
A function containing a nested if
function publishTweet(tweet) {
if (isLoggedIn()) {
if (tweet) {
if (isTweetDoubleChecked()) {
tweetIt(tweet);
} else {
throw new Error('Dont publish without double checking your tweet');
}
} else {
throw new Error("Your tweet is empty, can't publish it");
@NyaGarcia
NyaGarcia / guard-clauses.js
Last active May 11, 2021 17:13
Refactoring a function to use guard clauses
function publishTweet(tweet) {
if (!isLoggedIn()) {
throw new Error('You need to log in before tweeting');
}
if (!tweet) {
throw new Error("Your tweet is empty, can't publish it");
}
if (!isTweetDoubleChecked()) {
throw new Error('Dont publish without double checking your tweet');