{"id":22021415,"url":"https://github.com/lamansky/2","last_synced_at":"2025-08-23T17:35:25.247Z","repository":{"id":63252626,"uuid":"80501770","full_name":"lamansky/2","owner":"lamansky","description":"[Node.js] Type Conversion Library: Numbers, Strings, Arrays, Maps, Objects, and Iterators.","archived":false,"fork":false,"pushed_at":"2019-11-15T10:06:14.000Z","size":23,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T16:54:43.117Z","etag":null,"topics":["javascript","nodejs","type-conversion"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lamansky.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-31T08:04:18.000Z","updated_at":"2020-03-02T16:25:23.000Z","dependencies_parsed_at":"2022-11-15T20:30:59.609Z","dependency_job_id":null,"html_url":"https://github.com/lamansky/2","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2F2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2F2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2F2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2F2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lamansky","download_url":"https://codeload.github.com/lamansky/2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252826272,"owners_count":21810086,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","nodejs","type-conversion"],"created_at":"2024-11-30T06:11:51.442Z","updated_at":"2025-05-07T06:28:14.072Z","avatar_url":"https://github.com/lamansky.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# “2”: The Type Conversion Library\n\nA [Node.js](https://nodejs.org/) module for converting between various JavaScript types: arrays, iterators, maps, numbers, objects, and strings.\n\n```javascript\nconst {toArray, toIterator, toMap, toNumber, toObject, toString} = require('2')\n\nconst obj = {a: 1, b: 2}\nobj::toMap()::toArray()::toObject()::toIterator()\n  ::toArray()::toMap()::toObject() // {a: 1, b: 2}\n\nlet data = '1.23'\ndata = toNumber(data)\ndata = toString(data) // '1.23'\n```\n\n## Installation\n\nRequires [Node.js](https://nodejs.org/) 8.3.0 or above.\n\n```bash\nnpm i 2\n```\n\n## Usage\n\n### Requiring the Functions\n\nYou can require needed functions via a destructuring assignment:\n\n```javascript\nconst {toArray, toIterator, toMap, toNumber, toObject, toString} = require('2')\n```\n\n(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`.)\n\nYou can also require individual functions via submodules:\n\n```javascript\nconst toArray = require('2/array')\nconst toIterator = require('2/iterator')\nconst toMap = require('2/map')\nconst toNumber = require('2/number')\nconst toObject = require('2/object')\nconst toString = require('2/string')\n```\n\n### Converting to Arrays\n```javascript\nconst toArray = require('2/array')\n\n// Map =\u003e Array\nconst map = new Map()\nmap.set('a', 1)\nmap.set('b', 2)\ntoArray(map) // [['a', 1], ['b', 2]]\n\n// Iterator =\u003e Array\ntoArray(map.values()) // [1, 2]\n\n// Object =\u003e Array\ntoArray({a: 1, b: 2}) // [['a', 1], ['b', 2]]\n\n// Array-like object =\u003e Array\ntoArray({0: 'first', 1: 'second'}, {detectIndexKeys: true}) // ['first', 'second']\n\n// Primitive value =\u003e Array\ntoArray('test') // ['test']\n```\n\n### Converting to Iterators\n```javascript\nconst toIterator = require('2/iterator')\n\n// Object =\u003e Iterator\nlet iterator = toIterator({a: 1, b: 2})\niterator.next().value // ['a', 1]\niterator.next().value // ['b', 2]\niterator.next().done // true\n\n// Primitive value =\u003e Iterator\ntoIterator('test').next().value // 'test'\n```\n\n### Converting to Maps\n```javascript\nconst toMap = require('2/map')\n\n// Array of key/value pairs =\u003e Map\nconst map1 = toMap([['a', 1], ['b', 2]])\nmap1.get('a') // 1\nmap1.get('b') // 2\n\n// Array of values =\u003e Map\nconst map2 = toMap(['a', 'b'])\nmap2.get(0) // 'a'\nmap2.get(1) // 'b'\n\n// Object =\u003e Map\nconst map3 = toMap({a: 1, b: 2})\nmap3.get('a') // 1\nmap3.get('b') // 2\n```\n\n### Converting to Numbers\n```javascript\nconst toNumber = require('2/number')\n\ntoNumber('1.2') // 1.2\ntoNumber(Infinity) // 0\ntoNumber(NaN) // 0\ntoNumber('not a number') // 0\n\n// Can specify a fallback other than zero:\ntoNumber('not a number', {elseReturn: 100}) // 100\n\n// You can choose to throw an error for invalid inputs.\ntoNumber('not a number', {elseThrow: true}) // throws error\ntoNumber('not a number', {elseThrow: new TypeError('Not a number!')})\n\n// Option to round floats:\ntoNumber('4.7') // 4.7\ntoNumber('4.7', {round: true}) // 5\n\n// By default, Infinity is not considered a valid number,\n// but this can be changed:\ntoNumber(Infinity) // 0\ntoNumber(Infinity, {finite: false}) // Infinity\n\n// Number object =\u003e Number\nconst numberObject = new Number(123)\ntypeof numberObject // 'object'\ntypeof toNumber(numberObject) // 'number'\n\n// Can parse strings that have digit grouping:\ntoNumber('1,234') // 1234\n// The built-in Number function, on the other hand, cannot:\nNumber('1,234') // NaN\n\n// Can be configured to interpret the comma as a decimal point:\ntoNumber('1,234', {decimalComma: true}) // 1.234\n```\n\n### Converting to Objects\n```javascript\nconst toObject = require('2/object')\n\n// Array of key/value pairs =\u003e Object\nconst obj1 = toObject([['a', 1], ['b', 2]])\nobj1.a // 1\nobj1.b // 2\n\n// Array =\u003e Object\nconst obj2 = toObject(['first', 'second'])\nObject.keys(obj2).length // 2\nobj2[0] // 'first'\nobj2[1] // 'second'\n\n// In the above example, the array indices become the object keys.\n// But you can make the keys mirror the values instead:\nconst obj3 = toObject(['first', 'second'], {mirror: true})\nObject.keys(obj3).length // 2\nobj3.first // 'first'\nobj3.second // 'second'\n\n// Map =\u003e Object\nconst map = new Map()\nmap.set('key1', 'value1')\nmap.set('key2', 'value2')\nconst obj4 = toObject(map)\nobj4.key1 // 'value1'\nobj4.key2 // 'value2'\n\n// Duplicate keys\ntoObject([['key', 1], ['key', 2]]) // throws an error (default behavior)\nconst obj5 = toObject([['key', 1], ['key', 2]], {throwIfEquivKeys: false}) // error-throwing disabled\nObject.keys(obj5).length // 1\nobj5.key // 2\n\n// Setting property descriptors\nconst obj6 = toObject([['a', 1], ['b', 2]], {descriptors: {enumerable: false}})\nObject.keys(obj6).length // 0 (because the properties are non-enumerable)\nobj6.a // 1\nobj6.b // 2\n```\n\n### Converting to Strings\n```javascript\nconst toString = require('2/string')\n\ntoString(123) // '123'\ntoString(-0) // '0'\n\ntoString(true) // ''\ntoString(false) // ''\ntoString(undefined) // ''\ntoString(null) // ''\ntoString(Infinity) // ''\ntoString(NaN) // ''\ntoString({}) // ''\ntoString([]) // ''\ntoString(function () {}) // ''\ntoString(Symbol('test')) // ''\n\n// Compare the above to standard JavaScript string conversion:\nString(true) // 'true'\nString(false) // 'false'\nString(undefined) // 'undefined'\nString(null) // 'null'\nString(Infinity) // 'Infinity'\nString(NaN) // 'NaN'\nString({}) // '[object Object]'\nString([]) // ''\nString(function () {}) // 'function () {}'\nString(Symbol('test')) // 'Symbol(test)'\n\n// Default fallback is an empty string, but you can change it:\ntoString(undefined) // ''\ntoString(undefined, {elseReturn: 'N/A'}) // 'N/A'\n\n// You can choose to throw an error for invalid inputs.\ntoString(undefined, {elseThrow: true}) // throws error\n\n// String object =\u003e String\nconst stringObject = new String('test')\ntypeof stringObject // 'object'\ntypeof toString(stringObject) // 'string'\n```\n\n## Version Migration Guide\n\nHere are backward-incompatible changes you need to know about.\n\n### 2.x ⇒ 3.x\n\n* The minimum supported Node version is now 8.3.0 (instead of 7.0.0).\n* `toNumber` no lounger rounds the `elseReturn` value when `round` is `true`. If you need this behavior, apply `Math.round` to your `elseReturn` value manually.\n* `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`.\n\n### 1.x ⇒ 2.x\n\n* `fallback` has been renamed to `elseReturn`.\n* Use `elseThrow: true` instead of `fallback: null`.\n* Unlike the old `fallback` parameter, `elseReturn` does _not_ type-enforce its values.\n* `toObject` with `mirror: true` will now throw an error if any key would overwrite another key. In version 1, this would have been allowed.\n* `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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2F2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flamansky%2F2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2F2/lists"}