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
class CallbackLoop { | |
constructor(asyncFunc, ...params) { | |
this.toStop = false; | |
this.asyncFunc = asyncFunc; | |
this.params = params; | |
} | |
run() { | |
const { toStop, asyncFunc, params } = this; | |
if (toStop) return; |
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
/** | |
* Filter an array into two arrays. | |
* | |
* Defined by the callbackFn, the first array will be with the true elements | |
* and the second array will be with the false elements. | |
* | |
* @param {Array} array | |
* @param {Function} callbackfn | |
* @param {Object} thisArg | |
* @return {[Array, Array]} A matrix with two arrays. |
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
function randomNumber(lower = 0, upper = 1): number { | |
return Math.round((Math.random() * (upper - lower)) + lower); | |
} | |
interface Options { | |
uppercase?: boolean, | |
times?: number, | |
} | |
/** |
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
// Map | |
type MapExtract<MapType> = MapType extends Map<infer X, infer Y> ? [X, Y] : never; | |
// All types of objects | |
type ObjectExtract<ObjectType> = [keyof ObjectType, ObjectType[keyof ObjectType]]; |
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
function alphabeticalSort< | |
T extends Map<unknown, unknown>, | |
>(collection: T): (T extends Map<infer K, infer V> ? [K, V] : never)[]; | |
function alphabeticalSort< | |
T extends Array<unknown>, | |
>(collection: T): T; | |
function alphabeticalSort< | |
T extends Set<unknown>, | |
>(collection: T): (T extends Array<infer V> ? V : never)[]; | |
function alphabeticalSort< |
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
function iterate<T>( | |
iterations: number, | |
callback: (iteration: number) => T, | |
): T[] { | |
const result: T[] = Array(iterations).fill(null).map((_value, index) => { | |
return callback(index); | |
}); | |
return result; | |
} |
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
type Sum<T> = Array<T> | Array<Sum<T>> | |
function sum<T extends number>(...list: Sum<T>): number { | |
const innerSum = (a: number, b: number | Array<number>) => ( | |
a + (b instanceof Array ? sum(...b) : b)); | |
return (list as number[]).reduce(innerSum, 0); | |
} |
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
use std::{collections::VecDeque, path::PathBuf}; | |
use tokio::io; | |
#[derive(Debug, Clone, Copy)] | |
pub struct CopyOptions { | |
pub overwrite: bool, | |
} | |
impl Default for CopyOptions { | |
fn default() -> Self { |
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
export type Obj = Record<string | number | symbol, unknown>; | |
export function mergeObjects<A extends Obj, B extends Obj>(a: A, b: B): A & B { | |
if (Array.isArray(a) && Array.isArray(b)) { | |
return [...a, ...b] as unknown as A & B; | |
} else if (Array.isArray(b)) { | |
return b as unknown as A & B; | |
} | |
const mappedEntries = [...Object.keys(a), ...Object.keys(b)].map((key) => { |