@cumulus/pvl 16.0.2-alpha.0 → 16.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/.nycrc.json +11 -0
  2. package/index.js +103 -0
  3. package/package.json +4 -3
  4. package/t.js +0 -103
package/.nycrc.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../nyc.config.js",
3
+ "include": [
4
+ "*.js",
5
+ "lib/*.js"
6
+ ],
7
+ "statements": 93.0,
8
+ "functions": 92.0,
9
+ "branches": 88.0,
10
+ "lines": 93.0
11
+ }
package/index.js CHANGED
@@ -0,0 +1,103 @@
1
+ 'use strict';
2
+
3
+ const PVLRoot = require('./lib/models').PVLRoot;
4
+ const PVLObject = require('./lib/models').PVLObject;
5
+ const PVLGroup = require('./lib/models').PVLGroup;
6
+ const PVLNumeric = require('./lib/models').PVLNumeric;
7
+ const PVLDateTime = require('./lib/models').PVLDateTime;
8
+ const PVLTextString = require('./lib/models').PVLTextString;
9
+ const patterns = require('./lib/patterns');
10
+ const checkRegexes = require('./lib/utils').checkRegexes;
11
+
12
+ function parseValue(value, key) {
13
+ const numericValue = checkRegexes(value, patterns.numericPatterns);
14
+ if (numericValue !== null) return new PVLNumeric(numericValue);
15
+
16
+ const dateTimeValue = checkRegexes(value, patterns.dateTimePatterns);
17
+ if (dateTimeValue !== null) return new PVLDateTime(dateTimeValue);
18
+
19
+ const textStringValue = checkRegexes(value, patterns.textStringPatterns);
20
+ if (textStringValue !== null) return new PVLTextString(textStringValue);
21
+
22
+ throw new Error(`Failed to parse value ('${value}') of ${key}`);
23
+ }
24
+
25
+ function pvlToJS(pvlString) {
26
+ const result = new PVLRoot();
27
+ // Keep track of which aggregate is "active" in the stack,
28
+ // as far as assigning further attributes and aggregates
29
+ const aggregates = [result];
30
+
31
+ // Split into statements
32
+ // Currently assumes single-line statements, not allowing multi-line values
33
+ const pvlStatements = pvlString
34
+ .split('\n')
35
+ .map((s) => s.trim())
36
+ // Strip statement-ending semicolons
37
+ .map((s) => s.replace(/;$/, ''))
38
+ // Ignore blank lines
39
+ .filter((s) => s !== '')
40
+ // Ignore full-line comments
41
+ .filter((s) => !(s.startsWith('/*') && s.endsWith('*/')));
42
+
43
+ let keyAndValue;
44
+ let key;
45
+ let aggregate;
46
+ let s = pvlStatements.shift();
47
+ while (s !== undefined && s !== 'END') {
48
+ keyAndValue = s.split(/ = /);
49
+ key = keyAndValue[0].trim();
50
+ // Need to account for string-embedded `=`s
51
+ let value = keyAndValue.slice(1).join(' = ').trim();
52
+
53
+ if (['BEGIN_GROUP', 'GROUP', 'BEGIN_OBJECT', 'OBJECT'].includes(key)) {
54
+ // Group names _can_ be wrapped in quotes
55
+ value = value.replace(/["']/g, '');
56
+ aggregate = key.includes('GROUP') ? new PVLGroup(value) : new PVLObject(value);
57
+ aggregates[aggregates.length - 1].addAggregate(aggregate);
58
+ aggregates.push(aggregate);
59
+ } else if (['END_OBJECT', 'END_GROUP'].includes(key)) {
60
+ aggregates.pop();
61
+ } else {
62
+ aggregates[aggregates.length - 1].add(key, parseValue(value, key));
63
+ }
64
+
65
+ s = pvlStatements.shift();
66
+ }
67
+
68
+ return result;
69
+ }
70
+
71
+ function jsToPVL(pvlObject) {
72
+ const stringified = pvlObject.toPVL();
73
+ const INDENTATION_WIDTH = 2;
74
+
75
+ // Spec doesn't require indentation, but does highly recommended it
76
+ let depth = 0;
77
+ const indented = stringified.split('\n').map((s) => {
78
+ if (s.match(/^END_(GROUP|OBJECT)( = .+)?$/)) {
79
+ depth -= 1;
80
+ }
81
+ const thisLine = `${' '.repeat(depth * INDENTATION_WIDTH)}${s}`;
82
+ if (s.match(/^(BEGIN_)?(GROUP|OBJECT) = .+$/)) {
83
+ depth += 1;
84
+ }
85
+ return thisLine;
86
+ }).join('\n');
87
+
88
+ return indented;
89
+ }
90
+
91
+ module.exports = {
92
+ pvlToJS: pvlToJS,
93
+ jsToPVL: jsToPVL,
94
+ parseValue: parseValue,
95
+ models: {
96
+ PVLRoot: PVLRoot,
97
+ PVLObject: PVLObject,
98
+ PVLGroup: PVLGroup,
99
+ PVLNumeric: PVLNumeric,
100
+ PVLDateTime: PVLDateTime,
101
+ PVLTextString: PVLTextString,
102
+ },
103
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cumulus/pvl",
3
- "version": "16.0.2-alpha.0",
3
+ "version": "16.1.0",
4
4
  "description": "Parse and serialize Parameter Value Language, a data markup language used by NASA",
5
5
  "main": "index.js",
6
6
  "engine": {
@@ -8,7 +8,8 @@
8
8
  },
9
9
  "scripts": {
10
10
  "test": "../../node_modules/.bin/ava",
11
- "test:coverage": "../../node_modules/.bin/nyc npm test"
11
+ "test:coverage": "../../node_modules/.bin/nyc npm test",
12
+ "coverage": "python ../../scripts/coverage_handler/coverage.py"
12
13
  },
13
14
  "ava": {
14
15
  "serial": true,
@@ -33,5 +34,5 @@
33
34
  "dependencies": {
34
35
  "lodash": "^4.17.21"
35
36
  },
36
- "gitHead": "215833a8dbf46eff436099c9d056cf697df9903f"
37
+ "gitHead": "324e02b1396cd5510c9a9c54d01afd5d1c13064e"
37
38
  }
package/t.js DELETED
@@ -1,103 +0,0 @@
1
- 'use strict';
2
-
3
- const PVLRoot = require('./lib/models').PVLRoot;
4
- const PVLObject = require('./lib/models').PVLObject;
5
- const PVLGroup = require('./lib/models').PVLGroup;
6
- const PVLNumeric = require('./lib/models').PVLNumeric;
7
- const PVLDateTime = require('./lib/models').PVLDateTime;
8
- const PVLTextString = require('./lib/models').PVLTextString;
9
- const patterns = require('./lib/patterns');
10
- const checkRegexes = require('./lib/utils').checkRegexes;
11
-
12
- function parseValue(value, key) {
13
- const numericValue = checkRegexes(value, patterns.numericPatterns);
14
- if (numericValue !== null) return new PVLNumeric(numericValue);
15
-
16
- const dateTimeValue = checkRegexes(value, patterns.dateTimePatterns);
17
- if (dateTimeValue !== null) return new PVLDateTime(dateTimeValue);
18
-
19
- const textStringValue = checkRegexes(value, patterns.textStringPatterns);
20
- if (textStringValue !== null) return new PVLTextString(textStringValue);
21
-
22
- throw new Error(`Failed to parse value ('${value}') of ${key}`);
23
- }
24
-
25
- function pvlToJS(pvlString) {
26
- const result = new PVLRoot();
27
- // Keep track of which aggregate is "active" in the stack,
28
- // as far as assigning further attributes and aggregates
29
- const aggregates = [result];
30
-
31
- // Split into statements
32
- // Currently assumes single-line statements, not allowing multi-line values
33
- const pvlStatements = pvlString
34
- .split('\n')
35
- .map((s) => s.trim())
36
- // Strip statement-ending semicolons
37
- .map((s) => s.replace(/;$/, ''))
38
- // Ignore blank lines
39
- .filter((s) => s !== '')
40
- // Ignore full-line comments
41
- .filter((s) => !(s.startsWith('/*') && s.endsWith('*/')));
42
-
43
- let keyAndValue;
44
- let key;
45
- let aggregate;
46
- let s = pvlStatements.shift();
47
- while (s !== undefined && s !== 'END') {
48
- keyAndValue = s.split(/ = /);
49
- key = keyAndValue[0].trim();
50
- // Need to account for string-embedded `=`s
51
- let value = keyAndValue.slice(1).join(' = ').trim();
52
-
53
- if (['BEGIN_GROUP', 'GROUP', 'BEGIN_OBJECT', 'OBJECT'].includes(key)) {
54
- // Group names _can_ be wrapped in quotes
55
- value = value.replace(/["']/g, '');
56
- aggregate = key.includes('GROUP') ? new PVLGroup(value) : new PVLObject(value);
57
- aggregates[aggregates.length - 1].addAggregate(aggregate);
58
- aggregates.push(aggregate);
59
- } else if (['END_OBJECT', 'END_GROUP'].includes(key)) {
60
- aggregates.pop();
61
- } else {
62
- aggregates[aggregates.length - 1].add(key, parseValue(value, key));
63
- }
64
-
65
- s = pvlStatements.shift();
66
- }
67
-
68
- return result;
69
- }
70
-
71
- function jsToPVL(pvlObject) {
72
- const stringified = pvlObject.toPVL();
73
- const INDENTATION_WIDTH = 2;
74
-
75
- // Spec doesn't require indentation, but does highly recommended it
76
- let depth = 0;
77
- const indented = stringified.split('\n').map((s) => {
78
- if (s.match(/^END_(GROUP|OBJECT)( = .+)?$/)) {
79
- depth -= 1;
80
- }
81
- const thisLine = `${' '.repeat(depth * INDENTATION_WIDTH)}${s}`;
82
- if (s.match(/^(BEGIN_)?(GROUP|OBJECT) = .+$/)) {
83
- depth += 1;
84
- }
85
- return thisLine;
86
- }).join('\n');
87
-
88
- return indented;
89
- }
90
-
91
- module.exports = {
92
- pvlToJS: pvlToJS,
93
- jsToPVL: jsToPVL,
94
- parseValue: parseValue,
95
- models: {
96
- PVLRoot: PVLRoot,
97
- PVLObject: PVLObject,
98
- PVLGroup: PVLGroup,
99
- PVLNumeric: PVLNumeric,
100
- PVLDateTime: PVLDateTime,
101
- PVLTextString: PVLTextString,
102
- },
103
- };