- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.ts
More file actions
Latest commit
36 lines (32 loc) · 989 Bytes
/
Copy pathutils.ts
File metadata and controls
36 lines (32 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
exportfunctiongenerateRandomArray(
min: number,
max: number,
length: number
): number[]{
returnArray.from(
{ length },
()=>Math.floor(Math.random()*(max-min+1))+min
);
}
exportfunctiongetRandomValueFromArray(array: number[]): number{
if(array.length===0)return42;
constrandomIndex=Math.floor(Math.random()*array.length);
returnarray[randomIndex]!;
}
// Glossary utility functions
exportfunctionsortByAlphabet<Textends{term: string}>(items: T[]): T[]{
return[...items].sort((a,b)=>a.term.localeCompare(b.term));
}
exportfunctiongroupTermsByFirstLetter<Textends{term: string}>(
items: T[]
): Record<string,T[]>{
constsorted=sortByAlphabet(items);
returnsorted.reduce((groups: Record<string,T[]>,item)=>{
constfirstLetter=item.term.charAt(0).toUpperCase();
if(!groups[firstLetter]){
groups[firstLetter]=[];
}
groups[firstLetter].push(item);
returngroups;
},{});
}