utils: clean up

This commit is contained in:
wukko 2024-05-15 21:56:43 +06:00
parent dd77835599
commit 13524a4aa1
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2

View file

@ -1,30 +1,43 @@
const forbiddenCharsString = ['}', '{', '%', '>', '<', '^', ';', '`', '$', '"', "@", '=']; const forbiddenCharsString = ['}', '{', '%', '>', '<', '^', ';', '`', '$', '"', "@", '='];
export function metadataManager(obj) { export function metadataManager(obj) {
let keys = Object.keys(obj); const keys = Object.keys(obj);
let tags = ["album", "composer", "genre", "copyright", "encoded_by", "title", "language", "artist", "album_artist", "performer", "disc", "publisher", "track", "encoder", "compilation", "date", "creation_time", "comment"] const tags = [
"album",
"copyright",
"title",
"artist",
"track",
"date"
]
let commands = [] let commands = []
for (let i in keys) { if (tags.includes(keys[i])) commands.push('-metadata', `${keys[i]}=${obj[keys[i]]}`) } for (const i in keys) {
if (tags.includes(keys[i]))
commands.push('-metadata', `${keys[i]}=${obj[keys[i]]}`)
}
return commands; return commands;
} }
export function cleanString(string) { export function cleanString(string) {
for (let i in forbiddenCharsString) { for (const i in forbiddenCharsString) {
string = string.replaceAll("/", "_").replaceAll(forbiddenCharsString[i], '') string = string.replaceAll("/", "_")
.replaceAll(forbiddenCharsString[i], '')
} }
return string; return string;
} }
export function verifyLanguageCode(code) { export function verifyLanguageCode(code) {
return RegExp(/[a-z]{2}/).test(String(code.slice(0, 2).toLowerCase())) ? String(code.slice(0, 2).toLowerCase()) : "en" const languageCode = String(code.slice(0, 2).toLowerCase());
if (RegExp(/[a-z]{2}/).test(languageCode)) {
return languageCode
}
return "en"
} }
export function languageCode(req) { export function languageCode(req) {
return req.header('Accept-Language') ? verifyLanguageCode(req.header('Accept-Language')) : "en" if (req.header('Accept-Language')) {
return verifyLanguageCode(req.header('Accept-Language'))
} }
export function unicodeDecode(str) { return "en"
return str.replace(/\\u[\dA-F]{4}/gi, (unicode) => {
return String.fromCharCode(parseInt(unicode.replace(/\\u/g, ""), 16));
});
} }
export function cleanHTML(html) { export function cleanHTML(html) {
let clean = html.replace(/ {4}/g, ''); let clean = html.replace(/ {4}/g, '');