View with-timeout.ts
This file contains 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
/** | |
* Puts a timeout to given promise. | |
* Will not error out. Instead will return given fallback value | |
* @param source | |
* @param time | |
* @param fallbackTo | |
* @returns | |
*/ | |
export function withTimeout<T>( | |
source: Promise<T>, |
View group-by.ts
This file contains 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
/** | |
* Groups array by given key | |
* @param items | |
* @param fn | |
* @returns | |
*/ | |
function groupBy<T, K extends string | number>( | |
items: T[], | |
fn: (item: T) => K, | |
): { [key: string]: T[] } { |
View one-liners.js
This file contains 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
// 1. Get a random boolean (true/false) | |
const randomBoolean = () => Math.random() >= 0.5; | |
randomBoolean(); | |
// Result: a 50/50 change on returning true of false | |
// 2. Check if the provided day is a weekday | |
const isWeekday = (date) => date.getDay() % 6 !== 0; | |
isWeekday(new Date(2021, 0, 11)); | |
// Result: true (Monday) | |
// Result: false (Sunday) |
View recursive_combinations.js
This file contains 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
// thanks @david | |
var arrays = [[1], [1], [1,2], [1]]; | |
function combos(list, currentIndex = 0, combinations = [], current = []) { | |
if (currentIndex === list.length) { | |
combinations.push(current); | |
} else { | |
for (var item of list[currentIndex]) { | |
combos(list, currentIndex + 1, combinations, [...current, item]); | |
} |