1
0
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:
2020-02-06 00:32:05 +09:00
parent 31c4a40ab1
commit 16d29b00f4
4 changed files with 78 additions and 59 deletions

21
web/src/util/array.ts Normal file
View 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;
}

View File

@@ -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'), '');
}