Skip to content

Commit 56c17e3

Browse files
refactor: move handling of RP entries to a grammar postprocessor
1 parent a56f179 commit 56c17e3

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

src/grammar.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const zip =
1010
keys.reduce((o, k, i) =>
1111
(o[k] = values[i], o), {});
1212

13+
// Convert US date to date object
14+
// e.g. "06/30/2020" -> {year: "2020", month: "06", day: "30"}
15+
const from_mdy =
16+
str =>
17+
zip(['month', 'day', 'year'], str.split('/'));
18+
1319
const processName = (name) => {
1420
const initialsRegex = /(?:[a-zA-Z]\.)+/
1521
const [part1, part2 = '', part3 = ''] = name.split(',').map(s => s.trim());
@@ -54,6 +60,7 @@ var grammar = {
5460
{"name": "entry", "symbols": ["personEntry"], "postprocess": id},
5561
{"name": "entry", "symbols": ["urlEntry"], "postprocess": id},
5662
{"name": "entry", "symbols": ["dateaccessEntry"], "postprocess": id},
63+
{"name": "entry", "symbols": ["reprintEntry"], "postprocess": id},
5764
{"name": "entry", "symbols": ["otherEntry"], "postprocess": id},
5865
{"name": "personEntry", "symbols": [(lexer.has("person") ? {type: "person"} : person), (lexer.has("sep") ? {type: "sep"} : sep), "value"], "postprocess": ([{value: key},,name]) => ({key, value: processName(name)})},
5966
{"name": "urlEntry$ebnf$1", "symbols": ["value"]},
@@ -67,6 +74,12 @@ var grammar = {
6774
{"name": "dateaccessEntry", "symbols": [(lexer.has("dateaccess") ? {type: "dateaccess"} : dateaccess), (lexer.has("sep") ? {type: "sep"} : sep), "dateaccessEntry$ebnf$1"], "postprocess": ([{value: key},/*sep (ignored)*/, lines]) =>
6875
({ key
6976
, value: processUrls(lines.join(''))}) },
77+
{"name": "reprintEntry", "symbols": [(lexer.has("reprint") ? {type: "reprint"} : reprint), (lexer.has("sep") ? {type: "sep"} : sep), "value"], "postprocess": ([{value: key},/*sep (ignored)*/, value]) =>
78+
({ key
79+
, value: ((value === 'IN FILE' || value === 'NOT IN FILE')
80+
? { status: value }
81+
: { status: 'ON REQUEST'
82+
, date: from_mdy(value.match(/\d{2}\/\d{2}\/\d{4}/)[0]) })}) },
7083
{"name": "otherEntry$ebnf$1", "symbols": ["value"]},
7184
{"name": "otherEntry$ebnf$1", "symbols": ["otherEntry$ebnf$1", "value"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
7285
{"name": "otherEntry", "symbols": [(lexer.has("tag") ? {type: "tag"} : tag), (lexer.has("sep") ? {type: "sep"} : sep), "otherEntry$ebnf$1"], "postprocess": ([{value: key},,value]) => ({key, value: value.join(' ')})},

src/grammar.ne

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ const zip =
1111
keys.reduce((o, k, i) =>
1212
(o[k] = values[i], o), {});
1313

14+
// Convert US date to date object
15+
// e.g. "06/30/2020" -> {year: "2020", month: "06", day: "30"}
16+
const from_mdy =
17+
str =>
18+
zip(['month', 'day', 'year'], str.split('/'));
19+
1420
const processName = (name) => {
1521
const initialsRegex = /(?:[a-zA-Z]\.)+/
1622
const [part1, part2 = '', part3 = ''] = name.split(',').map(s => s.trim());
@@ -54,6 +60,7 @@ entry ->
5460
personEntry {% id %}
5561
| urlEntry {% id %}
5662
| dateaccessEntry {% id %}
63+
| reprintEntry {% id %}
5764
| otherEntry {% id %}
5865

5966
personEntry ->
@@ -73,6 +80,15 @@ dateaccessEntry ->
7380
({ key
7481
, value: processUrls(lines.join(''))}) %}
7582

83+
reprintEntry ->
84+
%reprint %sep value
85+
{% ([{value: key},/*sep (ignored)*/, value]) =>
86+
({ key
87+
, value: ((value === 'IN FILE' || value === 'NOT IN FILE')
88+
? { status: value }
89+
: { status: 'ON REQUEST'
90+
, date: from_mdy(value.match(/\d{2}\/\d{2}\/\d{4}/)[0]) })}) %}
91+
7692
otherEntry ->
7793
%tag %sep value:+
7894
{% ([{value: key},,value]) => ({key, value: value.join(' ')}) %}

src/lexer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const lexer =
1111
, type: /TY(?= - )/
1212
, person: /(?:AU|A1|A2|A3|A4|TA)(?= - )/
1313
, dateaccess: /DA(?= - )/
14+
, reprint: /RP(?= - )/
1415
, url: /UR(?= - )/
1516
, end: /ER(?= - )/
1617
, tag: /[A-Z][A-Z0-9](?= - )/

src/parser.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ const grammar = require('./grammar.js');
77
const type_map = require('./type-map.json');
88
const fields_map = require('./fields-map.json');
99

10-
const zip =
11-
(keys, values) =>
12-
keys.reduce((o, k, i) =>
13-
(o[k] = values[i], o), {});
14-
1510
const append =
1611
(acc, {key, value}) =>
1712
( acc[key] = (acc[key] || []).concat(value)
@@ -22,12 +17,6 @@ const add =
2217
( acc[key] = value
2318
, acc );
2419

25-
// Convert US date to date object
26-
// e.g. "06/30/2020" -> {year: "2020", month: "06", day: "30"}
27-
const from_mdy =
28-
str =>
29-
zip(['month', 'day', 'year'], str.split('/'));
30-
3120
const defaults =
3221
acc =>
3322
Object.assign
@@ -93,12 +82,7 @@ const OPS =
9382
, PY: add
9483
, RI: add
9584
, RN: add
96-
, RP: (acc, {value}) =>
97-
( acc.RP = (value === 'IN FILE' || value === 'NOT IN FILE')
98-
? { status: value }
99-
: { status: 'ON REQUEST'
100-
, date: from_mdy(value.match(/\d{2}\/\d{2}\/\d{4}/)[0]) }
101-
, acc )
85+
, RP: add
10286
, SE: add
10387
, SN: add
10488
, SP: add

0 commit comments

Comments
 (0)