Skip to content

Commit 98b8f69

Browse files
author
touchRED
committed
fix: replace moment with dayjs
1 parent e8ba7ec commit 98b8f69

File tree

4 files changed

+41
-46
lines changed

4 files changed

+41
-46
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,11 @@
118118
"@postlight/ci-failed-test-reporter": "^1.0",
119119
"browser-request": "github:postlight/browser-request#feat-add-headers-to-response",
120120
"cheerio": "^0.22.0",
121+
"dayjs": "^1.11.7",
121122
"difflib": "github:postlight/difflib.js",
122123
"ellipsize": "0.1.0",
123124
"iconv-lite": "0.5.0",
124125
"jquery": "^3.5.0",
125-
"moment": "^2.23.0",
126-
"moment-parseformat": "3.0.0",
127-
"moment-timezone": "0.5.37",
128126
"postman-request": "^2.88.1-postman.31",
129127
"string-direction": "^0.1.2",
130128
"turndown": "^7.1.1",

src/cleaners/date-published.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import moment from 'moment-timezone';
2-
import parseFormat from 'moment-parseformat';
3-
// Is there a compelling reason to use moment here?
4-
// Mostly only being used for the isValid() method,
5-
// but could just check for 'Invalid Date' string.
1+
import dayjs from 'dayjs';
2+
import customParseFormat from 'dayjs/plugin/customParseFormat';
3+
import utc from 'dayjs/plugin/utc';
4+
import timezonePlugin from 'dayjs/plugin/timezone';
5+
import advancedFormat from 'dayjs/plugin/advancedFormat';
66

77
import {
88
MS_DATE_STRING,
@@ -16,6 +16,11 @@ import {
1616
TIME_WITH_OFFSET_RE,
1717
} from './constants';
1818

19+
dayjs.extend(customParseFormat);
20+
dayjs.extend(utc);
21+
dayjs.extend(timezonePlugin);
22+
dayjs.extend(advancedFormat);
23+
1924
export function cleanDateString(dateString) {
2025
return (dateString.match(SPLIT_DATE_STRING) || [])
2126
.join(' ')
@@ -27,21 +32,24 @@ export function cleanDateString(dateString) {
2732

2833
export function createDate(dateString, timezone, format) {
2934
if (TIME_WITH_OFFSET_RE.test(dateString)) {
30-
return moment(new Date(dateString));
35+
return dayjs(new Date(dateString));
3136
}
3237

3338
if (TIME_AGO_STRING.test(dateString)) {
3439
const fragments = TIME_AGO_STRING.exec(dateString);
35-
return moment().subtract(fragments[1], fragments[2]);
40+
return dayjs().subtract(fragments[1], fragments[2]);
3641
}
3742

3843
if (TIME_NOW_STRING.test(dateString)) {
39-
return moment();
44+
return dayjs();
4045
}
4146

42-
return timezone
43-
? moment.tz(dateString, format || parseFormat(dateString), timezone)
44-
: moment(dateString, format || parseFormat(dateString));
47+
if (timezone) {
48+
return format
49+
? dayjs.tz(dateString, format, timezone)
50+
: dayjs.tz(new Date(dateString), timezone);
51+
}
52+
return format ? dayjs(dateString, format) : dayjs(new Date(dateString));
4553
}
4654

4755
// Take a date published string, and hopefully return a date out of

src/cleaners/date-published.test.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import assert from 'assert';
2-
import moment from 'moment-timezone';
2+
import dayjs from 'dayjs';
33

44
import cleanDatePublished, { cleanDateString } from './date-published';
55

66
describe('cleanDatePublished(dateString)', () => {
77
it('returns a date', () => {
88
const datePublished = cleanDatePublished('published: 1/1/2020');
99

10-
assert.equal(datePublished, moment('1/1/2020', 'MM/DD/YYYY').toISOString());
10+
assert.equal(datePublished, dayjs('1/1/2020', 'M/D/YYYY').toISOString());
1111
});
1212

1313
it('returns null if date is invalid', () => {
@@ -28,37 +28,37 @@ describe('cleanDatePublished(dateString)', () => {
2828
it('accepts a custom date format', () => {
2929
// The JS date parser is forgiving, but
3030
// it needs am/pm separated from a time
31-
const datePublished = cleanDatePublished('Mon Aug 03 12:45:00 EDT 2015', {
31+
const datePublished = cleanDatePublished('Aug 03 12:45:00 EDT 2015', {
3232
timezone: 'America/New_York',
33-
format: 'ddd MMM DD HH:mm:ss zz YYYY',
33+
format: 'MMM DD HH:mm:ss z YYYY',
3434
});
3535
assert.equal(datePublished, '2015-08-03T16:45:00.000Z');
3636
});
3737

3838
it('can handle dates formatted as "[just|right] now"', () => {
3939
const date1 = cleanDatePublished('now');
40-
const newDate1 = moment(date1)
40+
const newDate1 = dayjs(date1)
4141
.format()
4242
.split('T')[0];
43-
const expectedDate1 = moment()
43+
const expectedDate1 = dayjs()
4444
.format()
4545
.split('T')[0];
4646
assert.equal(newDate1, expectedDate1);
4747

4848
const date2 = cleanDatePublished('just now');
49-
const newDate2 = moment(date2)
49+
const newDate2 = dayjs(date2)
5050
.format()
5151
.split('T')[0];
52-
const expectedDate2 = moment()
52+
const expectedDate2 = dayjs()
5353
.format()
5454
.split('T')[0];
5555
assert.equal(newDate2, expectedDate2);
5656

5757
const date3 = cleanDatePublished('right now');
58-
const newDate3 = moment(date3)
58+
const newDate3 = dayjs(date3)
5959
.format()
6060
.split('T')[0];
61-
const expectedDate3 = moment()
61+
const expectedDate3 = dayjs()
6262
.format()
6363
.split('T')[0];
6464
assert.equal(newDate3, expectedDate3);
@@ -69,30 +69,30 @@ describe('cleanDatePublished(dateString)', () => {
6969
// "X days ago" will not be accurate down to the exact time
7070
// "X months ago" will not be accurate down to the exact day
7171
const date1 = cleanDatePublished('1 hour ago');
72-
const newDate1 = moment(date1)
72+
const newDate1 = dayjs(date1)
7373
.format()
7474
.split('T')[0];
75-
const expectedDate1 = moment()
75+
const expectedDate1 = dayjs()
7676
.subtract(1, 'hour')
7777
.format()
7878
.split('T')[0];
7979
assert.equal(newDate1, expectedDate1);
8080

8181
const date2 = cleanDatePublished('5 days ago');
82-
const newDate2 = moment(date2)
82+
const newDate2 = dayjs(date2)
8383
.format()
8484
.split('T')[0];
85-
const expectedDate2 = moment()
85+
const expectedDate2 = dayjs()
8686
.subtract(5, 'days')
8787
.format()
8888
.split('T')[0];
8989
assert.equal(newDate2, expectedDate2);
9090

9191
const date3 = cleanDatePublished('10 months ago');
92-
const newDate3 = moment(date3)
92+
const newDate3 = dayjs(date3)
9393
.format()
9494
.split('T')[0];
95-
const expectedDate3 = moment()
95+
const expectedDate3 = dayjs()
9696
.subtract(10, 'months')
9797
.format()
9898
.split('T')[0];

yarn.lock

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,6 +2577,11 @@ date-now@^0.1.4:
25772577
version "0.1.4"
25782578
resolved "https://registry.yarnpkg.com/date-now/-/date-now-0.1.4.tgz#eaf439fd4d4848ad74e5cc7dbef200672b9e345b"
25792579

2580+
dayjs@^1.11.7:
2581+
version "1.11.7"
2582+
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.7.tgz#4b296922642f70999544d1144a2c25730fce63e2"
2583+
integrity sha512-+Yw9U6YO5TQohxLcIkrXBeY73WP3ejHWVvx8XCk3gxvQDCTEmS48ZrSZCKciI7Bhl/uCMyxYtE9UqRILmFphkQ==
2584+
25802585
debug@2.6.9, debug@^2.1.2, debug@^2.3.3, debug@^2.6.8, debug@^2.6.9:
25812586
version "2.6.9"
25822587
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
@@ -6132,22 +6137,6 @@ module-deps@^6.0.0:
61326137
through2 "^2.0.0"
61336138
xtend "^4.0.0"
61346139

6135-
moment-parseformat@3.0.0:
6136-
version "3.0.0"
6137-
resolved "https://registry.yarnpkg.com/moment-parseformat/-/moment-parseformat-3.0.0.tgz#3a1dc438b4bc073b7e93cc298cfb6c5daac26dba"
6138-
6139-
moment-timezone@0.5.37:
6140-
version "0.5.37"
6141-
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.37.tgz#adf97f719c4e458fdb12e2b4e87b8bec9f4eef1e"
6142-
integrity sha512-uEDzDNFhfaywRl+vwXxffjjq1q0Vzr+fcQpQ1bU0kbzorfS7zVtZnCnGc8mhWmF39d4g4YriF6kwA75mJKE/Zg==
6143-
dependencies:
6144-
moment ">= 2.9.0"
6145-
6146-
"moment@>= 2.9.0", moment@^2.23.0:
6147-
version "2.29.4"
6148-
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"
6149-
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==
6150-
61516140
ms@0.7.2:
61526141
version "0.7.2"
61536142
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765"

0 commit comments

Comments
 (0)