parse.js
566 Bytes
function buffer2(xs) {
const ys = xs.filter((_, i) => !(i % 2));
const zs = xs.filter((_, i) => i % 2);
let ws = [];
for (let i = 0; i < ys.length; ++i) {
ws.push([ys[i], zs[i]]);
}
return ws;
}
function parse(raw) {
const [MAX_CAPACITY, rawRequests] = raw.split(/\r?\n/);
const requests = buffer2(rawRequests.split(" ").map(v => parseInt(v))).map(
([id, size]) =>
size
? { type: "allocate", payload: { id, size } }
: { type: "free", payload: id }
);
return { MAX_CAPACITY, requests }
}
module.exports = parse;