Skip to content

Commit

Permalink
add uuid generator
Browse files Browse the repository at this point in the history
  • Loading branch information
HypnosNova committed Feb 20, 2022
1 parent 5f2af7c commit ada31d4
Show file tree
Hide file tree
Showing 9 changed files with 700 additions and 488 deletions.
14 changes: 14 additions & 0 deletions build/IdGenerator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,18 @@ export default class IdGenerator implements IIncreaser {
* @returns {number} id
*/
skip(value?: number): number;
/**
* @method IdGenerator.prototype.skip
* @desc 生成新的32位uuid
* @public
* @returns {string} uuid
*/
uuid(): string;
/**
* @method IdGenerator.prototype.skip
* @desc 生成新的32位BigInt
* @public
* @returns {BigInt} uuid
*/
uuidBigInt(): BigInt;
}
43 changes: 40 additions & 3 deletions build/IdGenerator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/IdGenerator.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion build/IdGenerator.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 38 additions & 1 deletion build/IdGenerator.module.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const S4 = () => {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
/**
* @class
* @classdesc 数字id生成器,用于生成递增id
* @param {number} [initValue = 0] 从几开始生成递增id
* @implements IdGenerator.IIncreaser
*/
class IdGenerator {
initValue;
value;
/**
* @member IdGenerator.initValue
* @desc id从该值开始递增,在创建实例时进行设置。设置之后将无法修改。
Expand Down Expand Up @@ -47,6 +52,38 @@ class IdGenerator {
this.value += value;
return ++this.value;
}
/**
* @method IdGenerator.prototype.skip
* @desc 生成新的32位uuid
* @public
* @returns {string} uuid
*/
uuid() {
if (crypto.randomUUID) {
return crypto.randomUUID();
}
else {
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
}
/**
* @method IdGenerator.prototype.skip
* @desc 生成新的32位BigInt
* @public
* @returns {BigInt} uuid
*/
uuidBigInt() {
//return bi4(7) + bi4(6) + bi4(5) + bi4(4) + bi4(3) + bi4(2) + bi4(1) + bi4(0);
let arr = crypto.getRandomValues(new Uint16Array(8));
return BigInt(arr[0]) * 65536n * 65536n * 65536n * 65536n * 65536n * 65536n * 65536n
+ BigInt(arr[1]) * 65536n * 65536n * 65536n * 65536n * 65536n * 65536n
+ BigInt(arr[2]) * 65536n * 65536n * 65536n * 65536n * 65536n
+ BigInt(arr[3]) * 65536n * 65536n * 65536n * 65536n
+ BigInt(arr[4]) * 65536n * 65536n * 65536n
+ BigInt(arr[5]) * 65536n * 65536n
+ BigInt(arr[6]) * 65536n
+ BigInt(arr[6]);
}
}

export default IdGenerator;
export { IdGenerator as default };
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@valeera/idgenerator",
"version": "1.0.0",
"version": "1.1.0",
"description": "A light weight number ID generator written in TS.",
"main": "build/IdGenerator.js",
"repository": "ValeeraJS/IdGenerator",
Expand Down Expand Up @@ -52,7 +52,7 @@
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-tsdoc": "^0.2.11",
"esm": "^3.2.25",
"google-closure-compiler": "20210302.0.0",
"google-closure-compiler": "20220202.0.0",
"http-server": "^0.12.1",
"jsdoc": "^3.6.3",
"jsdoc-plugin-typescript": "^2.0.5",
Expand Down
11 changes: 6 additions & 5 deletions scripts/externs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var EventDispatcher;
var on;
var filt;
var off;
var dispatch;
var IdGenerator = function() {};
IdGenerator.prototpye.current = function () {};
IdGenerator.prototpye.next = function () {};
IdGenerator.prototpye.skip = function () {};
IdGenerator.prototpye.uuid = function () {};
IdGenerator.prototpye.uuidBigInt = function () {};
37 changes: 37 additions & 0 deletions src/IdGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import IIncreaser from "./IIncreaser";

const S4 = () => {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

/**
* @class
* @classdesc 数字id生成器,用于生成递增id
Expand Down Expand Up @@ -56,4 +60,37 @@ export default class IdGenerator implements IIncreaser {

return ++this.value;
}

/**
* @method IdGenerator.prototype.skip
* @desc 生成新的32位uuid
* @public
* @returns {string} uuid
*/
public uuid(): string {
if ((crypto as any).randomUUID) {
return (crypto as any).randomUUID();
} else {
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
}

/**
* @method IdGenerator.prototype.skip
* @desc 生成新的32位BigInt
* @public
* @returns {BigInt} uuid
*/
public uuidBigInt(): BigInt {
//return bi4(7) + bi4(6) + bi4(5) + bi4(4) + bi4(3) + bi4(2) + bi4(1) + bi4(0);
let arr = crypto.getRandomValues(new Uint16Array(8));
return BigInt(arr[0]) * 65536n * 65536n * 65536n * 65536n * 65536n * 65536n * 65536n
+ BigInt(arr[1]) * 65536n * 65536n * 65536n * 65536n * 65536n * 65536n
+ BigInt(arr[2]) * 65536n * 65536n * 65536n * 65536n * 65536n
+ BigInt(arr[3]) * 65536n * 65536n * 65536n * 65536n
+ BigInt(arr[4]) * 65536n * 65536n * 65536n
+ BigInt(arr[5]) * 65536n * 65536n
+ BigInt(arr[6]) * 65536n
+ BigInt(arr[6]);
}
}
Loading

0 comments on commit ada31d4

Please sign in to comment.