toString
lang
Converts value to a string.
Installation
Import
import { toString } from '@tulx/utils';Source Code
Implementation
/**
* Converts value to a string.
*
* @param value - The value to convert.
* @returns Returns the converted string.
*
* @example
* ```ts
* toString(null); // ''
* toString(-0); // '-0'
* toString([1, 2, 3]); // '1,2,3'
* ```
*/
export function toString(value: unknown): string {
if (value === null || value === undefined) {
return '';
}
if (typeof value === 'string') {
return value;
}
if (typeof value === 'symbol') {
return value.toString();
}
const result = String(value);
return result === '0' && 1 / (value as number) < 0 ? '-0' : result;
}
Example
import { toString } from '@tulx/utils';
toString(null); // ''
toString(-0); // '-0'
toString([1, 2, 3]); // '1,2,3'Related Functions
castArray
Casts value as an array if it's not one.
clone
Creates a shallow clone of value.
cloneDeep
This method is like clone except that it recursively clones value.
cloneDeepWith
This method is like cloneDeep except that it accepts customizer which is invoked to produce the cloned value.
cloneWith
This method is like clone except that it accepts customizer which is invoked to produce the cloned value.
conformsTo
Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object.