Skip to content

Instantly share code, notes, and snippets.

View munkacsitomi's full-sized avatar

Tamás Munkácsi munkacsitomi

  • Amsterdam, Netherlands
View GitHub Profile
@munkacsitomi
munkacsitomi / array-filter-sort.js
Created January 30, 2020 20:36
Sorting with filter() and sort()
// Sorting with Array.prototype.filter()
const startWith0 = (arr) => [...arr.filter(x => x === 0), ...arr.filter(x => x !== 0)];
// Sorting with Array.prototype.sort()
// const startWith0 = (arr) => arr.sort((x) => x === 0 ? -1 : 1);
console.time('time');
console.log('result', startWith0([1, null, 'e', 0, 2, 'a', 123, undefined, false, 0, true, 'd', 0, true]));
console.timeEnd('time');
@munkacsitomi
munkacsitomi / closures.js
Created January 30, 2020 20:10
Closures examples
// this is wrong
for (var i = 0; i < 5; i++) {
setTimeout(() => console.log(i), 1000); // => 5 5 5 5 5
}
// easy fix to use let instead of var. block level scoping instead of function level
for (let i = 0; i < 5; i++) {
setTimeout(() => console.log(i), 1000); // => 0 1 2 3 4
}
@munkacsitomi
munkacsitomi / sequential-parallel.js
Created January 30, 2020 19:55
Sequential and Parallel execution with async/await
const pause = (data, time) => new Promise(resolve => setTimeout(() => resolve(data), time));
const sequentialParallel = async () => {
console.time('time');
// Sequential
// const first = await pause('first', 3000);
// const second = await pause('second', 2000);
// Parallel
@munkacsitomi
munkacsitomi / reducer.js
Created January 30, 2020 15:48
Examples of Array.prototype.reduce()
const numbers = [1, 2, 3, 4, 12, 50];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const reducedNumbers = numbers.reduce((accumulator, currentValue) => accumulator + currentValue);
const multipliedNumbers = numbers.map(x => x * 2);
console.log(numbers.reduce(reducer)); // => 72
console.log(reducedNumbers); // => 72
console.log(multipliedNumbers); // => [ 2, 4, 6, 8, 24, 100 ]
@munkacsitomi
munkacsitomi / add-retrieve-values.js
Last active January 30, 2020 15:22
How to add and retrieve values in js
// How to add and retrieve values in js with ES5
const counterES5 = () => {
let storedNumber = 0;
return {
add: function(newNumber) {
storedNumber += newNumber;
},
retrieve: function() {
return storedNumber;
@munkacsitomi
munkacsitomi / higher-order-functions.js
Created January 30, 2020 14:43
Higher-Order Functions
// Higher-Order Functions with ES5
function createBaseES5(x) {
return function (y) {
return x + y;
}
}
const addTwo = createBaseES5(2);
console.log(addTwo(12)); // => 14
console.log(addTwo(23)); // => 25
@munkacsitomi
munkacsitomi / sequential-promises.js
Last active January 30, 2020 09:53
async/await is great but avoid going too sequential
// Not recommended - too sequential
async function logInOrder(urls) {
for (const url of urls) {
const response = await fetch(url);
console.log(await response.text());
}
}
// Recommended - nice and parallel
async function logInOrder(urls) {
@munkacsitomi
munkacsitomi / rest-spread.js
Created October 18, 2019 08:55
Rest and Spread operator examples
// Copying an array
let copy = [...arr]
// Creating an array of unique elements
let uniqueElements = [...new Set(arr)];
let elementsStartingWithA = [...new Set(arr)].filter(e => e.startsWith('a'));
// Concatenate arrays
let concat = [...arr1, ...arr2];
@munkacsitomi
munkacsitomi / async-await-error-handling.js
Last active March 1, 2020 15:46
Async/Await error handling
const fetchAndUpdatePosts = () => {
fetchPosts()
.then((posts) => {
updatePosts(posts)
.catch((err) => {
console.log('error in updating posts');
});
})
.catch(() => {
console.log('error in fetching posts');
@munkacsitomi
munkacsitomi / list-item-animations.scss
Last active August 28, 2019 19:22
Basic animations with keyframes
@keyframes listItemFadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
margin-top: 0px;
}
}