From 449f768dce23d9c07c7e28d3a1424f96ff45b166 Mon Sep 17 00:00:00 2001 From: Peter Kaukov Date: Tue, 23 Apr 2019 14:50:20 +0300 Subject: [PATCH 1/2] Update PBN parser to fix comment separation Currently, when a multi-line comment starting with { and immediately followed by content, but the content ends a few lines below, OR a comment that is part of the data, is not parsed properly. This commit fixes this issue and the document is parsed properly. --- lib/pbn-stream.js | 12 +++++++----- package.json | 6 ++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/pbn-stream.js b/lib/pbn-stream.js index a095dfa..de6edd9 100644 --- a/lib/pbn-stream.js +++ b/lib/pbn-stream.js @@ -54,7 +54,7 @@ function parseDeal(s) { PbnStream.prototype._transform = function(line, encoding, done) { if (this.comment) { - if (line[0] === '}') { + if (line.endsWith('}')) { this.produce(this.comment); this.comment = undefined; } @@ -117,13 +117,14 @@ PbnStream.prototype._transform = function(line, encoding, done) { this.produce(null); this.ingame = false; } - else if (line[0] === '{') { + else if (line.includes('{')) { + const braceIndex = line.indexOf('{') this.comment = { type: 'comment', text: '' }; - let rest = line.slice(1).trim(); - if (rest[rest.length - 1] === '}') { + let rest = line.slice(braceIndex + 1).trim(); + if (rest.endsWith('}')) { this.comment.text = rest.slice(0, -1).trim(); this.produce(this.comment); this.comment = undefined; @@ -162,8 +163,9 @@ PbnStream.prototype.produce = function(data) { this.produce(tag); } - if (data !== null) + if (data !== null) { this.push(data); + } }; let pbn = opts => pipe(lines(), new PbnStream(opts)); diff --git a/package.json b/package.json index da5a8d5..3fdb2b8 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,12 @@ "name": "Richard Schneider", "email": "makaretu@gmail.com" }, + "contributors": [ + { + "name": "Peter Kaukov", + "email": "peter.kaukov@gmail.com" + } + ], "repository": { "type": "git", "url": "https://github.com/richardschneider/pbn" From f7b05ae973df15842977d92cbf141560d0b594c1 Mon Sep 17 00:00:00 2001 From: Peter Kaukov Date: Tue, 23 Apr 2019 14:54:27 +0300 Subject: [PATCH 2/2] Fix test --- lib/pbn-stream.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pbn-stream.js b/lib/pbn-stream.js index de6edd9..2e54ebe 100644 --- a/lib/pbn-stream.js +++ b/lib/pbn-stream.js @@ -118,7 +118,7 @@ PbnStream.prototype._transform = function(line, encoding, done) { this.ingame = false; } else if (line.includes('{')) { - const braceIndex = line.indexOf('{') + let braceIndex = line.indexOf('{'); this.comment = { type: 'comment', text: ''