2e72f6faf4
yarn v1 is being deprecated and starts to have some issues Signed-off-by: Thomas Citharel <tcit@tcit.fr>
36 lines
900 B
TypeScript
36 lines
900 B
TypeScript
import { IActor } from "@/types/actor";
|
|
|
|
function convertToUsername(value: string | null): string {
|
|
if (!value) return "";
|
|
|
|
// https://stackoverflow.com/a/37511463
|
|
return value
|
|
.toLocaleLowerCase()
|
|
.normalize("NFD")
|
|
.replace(/[\u0300-\u036f]/g, "")
|
|
.replace(/\s{2,}/, " ")
|
|
.replace(/ /g, "_")
|
|
.replace(/[^a-z0-9_]/g, "")
|
|
.replace(/_{2,}/, "");
|
|
}
|
|
|
|
function autoUpdateUsername(
|
|
actor: IActor,
|
|
newDisplayName: string | null
|
|
): IActor {
|
|
const actor2 = { ...actor };
|
|
const oldUsername = convertToUsername(actor.name);
|
|
|
|
if (actor.preferredUsername === oldUsername) {
|
|
actor2.preferredUsername = convertToUsername(newDisplayName);
|
|
}
|
|
|
|
return actor2;
|
|
}
|
|
|
|
function validateUsername(actor: IActor): boolean {
|
|
return actor.preferredUsername === convertToUsername(actor.preferredUsername);
|
|
}
|
|
|
|
export { autoUpdateUsername, convertToUsername, validateUsername };
|