string-to-datatype

1.0.0 • Public • Published

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

/string-to-datatype/

    Package Sidebar

    Install

    npm i string-to-datatype

    Weekly Downloads

    16

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    2.98 kB

    Total Files

    4

    Last publish

    Collaborators

    • nguru