Skip to content

Instantly share code, notes, and snippets.

Avatar
🛐
专一

Daniel Cardenas danielcardeenas

🛐
专一
View GitHub Profile
@danielcardeenas
danielcardeenas / with-timeout.ts
Last active August 3, 2021 03:13
Timeout Promise gracefully (Typed)
View with-timeout.ts
/**
* 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>,
@danielcardeenas
danielcardeenas / group-by.ts
Last active August 3, 2021 03:37
Group array of objects by property (Typed)
View group-by.ts
/**
* 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[] } {
@danielcardeenas
danielcardeenas / one-liners.js
Last active August 3, 2021 03:15
Javascript utility one-liners
View one-liners.js
// 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)
@danielcardeenas
danielcardeenas / recursive_combinations.js
Created February 13, 2020 01:34
List of list combinations
View recursive_combinations.js
// 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]);
}