Class: To
To - A collection of conversion utilities for various data types.
Author
Heliomar P. Marques https://navto.me/heliomarpm
To.boolean
boolean()
static boolean(value): boolean;Converts a value to a boolean, following these rules:
- Numeric values are considered
trueexcept for the string '0'. - Strings that match the regular expression
/^true$/iare consideredtrue. - All other values are considered
false.
Parameters
value
The value to be converted to a boolean.
string | number
Returns
boolean
boolean The boolean representation of the input value.
Example
To.boolean(1); //output: true
To.boolean('true'); //output: true
To.boolean('0'); //output: false
To.boolean('false'); //output: false
To.boolean(null); //output: false
To.boolean(undefined); //output: false
To.boolean(''); //output: falseTo.date
date()
static date(input): Date;Converts a value to a Date object.
Parameters
input
The value to be converted to a Date object.
string | number | Date
Returns
Date
The Date object representation of the input value.
Example
To.date(new Date()); //output: <Date>
To.date(1693456000000); //output: <Date>
To.date('2023-08-01'); //output: <Date>
To.date('2023-08-01T00:00:00'); //output: <Date>
To.date('2023-08-01 00:00:00'); //output: <Date>
To.date('2023-08-01 00:00:00.000'); //output: <Date>
To.date('2023-08-01 00:00:00.000Z'); //output: <Date>To.dateParts
dateParts()
static dateParts(date): DatePartsType;Extracts parts of a date such as year, month, day, etc.
Parameters
date
The date to be parsed, can be a Date object or a string.
string | number | Date
Returns
An object containing year, month, day, hour, minute, second, and timestamp.
Example
To.dateParts(new Date()); //output: { year: 2023, month: 8, day: 1, hour: 0, minute: 0, second: 0, timestamp: 1693456000000 }
To.dateParts('2023-08-01'); //output: { year: 2023, month: 8, day: 1, hour: 0, minute: 0, second: 0, timestamp: 1693456000000 }
To.dateParts('2023-08-01T00:00:00'); //output: { year: 2023, month: 8, day: 1, hour: 0, minute: 0, second: 0, timestamp: 1693456000000 }
To.dateParts('2023-08-01 00:00:00'); //output: { year: 2023, month: 8, day: 1, hour: 0, minute: 0, second: 0, timestamp: 1693456000000 }
To.dateParts('2023-08-01 00:00:00.000'); //output: { year: 2023, month: 8, day: 1, hour: 0, minute: 0, second: 0, timestamp: 1693456000000 }
To.dateParts('2023-08-01 00:00:00.000Z'); //output: { year: 2023, month: 8, day: 1, hour: 0, minute: 0, second: 0, timestamp: 1693456000000 }To.dictionary
dictionary()
static dictionary<T>(jsonObject): Record<string, T>;Converts a JSON object to a Record<string, T> type.
Type Parameters
T
T
The type defined by the input JSON object.
Parameters
jsonObject
The JSON object to convert.
Returns
Record<string, T>
A new Record<string, T> object with the same key-value pairs as the original JSON object.
Example
import { to } from '@heliomarpm/helpers';
import jsonLanguages from './languages.json';
const languages: Record<string, string> = to.dictionary<string>(jsonLanguages);
let jsonLocale: Record<string, string>
jsonLocale = require(`locales/${navigator.language}`);To.number
number()
static number(value): number;Converts a value to a number, following these rules:
- Numeric values are considered as-is.
- Strings that match the regular expression
/^\d+$/are considered as numeric. - All other values are considered
NaN.
Parameters
value
unknown
The value to be converted to a number.
Returns
number
number The numeric representation of the input value.
Example
To.number(123); //output: 123
To.number('123'); //output: 123
To.number('123.45'); //output: NaN
To.number('abc'); //output: NaN
To.number(null); //output: NaN