Skip to main content
@spa-tools/utilities

ensureStr()

The ensureStr<F>() function checks if the given value is a valid string and if so returns it; otherwise, returns fallback value.

The generic F type is used to define the type of the fallback value.

Usage

import { ensureStr } from '@spa-tools/utilities';

const val1 = ensureStr('Hello, World!');
console.log(val1);
// --> 'Hello, World!'

const val2 = ensureStr(123, 'not a string');
console.log(val2);
// --> 'not a string'

Arguments

NameTypeDescription
strunknownThe value to check
fallbackFThe value that will be returned if value is not a string

Returns

Returns value if it is a string else returns the fallback value of type F.