2020-08-27 11:53:24 +02:00
|
|
|
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, "")
|
2021-11-09 09:01:40 +01:00
|
|
|
.replace(/\s{2,}/, " ")
|
2020-08-27 11:53:24 +02:00
|
|
|
.replace(/ /g, "_")
|
2021-11-09 09:01:40 +01:00
|
|
|
.replace(/[^a-z0-9_]/g, "")
|
|
|
|
.replace(/_{2,}/, "");
|
2020-08-27 11:53:24 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 10:24:11 +01:00
|
|
|
function autoUpdateUsername(
|
|
|
|
actor: IActor,
|
|
|
|
newDisplayName: string | null
|
|
|
|
): IActor {
|
2020-11-27 19:27:44 +01:00
|
|
|
const actor2 = { ...actor };
|
|
|
|
const oldUsername = convertToUsername(actor.name);
|
|
|
|
|
|
|
|
if (actor.preferredUsername === oldUsername) {
|
|
|
|
actor2.preferredUsername = convertToUsername(newDisplayName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return actor2;
|
|
|
|
}
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
function validateUsername(actor: IActor): boolean {
|
|
|
|
return actor.preferredUsername === convertToUsername(actor.preferredUsername);
|
|
|
|
}
|
|
|
|
|
|
|
|
export { autoUpdateUsername, convertToUsername, validateUsername };
|