Skip to content

Commit cf21329

Browse files
committed
feat: package now creates IDs just like YouTube
1 parent 91c1b37 commit cf21329

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

.changeset/fast-cups-cry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ytid": minor
3+
---
4+
5+
package now creates IDs just like YouTube

index.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export function ytid(){
2+
const characters = 'ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789-_';
3+
const length = 11;
4+
let id = '';
5+
6+
for (let i = 0; i < length; i++) {
7+
const randomIndex = Math.floor(Math.random() * characters.length);
8+
const selectedCharacter = characters[randomIndex];
9+
10+
// Preventing identical three or more consecutive characters
11+
if (i >= 2 && id[i - 1] === selectedCharacter && id[i - 2] === selectedCharacter) {
12+
i--;
13+
continue;
14+
}
15+
16+
// Preventing two consecutive underscores and hyphens
17+
if ((selectedCharacter === '_' && id[i - 1] === '_') || (selectedCharacter === '-' && id[i - 1] === '-')) {
18+
i--;
19+
continue;
20+
}
21+
22+
id += selectedCharacter;
23+
}
24+
25+
return id;
26+
}

0 commit comments

Comments
 (0)