Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

convert-to

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

convert-to

Convert any type to any type.

  • 0.0.3
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

convert-to

Convert any type to any type.

Installation

$ npm install convert-to

Usage

const convert = require('convert-to');

// -- to Boolean
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));

// -- to String
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));

// -- to Number
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));

// -- to Json
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));

// -- to Object
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));

// -- to Array
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));

// -- to Serialize
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));

// -- to Serialize Object
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));

Keywords

FAQs

Package last updated on 12 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc