{"action":"created","issue":{"url":"https://api.github.com/repos/lodash/lodash/issues/1574","labels_url":"https://api.github.com/repos/lodash/lodash/issues/1574/labels{/name}","comments_url":"https://api.github.com/repos/lodash/lodash/issues/1574/comments","events_url":"https://api.github.com/repos/lodash/lodash/issues/1574/events","html_url":"https://github.com/lodash/lodash/issues/1574","id":113688169,"number":1574,"title":"Enhancement: toString and toNumber, possibly isBinary and isOctal","user":{"login":"Xotic750","id":216041,"avatar_url":"https://avatars.githubusercontent.com/u/216041?v=3","gravatar_id":"","url":"https://api.github.com/users/Xotic750","html_url":"https://github.com/Xotic750","followers_url":"https://api.github.com/users/Xotic750/followers","following_url":"https://api.github.com/users/Xotic750/following{/other_user}","gists_url":"https://api.github.com/users/Xotic750/gists{/gist_id}","starred_url":"https://api.github.com/users/Xotic750/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Xotic750/subscriptions","organizations_url":"https://api.github.com/users/Xotic750/orgs","repos_url":"https://api.github.com/users/Xotic750/repos","events_url":"https://api.github.com/users/Xotic750/events{/privacy}","received_events_url":"https://api.github.com/users/Xotic750/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/lodash/lodash/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-10-27T21:01:24Z","updated_at":"2015-10-27T23:34:24Z","closed_at":null,"body":"```\r\n// Regexs not taking into account leading or trailing whitespace.\r\nvar reBinary = /^0b[0..1]+$/i,\r\n  reOctal = /^0o[0..7]+$/i;\r\n\r\n// Some older browsers don't convert null and undefined to humanised strings.\r\n// Opera 9 (I think) doesn't like the use of toString as a name.\r\n// Symbol should convert to value, but many not up to spec yet, so '' is\r\n// sensible for exposed toString.\r\n// Javascript toString doesn't preserve sign of 0.\r\nfunction toStr(value) {\r\n  if (value === 0) {\r\n    return 1 / value !== Infinity ? '-0' : '0';\r\n  }\r\n  var type = typeof value;\r\n  // Exit early for strings to avoid a performance hit in some environments.\r\n  if (type === 'string') {\r\n    return value;\r\n  }\r\n  return type !== 'symbol' ? value + '' : '';\r\n}\r\n\r\n// Expose or internal only?\r\n// Should leading and trailing whitespaces be accounted for, i.e. '   0b0101   ', probably?\r\nfunction isBinary(value) {\r\n  return reBinary.test(toStr(value));\r\n}\r\n\r\n// Expose or internal only?\r\n// Should leading and trailing whitespaces be accounted for, i.e. '   0b0101   ', probably?\r\nfunction isOctal(value) {\r\n  return reOctal.test(toStr(value));\r\n}\r\n\r\n// Internal use.\r\n// I believe there is also a hex conversion bug in some old browsers (not sure),\r\n// which could also be fixed/avoided here.\r\n// https://github.com/es-shims/es5-shim/blob/master/es5-shim.js#L1600\r\nfunction str2num(value) {\r\n  value = toStr(value);\r\n  // Should leading and trailing whitespaces be accounted for, i.e. '   0b0101   ', probably?\r\n  if (isBinary(value)) {\r\n    // returns or fallthrough, I don't know what your preference is?\r\n    value = parseInt(value.slice(2), 2);\r\n  } else if (isOctal(value)) {\r\n    // returns or fallthrough, I don't know what your preference is?\r\n    value = parseInt(value.slice(2), 8);\r\n  }\r\n  return +value;\r\n}\r\n\r\n// Number() gives 0, but Number(undefined) and +(undefined) gives NaN.\r\n// The ES6 spec says throw a TypeError for symbols but NaN seems more\r\n// appropriate for an exposed toNumber method.\r\n// Spec tries to convert by valueOf then toString.\r\n// At minimum Opera 9 throws if object has valueOf that is not a function.\r\n// Many host objects are `Object` objects that can coerce to strings\r\n// despite having improperly defined `toString` methods.\r\nfunction toNum(value) {\r\n  if (value === null) {\r\n    return 0;\r\n  }\r\n  var type = typeof value;\r\n  if (value === 'undefined' || type === 'symbol') {\r\n    return NaN;\r\n  }\r\n  if (type === 'string') {\r\n    return str2num(value);\r\n  }\r\n  if (type === 'number' || type === 'boolean' || _.isFunction(value.valueOf)) {\r\n    value = +value;\r\n  }\r\n  if (typeof value !== 'number') {\r\n     // returns or fallthrough, I don't know what your preference is?\r\n    value = str2num(value);\r\n  }\r\n  return value;\r\n}\r\n```\r\ntoString as variable name\r\nhttps://github.com/es-shims/es5-shim/blob/master/CHANGES#L142\r\nhttps://github.com/es-shims/es5-shim/blob/master/es5-shim.js#L62"},"comment":{"url":"https://api.github.com/repos/lodash/lodash/issues/comments/151675289","html_url":"https://github.com/lodash/lodash/issues/1574#issuecomment-151675289","issue_url":"https://api.github.com/repos/lodash/lodash/issues/1574","id":151675289,"user":{"login":"Xotic750","id":216041,"avatar_url":"https://avatars.githubusercontent.com/u/216041?v=3","gravatar_id":"","url":"https://api.github.com/users/Xotic750","html_url":"https://github.com/Xotic750","followers_url":"https://api.github.com/users/Xotic750/followers","following_url":"https://api.github.com/users/Xotic750/following{/other_user}","gists_url":"https://api.github.com/users/Xotic750/gists{/gist_id}","starred_url":"https://api.github.com/users/Xotic750/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Xotic750/subscriptions","organizations_url":"https://api.github.com/users/Xotic750/orgs","repos_url":"https://api.github.com/users/Xotic750/repos","events_url":"https://api.github.com/users/Xotic750/events{/privacy}","received_events_url":"https://api.github.com/users/Xotic750/received_events","type":"User","site_admin":false},"created_at":"2015-10-27T23:34:24Z","updated_at":"2015-10-27T23:34:24Z","body":"How about toString preserving sign, (something that I use regular)? I guess you're not worried about Opera 9/10 (toStr:name & valueOf not a function) if you are dropping support for older environments? And, do you have a preference regarding single or multiple returns?"}}