7ae91a24b19c9d0eaa4d44d14e6adfcc7e24aa53a89608b7143e8310fbd360808685dc291455bd339e69761398321fff370e21df328e0723722d8484798bf2 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. ## Miscellaneous
  3. */
  4. var DICT = require('./address_dict')
  5. module.exports = {
  6. // Dice
  7. d4: function() {
  8. return this.natural(1, 4)
  9. },
  10. d6: function() {
  11. return this.natural(1, 6)
  12. },
  13. d8: function() {
  14. return this.natural(1, 8)
  15. },
  16. d12: function() {
  17. return this.natural(1, 12)
  18. },
  19. d20: function() {
  20. return this.natural(1, 20)
  21. },
  22. d100: function() {
  23. return this.natural(1, 100)
  24. },
  25. /*
  26. 随机生成一个 GUID。
  27. http://www.broofa.com/2008/09/javascript-uuid-function/
  28. [UUID 规范](http://www.ietf.org/rfc/rfc4122.txt)
  29. UUIDs (Universally Unique IDentifier)
  30. GUIDs (Globally Unique IDentifier)
  31. The formal definition of the UUID string representation is provided by the following ABNF [7]:
  32. UUID = time-low "-" time-mid "-"
  33. time-high-and-version "-"
  34. clock-seq-and-reserved
  35. clock-seq-low "-" node
  36. time-low = 4hexOctet
  37. time-mid = 2hexOctet
  38. time-high-and-version = 2hexOctet
  39. clock-seq-and-reserved = hexOctet
  40. clock-seq-low = hexOctet
  41. node = 6hexOctet
  42. hexOctet = hexDigit hexDigit
  43. hexDigit =
  44. "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
  45. "a" / "b" / "c" / "d" / "e" / "f" /
  46. "A" / "B" / "C" / "D" / "E" / "F"
  47. https://github.com/victorquinn/chancejs/blob/develop/chance.js#L1349
  48. */
  49. guid: function() {
  50. var pool = "abcdefABCDEF1234567890",
  51. guid = this.string(pool, 8) + '-' +
  52. this.string(pool, 4) + '-' +
  53. this.string(pool, 4) + '-' +
  54. this.string(pool, 4) + '-' +
  55. this.string(pool, 12);
  56. return guid
  57. },
  58. uuid: function() {
  59. return this.guid()
  60. },
  61. /*
  62. 随机生成一个 18 位身份证。
  63. [身份证](http://baike.baidu.com/view/1697.htm#4)
  64. 地址码 6 + 出生日期码 8 + 顺序码 3 + 校验码 1
  65. [《中华人民共和国行政区划代码》国家标准(GB/T2260)](http://zhidao.baidu.com/question/1954561.html)
  66. */
  67. id: function() {
  68. var id,
  69. sum = 0,
  70. rank = [
  71. "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"
  72. ],
  73. last = [
  74. "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"
  75. ]
  76. id = this.pick(DICT).id +
  77. this.date('yyyyMMdd') +
  78. this.string('number', 3)
  79. for (var i = 0; i < id.length; i++) {
  80. sum += id[i] * rank[i];
  81. }
  82. id += last[sum % 11];
  83. return id
  84. },
  85. /*
  86. 生成一个全局的自增整数。
  87. 类似自增主键(auto increment primary key)。
  88. */
  89. increment: function() {
  90. var key = 0
  91. return function(step) {
  92. return key += (+step || 1) // step?
  93. }
  94. }(),
  95. inc: function(step) {
  96. return this.increment(step)
  97. }
  98. }