-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
35 lines (29 loc) · 1.04 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const status = require('./status');
const mainRegex = /^([a-zA-Z0-9$ ]+?)( (feat|ft)\.? ?([a-zA-Z0-9$ ]+?( x [a-zA-Z0-9$ ]+?)*))?( (prod\.? ?by|prod)\.? ?([a-zA-Z0-9$ ]+?( x [a-zA-Z0-9$ ]+?)*))? *?(.mp3)$/;
const prepare = (str) => {
return str.replace(/(\[|\]|\(|\))/g,'').replace(/[ _]+/g, ' ');
}
const test = (str) => {
const [artist, rawTitle] = str.toLowerCase().split(/ +- +/);
const preparedTitle = prepare(rawTitle);
return mainRegex.test(preparedTitle);
}
const rename = (str, cur, max) => {
status.process(str, cur, max);
const [artist, rawTitle] = str.toLowerCase().split(/ +- +/);
const preparedTitle = prepare(rawTitle);
const title = preparedTitle
.replace(mainRegex,
(...x) => {
if (x[4] && x[8]) return `${x[1]} (w/ ${x[4]}) (p/ ${x[8]})`;
if (x[4]) return `${x[1]} (w/ ${ x[4]})`;
if (x[8]) return `${x[1]} (p/ ${ x[8]})`;
return x[1];
});
return {
artist,
title,
filename: `${artist} - ${title}.mp3`.replace(/\//g,'')
}
}
module.exports = { rename, test };