You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.8 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # ❄ idgenerator-TypeScript
  2. ## 介绍
  3. 项目更多介绍参照:https://github.com/yitter/idgenerator
  4. 代码贡献者:zhupengfei(在 bubao 布宝 的JS基础上改版,感谢bubao 布宝)
  5. js Number 类型最大数值:9007199254740992(16位),
  6. 在JS中没有bigint类型,所以建议将ID控制在16位以内,统一使用number类型
  7. ## 使用
  8. ### 1.文件引用
  9. ```js
  10. import { snowflakeIdv1 } from '../snowflakeIdv1'
  11. const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
  12. let gen1 = new snowflakeIdv1({ WorkerId: WorkerId})
  13. let id1 = gen1.NextId()
  14. console.log(id1, id1.toString().length)
  15. ```
  16. ### 2.npm库安装
  17. ```sh
  18. npm i simple-flakeid
  19. ```
  20. ```js
  21. const snowId = require('simple-flakeid')
  22. let gen1 = new snowId.SnowflakeIdv1({ workerId: 1 })
  23. for (let i = 0; i < 10; i++) {
  24. let id1 = gen1.NextId()
  25. console.log(`${i} ID:${id1} ${typeof id1} length:${id1.toString().length}`)
  26. }
  27. ```
  28. ## function
  29. ### function NextId()
  30. 根据输出数值判断,小于number最大值时输出number类型,大于时输出bigint
  31. ### function NextNumber()
  32. 始终输出number类型,超过时throw error
  33. ### function NextBigId()
  34. 始终输出bigint类型
  35. ## 测试
  36. ```bash
  37. ts-node test/test1.ts
  38. ```
  39. ## 使用
  40. ```js
  41. import { snowflakeIdv1 } from '../snowflakeIdv1'
  42. const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
  43. let gen1 = new snowflakeIdv1({ WorkerId: WorkerId})
  44. let id1 = gen1.NextId()
  45. console.log(id1, id1.toString().length)
  46. ```
  47. ## 示例
  48. ```js
  49. import { snowflakeIdv1 } from '../snowflakeIdv1'
  50. const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
  51. let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 6 })
  52. for (let i = 0; i < 10; i++) {
  53. let id1 = gen1.NextId()
  54. console.log(`${i} ID:${id1} 长度:${id1.toString().length}`)
  55. }
  56. $ ts-node test/test4.ts
  57. 0 ID:234712552579141 长度:15
  58. 1 ID:234712552587333 长度:15
  59. 2 ID:234712552587334 长度:15
  60. 3 ID:234712552587335 长度:15
  61. 4 ID:234712552587336 长度:15
  62. 5 ID:234712552591429 长度:15
  63. 6 ID:234712552591430 长度:15
  64. 7 ID:234712552591431 长度:15
  65. 8 ID:234712552591432 长度:15
  66. 9 ID:234712552591433 长度:15
  67. import { snowflakeIdv1 } from '../snowflakeIdv1'
  68. const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
  69. let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 11 })
  70. for (let i = 0; i < 10; i++) {
  71. let id1 = gen1.NextId()
  72. console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
  73. }
  74. $ ts-node test/test4.ts
  75. 0 ID:7510958933018629 number 长度:16
  76. 1 ID:7510958933280773 number 长度:16
  77. 2 ID:7510958933280774 number 长度:16
  78. 3 ID:7510958933280775 number 长度:16
  79. 4 ID:7510958933411845 number 长度:16
  80. 5 ID:7510958933411846 number 长度:16
  81. 6 ID:7510958933542917 number 长度:16
  82. 7 ID:7510958933542918 number 长度:16
  83. 8 ID:7510958933542919 number 长度:16
  84. 9 ID:7510958933673989 number 长度:16
  85. import { snowflakeIdv1 } from '../snowflakeIdv1'
  86. const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
  87. let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 12 })
  88. for (let i = 0; i < 10; i++) {
  89. let id1 = gen1.NextId()
  90. console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
  91. }
  92. $ ts-node test/test4.ts
  93. 0 ID:15021931987734533 bigint 长度:17
  94. 1 ID:15021931987996677 bigint 长度:17
  95. 2 ID:15021931987996678 bigint 长度:17
  96. 3 ID:15021931987996679 bigint 长度:17
  97. 4 ID:15021931987996680 bigint 长度:17
  98. 5 ID:15021931988258821 bigint 长度:17
  99. 6 ID:15021931988258822 bigint 长度:17
  100. 7 ID:15021931988258823 bigint 长度:17
  101. 8 ID:15021931988258824 bigint 长度:17
  102. 9 ID:15021931988520965 bigint 长度:17
  103. import { snowflakeIdv1 } from '../snowflakeIdv1'
  104. const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
  105. let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 13 })
  106. for (let i = 0; i < 10; i++) {
  107. let id1 = gen1.NextId()
  108. console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
  109. }
  110. $ ts-node test/test4.ts
  111. 0 ID:30043877337997317 bigint 长度:17
  112. 1 ID:30043877338521605 bigint 长度:17
  113. 2 ID:30043877338521606 bigint 长度:17
  114. 3 ID:30043877339045893 bigint 长度:17
  115. 4 ID:30043877339045894 bigint 长度:17
  116. 5 ID:30043877339045895 bigint 长度:17
  117. 6 ID:30043877339045896 bigint 长度:17
  118. 7 ID:30043877339570181 bigint 长度:17
  119. 8 ID:30043877339570182 bigint 长度:17
  120. 9 ID:30043877339570183 bigint 长度:17
  121. ```
  122. ## 其他帮助
  123. 在mysql中int类型最大长度是10位数字,由于本算法默认生成的是15位,最短也是11位,所以在mysql中需要使用bigint数据类型