Skip to content

Commit cddf995

Browse files
author
Nic Bradley
committed
Add libUUID
1 parent a7cf2d8 commit cddf995

File tree

14 files changed

+3764
-0
lines changed

14 files changed

+3764
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
22
.idea
3+
**/node_modules

libUUID/.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodejs 22.12.0

libUUID/0.0.1/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default class libUUID {
2+
private static base64Chars;
3+
private static base;
4+
private static previousTime;
5+
private static counter;
6+
private static toBase64;
7+
private static generateRandomBase64;
8+
static generateUUID(): string;
9+
static generateRowID(): string;
10+
}

libUUID/0.0.1/libUUID.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// libUUID v0.0.1 by GUD Team | Tired of copying and pasting a UUID function over and over? Me too. This script provides a couple of functions to generate UUIDs.
2+
var libUUID = (function () {
3+
'use strict';
4+
5+
class libUUID {
6+
static base64Chars = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
7+
static base = 64;
8+
static previousTime = 0;
9+
static counter = new Array(12).fill(0);
10+
static toBase64(num, length) {
11+
let result = "";
12+
for (let i = 0; i < length; i++) {
13+
result = this.base64Chars[num % this.base] + result;
14+
num = Math.floor(num / this.base);
15+
}
16+
return result;
17+
}
18+
;
19+
static generateRandomBase64(length) {
20+
let result = "";
21+
for (let i = 0; i < length; i++) {
22+
result += this.base64Chars[Math.floor(Math.random() * this.base)];
23+
}
24+
return result;
25+
}
26+
;
27+
static generateUUID() {
28+
const currentTime = Date.now();
29+
const timeBase64 = this.toBase64(currentTime, 8);
30+
let randomOrCounterBase64 = "";
31+
if (currentTime === this.previousTime) {
32+
// Increment the counter
33+
for (let i = this.counter.length - 1; i >= 0; i--) {
34+
this.counter[i]++;
35+
if (this.counter[i] < this.base) {
36+
break;
37+
}
38+
else {
39+
this.counter[i] = 0;
40+
}
41+
}
42+
randomOrCounterBase64 = this.counter.map(index => this.base64Chars[index]).join("");
43+
}
44+
else {
45+
// Generate new random values and initialize counter with random starting values
46+
randomOrCounterBase64 = this.generateRandomBase64(12);
47+
// Initialize counter with random values instead of zeros to avoid hyphen-heavy sequences
48+
for (let i = 0; i < this.counter.length; i++) {
49+
this.counter[i] = Math.floor(Math.random() * this.base);
50+
}
51+
this.previousTime = currentTime;
52+
}
53+
return timeBase64 + randomOrCounterBase64;
54+
}
55+
;
56+
static generateRowID() {
57+
return this.generateUUID().replace(/_/g, "Z");
58+
}
59+
;
60+
}
61+
62+
return libUUID;
63+
64+
})();

libUUID/eslint.config.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { defineConfig } from "eslint/config"; // While not strictly necessary, it's good practice.
2+
import stylistic from "@stylistic/eslint-plugin";
3+
import jslint from "@eslint/js";
4+
import tslint from "typescript-eslint";
5+
6+
export default defineConfig(
7+
{
8+
ignores: ["**/[0-9]*.[0-9]*.[0-9]*/", "*.d.ts", "dist/**", "build/**", "node_modules/**"],
9+
},
10+
jslint.configs.recommended,
11+
...tslint.configs.recommended,
12+
{
13+
plugins: {
14+
"@stylistic": stylistic,
15+
},
16+
rules: {
17+
"@stylistic/quotes": ["error", "double"],
18+
"@stylistic/semi": ["error", "always"],
19+
"@stylistic/indent": ["error", 2],
20+
},
21+
},
22+
);

0 commit comments

Comments
 (0)