{"id":1735,"date":"2017-08-07T15:07:37","date_gmt":"2017-08-07T07:07:37","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1735"},"modified":"2017-08-07T15:07:37","modified_gmt":"2017-08-07T07:07:37","slug":"javascript%e5%94%af%e4%b8%80id%e7%94%9f%e6%88%90%e5%99%a8nano-id","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1735","title":{"rendered":"JavaScript\u552f\u4e00ID\u751f\u6210\u5668Nano ID"},"content":{"rendered":"<p>\n\t<a href=\"https:\/\/github.com\/ai\/nanoid\" name=\"nano id\" target=\"_blank\" title=\"nano id\">Nano ID<\/a>\u662f\u4e00\u4e2a\u673a\u9047JavaScript\u7684\u4e00\u4e2a\u968f\u673a ID \u751f\u6210\u6a21\u5757\u3002\u636e\u4f5c\u8005\u8bf4\uff0c\u6bd4 uuid \u66f4\u53ef\u9760\u548c\u597d\u7528\u3002\u81f3\u5c11\u4ece API \u7684\u529f\u80fd\u548c\u6613\u7528\u6027\u6765\u770b\uff0c\u4f3c\u4e4e\u786e\u5b9e\u5982\u6b64\u3002\n<\/p>\n<p>\n\tnanoid\u975e\u5e38\u5c0f\u5de7\u5b89\u5168\u7684\u7f51\u5740\u53cb\u597d\u7684JavaScript\u552f\u4e00ID\u751f\u6210\u5668\u3002\u6e90\u4ee3\u7801\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/ai\/nanoid\" name=\"nanoid\" target=\"_blank\" title=\"nanoid\">https:\/\/github.com\/ai\/nanoid<\/a>\n<\/p>\n<p>\n\t\u5728JavaScript\u4e2d\u7684\u7528\u6cd5\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar nanoid = require(&#039;nanoid&#039;);\r\nmodel.id = nanoid(); \/\/=&gt; &quot;Uakgb_J5m9g~0JDMbcJqLJ&quot;<\/pre>\n<h2>\n\tnanoid\u7684\u7279\u70b9<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t\u5b89\u5168\u3002\u5b83\u4f7f\u7528\u52a0\u5bc6\u5f3a\u5927\u7684\u968f\u673aAPI\uff0c\u5e76\u4fdd\u8bc1\u7b26\u53f7\u7684\u6b63\u786e\u5206\u914d\u3002\n\t<\/li>\n<li>\n\t\t\u5c0f\u3002\u53ea\u6709258\u5b57\u8282\uff08\u7f29\u5c0f\u548cgzipped\uff09\u3002\u6ca1\u6709\u4f9d\u8d56\u3002\n\t<\/li>\n<li>\n\t\t\u7d27\u51d1\u3002\u5b83\u4f7f\u7528\u6bd4UUID\uff08A-Za-z0-9_~\uff09\u66f4\u591a\u7684\u7b26\u53f7\uff0c\u5e76\u4e14\u53ea\u670922\u4e2a\u7b26\u53f7\u800c\u4e0d\u662f36\u4e2a\u5177\u6709\u76f8\u540c\u6570\u91cf\u7684\u552f\u4e00\u9009\u9879\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u8be5\u751f\u6210\u5668\u652f\u6301Node.js\u548c\u4eceIE 11\u5f00\u59cb\u7684<a href=\"http:\/\/caniuse.com\/#feat=getrandomvalues\" name=\"\u6d4f\u89c8\u5668\u6d4b\u8bd5\" target=\"_blank\" title=\"\u6d4f\u89c8\u5668\u6d4b\u8bd5\">\u6240\u6709\u6d4f\u89c8\u5668<\/a>\u3002\n<\/p>\n<h2>\n\tNano ID\u5747\u5300\u5ea6<br \/>\n<\/h2>\n<p>\n\trandom % alphabet\u5728\u7f16\u7801ID\u751f\u6210\u5668\u65f6\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u9519\u8bef\u3002\u5dee\u8ddd\u4e0d\u5e73\u5747; \u4e0e\u5176\u4ed6\u7b26\u53f7\u76f8\u6bd4\uff0c\u67d0\u4e9b\u7b26\u53f7\u7684\u51fa\u73b0\u673a\u4f1a\u5c06\u4f1a\u66f4\u4f4e\uff0c\u56e0\u6b64\u5728\u5f3a\u5236\u5f3a\u5236\u65f6\u4f1a\u51cf\u5c11\u5c1d\u8bd5\u6b21\u6570\u3002<br \/>\n\tNano ID\u4f7f\u7528\u66f4\u597d\u7684\u7b97\u6cd5\u5e76\u6d4b\u8bd5\u5747\u5300\u6027\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Nano ID\u5747\u5300\u5ea6\" src=\"http:\/\/ocuzluml6.bkt.clouddn.com\/distribution.png\" style=\"width: 681px;height: 271px\" \/>\n<\/p>\n<h2>\n\t\u81ea\u5b9a\u4e49\u5b57\u6bcd\u6216\u957f\u5ea6<br \/>\n<\/h2>\n<p>\n\t\u5982\u679c\u8981\u66f4\u6539ID\u5b57\u6bcd\u8868\u6216\u957f\u5ea6\u53ef\u4ee5\u4f7f\u7528\u4f4e\u7ea7generate\u6a21\u5757\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar generate = require(&#039;nanoid\/generate&#039;);\r\nmodel.id = generate(&#039;1234567890abcdef&#039;, 10); \/\/=&gt; &quot;4f90d13a42&quot;<\/pre>\n<p>\n\t\u5982\u679c\u8981\u4f7f\u7528\u76f8\u540c\u7684URL\u53cb\u597d\u7b26\u53f7\uff0c\u53ea\u9700\u66f4\u6539\u957f\u5ea6\uff0c\u5c31\u53ef\u4ee5\u4eceurl \u6a21\u5757\u83b7\u53d6\u9ed8\u8ba4\u5b57\u6bcd\u8868 &nbsp;\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar url = require(&#039;nanoid\/url&#039;);\r\nmodel.id = generate(url, 10); \/\/=&gt; &quot;Uakgb_J5m9&quot;<\/pre>\n<h2>\n\t\u81ea\u5b9a\u4e49\u968f\u673a\u5b57\u8282\u53d1\u751f\u5668<br \/>\n<\/h2>\n<p>\n\t\u60a8\u53ef\u4ee5\u4f7f\u7528format\u6a21\u5757\u66ff\u6362\u9ed8\u8ba4\u7684\u5b89\u5168\u968f\u673a\u751f\u6210\u5668\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u57fa\u4e8e\u79cd\u5b50\u7684\u751f\u6210\u5668\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar format = require(&#039;nanoid\/format&#039;);\r\nfunction random (size) {\r\n  var result = [];\r\n  for (var i = 0; i &lt; size; i++) result.push(randomByte())\r\n  return result;\r\n}\r\nformat(random, &quot;abcdef&quot;, 10); \/\/=&gt; &quot;fbaefaadeb&quot;<\/pre>\n<p>\n\trandom \u56de\u8c03\u51fd\u6570\u5fc5\u987b\u63a5\u53d7\u6570\u7ec4\u5927\u5c0f\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e26\u6709\u6570\u5b57\u7684\u6570\u7ec4\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1735\">JavaScript\u552f\u4e00ID\u751f\u6210\u5668Nano ID<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Nano ID\u662f\u4e00\u4e2a\u673a\u9047JavaScript\u7684\u4e00\u4e2a\u968f\u673a ID \u751f\u6210\u6a21\u5757\u3002\u636e\u4f5c\u8005\u8bf4\uff0c\u6bd4 uuid \u66f4\u53ef\u9760\u548c\u597d\u7528\u3002 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[1106,1411,1410],"class_list":["post-1735","post","type-post","status-publish","format-standard","hentry","category-html5","tag-javascript","tag-nanoid","tag-uuid"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1735"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1735\/revisions"}],"predecessor-version":[{"id":1736,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1735\/revisions\/1736"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}