File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " ytid " : minor
3
+ ---
4
+
5
+ package now creates IDs just like YouTube
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments