UNPKG

3.26 kBJavaScriptView Raw
1'use strict';
2
3const areIDsSame = require('./utils').areIDsSame;
4
5class 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 // Since OBJECT and GROUP are reserved keywords, this won't collide with attribute keys
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
35class 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
44class PVLObject extends PVLAggregate {
45 constructor (identifier) {
46 super();
47 this.identifier = identifier;
48 this.type = 'OBJECT';
49 }
50}
51
52class PVLGroup extends PVLAggregate {
53 constructor (identifier) {
54 super();
55 this.identifier = identifier;
56 this.type = 'GROUP';
57 }
58}
59
60class PVLValue {}
61
62class PVLScalar extends PVLValue {
63 constructor (value) {
64 super(value);
65 this.value = value;
66 }
67}
68
69// class PVLSequence extends PVLValue {
70// constructor (value) {
71// super()
72// this.type = 'sequence'
73// }
74// }
75
76// class PVLSet extends PVLValue {
77// constructor (value) {
78// super()
79// this.type = 'set'
80// }
81// }
82
83class 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
93class 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// class PVLDate extends PVLScalar {
103// constructor (value) {
104// super(value)
105// this.type = 'date'
106// }
107// }
108
109// class PVLTime extends PVLScalar {
110// constructor (value) {
111// super(value)
112// this.type = 'time'
113// }
114// }
115
116class 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
124module.exports = {
125 PVLAggregate: PVLAggregate,
126 PVLRoot: PVLRoot,
127 PVLObject: PVLObject,
128 PVLGroup: PVLGroup,
129 PVLNumeric: PVLNumeric,
130 PVLDateTime: PVLDateTime,
131 PVLTextString: PVLTextString
132};