ts-uf

function

validation

isFunction

function isFunction<T>(value: CheckT<T>): value is SmartExtract<T, Func>;

Checks whether a value is a function.

Example
function foo(valueOrFn: string | (() => string)) {
	const value = isFunction(valueOrFn) ? valueOrFn() : valueOrFn;
}

isNotFunction

function isNotFunction<T>(value: CheckT<T>): value is Exclude<T, Func>;

Checks if the value is not a function.

Example
function foo(valueOrFn: string | (() => string)) {
	const value = isNotFunction(valueOrFn) ? valueOrFn : valueOrFn();
}

repeat

repeat

function repeat(func: RepeatCb, data: RepeatData): void;

type RepeatCb = Func<RepeatCbParams, RepeatCbRT>;
type RepeatCbParams = [index: number];
type RepeatCbRT = boolean | number | void;

type RepeatData = ({ end: number } | { count: number }) & { start?: number; step?: number };

Repeats a function a given number of times.

  • Return false to break the loop.
  • Return a number to jump to that index.
Example
repeat(console.log, { start: 10, end: 13 }); // logs "10", "11", "12"
repeat(console.log, { start: 10, count: 3 }); // logs "10", "11", "12"

repeat((index) => {
	if (index === 3) return 5; // jump to index 5
	if (index === 8) return false; // break the loop
}, 10);

repeatAsync

async function repeatAsync(func: RepeatAsyncCb, data: RepeatData): Promise<void>;

type RepeatAsyncCb = Func<RepeatCbParams, Promise<RepeatCbRT>>;
type RepeatCbParams = [index: number];
type RepeatCbRT = boolean | number | void;

type RepeatData = ({ end: number } | { count: number }) & { start?: number; step?: number };

An asynchronous version of repeat.

repeatWhile

function repeatWhile(func: RepeatCb, condition: RepeatWhileCondition): void;

type RepeatCb = Func<RepeatCbParams, RepeatCbRT>;
type RepeatCbRT = boolean | number | void;

type RepeatWhileCondition = Func<RepeatCbParams, boolean>;
type RepeatCbParams = [index: number];

Repeats a function while a given condition is true.

  • Return false to break the loop.
  • Return a number to jump to that index.
Example
repeatWhile(console.log, (index) => index < 3); // logs "0", "1", "2"

repeatWhileAsync

async function repeatWhileAsync(
	func: RepeatAsyncCb,
	condition: RepeatWhileAsyncCondition,
): Promise<void>;

type RepeatAsyncCb = Func<RepeatCbParams, Promise<RepeatCbRT>>;
type RepeatCbRT = boolean | number | void;

type RepeatWhileAsyncCondition = Func<RepeatCbParams, Promise<boolean>>;
type RepeatCbParams = [index: number];

An asynchronous version of repeatWhile.

utils

iife

function iife<const A extends ROList, const R>(func: Func<A, R>, ...args: A): R;

Immediately invokes a function and returns its result. Useful for creating a local scope to organize your code.

Example
function cliAction() {
	const folders = iife(() => {
		const source = "...";
		const options = { ... };
		const folders = glob.sync(source, options);
		return folders.filter(...);
	});
	const files = iife(() => {
		const source = "...";
		const options = { ... };
		const files = glob.sync(source, options);
		return files.filter(...);
	});
}

sleep

async function sleep(ms: number): Promise<void>;

Sleeps for the specified number of milliseconds.

withDelay

async function withDelay(func: Func, ms: number): Promise<void>;

Sleeps for the specified number of milliseconds and then calls the given function.

On this page