Skip to content

Commit

Permalink
fix: more robust tweet poll parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Feb 9, 2020
1 parent 1d7db40 commit 0dcb359
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/common/parse-tweet-file-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const EOL = require("os").EOL;

const { parseTweet } = require("twitter-text");

const OPTION_REGEX = /^\(\s?\)\s+/;

function parseTweetFileContent(text) {
const pollOptions = [];
let lastLine;
while ((lastLine = getlastLineMatchingPollOption(text))) {
pollOptions.push(lastLine.replace(/\( \)\s+/, ""));
pollOptions.push(lastLine.replace(OPTION_REGEX, ""));
text = withLastLineRemoved(text);
}

Expand All @@ -22,7 +24,7 @@ function parseTweetFileContent(text) {
function getlastLineMatchingPollOption(text) {
const lines = text.trim().split(EOL);
const [lastLine] = lines.reverse();
return /^\( \) /.test(lastLine) ? lastLine : null;
return OPTION_REGEX.test(lastLine) ? lastLine : null;
}

function withLastLineRemoved(text) {
Expand Down

0 comments on commit 0dcb359

Please sign in to comment.