convert-to
Convert any type to any type.
Installation
$ npm install convert-to
Usage
const convert = require('convert-to');
console.log('Boolean');
console.log(tmp = convert.bool("true"), typeof(tmp));
console.log(tmp = convert.boolean(52), typeof(tmp));
console.log(tmp = convert(42).to('boolean'), typeof(tmp));
console.log(tmp = convert().to('boolean'), typeof(tmp));
console.log(tmp = convert(null).to('boolean'), typeof(tmp));
console.log(tmp = convert.boolean(['titi', 'tutu']), typeof(tmp));
console.log(tmp = convert({titi: 'tutu'}).to(Boolean), typeof(tmp));
console.log('String');
console.log(tmp = convert.str(52), typeof(tmp));
console.log(tmp = convert(42).to('string'), typeof(tmp));
console.log(tmp = convert().to('string'), typeof(tmp));
console.log(tmp = convert(null).to('string'), typeof(tmp));
console.log(tmp = convert.string(['titi', 'tutu']), typeof(tmp));
console.log(tmp = convert({titi: 'tutu'}).to(String), typeof(tmp));
console.log('Number');
console.log(tmp = convert.num('52'), typeof(tmp));
console.log(tmp = convert('42').to('number'), typeof(tmp));
console.log(tmp = convert().to('number'), typeof(tmp));
console.log(tmp = convert(null).to('number'), typeof(tmp));
console.log(tmp = convert.number(['titi', 'tutu']), typeof(tmp));
console.log(tmp = convert({value: 'tutu'}).to(Number), typeof(tmp));
console.log('Json');
console.log(tmp = convert.JSON(' ,toto: "tutu", \'titi\': tutu, \'test\': \'tutu\', "tutu": \'toto, titi and test don\'t do this !\' '), typeof(tmp));
console.log(tmp = convert.json(" { 'test': { 'test2' : 'test3' } } "), typeof(tmp));
console.log(tmp = convert.json(" { titi: 'tutu et l'autre', titi2: 42, } "), typeof(tmp));
console.log(tmp = convert.json(" { titi.tutu.test: 'damn' } "), typeof(tmp));
console.log('Object');
console.log(tmp = convert.obj('52'), typeof(tmp));
console.log(tmp = convert('42').to('object'), typeof(tmp));
console.log(tmp = convert().to('object'), typeof(tmp));
console.log(tmp = convert(null).to('object'), typeof(tmp));
console.log(tmp = convert.object("['titi', 'tutu']"), typeof(tmp));
console.log(tmp = convert("{ titi: 'tutu et l'autre', titi2: 42, }").to(Object), typeof(tmp));
console.log(tmp = convert.object({"abc.def.g": 2}), typeof(tmp));
console.log('Array');
console.log(tmp = convert.arr('52'), Array.isArray(tmp) ? 'array' : typeof(tmp));
console.log(tmp = convert('42').to('array'), Array.isArray(tmp) ? 'array' : typeof(tmp));
console.log(tmp = convert().to('array'), Array.isArray(tmp) ? 'array' : typeof(tmp));
console.log(tmp = convert(null).to('array'), Array.isArray(tmp) ? 'array' : typeof(tmp));
console.log(tmp = convert.array("[ 'titi', 'tutu', ]"), Array.isArray(tmp) ? 'array' : typeof(tmp));
console.log(tmp = convert.array("[{test: 'testouille'}, 'tutu']"), Array.isArray(tmp) ? 'array' : typeof(tmp));
console.log(tmp = convert("{0: 'tutu', 1: 'titi'}").to(Array), Array.isArray(tmp) ? 'array' : typeof(tmp));
console.log('Serialize');
console.log(tmp = convert.slz('52'), typeof(tmp));
console.log(tmp = convert('42').to('serialize'), typeof(tmp));
console.log(tmp = convert().to('serialize'), typeof(tmp));
console.log(tmp = convert(null).to('serialize'), typeof(tmp));
console.log(tmp = convert.serialize(['titi', 'tutu']), typeof(tmp));
console.log(tmp = convert.serialize({test: 'testouille', test2: 'tutu'}), typeof(tmp));
console.log(tmp = convert({0: 'tutu', 1: 'titi'}).to('serialize'), typeof(tmp));
console.log('Serialize Object');
console.log(tmp = convert.slzObj('52'), typeof(tmp));
console.log(tmp = convert('42').to('serializeObject'), typeof(tmp));
console.log(tmp = convert().to('serializeObject'), typeof(tmp));
console.log(tmp = convert(null).to('serializeObject'), typeof(tmp));
console.log(tmp = convert.serializeObject("?a=b&c=d"), typeof(tmp));
console.log(tmp = convert.serializeObject("a=b&c=d=c"), typeof(tmp));
console.log(tmp = convert.serializeObject("['titi', 'tutu']"), typeof(tmp));
console.log(tmp = convert.serializeObject("[{test: 'testouille'}, 'tutu']"), typeof(tmp));
console.log(tmp = convert("{0: 'tutu', 1: 'titi'}").to('serializeObject'), typeof(tmp));