Skip to content

Instantly share code, notes, and snippets.

@AyAyEm
AyAyEm / CallbackLoop.js
Last active August 9, 2020 03:25
JavaScript Callback loop class
class CallbackLoop {
constructor(asyncFunc, ...params) {
this.toStop = false;
this.asyncFunc = asyncFunc;
this.params = params;
}
run() {
const { toStop, asyncFunc, params } = this;
if (toStop) return;
@AyAyEm
AyAyEm / biFilter.js
Last active August 21, 2020 23:56
An array filter but designed to return both true and false values.
/**
* 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.
function randomNumber(lower = 0, upper = 1): number {
return Math.round((Math.random() * (upper - lower)) + lower);
}
interface Options {
uppercase?: boolean,
times?: number,
}
/**
@AyAyEm
AyAyEm / ExtractsTypes.ts
Last active November 3, 2020 12:36
All sort of generics used to extract types from types
// 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]];
@AyAyEm
AyAyEm / alphabeticalSort.ts
Last active January 29, 2021 18:42
Alphabetical sort
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<
@AyAyEm
AyAyEm / iterate.ts
Created February 3, 2021 21:00
Simple iteration function
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;
}
@AyAyEm
AyAyEm / recursiveSum.ts
Created April 13, 2021 20:29
Sum with variable args from just numbers to arrays of arrays of numbers
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);
}
@AyAyEm
AyAyEm / copy.rs
Created August 14, 2022 19:07
Non recursive async directory copy
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 {
@AyAyEm
AyAyEm / mergeObjects.ts
Last active October 10, 2022 14:21
A functional approach to merging objects
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) => {