增加了三种function和更详细的说明
This commit is contained in:
@@ -198,11 +198,60 @@ class Genid {
|
|||||||
return tempTimeTicker;
|
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() {
|
NextId() {
|
||||||
if (this._IsOverCost) {
|
if (this._IsOverCost) {
|
||||||
return parseInt(this.NextOverCostId());
|
let id = this.NextOverCostId()
|
||||||
|
if (id >= 9007199254740992n)
|
||||||
|
return id
|
||||||
|
else
|
||||||
|
return parseInt(id)
|
||||||
} else {
|
} 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类型
|
在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
|
```bash
|
||||||
ts-node test/test1.ts
|
ts-node test/test1.ts
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 使用
|
## 使用
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@@ -127,50 +168,6 @@ $ ts-node test/test4.ts
|
|||||||
8 ID:30043877339570182 bigint 长度:17
|
8 ID:30043877339570182 bigint 长度:17
|
||||||
9 ID:30043877339570183 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,13 +80,11 @@ export class snowflakeIdv1 {
|
|||||||
*/
|
*/
|
||||||
private _OverCostCountInOneTerm
|
private _OverCostCountInOneTerm
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*Creates an instance of Genid.
|
*Creates an instance of Genid.
|
||||||
* @author bubao
|
* @author zhupengfeivip
|
||||||
* @param {{
|
* @param {{
|
||||||
* Method: 1, // 雪花计算方法,(1-漂移算法|2-传统算法),默认 1
|
* BaseTime: 1577836800000, // 基础时间(ms 单位),默认2020年1月1日,不能超过当前系统时间,一旦投入使用就不能再更改,更改后产生的ID可能会和以前的重复
|
||||||
* BaseTime: 1577836800000, // 基础时间(ms 单位),不能超过当前系统时间
|
|
||||||
* WorkerId: Number, // 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1
|
* WorkerId: Number, // 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1
|
||||||
* WorkerIdBitLength: 6, // 机器码位长,默认值 6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过 22)
|
* WorkerIdBitLength: 6, // 机器码位长,默认值 6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过 22)
|
||||||
* SeqBitLength: 6, // 序列数位长,默认值 6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过 22)
|
* SeqBitLength: 6, // 序列数位长,默认值 6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过 22)
|
||||||
@@ -94,54 +94,53 @@ export class snowflakeIdv1 {
|
|||||||
* }} options
|
* }} options
|
||||||
* @memberof Genid
|
* @memberof Genid
|
||||||
*/
|
*/
|
||||||
constructor(options: any) {
|
constructor(options: snowflakeIdv1Option) {
|
||||||
if (options.WorkerId === undefined)
|
if (options.workerId === undefined)
|
||||||
throw new Error("lost WorkerId")
|
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
|
const BaseTime = 1577836800000
|
||||||
if (!options.BaseTime || options.BaseTime < 0)
|
if (!options.baseTime || options.baseTime < 0)
|
||||||
options.BaseTime = BaseTime
|
options.baseTime = BaseTime
|
||||||
|
|
||||||
// 2.WorkerIdBitLength
|
// 2.WorkerIdBitLength
|
||||||
const WorkerIdBitLength = 6
|
const WorkerIdBitLength = 6
|
||||||
if (!options.WorkerIdBitLength || options.WorkerIdBitLength < 0)
|
if (!options.workerIdBitLength || options.workerIdBitLength < 0)
|
||||||
options.WorkerIdBitLength = WorkerIdBitLength
|
options.workerIdBitLength = WorkerIdBitLength
|
||||||
|
|
||||||
// 4.SeqBitLength
|
// 4.SeqBitLength
|
||||||
const SeqBitLength = 6
|
const SeqBitLength = 6
|
||||||
if (!options.SeqBitLength || options.SeqBitLength < 0)
|
if (!options.seqBitLength || options.seqBitLength < 0)
|
||||||
options.SeqBitLength = SeqBitLength
|
options.seqBitLength = SeqBitLength
|
||||||
|
|
||||||
// 5.MaxSeqNumber
|
// 5.MaxSeqNumber
|
||||||
const MaxSeqNumber = (1 << SeqBitLength) - 1
|
if (options.maxSeqNumber == undefined || options.maxSeqNumber <= 0)
|
||||||
if (options.MaxSeqNumber <= 0 || options.MaxSeqNumber === undefined) {
|
options.maxSeqNumber = (1 << SeqBitLength) - 1
|
||||||
options.MaxSeqNumber = MaxSeqNumber
|
|
||||||
}
|
|
||||||
// 6.MinSeqNumber
|
// 6.MinSeqNumber
|
||||||
const MinSeqNumber = 5
|
const MinSeqNumber = 5
|
||||||
if (!options.MinSeqNumber || options.MinSeqNumber < 0)
|
if (options.minSeqNumber == undefined || options.minSeqNumber < 0)
|
||||||
options.MinSeqNumber = MinSeqNumber
|
options.minSeqNumber = MinSeqNumber
|
||||||
|
|
||||||
// 7.Others
|
// 7.Others
|
||||||
const topOverCostCount = 2000
|
const topOverCostCount = 2000
|
||||||
if (!options.TopOverCostCount || options.TopOverCostCount < 0)
|
if (options.topOverCostCount == undefined || options.topOverCostCount < 0)
|
||||||
options.TopOverCostCount = topOverCostCount
|
options.topOverCostCount = topOverCostCount
|
||||||
|
|
||||||
|
|
||||||
if (options.Method !== 2)
|
if (options.method !== 2)
|
||||||
options.Method = 1
|
options.method = 1
|
||||||
else
|
else
|
||||||
options.Method = 2
|
options.method = 2
|
||||||
|
|
||||||
this.Method = BigInt(options.Method)
|
this.Method = BigInt(options.method)
|
||||||
this.BaseTime = BigInt(options.BaseTime)
|
this.BaseTime = BigInt(options.baseTime)
|
||||||
this.WorkerId = BigInt(options.WorkerId)
|
this.WorkerId = BigInt(options.workerId)
|
||||||
this.WorkerIdBitLength = BigInt(options.WorkerIdBitLength)
|
this.WorkerIdBitLength = BigInt(options.workerIdBitLength)
|
||||||
this.SeqBitLength = BigInt(options.SeqBitLength)
|
this.SeqBitLength = BigInt(options.seqBitLength)
|
||||||
this.MaxSeqNumber = BigInt(options.MaxSeqNumber)
|
this.MaxSeqNumber = BigInt(options.maxSeqNumber)
|
||||||
this.MinSeqNumber = BigInt(options.MinSeqNumber)
|
this.MinSeqNumber = BigInt(options.minSeqNumber)
|
||||||
this.TopOverCostCount = BigInt(options.TopOverCostCount)
|
this.TopOverCostCount = BigInt(options.topOverCostCount)
|
||||||
|
|
||||||
const timestampShift = this.WorkerIdBitLength + this.SeqBitLength
|
const timestampShift = this.WorkerIdBitLength + this.SeqBitLength
|
||||||
const currentSeqNumber = this.MinSeqNumber
|
const currentSeqNumber = this.MinSeqNumber
|
||||||
@@ -156,6 +155,7 @@ export class snowflakeIdv1 {
|
|||||||
this._OverCostCountInOneTerm = 0
|
this._OverCostCountInOneTerm = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前序列号超过最大范围,开始透支使用序号号的通知事件,,本项暂未实现
|
* 当前序列号超过最大范围,开始透支使用序号号的通知事件,,本项暂未实现
|
||||||
* @returns
|
* @returns
|
||||||
@@ -326,9 +326,53 @@ export class snowflakeIdv1 {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成ID
|
* 生成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) {
|
if (this._IsOverCost) {
|
||||||
//
|
//
|
||||||
return this.NextOverCostId()
|
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
|
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()
|
let id1 = gen1.NextId()
|
||||||
console.log(`ID:${id1} 长度:${id1.toString().length}`)
|
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("WorkerId:" + WorkerId)
|
||||||
console.log("Method:" + Method)
|
console.log("Method:" + Method)
|
||||||
console.log("--------------------")
|
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
|
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()
|
let id1 = gen1.NextId()
|
||||||
console.log(id1, id1.toString().length)
|
console.log(id1, id1.toString().length)
|
||||||
@@ -2,7 +2,7 @@ import { snowflakeIdv1 } from '../snowflakeIdv1'
|
|||||||
|
|
||||||
const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId
|
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++) {
|
for (let i = 0; i < 10; i++) {
|
||||||
let id1 = gen1.NextId()
|
let id1 = gen1.NextId()
|
||||||
console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
|
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
|
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++) {
|
// for (let i = 0; i < 10; i++) {
|
||||||
// let id1 = gen1.NextId()
|
// let id1 = gen1.NextId()
|
||||||
// console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
|
// console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`)
|
||||||
|
|||||||
Reference in New Issue
Block a user