Skip to content

Commit

Permalink
uuid from uuid lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed May 8, 2024
1 parent 9de7cf8 commit c38ad70
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 19 deletions.
11 changes: 2 additions & 9 deletions libs/hashCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports.generateUUID = exports.generateRandomString = void 0;
* @param str - string to hash
*/
const crypto = require("crypto");
const uuid_1 = require("uuid");
function hashCode(str) {
return crypto.createHash("sha256").update(str).digest("hex");
}
Expand All @@ -16,14 +17,6 @@ function generateRandomString(length) {
}
exports.generateRandomString = generateRandomString;
function generateUUID() {
const uuid = [];
const hex = '0123456789abcdef';
for (let i = 0; i < 36; i++) {
uuid[i] = hex[(Math.random() * 16) | 0];
}
uuid[14] = '4'; // Set version 4
uuid[19] = hex[(uuid[19] & 0x3) | 0x8]; // Set variant
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
return uuid.join('');
return (0, uuid_1.v4)();
}
exports.generateUUID = generateUUID;
12 changes: 2 additions & 10 deletions libs/hashCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @param str - string to hash
*/
const crypto = require("crypto");
import { v4 as uuid } from "uuid";

export default function hashCode(str: string): string {
return crypto.createHash("sha256").update(str).digest("hex");
Expand All @@ -14,14 +15,5 @@ export function generateRandomString(length: number): string {
}

export function generateUUID(): string {
const uuid = [];
const hex = '0123456789abcdef';
for (let i = 0; i < 36; i++) {
uuid[i] = hex[(Math.random() * 16) | 0];
}
uuid[14] = '4'; // Set version 4
uuid[19] = hex[(uuid[19] & 0x3) | 0x8]; // Set variant
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';

return uuid.join('');
return uuid();
}

0 comments on commit c38ad70

Please sign in to comment.