Skip to content

Commit 6a5f892

Browse files
author
touchRED
committed
fix: update tests to remove dayjs where possible, update formats
1 parent 98b8f69 commit 6a5f892

File tree

29 files changed

+43
-81
lines changed

29 files changed

+43
-81
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,14 @@
132132
},
133133
"bundleDependencies": [
134134
"jquery",
135-
"moment-timezone",
136135
"browser-request"
137136
],
138137
"browser": {
139138
"main": "./dist/mercury.web.js",
140139
"cheerio": "./src/shims/cheerio-query",
141140
"jquery": "./node_modules/jquery/dist/jquery.min.js",
142141
"postman-request": "browser-request",
143-
"iconv-lite": "./src/shims/iconv-lite",
144-
"moment-timezone": "./node_modules/moment-timezone/builds/moment-timezone-with-data-2012-2022.min.js"
142+
"iconv-lite": "./src/shims/iconv-lite"
145143
},
146144
"husky": {
147145
"hooks": {

src/cleaners/date-published.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ export function createDate(dateString, timezone, format) {
4545
}
4646

4747
if (timezone) {
48-
return format
49-
? dayjs.tz(dateString, format, timezone)
50-
: dayjs.tz(new Date(dateString), timezone);
48+
try {
49+
return format
50+
? dayjs.tz(dateString, format, timezone)
51+
: dayjs.tz(dayjs(dateString).format('YYYY-MM-DD HH:mm:ss'), timezone);
52+
} catch (error) {
53+
// return an intentionally invalid dayjs object,
54+
// in case the input needs to be cleaned first
55+
return dayjs('');
56+
}
5157
}
52-
return format ? dayjs(dateString, format) : dayjs(new Date(dateString));
58+
return format ? dayjs(dateString, format) : dayjs(dateString);
5359
}
5460

5561
// Take a date published string, and hopefully return a date out of
@@ -70,7 +76,7 @@ export default function cleanDatePublished(
7076

7177
if (!date.isValid()) {
7278
dateString = cleanDateString(dateString);
73-
date = createDate(dateString, timezone, format);
79+
date = createDate(dateString, timezone);
7480
}
7581

7682
return date.isValid() ? date.toISOString() : null;

src/extractors/custom/clinicaltrials.gov/index.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import assert from 'assert';
22
import URL from 'url';
33
import cheerio from 'cheerio';
4-
import moment from 'moment-timezone';
54

65
import Mercury from 'mercury';
76
import getExtractor from 'extractors/get-extractor';
@@ -59,7 +58,7 @@ describe('ClinicaltrialsGovExtractor', () => {
5958

6059
// Update these values with the expected values from
6160
// the article.
62-
assert.equal(moment(date_published).format('YYYY-MM-DD'), '2018-11-21');
61+
assert.equal(date_published, '2018-11-21T05:00:00.000Z');
6362
});
6463

6564
it('returns the content', async () => {

src/extractors/custom/fortune.com/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ export const FortuneComExtractor = {
1111

1212
date_published: {
1313
selectors: ['.MblGHNMJ'],
14-
15-
timezone: 'UTC',
1614
},
1715

1816
lead_image_url: {

src/extractors/custom/genius.com/index.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import assert from 'assert';
22
import URL from 'url';
33
import cheerio from 'cheerio';
4-
import moment from 'moment';
54

65
import Mercury from 'mercury';
76
import getExtractor from 'extractors/get-extractor';
@@ -51,11 +50,10 @@ describe('GeniusComExtractor', () => {
5150
// To pass this test, fill out the date_published selector
5251
// in ./src/extractors/custom/genius.com/index.js.
5352
const { date_published } = await result;
54-
const newDatePublished = moment(date_published).format();
5553

5654
// Update these values with the expected values from
5755
// the article.
58-
assert.equal(newDatePublished.split('T')[0], '1984-06-25');
56+
assert.equal(date_published, '1984-06-25T04:00:00.000Z');
5957
});
6058

6159
it('returns the lead_image_url', async () => {

src/extractors/custom/news.nationalgeographic.com/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export const NewsNationalgeographicComExtractor = {
1111

1212
date_published: {
1313
selectors: [['meta[name="article:published_time"]', 'value']],
14-
format: 'ddd MMM DD HH:mm:ss zz YYYY',
15-
timezone: 'EST',
14+
timezone: 'America/New_York',
1615
},
1716

1817
dek: {

src/extractors/custom/news.nationalgeographic.com/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('NewsNationalgeographicComExtractor', () => {
4949

5050
// Update these values with the expected values from
5151
// the article.
52-
assert.equal(date_published, '2015-08-03T17:45:00.000Z');
52+
assert.equal(date_published, '2015-08-03T16:45:00.000Z');
5353
});
5454

5555
it('returns the dek', async () => {

src/extractors/custom/people.com/index.test.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import assert from 'assert';
22
import URL from 'url';
33
import cheerio from 'cheerio';
4-
import moment from 'moment-timezone';
54

65
import Mercury from 'mercury';
76
import getExtractor from 'extractors/get-extractor';
@@ -55,13 +54,9 @@ describe('PeopleComExtractor', () => {
5554
// in ./src/extractors/custom/people.com/index.js.
5655
const { date_published } = await result;
5756

58-
const new_date_published = moment(date_published)
59-
.format()
60-
.split('T')[0];
61-
6257
// Update these values with the expected values from
6358
// the article.
64-
assert.equal(new_date_published, '2016-12-12');
59+
assert.equal(date_published, '2016-12-12T14:22:00.000Z');
6560
});
6661

6762
it('returns the lead_image_url', async () => {

src/extractors/custom/pitchfork.com/index.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import assert from 'assert';
22
import URL from 'url';
33
import cheerio from 'cheerio';
4-
import moment from 'moment-timezone';
54

65
import Mercury from 'mercury';
76
import getExtractor from 'extractors/get-extractor';
@@ -41,11 +40,8 @@ describe('PitchforkComExtractor', () => {
4140

4241
it('returns the date_published', async () => {
4342
const { date_published } = await result;
44-
const new_date_published = moment(date_published)
45-
.format()
46-
.split('T')[0];
4743

48-
assert.equal(new_date_published, '2019-06-07');
44+
assert.equal(date_published, '2019-06-07T04:00:00.000Z');
4945
});
5046

5147
it('returns the dek', async () => {

src/extractors/custom/takagi-hiromitsu.jp/index.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import assert from 'assert';
22
import URL from 'url';
33
import cheerio from 'cheerio';
4-
import moment from 'moment';
54

65
import Mercury from 'mercury';
76
import getExtractor from 'extractors/get-extractor';
@@ -57,11 +56,10 @@ describe('TakagihiromitsuJpExtractor', () => {
5756
// To pass this test, fill out the date_published selector
5857
// in ./src/extractors/custom/takagi-hiromitsu.jp/index.js.
5958
const { date_published } = await result;
60-
const newDatePublished = moment(date_published).format();
6159

6260
// Update these values with the expected values from
6361
// the article.
64-
assert.equal(newDatePublished.split('T')[0], '2019-02-17');
62+
assert.equal(date_published, '2019-02-17T14:34:06.000Z');
6563
});
6664

6765
it('returns the dek', async () => {

0 commit comments

Comments
 (0)