Skip to content

Instantly share code, notes, and snippets.

@NguyenTungs
Created May 17, 2018 01:50
Show Gist options
  • Save NguyenTungs/9ac8f9b9518e1548a11b2bcdd18359b5 to your computer and use it in GitHub Desktop.
Save NguyenTungs/9ac8f9b9518e1548a11b2bcdd18359b5 to your computer and use it in GitHub Desktop.
es6
const people = Array.from(document.querySelectorAll('.people p'));
const names = peopleArray.map(person => person.textContent);
// ---------------------------------------------------------------
const names = Array.from(document.querySelectorAll('.people p'), person => {
return person.textContent
});
const ages = Array.of(1, 2, 3, 4, 5); // [1, 2, 3, 4, 5]
// ---------------------------------------------------------------
const code = 'VBgtGQcSf';
const post = posts.find(post => post.code === code);
const postIndex = posts.findIndex(post => post.code === code);
console.log(postIndex);
// ---------------------------------------------------------------
const ages = [32, 15, 19, 12];
// 👵👨 is there at least one adult in the group?
const adultPresent = ages.some(age => age >= 18);
console.log(adultPresent); // true
// 🍻 is everyone old enough to drink?
const allOldEnough = ages.every(age => age >= 19);
console.log(allOldEnough); // false
const cuts = ['Chuck', 'Brisket', 'Shank', 'Short Rib'];
for (const [i, cut] of cuts.entries()) {
console.log(`${cut} is the ${i + 1} item`);
}
// -------------------------------------------------------
function addUpNumbers(...args) {
return console.log(args.reduce((a, b) => a + b, 0));
}
addUpNumbers(10,23,52,34,12,13,123);
// -------------------------------------------------------
const apple = {
color: 'Red',
size: 'Medium',
weight: 50,
sugar: 10
};
for (const prop in apple) {
const value = apple[prop];
console.log(value, prop);
}
// promise
const postsPromise = fetch('http://wesbos.com/wp-json/wp/v2/posts');
// promise listener
postsPromise
.then(data => data.json())
.then(data => { console.log(data) })
.catch((err) => {
console.error(err);
});
// -----------------------------------------------------------------
const p = new Promise((resolve, reject) => {
setTimeout(() => {
reject(Error('Err wes isn\'t cool'));
}, 1000);
});
p
.then(data => {
console.log(data);
})
.catch(err => {
console.error(err);
});
// -----------------------------------------------------------------
const posts = [
{ title: 'I love JavaScript', author: 'Wes Bos', id: 1 },
{ title: 'CSS!', author: 'Chris Coyier', id: 2 },
{ title: 'Dev tools tricks', author: 'Addy Osmani', id: 3 },
];
const authors = [
{ name: 'Wes Bos', twitter: '@wesbos', bio: 'Canadian Developer' },
{ name: 'Chris Coyier', twitter: '@chriscoyier', bio: 'CSS Tricks and CodePen' },
{ name: 'Addy Osmani', twitter: '@addyosmani', bio: 'Googler' },
];
function getPostById(id) {
// create a new promise
return new Promise((resolve, reject) => {
// using a settimeout to mimick a databse
setTimeout(() => {
// find the post we want
const post = posts.find(post => post.id === id);
if(post) {
resolve(post); // send the post back
} else {
reject(Error('No Post Was Found!'));
}
}, 200);
});
}
function hydrateAuthor(post) {
// create a new promise
return new Promise((resolve, reject) => {
// find the author
const authorDetails = authors.find(person => person.name === post.author);
if(authorDetails) {
// "hydrate" the post object with the author object
post.author = authorDetails;
resolve(post);
} else {
reject(Error('Can not find the author'));
}
});
}
getPostById(3)
.then(post => {
return hydrateAuthor(post);
})
.then(post => {
console.log(post);
})
.catch(err => {
console.error(err);
});
// -----------------------------------------------------------------
const postsPromise = fetch('http://wesbos.com/wp-json/wp/v2/posts');
const streetCarsPromise = fetch('http://data.ratp.fr/api/datasets/1.0/search/?q=paris');
Promise
.all([postsPromise, streetCarsPromise])
.then(responses => {
return Promise.all(responses.map(res => res.json()))
})
.then(responses => {
const [posts, streetCars] = responses;
console.log(posts ,streetCars);
});
const first = 'snickers';
const last = 'bos';
const age = 2;
const breed = 'King Charles Cav';
const dog = {
firstName: first,
last,
age,
breed,
pals: ['Hugo', 'Sunny']
};
console.log(dog);
// -----------------------------------------------------------------
const first = 'snickers';
const last = 'bos';
const age = 2;
const breed = 'King Charles Cav';
const dog = {
firstName: first,
last,
age,
breed,
pals: ['Hugo', 'Sunny']
};
console.log(dog);
const modal = {
create(selector) {
// code
},
open(content) {
// code
},
close(goodbye) {
// code
}
};
function invertColor(color) {
return '#' + ("000000" + (0xFFFFFF ^ parseInt(color.substring(1), 16)).toString(16)).slice(-6);
}
const key = 'pocketColor';
const value = '#ffc600';
const tShirt = {
[key]: value,
[`${key}Opposite`]: invertColor(value)
};
const keys = ['size', 'color', 'weight'];
const values = ['medium', 'red', 100];
const shirt = {
[keys.shift()]: values.shift(),
[keys.shift()]: values.shift(),
[keys.shift()]: values.shift(),
}
console.log(shirt);
const heading = document.querySelector('.jump');
heading.innerHTML = sparanWrap(heading.textContent);
function sparanWrap(word) {
return [...word].map(letter => `<span>${letter}</span>`).join('');
}
// -----------------------------------------------------------------
const comments = [
{ id: 209384, text: 'I love your dog!' },
{ id: 523423, text: 'Cuuute! 🐐' },
{ id: 632429, text: 'You are so dumb' },
{ id: 192834, text: 'Nice work on this wes!' },
];
const id = 632429;
const commentIndex = comments.findIndex(comment => comment.id === id);
const newComments = [...comments.slice(0,commentIndex), ...comments.slice(commentIndex + 1)];
console.log(newComments);
// -----------------------------------------------------------------
const inventors = ['Einstein', 'Newton', 'Galileo'];
const newInventors = ['Musk', 'Jobs'];
inventors.push(...newInventors);
console.log(inventors);
const name = ['Wes', 'Bos'];
function sayHi(first, last) {
alert(`Hey there ${first} ${last}`);
}
sayHi(...name);
// -----------------------------------------------------------------
function convertCurrency(rate, tax, tip, ...amounts) {
console.log(rate, tax, tip, amounts);
return amounts.map(amount => amount * rate);
}
const amounts = convertCurrency(1.54, 10, 23, 52, 1, 56);
console.log(amounts);
const runner = ['Wes Bos', 123, 5.5, 5, 3, 6, 35];
const [name, id, ...runs] = runner;
console.log(name, id, runs);
const team = ['Wes', 'Kait', 'Lux', 'Sheena', 'Kelly'];
const [captian, assistant, ...players] = team;
console.log(captian, assistant, players);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment