Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Switch to RegExp.test
We only need a boolean
  • Loading branch information
XhmikosR committed Mar 20, 2020
commit 00a035a02636fb4b07f040d99a30bd36fa1b000f
9 changes: 6 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { getLiveUpdates } = require('./lib/reddit.js');

const app = express();
const port = process.env.PORT || 3001;
const IS_CURL_RE = /\bcurl\b/gim;

function errorHandler(error, res) {
console.error(error);
Expand All @@ -24,7 +25,7 @@ app.use((req, res, next) => {
});

app.get('/', (req, res) => {
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gim) !== null;
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
const format = req.query.format ? req.query.format : '';
const minimal = req.query.minimal === 'true';
const emojis = req.query.emojis === 'true';
Expand All @@ -43,8 +44,9 @@ app.get('/', (req, res) => {
});

app.get('/updates', (req, res) => {
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
const format = req.query.format ? req.query.format : '';
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gim) !== null;

if (format.toLowerCase() === 'json') {
return getLiveUpdates({ json: true, isCurl }).then(result => {
return res.json(result);
Expand All @@ -58,10 +60,11 @@ app.get('/updates', (req, res) => {

app.get('/:country', (req, res) => {
const { country } = req.params;
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gim) !== null;
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
const format = req.query.format ? req.query.format : '';
const minimal = req.query.minimal === 'true';
const emojis = req.query.emojis === 'true';

if (!country || country.toUpperCase() === 'ALL') {
if (format.toLowerCase() === 'json') {
return getJSONData().then(result => {
Expand Down