@augment-vir/common
Version:
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
68 lines (67 loc) • 1.95 kB
TypeScript
/**
* A suite of functions that each convert an `unknown` value into a specific type, throwing an error
* if the conversion is not possible.
*
* @category Convert
* @category Package : @augment-vir/common
* @example
*
* ```ts
* import {convertTo} from '@augment-vir/common';
*
* convertTo.string(42); // `'42'`
* convertTo.string(undefined); // throws an error
* ```
*
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export declare const convertTo: {
/**
* Converts anything into a string.
*
* @example
*
* ```ts
* import {convertTo} from '@augment-vir/common';
*
* convertTo.string('hi'); // `'hi'`
* convertTo.string(42); // `'42'`
* convertTo.string(null); // `'hi'`
* convertTo.string({}); // `'{}'`
* ```
*/
string(this: void, value: unknown): string;
/**
* Converts almost anything into a string but maps `null`, `undefined`, and empty string results
* to `undefined`.
*
* @example
*
* ```ts
* import {convertTo} from '@augment-vir/common';
*
* convertTo.string('hi'); // `'hi'`
* convertTo.string(42); // `'42'`
* convertTo.string(null); // `undefined`
* convertTo.string(''); // `undefined`
* convertTo.string({}); // `'{}'`
* ```
*/
maybeString(this: void, value: unknown): string | undefined;
/**
* Converts almost anything into a number but maps `null`, `undefined`, and any conversion that
* results in `NaN` to `undefined`.
*
* @example
*
* ```ts
* import {convertTo} from '@augment-vir/common';
*
* convertTo.maybeNumber('42'); // `42`
* convertTo.maybeNumber(42); // `42`
* convertTo.maybeNumber(null); // `undefined`
* convertTo.maybeNumber('hello'); // `undefined`
* ```
*/
maybeNumber(this: void, value: unknown): number | undefined;
};