ts-uf

other

validation

isOneOf

function isOneOf<const T, const L extends ROList>(
	value: T,
	list: L,
): value is SmartExtract<T, L[number]>;

checks whether a value is one of the expected values.

Example
const colors = ["red", "green", "blue"] as const;

function foo(color: string) {
	if (isOneOf(color, colors)) {
		color; // "red" | "green" | "blue"
	}
}

isNotOneOf

function isNotOneOf<const T, const L extends ROList>(
	value: T,
	list: L,
): value is Exclude<T, L[number]>;

Checks whether a value is not one of the expected values.

Example
type Action = "start" | "stop" | "pause";

function foo(action: string) {
	if (isNotOneOf(action, ["stop", "pause"])) {
		startSomething();
		action; // "start"
	}
}

isNullish

function isNullish<T>(value: T): value is SmartExtract<T, null | undefined>;

Checks whether a value is nullish (null | undefined).

isUndefined

function isUndefined<T>(value: T): value is SmartExtract<T, undefined>;

Checks whether a value is undefined.

isNull

function isNull<T>(value: T): value is SmartExtract<T, null>;

Checks whether a value is null.

isNotUndefined

function isNotUndefined<T>(value: T): value is Exclude<T, undefined>;

Checks whether a value is not undefined.

isNotNull

function isNotNull<T>(value: T): value is Exclude<T, null>;

Checks whether a value is not null.

isNonNullish

function isNonNullish<T>(value: T): value is Exclude<T, null | undefined>;

Checks whether a value is neither null nor undefined.

isEmpty

function isEmpty<T>(value: _CheckT<T, string | ROList | Dict>): value is IsEmptyRT<T>;

A wrapper for isEmptyString, isEmptyArray and isEmptyObject.

isNonEmpty

function isNonEmpty<T>(value: _CheckT<T, string | ROList | Dict>): value is IsNonEmptyRT<T>;

A wrapper for isNonEmptyString, isNonEmptyArray and isNonEmptyObject.

utils

mapDefined

function mapDefined<T, const R>(
	value: T,
	fn: Func<[value: Exclude<T, undefined | null>], R>,
): R | Extract<T, undefined | null>;

Maps a value to another value if it is not undefined or null.

Example
// Instead of:
function getDescription(func: FunctionDeclaration) {
	const description = func.getJsDocs().at(-1)?.getDescription();
	if (description === undefined) return; // Notice that we used `description === undefined` and not `!description`.
	return convertJsDocLinks(description);
}
// You can do:
function getDescription(func: FunctionDeclaration) {
	return mapDefined(func.getJsDocs().at(-1)?.getDescription(), convertJSDocLinks);
}

toArray

function toArray<T>(value: T | T[]): T[];

Converts a value to an array if it's not already one. In other words, it converts a value of type T | T[] to T[].

Example
function foo(value: string | string[]) {
	const values = toArray(value); // string[]
}

unwrapValue

function unwrapValue<T, A extends ROList, R = never>(
	value: T | Func<A, R>,
	...args: A
): T | R;

Extracts a value by invoking it if it's a function, otherwise returns it unchanged. In other words, it converts a value of type T | ((...args: A) => R) to T | R.

Example
function foo(valueOrFunc: string | (() => string)) {
	const value = unwrapValue(valueOrFunc); // string
}

findLastMatch

function findLastMatch(input: string, regex: RegExp, n = 1): RegExpExecArray | undefined;

Finds the nth-to-last match of a regex in a string.

Throws a RangeError if n is not a positive integer.

Example
findLastMatch("a1 b2 c3", /\d/); // [ '3', index: 7, input: 'a1 b2 c3', groups: undefined ]

weightedRandom

function weightedRandom<T>(items: ROList<T>, weights: ROList<number>): T | undefined;

Selects a random item using the corresponding weights as selection probabilities.

  • Items with larger weights are more likely to be selected.
  • Items with missing weights will never be selected.
  • Extra weights can return undefined.
Example
weightedRandom([1,2,3], [1, 1, 8]); // 3 // there's 80% chance 3 is selected

weightedRandom([1,2,3], []); // undefined
weightedRandom([1,2,3], [1, 1]); // 1 // 3 will never be selected
weightedRandom([1,2,3], [1, 1, 1, 3]) // 1 // there's a 50% chance an item is selected, and another 50% chance `undefined` is returned.

len

function len(value: Stringable | ROList | Dict): number;

Returns the length of a Stringable, array or object.

Example
const strings = ["hello", "!", "foo"];
const lengths = strings.map(len);

On this page