-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
52 lines (45 loc) · 1.57 KB
/
index.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
const protocolRegex = /.*?irealb:\/\/([^"]*)/;
const musicPrefix = "1r34LbKcu7";
const unscramble = require('./unscramble');
const parser = require('./Parser');
function parseMusic(data) {
const parts = data.split(musicPrefix);
return parser(unscramble.ireal(parts[1]));
}
function makeSong(data) {
const parts = data.split(/=+/).filter(x => x != ""); //split on one or more equal signs, remove the blanks
let title, composer, style, key, transpose, music, compStyle, bpm, repeats = null;
if (parts.length === 7) {
[title, composer, style, key, music, bpm, repeats] = parts;
}
if (parts.length === 8 && parts[4].startsWith(musicPrefix)) {
[title, composer, style, key, music, compStyle, bpm, repeats] = parts;
}
if (parts.length === 8 && parts[5].startsWith(musicPrefix)) {
[title, composer, style, key, transpose, music, bpm, repeats] = parts;
}
if (parts.length === 9) {
[title, composer, style, key, transpose, music, compStyle, bpm, repeats] = parts;
}
return {
title,
composer,
style,
key,
transpose: transpose ? parseInt(transpose) : null,
music: parseMusic(music),
compStyle,
bpm: bpm ? parseInt(bpm) : null,
repeats: repeats ? parseInt(repeats) : null
}
}
module.exports = function (data) {
const percentEncoded = protocolRegex.exec(data);
const percentDecoded = decodeURIComponent(percentEncoded[1]);
const parts = percentDecoded.split("==="); //songs are separated by ===
return {
name: (parts.length > 1) ? parts.pop() : undefined,
songs: parts.map(x => makeSong(x))
}
};