增加了三种function和更详细的说明
This commit is contained in:
@@ -198,11 +198,60 @@ class Genid {
|
||||
return tempTimeTicker;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成ID
|
||||
* @returns 始终输出number类型,超过时throw error
|
||||
*/
|
||||
NextNumber() {
|
||||
if (this._IsOverCost) {
|
||||
//
|
||||
let id = this.NextOverCostId()
|
||||
if (id >= 9007199254740992n)
|
||||
throw Error(`${id.toString()} over max of Number 9007199254740992`)
|
||||
|
||||
return parseInt(id.toString())
|
||||
} else {
|
||||
//
|
||||
let id = this.NextNormalId()
|
||||
if (id >= 9007199254740992n)
|
||||
throw Error(`${id.toString()} over max of Number 9007199254740992`)
|
||||
|
||||
return parseInt(id.toString())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成ID
|
||||
* @returns 根据输出数值判断,小于number最大值时输出number类型,大于时输出bigint
|
||||
*/
|
||||
NextId() {
|
||||
if (this._IsOverCost) {
|
||||
return parseInt(this.NextOverCostId());
|
||||
let id = this.NextOverCostId()
|
||||
if (id >= 9007199254740992n)
|
||||
return id
|
||||
else
|
||||
return parseInt(id)
|
||||
} else {
|
||||
return parseInt(this.NextNormalId());
|
||||
let id = this.NextNormalId()
|
||||
if (id >= 9007199254740992n)
|
||||
return id
|
||||
else
|
||||
return parseInt(id)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成ID
|
||||
* @returns 始终输出bigint类型
|
||||
*/
|
||||
NextBigId() {
|
||||
if (this._IsOverCost) {
|
||||
//
|
||||
return this.NextOverCostId()
|
||||
} else {
|
||||
//
|
||||
return this.NextNormalId()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,14 +11,55 @@ js Number 类型最大数值:9007199254740992(16位),
|
||||
在JS中没有bigint类型,所以建议将ID控制在16位以内,统一使用number类型
|
||||
|
||||
|
||||
执行测试代码
|
||||
## 使用
|
||||
|
||||
### 1.文件引用
|
||||
|
||||
```js
|
||||
import { snowflakeIdv1 } from '../snowflakeIdv1'
|
||||
|
||||
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
|
||||
|
||||
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId})
|
||||
let id1 = gen1.NextId()
|
||||
console.log(id1, id1.toString().length)
|
||||
|
||||
```
|
||||
|
||||
### 2.npm库安装
|
||||
|
||||
```sh
|
||||
npm i simple-flakeid
|
||||
```
|
||||
|
||||
```js
|
||||
const snowId = require('simple-flakeid')
|
||||
let gen1 = new snowId.SnowflakeIdv1({ workerId: 1 })
|
||||
for (let i = 0; i < 10; i++) {
|
||||
let id1 = gen1.NextId()
|
||||
console.log(`${i} ID:${id1} ${typeof id1} length:${id1.toString().length}`)
|
||||
}
|
||||
|
||||
```
|
||||
## function
|
||||
|
||||
### function NextId()
|
||||
根据输出数值判断,小于number最大值时输出number类型,大于时输出bigint
|
||||
|
||||
### function NextNumber()
|
||||
始终输出number类型,超过时throw error
|
||||
|
||||
### function NextBigId()
|
||||
始终输出bigint类型
|
||||
|
||||
|
||||
## 测试
|
||||
|
||||
```bash
|
||||
ts-node test/test1.ts
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 使用
|
||||
|
||||
```js
|
||||
@@ -127,50 +168,6 @@ $ ts-node test/test4.ts
|
||||
8 ID:30043877339570182 bigint 长度:17
|
||||
9 ID:30043877339570183 bigint 长度:17
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
## 同时兼容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())
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## 其他帮助
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { snowflakeIdv1Option } from "./snowflakeIdv1Option"
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -78,70 +80,67 @@ export class snowflakeIdv1 {
|
||||
*/
|
||||
private _OverCostCountInOneTerm
|
||||
|
||||
|
||||
/**
|
||||
*Creates an instance of Genid.
|
||||
* @author bubao
|
||||
* @param {{
|
||||
* Method: 1, // 雪花计算方法,(1-漂移算法|2-传统算法),默认 1
|
||||
* BaseTime: 1577836800000, // 基础时间(ms 单位),不能超过当前系统时间
|
||||
* WorkerId: Number, // 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1
|
||||
* WorkerIdBitLength: 6, // 机器码位长,默认值 6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过 22)
|
||||
* SeqBitLength: 6, // 序列数位长,默认值 6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过 22)
|
||||
* MaxSeqNumber: 5, // 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值 0,表示最大序列数取最大值(2^SeqBitLength-1])
|
||||
* MinSeqNumber: 5, // 最小序列数(含),默认值 5,取值范围 [5, MaxSeqNumber],每毫秒的前 5 个序列数对应编号 0-4 是保留位,其中 1-4 是时间回拨相应预留位,0 是手工新值预留位
|
||||
* TopOverCostCount: 2000// 最大漂移次数(含),默认 2000,推荐范围 500-10000(与计算能力有关)
|
||||
* }} options
|
||||
* @memberof Genid
|
||||
*/
|
||||
constructor(options: any) {
|
||||
if (options.WorkerId === undefined)
|
||||
*Creates an instance of Genid.
|
||||
* @author zhupengfeivip
|
||||
* @param {{
|
||||
* BaseTime: 1577836800000, // 基础时间(ms 单位),默认2020年1月1日,不能超过当前系统时间,一旦投入使用就不能再更改,更改后产生的ID可能会和以前的重复
|
||||
* WorkerId: Number, // 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1
|
||||
* WorkerIdBitLength: 6, // 机器码位长,默认值 6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过 22)
|
||||
* SeqBitLength: 6, // 序列数位长,默认值 6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过 22)
|
||||
* MaxSeqNumber: 5, // 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值 0,表示最大序列数取最大值(2^SeqBitLength-1])
|
||||
* MinSeqNumber: 5, // 最小序列数(含),默认值 5,取值范围 [5, MaxSeqNumber],每毫秒的前 5 个序列数对应编号 0-4 是保留位,其中 1-4 是时间回拨相应预留位,0 是手工新值预留位
|
||||
* TopOverCostCount: 2000// 最大漂移次数(含),默认 2000,推荐范围 500-10000(与计算能力有关)
|
||||
* }} options
|
||||
* @memberof Genid
|
||||
*/
|
||||
constructor(options: snowflakeIdv1Option) {
|
||||
if (options.workerId === undefined)
|
||||
throw new Error("lost WorkerId")
|
||||
|
||||
// 1.BaseTime 2020年1月1日
|
||||
// 1.BaseTime 2020年1月1日 Wed, 01 Jan 2020 00:00:00 GMT 0时区的2020年1月1日
|
||||
const BaseTime = 1577836800000
|
||||
if (!options.BaseTime || options.BaseTime < 0)
|
||||
options.BaseTime = BaseTime
|
||||
if (!options.baseTime || options.baseTime < 0)
|
||||
options.baseTime = BaseTime
|
||||
|
||||
// 2.WorkerIdBitLength
|
||||
const WorkerIdBitLength = 6
|
||||
if (!options.WorkerIdBitLength || options.WorkerIdBitLength < 0)
|
||||
options.WorkerIdBitLength = WorkerIdBitLength
|
||||
if (!options.workerIdBitLength || options.workerIdBitLength < 0)
|
||||
options.workerIdBitLength = WorkerIdBitLength
|
||||
|
||||
// 4.SeqBitLength
|
||||
const SeqBitLength = 6
|
||||
if (!options.SeqBitLength || options.SeqBitLength < 0)
|
||||
options.SeqBitLength = SeqBitLength
|
||||
if (!options.seqBitLength || options.seqBitLength < 0)
|
||||
options.seqBitLength = SeqBitLength
|
||||
|
||||
// 5.MaxSeqNumber
|
||||
const MaxSeqNumber = (1 << SeqBitLength) - 1
|
||||
if (options.MaxSeqNumber <= 0 || options.MaxSeqNumber === undefined) {
|
||||
options.MaxSeqNumber = MaxSeqNumber
|
||||
}
|
||||
if (options.maxSeqNumber == undefined || options.maxSeqNumber <= 0)
|
||||
options.maxSeqNumber = (1 << SeqBitLength) - 1
|
||||
|
||||
// 6.MinSeqNumber
|
||||
const MinSeqNumber = 5
|
||||
if (!options.MinSeqNumber || options.MinSeqNumber < 0)
|
||||
options.MinSeqNumber = MinSeqNumber
|
||||
if (options.minSeqNumber == undefined || options.minSeqNumber < 0)
|
||||
options.minSeqNumber = MinSeqNumber
|
||||
|
||||
// 7.Others
|
||||
const topOverCostCount = 2000
|
||||
if (!options.TopOverCostCount || options.TopOverCostCount < 0)
|
||||
options.TopOverCostCount = topOverCostCount
|
||||
if (options.topOverCostCount == undefined || options.topOverCostCount < 0)
|
||||
options.topOverCostCount = topOverCostCount
|
||||
|
||||
|
||||
if (options.Method !== 2)
|
||||
options.Method = 1
|
||||
if (options.method !== 2)
|
||||
options.method = 1
|
||||
else
|
||||
options.Method = 2
|
||||
options.method = 2
|
||||
|
||||
this.Method = BigInt(options.Method)
|
||||
this.BaseTime = BigInt(options.BaseTime)
|
||||
this.WorkerId = BigInt(options.WorkerId)
|
||||
this.WorkerIdBitLength = BigInt(options.WorkerIdBitLength)
|
||||
this.SeqBitLength = BigInt(options.SeqBitLength)
|
||||
this.MaxSeqNumber = BigInt(options.MaxSeqNumber)
|
||||
this.MinSeqNumber = BigInt(options.MinSeqNumber)
|
||||
this.TopOverCostCount = BigInt(options.TopOverCostCount)
|
||||
this.Method = BigInt(options.method)
|
||||
this.BaseTime = BigInt(options.baseTime)
|
||||
this.WorkerId = BigInt(options.workerId)
|
||||
this.WorkerIdBitLength = BigInt(options.workerIdBitLength)
|
||||
this.SeqBitLength = BigInt(options.seqBitLength)
|
||||
this.MaxSeqNumber = BigInt(options.maxSeqNumber)
|
||||
this.MinSeqNumber = BigInt(options.minSeqNumber)
|
||||
this.TopOverCostCount = BigInt(options.topOverCostCount)
|
||||
|
||||
const timestampShift = this.WorkerIdBitLength + this.SeqBitLength
|
||||
const currentSeqNumber = this.MinSeqNumber
|
||||
@@ -156,6 +155,7 @@ export class snowflakeIdv1 {
|
||||
this._OverCostCountInOneTerm = 0
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 当前序列号超过最大范围,开始透支使用序号号的通知事件,,本项暂未实现
|
||||
* @returns
|
||||
@@ -326,9 +326,53 @@ export class snowflakeIdv1 {
|
||||
|
||||
/**
|
||||
* 生成ID
|
||||
* @returns
|
||||
* @returns 始终输出number类型,超过时throw error
|
||||
*/
|
||||
public NextId(): bigint {
|
||||
public NextNumber(): number {
|
||||
if (this._IsOverCost) {
|
||||
//
|
||||
let id = this.NextOverCostId()
|
||||
if (id >= 9007199254740992n)
|
||||
throw Error(`${id.toString()} over max of Number 9007199254740992`)
|
||||
|
||||
return parseInt(id.toString())
|
||||
} else {
|
||||
//
|
||||
let id = this.NextNormalId()
|
||||
if (id >= 9007199254740992n)
|
||||
throw Error(`${id.toString()} over max of Number 9007199254740992`)
|
||||
|
||||
return parseInt(id.toString())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成ID
|
||||
* @returns 根据输出数值判断,小于number最大值时输出number类型,大于时输出bigint
|
||||
*/
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成ID
|
||||
* @returns 始终输出bigint类型
|
||||
*/
|
||||
public NextBigId(): bigint {
|
||||
if (this._IsOverCost) {
|
||||
//
|
||||
return this.NextOverCostId()
|
||||
|
||||
43
TypeScript/snowflakeIdv1Option.ts
Normal file
43
TypeScript/snowflakeIdv1Option.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
export class snowflakeIdv1Option {
|
||||
|
||||
/**
|
||||
* 雪花计算方法,(1-漂移算法|2-传统算法),默认 1
|
||||
*/
|
||||
method?: number = 1
|
||||
|
||||
/**
|
||||
* 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1
|
||||
*/
|
||||
workerId: number = 1
|
||||
|
||||
/**
|
||||
* 机器码位长,默认值 6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过 22)
|
||||
*/
|
||||
workerIdBitLength?: number = 6
|
||||
|
||||
/**
|
||||
* 基础时间(ms 单位),不能超过当前系统时间, 默认2020年1月1日
|
||||
*/
|
||||
baseTime?: number = 1577836800000
|
||||
|
||||
/**
|
||||
* 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值 0,表示最大序列数取最大值(2^SeqBitLength-1])
|
||||
*/
|
||||
maxSeqNumber?: number = undefined
|
||||
|
||||
/**
|
||||
* 最小序列数(含),默认值 5,取值范围 [5, MaxSeqNumber],每毫秒的前 5 个序列数对应编号 0-4 是保留位,其中 1-4 是时间回拨相应预留位,0 是手工新值预留位
|
||||
*/
|
||||
minSeqNumber?: number = 5
|
||||
|
||||
/**
|
||||
* 序列数位长,默认值 6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过 22)
|
||||
*/
|
||||
seqBitLength?: number = 6
|
||||
|
||||
/**
|
||||
* 最大漂移次数(含),默认 2000,推荐范围 500-10000(与计算能力有关)
|
||||
*/
|
||||
topOverCostCount?: number = 2000
|
||||
|
||||
}
|
||||
@@ -2,6 +2,6 @@ import { snowflakeIdv1 } from '../snowflakeIdv1'
|
||||
|
||||
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
|
||||
|
||||
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId })
|
||||
let gen1 = new snowflakeIdv1({ workerId: 1 })
|
||||
let id1 = gen1.NextId()
|
||||
console.log(`ID:${id1} 长度:${id1.toString().length}`)
|
||||
@@ -8,7 +8,7 @@ const Method = process.env.Method == undefined ? 1 : process.env.Method
|
||||
console.log("WorkerId:" + WorkerId)
|
||||
console.log("Method:" + Method)
|
||||
console.log("--------------------")
|
||||
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method })
|
||||
let gen1 = new snowflakeIdv1({ workerId: 1 })
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,6 @@ const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
|
||||
|
||||
const Method = process.env.Method == undefined ? 1 : process.env.Method
|
||||
|
||||
let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method })
|
||||
let gen1 = new snowflakeIdv1({ workerId: 1 })
|
||||
let id1 = gen1.NextId()
|
||||
console.log(id1, id1.toString().length)
|
||||
@@ -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: 10 })
|
||||
let gen1 = new snowflakeIdv1({ workerId: 1, seqBitLength: 10 })
|
||||
for (let i = 0; i < 10; i++) {
|
||||
let id1 = gen1.NextId()
|
||||
console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
|
||||
|
||||
@@ -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: 10 })
|
||||
let gen1 = new snowflakeIdv1({ workerId: 1, seqBitLength: 10 })
|
||||
// for (let i = 0; i < 10; i++) {
|
||||
// let id1 = gen1.NextId()
|
||||
// console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
|
||||
|
||||
Reference in New Issue
Block a user