cyp/app/js/parser.js

65 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-03-22 22:35:04 +08:00
export function linesToStruct(lines) {
let result = {};
lines.forEach(line => {
let cindex = line.indexOf(":");
if (cindex == -1) { throw new Error(`Malformed line "${line}"`); }
2019-03-25 22:49:23 +08:00
let key = line.substring(0, cindex);
let value = line.substring(cindex+2);
if (key in result) {
let old = result[key];
if (old instanceof Array) {
old.push(value);
} else {
result[key] = [old, value];
}
} else {
result[key] = value;
}
2019-03-22 22:35:04 +08:00
});
return result;
}
export function songList(lines) {
let songs = [];
let batch = [];
while (lines.length) {
let line = lines[0];
if (line.startsWith("file:") && batch.length) {
let song = linesToStruct(batch);
songs.push(song);
2019-03-25 22:49:23 +08:00
batch = [];
2019-03-22 22:35:04 +08:00
}
batch.push(lines.shift());
}
if (batch.length) {
let song = linesToStruct(batch);
songs.push(song);
}
return songs;
2019-03-25 22:49:23 +08:00
}
export function pathContents(lines) {
const prefixes = ["file", "directory", "playlist"];
let batch = [];
let result = {};
let batchPrefix = null;
prefixes.forEach(prefix => result[prefix] = []);
while (lines.length) {
let line = lines[0];
let prefix = line.split(":")[0];
if (prefixes.includes(prefix)) { // begin of a new batch
if (batch.length) { result[batchPrefix].push(linesToStruct(batch)); }
batchPrefix = prefix;
batch = [];
}
batch.push(lines.shift());
}
if (batch.length) { result[batchPrefix].push(linesToStruct(batch)); }
return result;
2019-03-22 22:35:04 +08:00
}