-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
33 lines (25 loc) · 827 Bytes
/
parser.js
File metadata and controls
33 lines (25 loc) · 827 Bytes
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
'use strict';
const fs = require('fs');
const util = require('util');
const read = util.promisify(fs.readFile);
const write = util.promisify(fs.writeFile);
const parse = async filename => {
const data = await read(filename, 'utf8');
const [, ...lines] = data.split('\n');
const dataset = new Map();
// delete last empty line
lines.pop();
lines.forEach((line, id) => {
const [orientation,, ...tags] = line.split(' ');
const vertical = orientation === 'V';
dataset.set(id, { id, vertical, tags });
});
return dataset;
};
const save = async (filename, transitions) => {
const count = transitions.length;
const text = transitions.map(({ ids }) => `${ids.join(' ')}`).join('\n');
const data = `${count}\n${text}`;
await write(filename, data, 'utf8');
};
module.exports = { parse, save };