This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const stringNumbers = ['123', '4.45', '5,25', 'abc', '10000']; | |
const numbers = stringNumbers.map(Number); | |
console.log(numbers); // [123, 4.45, NaN, NaN, 10000] | |
const stringBoolean = ['false', false, '0', 0, null, undefined]; | |
const booleans = stringBoolean.map(Boolean); | |
console.log(booleans); // [true, false, true, false, false, false] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async send() { | |
let success = false; | |
try { | |
const { data } = await httpPost(); | |
success = !data?.errors || data.errors.length === 0; | |
} catch(e) { | |
// handled below because failure can happen on two occasions | |
// 1. httpPost fails | |
// 2. the call was successful but the BE gives back error | |
throw e; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compares 2 arrays of (primitive) values to see if they are the same | |
const doArraysMatch = (arr1, arr2) => arr1.length === arr2.length && arr1.every((element) => arr2.includes(element)); | |
// Reusable function to combine arrays | |
const mergeArr = (...arr) => arr.reduce((acc, val) => [...acc, ...val], []); | |
// Group an array of objects by a specific key or property value | |
const movies = [ | |
{ title: 'Test 1', year: 2020 }, | |
{ title: 'Test 2', year: 2020 }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Old, imperative way | |
const originalDoubleList = list => { | |
const newList = []; | |
for (let i = 0; i < list.length; i++) { | |
newList[i] = list[i] * 2; | |
} | |
return newList; | |
}; | |
console.log(originalDoubleList([20])); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cached(fn) { | |
// Create an object to store the results returned after each function execution. | |
const cache = Object.create(null); | |
// Returns the wrapped function | |
return function cachedFn(str) { | |
// If the cache is not hit, the function will be executed | |
if (!cache[str]) { | |
let result = fn(str); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const swapKeyVal = (obj) => Object.assign({}, ...Object.entries(obj).map(([a,b]) => ({ [b]: a }))); | |
const getNumber = (num) => { | |
const isStringType = typeof num === 'string'; | |
const isNumberType = typeof num === 'number'; | |
let hasNumber = false; | |
let keyVal = { one: 1, two: 2, three: 3 }; | |
if (!isStringType && !isNumberType) { | |
throw new Error('Unknown type'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get all branches | |
git fetch | |
# undo any unplanned changes | |
git reset --hard | |
# revert last commit | |
git reset --hard HEAD^ | |
# reset to develop branch | |
git reset --hard origin/develop | |
# set the author name and email address to your commits | |
git config --global user.name "Your Name" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const tmp = [1, 2, 3, 1, 3, 4, 5]; | |
const uniqueFirst = Array.from(new Set(tmp)); | |
const uniqueSecond = tmp.filter((item, index) => tmp.indexOf(item) === index); | |
const uniqueThird = tmp.reduce((unique, item) => unique.includes(item) ? unique : [...unique, item], []); | |
const duplicatedValues = tmp.filter((item, index) => tmp.indexOf(item) !== index); | |
console.log(uniqueFirst); | |
console.log(uniqueSecond); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ids = [100, 101, 102, 103]; | |
// We can use reduce to wrap ids in an object | |
const first = ids.reduce((out, id) => { | |
out.push({ id }); | |
return out; | |
}, []); | |
// Or use map to do the same | |
const second = ids.map((id) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pause = (data, time) => new Promise(resolve => setTimeout(() => resolve(data), time)); | |
const pauseReject = (data, time) => | |
new Promise((resolve, reject) => | |
setTimeout(() => reject(new Error(`Something went wrong in the ${data} promise`)), time) | |
); | |
const parallelErrorHandlingWrong = () => { | |
const firstPromise = pause('first', 3000); | |
const secondPromise = pauseReject('second', 2000); | |
const thirdPromise = pause('third', 1000); |
NewerOlder