Skip to content

Commit 7844129

Browse files
toufic-madampash
authored andcommitted
feat: Add custom parser for Reddit (#307)
1 parent 13581cd commit 7844129

File tree

16 files changed

+491
-1
lines changed

16 files changed

+491
-1
lines changed

fixtures/www.reddit.com/1551705199548.html

Lines changed: 9 additions & 0 deletions
Large diffs are not rendered by default.

fixtures/www.reddit.com/1552069905710.html

Lines changed: 4 additions & 0 deletions
Large diffs are not rendered by default.

fixtures/www.reddit.com/1552069933451.html

Lines changed: 8 additions & 0 deletions
Large diffs are not rendered by default.

fixtures/www.reddit.com/1552069947100.html

Lines changed: 3 additions & 0 deletions
Large diffs are not rendered by default.

fixtures/www.reddit.com/1552069958273.html

Lines changed: 5 additions & 0 deletions
Large diffs are not rendered by default.

fixtures/www.reddit.com/1552069973740.html

Lines changed: 29 additions & 0 deletions
Large diffs are not rendered by default.

fixtures/www.reddit.com/1552069996237.html

Lines changed: 23 additions & 0 deletions
Large diffs are not rendered by default.

fixtures/www.reddit.com/1552070031501.html

Lines changed: 7 additions & 0 deletions
Large diffs are not rendered by default.

src/cleaners/constants.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ export const SEC_DATE_STRING = /^\d{10}$/i;
2727
export const CLEAN_DATE_STRING_RE = /^\s*published\s*:?\s*(.*)/i;
2828
export const TIME_MERIDIAN_SPACE_RE = /(.*\d)(am|pm)(.*)/i;
2929
export const TIME_MERIDIAN_DOTS_RE = /\.m\./i;
30+
export const TIME_NOW_STRING = /^\s*(just|right)?\s*now\s*/i;
31+
const timeUnits = [
32+
'seconds?',
33+
'minutes?',
34+
'hours?',
35+
'days?',
36+
'weeks?',
37+
'months?',
38+
'years?',
39+
];
40+
const allTimeUnits = timeUnits.join('|');
41+
export const TIME_AGO_STRING = new RegExp(
42+
`(\\d+)\\s+(${allTimeUnits})\\s+ago`,
43+
'i'
44+
);
3045
const months = [
3146
'jan',
3247
'feb',

src/cleaners/date-published.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
SEC_DATE_STRING,
1010
CLEAN_DATE_STRING_RE,
1111
SPLIT_DATE_STRING,
12+
TIME_AGO_STRING,
13+
TIME_NOW_STRING,
1214
TIME_MERIDIAN_SPACE_RE,
1315
TIME_MERIDIAN_DOTS_RE,
1416
TIME_WITH_OFFSET_RE,
@@ -28,6 +30,15 @@ export function createDate(dateString, timezone, format) {
2830
return moment(new Date(dateString));
2931
}
3032

33+
if (TIME_AGO_STRING.test(dateString)) {
34+
const fragments = TIME_AGO_STRING.exec(dateString);
35+
return moment().subtract(fragments[1], fragments[2]);
36+
}
37+
38+
if (TIME_NOW_STRING.test(dateString)) {
39+
return moment();
40+
}
41+
3142
return timezone
3243
? moment.tz(dateString, format || parseFormat(dateString), timezone)
3344
: moment(dateString, format || parseFormat(dateString));

0 commit comments

Comments
 (0)