“2”: The Type Conversion Library
A Node.js module for converting between various JavaScript types: arrays, iterators, maps, numbers, objects, and strings.
const {toArray, toIterator, toMap, toNumber, toObject, toString} = require('2')
const obj = {a: 1, b: 2}
obj::toMap()::toArray()::toObject()::toIterator()
::toArray()::toMap()::toObject()
let data = '1.23'
data = toNumber(data)
data = toString(data)
Installation
Requires Node.js 8.3.0 or above.
npm i 2
Usage
Requiring the Functions
You can require needed functions via a destructuring assignment:
const {toArray, toIterator, toMap, toNumber, toObject, toString} = require('2')
(If your project has a linting rule that precludes shadowing the global toString
function, you can also destructure the toStr
function, which is the same as toString
.)
You can also require individual functions via submodules:
const toArray = require('2/array')
const toIterator = require('2/iterator')
const toMap = require('2/map')
const toNumber = require('2/number')
const toObject = require('2/object')
const toString = require('2/string')
Converting to Arrays
const toArray = require('2/array')
const map = new Map()
map.set('a', 1)
map.set('b', 2)
toArray(map)
toArray(map.values())
toArray({a: 1, b: 2})
toArray({0: 'first', 1: 'second'}, {detectIndexKeys: true})
toArray('test')
Converting to Iterators
const toIterator = require('2/iterator')
let iterator = toIterator({a: 1, b: 2})
iterator.next().value
iterator.next().value
iterator.next().done
toIterator('test').next().value
Converting to Maps
const toMap = require('2/map')
const map1 = toMap([['a', 1], ['b', 2]])
map1.get('a')
map1.get('b')
const map2 = toMap(['a', 'b'])
map2.get(0)
map2.get(1)
const map3 = toMap({a: 1, b: 2})
map3.get('a')
map3.get('b')
Converting to Numbers
const toNumber = require('2/number')
toNumber('1.2')
toNumber(Infinity)
toNumber(NaN)
toNumber('not a number')
toNumber('not a number', {elseReturn: 100})
toNumber('not a number', {elseThrow: true})
toNumber('not a number', {elseThrow: new TypeError('Not a number!')})
toNumber('4.7')
toNumber('4.7', {round: true})
toNumber(Infinity)
toNumber(Infinity, {finite: false})
const numberObject = new Number(123)
typeof numberObject
typeof toNumber(numberObject)
toNumber('1,234')
Number('1,234')
toNumber('1,234', {decimalComma: true})
Converting to Objects
const toObject = require('2/object')
const obj1 = toObject([['a', 1], ['b', 2]])
obj1.a
obj1.b
const obj2 = toObject(['first', 'second'])
Object.keys(obj2).length
obj2[0]
obj2[1]
const obj3 = toObject(['first', 'second'], {mirror: true})
Object.keys(obj3).length
obj3.first
obj3.second
const map = new Map()
map.set('key1', 'value1')
map.set('key2', 'value2')
const obj4 = toObject(map)
obj4.key1
obj4.key2
toObject([['key', 1], ['key', 2]])
const obj5 = toObject([['key', 1], ['key', 2]], {throwIfEquivKeys: false})
Object.keys(obj5).length
obj5.key
const obj6 = toObject([['a', 1], ['b', 2]], {descriptors: {enumerable: false}})
Object.keys(obj6).length
obj6.a
obj6.b
Converting to Strings
const toString = require('2/string')
toString(123)
toString(-0)
toString(true)
toString(false)
toString(undefined)
toString(null)
toString(Infinity)
toString(NaN)
toString({})
toString([])
toString(function () {})
toString(Symbol('test'))
String(true)
String(false)
String(undefined)
String(null)
String(Infinity)
String(NaN)
String({})
String([])
String(function () {})
String(Symbol('test'))
toString(undefined)
toString(undefined, {elseReturn: 'N/A'})
toString(undefined, {elseThrow: true})
const stringObject = new String('test')
typeof stringObject
typeof toString(stringObject)
Version Migration Guide
Here are backward-incompatible changes you need to know about.
2.x ⇒ 3.x
- The minimum supported Node version is now 8.3.0 (instead of 7.0.0).
toNumber
no lounger rounds the elseReturn
value when round
is true
. If you need this behavior, apply Math.round
to your elseReturn
value manually.toObject
will now throw an error if an entries array contains duplicate keys. In version 2, the last equivalent key would have silently overwritten the prior ones. You can restore the previous behavior by setting the new throwIfEquivKeys
option to false
.
1.x ⇒ 2.x
fallback
has been renamed to elseReturn
.- Use
elseThrow: true
instead of fallback: null
. - Unlike the old
fallback
parameter, elseReturn
does not type-enforce its values. toObject
with mirror: true
will now throw an error if any key would overwrite another key. In version 1, this would have been allowed.toObject
with mirror: true
will now allow an object to become an object key, so long as its string representation is not equivalent to that of any other key. In version 1, attempting to use an object as an object key would silently fail and would result in numeric index keys being used instead.