Skip to content

Commit 00a035a

Browse files
committed
Switch to RegExp.test
We only need a boolean
1 parent bc6ad2a commit 00a035a

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

app.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { getLiveUpdates } = require('./lib/reddit.js');
88

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

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

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

4546
app.get('/updates', (req, res) => {
47+
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
4648
const format = req.query.format ? req.query.format : '';
47-
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gim) !== null;
49+
4850
if (format.toLowerCase() === 'json') {
4951
return getLiveUpdates({ json: true, isCurl }).then(result => {
5052
return res.json(result);
@@ -58,10 +60,11 @@ app.get('/updates', (req, res) => {
5860

5961
app.get('/:country', (req, res) => {
6062
const { country } = req.params;
61-
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gim) !== null;
63+
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
6264
const format = req.query.format ? req.query.format : '';
6365
const minimal = req.query.minimal === 'true';
6466
const emojis = req.query.emojis === 'true';
67+
6568
if (!country || country.toUpperCase() === 'ALL') {
6669
if (format.toLowerCase() === 'json') {
6770
return getJSONData().then(result => {

0 commit comments

Comments
 (0)