提交 d8d8714e 编写于 作者: 当代码农's avatar 当代码农

统一输入为bigint类型

上级 21e30e55
......@@ -8,10 +8,11 @@ function test1() {
}
}
function test2() {
const genid = new GenId({ WorkerId: 1 })
const id = genid.NextId()
console.log(typeof (id))
console.log(id, id.toString().length)
const genid = new GenId({ WorkerId: 1, SeqBitLength: 14 })
for (let i = 0; i < 10; i++) {
let id1 = genid.NextId()
console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
}
}
function main() {
......
......@@ -144,3 +144,31 @@ $ ts-node test/test4.ts
```
## 同时兼容number和bigint的写法
如果您觉得这个用法更好,可以手动替换对应方法
```js
/**
* 生成ID
* @returns
*/
public NextId(): number | bigint {
if (this._IsOverCost) {
//
let id = this.NextOverCostId()
if (id >= 9007199254740992n)
return id
else
return parseInt(id.toString())
} else {
//
let id = this.NextNormalId()
if (id >= 9007199254740992n)
return id
else
return parseInt(id.toString())
}
}
```
\ No newline at end of file
......@@ -328,21 +328,13 @@ export class snowflakeIdv1 {
* 生成ID
* @returns
*/
public NextId(): number | bigint {
public NextId(): bigint {
if (this._IsOverCost) {
//
let id = this.NextOverCostId()
if (id >= 9007199254740992n)
return id
else
return parseInt(id.toString())
return this.NextOverCostId()
} else {
//
let id = this.NextNormalId()
if (id >= 9007199254740992n)
return id
else
return parseInt(id.toString())
return this.NextNormalId()
}
}
}
......
......@@ -2,7 +2,7 @@ import { snowflakeIdv1 } from '../snowflakeIdv1'
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 13 })
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 10 })
for (let i = 0; i < 10; i++) {
let id1 = gen1.NextId()
console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册