string-to-datatype
v1.0.0
Published
Module to convert strings to their respective datatypes
Downloads
40
Maintainers
Readme
string-to-datatype
This module builds upon the work of stereotype and adds typecasting for date and JSON strings.
Using it:
let toType = require('string-to-datatype');
// an array of different values
let vals = [
1,
'1678',
JSON.stringify({ this: 'that' }),
new Date().toISOString(),
'2023-02-11T13:54:57.939Z',
'"2023-02-11T13:54:57.939Z"',
'null',
null,
'"null"',
'undefined',
true,
'false',
{ a: 2 },
'{b:2}',
'{"c":2}',
];
for (let val of vals) {
// cast
let casted = toType(val);
// log
console.log(val, 'cast to', typeof casted, 'type =>', casted);
}
The log should look something like below: