1 | 'use strict';
|
2 |
|
3 | const areIDsSame = require('./utils').areIDsSame;
|
4 |
|
5 | class PVLAggregate {
|
6 | constructor () {
|
7 | this.store = [];
|
8 | }
|
9 |
|
10 | add (key, value) {
|
11 | this.store.push([key, value]);
|
12 | return this;
|
13 | }
|
14 | get (key) { return this.store.find((item) => item[0] === key) ? this.store.find((item) => item[0] === key)[1] : null; }
|
15 | getAll (key) { return this.store.filter((item) => item[0] === key).map((item) => item[1]); }
|
16 | removeAll (key) { this.store = this.store.filter((item) => item[0] !== key); }
|
17 |
|
18 |
|
19 | addAggregate (aggregate) {
|
20 | this.store.push([aggregate.type, aggregate]);
|
21 | return this;
|
22 | }
|
23 | objects (key) { return this.getAll('OBJECT').filter((o) => key ? areIDsSame(o.identifier, key) : true); }
|
24 | groups (key) { return this.getAll('GROUP').filter((g) => key ? areIDsSame(g.identifier, key) : true); }
|
25 | aggregates (key) { return this.objects(key).concat(this.groups(key)); }
|
26 |
|
27 | toPVL () {
|
28 | return this.store.reduce(
|
29 | (last, curr) => last.concat(`${curr[0]} = ${curr[1].toPVL()};\n`),
|
30 | `${this.identifier};\n`
|
31 | ).concat(`END_${this.type} = ${this.identifier}`);
|
32 | }
|
33 | }
|
34 |
|
35 | class PVLRoot extends PVLAggregate {
|
36 | constructor () {
|
37 | super();
|
38 | this.type = 'ROOT';
|
39 | this.depth = 0;
|
40 | }
|
41 | toPVL () { return this.store.reduce((last, curr) => last.concat(`${curr[0]} = ${curr[1].toPVL()};\n`), ''); }
|
42 | }
|
43 |
|
44 | class PVLObject extends PVLAggregate {
|
45 | constructor (identifier) {
|
46 | super();
|
47 | this.identifier = identifier;
|
48 | this.type = 'OBJECT';
|
49 | }
|
50 | }
|
51 |
|
52 | class PVLGroup extends PVLAggregate {
|
53 | constructor (identifier) {
|
54 | super();
|
55 | this.identifier = identifier;
|
56 | this.type = 'GROUP';
|
57 | }
|
58 | }
|
59 |
|
60 | class PVLValue {}
|
61 |
|
62 | class PVLScalar extends PVLValue {
|
63 | constructor (value) {
|
64 | super(value);
|
65 | this.value = value;
|
66 | }
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | class PVLNumeric extends PVLScalar {
|
84 | constructor (value, units) {
|
85 | super(value);
|
86 | this.value = Number(this.value);
|
87 | if (typeof units === 'string') { this.units = units.toUpperCase(); }
|
88 | this.type = 'numeric';
|
89 | }
|
90 | toPVL () { return this.units ? `${this.value} <${this.units}>` : `${this.value}`; }
|
91 | }
|
92 |
|
93 | class PVLDateTime extends PVLScalar {
|
94 | constructor (value) {
|
95 | super(value);
|
96 | this.value = new Date(this.value);
|
97 | this.type = 'date time';
|
98 | }
|
99 | toPVL () { return this.value.toISOString(); }
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | class PVLTextString extends PVLScalar {
|
117 | constructor (value) {
|
118 | super(value);
|
119 | this.type = 'text string';
|
120 | }
|
121 | toPVL () { return this.value.includes('"') ? `'${this.value}'` : `"${this.value}"`; }
|
122 | }
|
123 |
|
124 | module.exports = {
|
125 | PVLAggregate: PVLAggregate,
|
126 | PVLRoot: PVLRoot,
|
127 | PVLObject: PVLObject,
|
128 | PVLGroup: PVLGroup,
|
129 | PVLNumeric: PVLNumeric,
|
130 | PVLDateTime: PVLDateTime,
|
131 | PVLTextString: PVLTextString
|
132 | };
|