Skip to content

Instantly share code, notes, and snippets.

@marsicdev
Created September 23, 2019 20:50
Show Gist options
  • Save marsicdev/31216f173e724e45c53e3bc3e0a7135b to your computer and use it in GitHub Desktop.
Save marsicdev/31216f173e724e45c53e3bc3e0a7135b to your computer and use it in GitHub Desktop.
/******************************
* trimStart & trimEnd
******************************/
let message = ' ES2109 meetup '
message.trimRight()
// ' ES2109 meetup'
message.trimLeft()
// 'ES2109 meetup '
message.trimRight().trimLeft()
// 'ES2109 meetup'
message.trimStart()
// 'ES2109 meetup '
message.trimEnd().trimStart()
// 'ES2109 meetup'
/******************************
* map and flatMap
******************************/
let courseStudents = [
['Janet', 'Martha', 'Bob', ['Phil', 'Candace']],
['Wilson', 'Taylor'],
['Edith', 'Jacob', 'Peter', 'Betty']
]
let defaultFlattened = courseStudents.flat()
console.log(defaultFlattened)
let alwaysFlattened = courseStudents.flat(Infinity)
console.log(alwaysFlattened)
// flatMap
let grades = [78, 62, 80, 64]
let flatMapped = grades.map(grade => [grade, grade + 7]).flat()
// [
// 78, 85, 62, 69,
// 80, 87, 64, 71
// ]
let flatMapped = grades.flatMap(grade => [grade, grade + 7])
// [
// 78, 85, 62, 69,
// 80, 87, 64, 71
// ]
/******************************
* Object.entries & Object.fromEntries
******************************/
const students = {
pera: 20,
mike: 22,
zika: 20
}
const studentEntires = Object.entries(students)
console.log(studentEntires)
// convert to array in order to make use of .filter() function
let overTwentyOne = Object.entries(students).filter(([, age]) => {
return age >= 21
})
console.log(overTwentyOne)
// turn multidimensional array back into an object using loop
const drinkingAgeStudents = {}
for (let [name, age] of overTwentyOne) {
drinkingAgeStudents[name] = age
}
// Object.fromEntires
const students1 = Object.fromEntries(studentEntires)
console.log(students1)
/******************************
* Optional catch binding
******************************/
// Earlier
try {
let parsed = JSON.parse(obj)
} catch (_) {
// ignore err, or use
alert('This catch is a catch!')
}
// Now
try {
let parsed = JSON.parse(obj)
} catch {
console.log(obj)
}
/******************************
* Private class fields
******************************/
class Dog {
#sound = 'Woof! Woof!' // this is private
field = 'bla'
makeSound() {
console.log(this.#sound)
}
}
// create instance
const doggy = new Dog()
doggy.makeSound() // Woof! Woof!
doggy.myProp
// console.log( tommy.#sound ); // SyntaxError
/******************************
* Promise.allSettled
******************************/
let p1 = () => new Promise((resolve, reject) => setTimeout(() => resolve('val1'), 2000))
let p2 = () => new Promise((resolve, reject) => setTimeout(() => resolve('val2'), 2000))
let p3 = () => new Promise((resolve, reject) => setTimeout(() => reject('err3'), 2000))
let p = Promise.allSettled([p1(), p2(), p3()]).then(values => console.log(values))
// Output
// [ {status: "fulfilled", value: "val1"}
// {status: "fulfilled", value: "val2"}
// {status: "rejected", value: "err3"}
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment