{"id":251335,"date":"2022-09-07T08:01:38","date_gmt":"2022-09-07T00:01:38","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=251335"},"modified":"2022-08-29T09:02:33","modified_gmt":"2022-08-29T01:02:33","slug":"lodash-to-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/lodash-to-linux.html","title":{"rendered":"lodash\u91ccto\u7cfb\u5217\u4e4b\u5c06\u6570\u636e\u8f6c\u6362\u6210\u6570\u5b57\u7c7b\u578b\u5b9e\u73b0\u793a\u4f8b\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86lodash\u91ccto\u7cfb\u5217\u4e4b\u5c06\u6570\u636e\u8f6c\u6362\u6210\u6570\u5b57\u7c7b\u578b\u5b9e\u73b0\u793a\u4f8b\u8be6\u89e3\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6b63\u6587<\/strong><\/div>\n

\u5728lodash\u91cc\u7684to\u7cfb\u5217\u91cc\uff0c\u5c06\u76ee\u6807\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u7684\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4e86toNumber\u65b9\u6cd5\u3001toFinit\u65b9\u6cd5\u3001toInteger\u65b9\u6cd5\uff0ctoSafeInteger\u65b9\u6cd5\uff0c\u4e0b\u9762\u6765\u770b\u770b\u5404\u4e2a\u65b9\u6cd5\u7684\u4f7f\u7528\u548c\u5b9e\u73b0\u3002<\/p>\n

toNumber<\/strong><\/div>\n

toNumber\u65b9\u6cd5\u4e3b\u8981\u662f\u5c06\u53c2\u6570value\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6570\u5b57\u7c7b\u578b\u3002<\/p>\n

\u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n

toNumber(3.2)\r\n\/\/ => 3.2\r\ntoNumber(Number.MIN_VALUE)\r\n\/\/ => 5e-324\r\ntoNumber(Infinity)\r\n\/\/ => Infinity\r\ntoNumber('3.2')\r\n \/\/ => 3.2<\/pre>\n

toNumber\u65b9\u6cd5\u5728\u5b9e\u73b0\u4e0a\u501f\u52a9\u4e86\u5185\u90e8\u5c01\u88c5\u7684is\u7cfb\u5217\u65b9\u6cd5\uff0c\u4e3b\u8981\u662fisObject\u65b9\u6cd5\u548cisSymbol\u65b9\u6cd5\u3002<\/p>\n

\u5b9e\u73b0\u4e0a\u501f\u52a9typeof\uff0c\u5177\u4f53\u5904\u7406\u5982\u4e0b\uff1a<\/p>\n

  • \u5bf9\u4e8e\u6570\u636e\u7c7b\u578b\u76f4\u63a5\u8fd4\u56de\u53c2\u6570\u3002<\/li>\n
  • \u5bf9\u4e8esymbol\u7c7b\u578b\u76f4\u63a5\u8fd4\u56deNaN\u3002<\/li>\n
  • \u5bf9\u4e8e\u5bf9\u8c61\u7c7b\u578b\uff0c\u5206\u4e24\u79cd\u60c5\u51b5\u5904\u7406\u3002<\/li>\n
  • \u5982\u679c\u53c2\u6570\u539f\u578b\u94fe\u4e0a\u5b58\u5728valueOf\u65b9\u6cd5\uff0c\u76f4\u63a5\u8c03\u7528\u5176\u8fd4\u56de\u7ed3\u679c\u4f9b\u540e\u7eed\u5904\u7406\u3002<\/li>\n
  • \u5982\u679c\u53c2\u6570\u539f\u578b\u94fe\u4e0a\u4e0d\u5b58\u5728valueOf\u65b9\u6cd5\uff0c\u76f4\u63a5\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u7c7b\u578b\u4f9b\u540e\u7eed\u5904\u7406\u3002<\/li>\n
  • \u5bf9\u4e8e\u975e\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\uff0c\u8c03\u7528\u9690\u5f0f\u8f6c\u6362\u3002<\/li>\n
  • \u5bf9\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\uff0c\u4f1a\u8fdb\u884c\u6b63\u5219\u5339\u914d\u5904\u7406\u6570\u636e\u683c\u5f0f\uff0creTrim\u662f\u53bb\u9664\u7a7a\u683c\uff0creIsBadHex\u662f\u53bb\u9664\u5341\u516d\u8fdb\u5236\uff0creIsBinary\u662f\u53bb\u9664\u4e8c\u8fdb\u5236\uff0creIsOctal\u662f\u53bb\u9664\u516b\u8fdb\u5236\u3002<\/li>\n
  • \u5bf9\u4e8e\u4e8c\u8fdb\u5236\u548c\u516b\u8fdb\u5236\u7684\u5b57\u7b26\u4e32\u4f1a\u8c03\u7528\u539f\u751f\u7684parseInt\u65b9\u6cd5\u5c06\u53c2\u6570\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684\u8fdb\u5236\u6570\u3002\u800c\u5bf9\u4e8e\u5341\u516d\u8fdb\u5236\u5219\u8fd4\u56deNaN\uff0c\u5426\u5219\u8c03\u7528\u9690\u5f0f\u8f6c\u6362\u3002<\/li>\n

    \u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n

    import isObject from '.\/isObject.js'\r\nimport isSymbol from '.\/isSymbol.js'\r\nconst NAN = 0 \/ 0\r\nconst reTrim = \/^\\s+|\\s+$\/g\r\nconst reIsBadHex = \/^[-+]0x[0-9a-f]+$\/i\r\nconst reIsBinary = \/^0b[01]+$\/i\r\nconst reIsOctal = \/^0o[0-7]+$\/i\r\nconst freeParseInt = parseInt\r\nfunction toNumber(value) {\r\n  if (typeof value === 'number') {\r\n    return value\r\n  }\r\n  if (isSymbol(value)) {\r\n    return NAN\r\n  }\r\n  if (isObject(value)) {\r\n    const other = typeof value.valueOf === 'function' ? value.valueOf() : value\r\n    value = isObject(other) ? `${other}` : other\r\n  }\r\n  if (typeof value !== 'string') {\r\n    return value === 0 ? value : +value\r\n  }\r\n  value = value.replace(reTrim, '')\r\n  const isBinary = reIsBinary.test(value)\r\n  return (isBinary || reIsOctal.test(value))\r\n    ? freeParseInt(value.slice(2), isBinary ? 2 : 8)\r\n    : (reIsBadHex.test(value) ? NAN : +value)\r\n}<\/pre>\n
    toFinit<\/strong><\/div>\n

    toFinit\u65b9\u6cd5\u4e3b\u8981\u662f\u5c06\u53c2\u6570value\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6709\u9650\u7684\u6570\u5b57\u3002<\/p>\n

    \u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n

    toFinite(3.2)\r\n\/\/ => 3.2\r\ntoFinite(Number.MIN_VALUE)\r\n\/\/ => 5e-324\r\ntoFinite(Infinity)\r\n\/\/ => 1.7976931348623157e+308\r\ntoFinite('3.2')\r\n\/\/ => 3.2<\/pre>\n

    toFinit\u65b9\u6cd5\u5b9e\u73b0\u4e0a\u501f\u52a9toNumber\u65b9\u6cd5\uff0c\u5177\u4f53\u5904\u7406\u5982\u4e0b\uff1a<\/p>\n

  • \u5bf9\u4e8e\u4e0d\u5b58\u5728\u7684\u53c2\u6570\u4f1a\u76f4\u63a5\u8fd4\u56de\u53c2\u6570\uff0c0\u7684\u8bdd\u8fd4\u56de0\u3002<\/li>\n
  • \u5176\u6b21\u8c03\u7528toNumber\u65b9\u6cd5\u8f6c\u6362\u53c2\u6570\uff0c\u518d\u901a\u8fc7if\u5224\u65ad\u5339\u914dINFINITY\u503c\uff0c\u5c06\u5176\u8fd4\u56de\u5bf9\u5e94\u7684MAX_INTEGER\u3002<\/li>\n

    \u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n

    import toNumber from '.\/toNumber.js'\r\nconst INFINITY = 1 \/ 0\r\nconst MAX_INTEGER = 1.7976931348623157e+308\r\nfunction toFinite(value) {\r\n  if (!value) {\r\n    return value === 0 ? value : 0\r\n  }\r\n  value = toNumber(value)\r\n  if (value === INFINITY || value === -INFINITY) {\r\n    const sign = (value < 0 ? -1 : 1)\r\n    return sign * MAX_INTEGER\r\n  }\r\n  return value === value ? value : 0\r\n}<\/pre>\n
    toInteger<\/strong><\/div>\n

    toInteger\u65b9\u6cd5\u4e3b\u8981\u662f\u5c06\u53c2\u6570value\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n

    \u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n

    toInteger(3.2)\r\n\/\/ => 3\r\ntoInteger(Number.MIN_VALUE)\r\n\/\/ => 0\r\ntoInteger(Infinity)\r\n\/\/ => 1.7976931348623157e+308\r\ntoInteger('3.2')\r\n\/\/ => 3<\/pre>\n

    \u5b9e\u73b0\u4e0a\u501f\u52a9toFinite\u65b9\u6cd5\uff0c\u9996\u5148\u8c03\u7528toInteger\u65b9\u6cd5\u5c06\u53c2\u6570\u8f6c\u6362\u4e3a\u6709\u9650\u7684\u503c\uff0c\u7136\u540e\u901a\u8fc7\u53d6\u6a21\u64cd\u4f5c\u83b7\u53d6\u5c0f\u6570\u90e8\u5206\uff0c\u7136\u540e\u53d6\u5dee\u503c\u3002<\/p>\n

    \u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n

    import toFinite from '.\/toFinite.js'\r\nfunction toInteger(value) {\r\n  const result = toFinite(value)\r\n  const remainder = result % 1\r\n  return remainder ? result - remainder : result\r\n}\r\nexport default toInteger<\/pre>\n
    toSafeInteger<\/strong><\/div>\n

    toSafeInteger\u65b9\u6cd5\u4e3b\u8981\u662f\u5c06\u53c2\u6570value\u8f6c\u6362\u4e3a\u5b89\u5168\u6574\u6570\uff0c\u5b89\u5168\u6574\u6570\u53ef\u4ee5\u7528\u4e8e\u6bd4\u8f83\u548c\u51c6\u786e\u7684\u8868\u793a\u3002<\/p>\n

    \u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n

    toSafeInteger(3.2)\r\n\/\/ => 3\r\ntoSafeInteger(Number.MIN_VALUE)\r\n\/\/ => 0\r\ntoSafeInteger(Infinity)\r\n\/\/ => 9007199254740991\r\ntoSafeInteger('3.2')\r\n\/\/ => 3<\/pre>\n

    toSafeInteger\u65b9\u6cd5\u5728\u5b9e\u73b0\u4e0a\u4e3b\u8981\u901a\u8fc7toInteger\u65b9\u6cd5\uff0c\u540c\u6837\u7684\uff0c\u5bf9\u4e8e\u4e0d\u5b58\u5728\u7684\u53c2\u6570\u76f4\u63a5\u8fd4\u56de\u53c2\u6570\uff0c\u800c0\u5219\u8fd4\u56de0\u3002\u5bf9\u4e8e\u5c0f\u4e8e\u6700\u5c0f\u5b89\u5168\u6570MAX_SAFE_INTEGER\u5219\u8fd4\u56deMAX_SAFE_INTEGER\uff0c\u503c\u90fd\u5c06\u9650\u5236\u5728[-MAX_SAFE_INTEGER,MAX_SAFE_INTEGER]\u7684\u533a\u95f4\u3002<\/p>\n

    \u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n

    import toInteger from '.\/toInteger.js'\r\nconst MAX_SAFE_INTEGER = 9007199254740991\r\nfunction toSafeInteger(value) {\r\n  if (!value) {\r\n    return value === 0 ? value : 0\r\n  }\r\n  value = toInteger(value)\r\n  if (value < -MAX_SAFE_INTEGER) {\r\n    return -MAX_SAFE_INTEGER\r\n  }\r\n  if (value > MAX_SAFE_INTEGER) {\r\n    return MAX_SAFE_INTEGER\r\n  }\r\n  return value\r\n}\r\nexport default toSafeInteger<\/pre>\n
    \u5c0f\u7ed3<\/strong><\/div>\n

    \u672c\u7bc7\u7ae0\u6211\u4eec\u4e86\u89e3\u4e86toNumber\u3001toFinit\u3001toInteger\u4ee5\u53catoSafeInteger\u56db\u4e2a\u65b9\u6cd5\uff0c\u5728\u5b9e\u73b0\u4e0a\u4f9d\u6b21\u501f\u52a9\u8c03\u7528\u5b9e\u73b0\u3002<\/p>\n

    toNumber\u65b9\u6cd5\u662f\u5176\u4ed6\u65b9\u6cd5\u7684\u6838\u5fc3\uff0c\u5176\u4ed6\u65b9\u6cd5\u662ftoNumber\u65b9\u6cd5\u7684\u62d3\u5c55\u3002toNumber\u65b9\u6cd5\u7684\u6838\u5fc3\u662fisObject\u548cisSymbol\u4e24\u4e2a\u5224\u65ad\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u5728lodash\u91cc\u7684to\u7cfb\u5217\u91cc\uff0c\u5c06\u76ee\u6807\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u7684\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4e86toNumber\u65b9\u6cd5\u3001toFinit […]<\/p>\n","protected":false},"author":1479,"featured_media":241048,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-251335","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=251335"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251335\/revisions"}],"predecessor-version":[{"id":251337,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251335\/revisions\/251337"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/241048"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=251335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=251335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=251335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}