mirror of
https://github.com/uetchy/namae.git
synced 2025-08-20 18:08:11 +09:00
chore: shuffle example words
This commit is contained in:
21
web/src/util/array.ts
Normal file
21
web/src/util/array.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export function shuffleArray<T>(array: T[]): T[] {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
const temp = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = temp;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
export function sampleFromArray<T>(array: T[], maximum: number): T[] {
|
||||
return shuffleArray(array).slice(0, maximum);
|
||||
}
|
||||
|
||||
export function fillArray<T>(array: T[], filler: string, maximum: number): T[] {
|
||||
const deficit = maximum - array.length;
|
||||
if (deficit > 0) {
|
||||
array = [...array, ...Array(deficit).fill(filler)];
|
||||
}
|
||||
return array;
|
||||
}
|
@@ -6,3 +6,23 @@ export function capitalize(text: string): string {
|
||||
export function sanitize(text: string): string {
|
||||
return text.replace(/[\s@+!#$%^&*()[\]./<>{}]/g, '');
|
||||
}
|
||||
|
||||
export function upper(word: string): string {
|
||||
return word.toUpperCase();
|
||||
}
|
||||
|
||||
export function lower(word: string): string {
|
||||
return word.toLowerCase();
|
||||
}
|
||||
|
||||
export function stem(word: string): string {
|
||||
return word.replace(/[aiueo]$/, '');
|
||||
}
|
||||
|
||||
export function germanify(word: string): string {
|
||||
return word.replace('c', 'k').replace('C', 'K');
|
||||
}
|
||||
|
||||
export function njoin(lhs: string, rhs: string): string {
|
||||
return lhs + rhs.replace(new RegExp(`^${lhs[-1]}`, 'i'), '');
|
||||
}
|
||||
|
Reference in New Issue
Block a user