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