1.0.0 • Published 1 year ago

string-to-datatype v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

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:

console.log image