typescript on both client and server with communication via socket.io
This commit is contained in:
12
tools/getRandomItems.js
Normal file
12
tools/getRandomItems.js
Normal file
@@ -0,0 +1,12 @@
|
||||
export default function getRandomItems(items, count) {
|
||||
const shuffled = [...items];
|
||||
|
||||
// Fisher-Yates shuffle
|
||||
for (let i = shuffled.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
||||
}
|
||||
|
||||
return count > shuffled.length ? shuffled : shuffled.slice(0, count);
|
||||
}
|
||||
|
||||
11
tools/getRandomItems.ts
Normal file
11
tools/getRandomItems.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export default function getRandomItems<T>(items: T[], count: number): T[] {
|
||||
const shuffled = [...items];
|
||||
|
||||
// Fisher-Yates shuffle
|
||||
for (let i = shuffled.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
||||
}
|
||||
|
||||
return count > shuffled.length ? shuffled : shuffled.slice(0, count);
|
||||
}
|
||||
8
tools/simpleID.ts
Normal file
8
tools/simpleID.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { customAlphabet } from 'nanoid';
|
||||
|
||||
const alphabet =
|
||||
'0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
|
||||
const generateID = (length: number = 6) => customAlphabet(alphabet, length);
|
||||
|
||||
export default generateID;
|
||||
Reference in New Issue
Block a user