Skip to content

Commit 0f07efc

Browse files
Replace moment.js with day.js
1 parent a5f4dd1 commit 0f07efc

18 files changed

+77
-94
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This application depends on the following third-party components:
2828

2929
1. [Capital Framework](https://cfpb.github.io/capital-framework/) - CFPB standard styling and controls
3030
1. [History](https://github.com/reacttraining/history) - Integrating the address bar with the application
31-
1. [moment](https://momentjs.com/) - Better date handling than native JavaScript
31+
1. [dayjs](https://day.js.org/) - Better date handling than native JavaScript
3232

3333
It also contains portions adapted from:
3434

TERMS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ author or the affirmer.
4848
## Exceptions
4949

5050
1. [History](https://github.com/reacttraining/history)
51-
1. [moment](https://momentjs.com/)
51+
1. [dayjs](https://day.js.org/)

config/webpack.config.dev.js

-6
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,6 @@ module.exports = {
250250
// This plugin makes the discovery automatic so you don't have to restart.
251251
// See https://github.com/facebookincubator/create-react-app/issues/186
252252
new WatchMissingNodeModulesPlugin(paths.appNodeModules),
253-
// Moment.js is an extremely popular library that bundles large locale files
254-
// by default due to how Webpack interprets its code. This is a practical
255-
// solution that requires the user to opt into importing specific locales.
256-
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
257-
// You can remove this if you don't use Moment.js:
258-
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
259253
],
260254
// Some libraries import Node modules but don't use them in the browser.
261255
// Tell Webpack to provide empty mocks for them so importing them works.

config/webpack.config.prod.js

-6
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,6 @@ module.exports = {
500500
// both options are optional
501501
filename: 'main.css',
502502
}),
503-
// Moment.js is an extremely popular library that bundles large locale files
504-
// by default due to how Webpack interprets its code. This is a practical
505-
// solution that requires the user to opt into importing specific locales.
506-
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
507-
// You can remove this if you don't use Moment.js:
508-
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
509503
].filter(Boolean),
510504
// Some libraries import Node modules but don't use them in the browser.
511505
// Tell Webpack to provide empty mocks for them so importing them works.

cypress/integration/common/filters-spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="cypress" />
22

3-
import moment from 'moment';
3+
import dayjs from 'dayjs';
44

55
describe( 'Filter Panel', () => {
66
beforeEach( () => {
@@ -83,14 +83,14 @@ describe( 'Filter Panel', () => {
8383
.click();
8484
cy.wait( '@getGeo' );
8585

86-
const maxDate = moment( new Date() ).format( 'YYYY-MM-DD' );
87-
let minDate = moment( new Date() ).subtract( 3, 'years' ).format( 'YYYY-MM-DD' );
86+
const maxDate = dayjs( new Date() ).format( 'YYYY-MM-DD' );
87+
let minDate = dayjs( new Date() ).subtract( 3, 'years' ).format( 'YYYY-MM-DD' );
8888
cy.get( '.date-ranges .a-btn.range-3y' )
8989
.contains( '3y' )
9090
.click();
9191
cy.url()
9292
.should( 'include', `date_received_max=${ maxDate }&date_received_min=${ minDate }` );
93-
minDate = moment( new Date() ).subtract( 6, 'months' ).format( 'YYYY-MM-DD' );
93+
minDate = dayjs( new Date() ).subtract( 6, 'months' ).format( 'YYYY-MM-DD' );
9494
cy.get( '.date-ranges .a-btn.range-6m' )
9595
.contains( '6m' )
9696
.click();

dist/ccdb5.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ccdb5.min.js.LICENSE.txt

-12
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,3 @@ object-assign
6868
* This source code is licensed under the MIT license found in the
6969
* LICENSE file in the root directory of this source tree.
7070
*/
71-
72-
//! Copyright (c) JS Foundation and other contributors
73-
74-
//! github.com/moment/moment-timezone
75-
76-
//! license : MIT
77-
78-
//! moment-timezone.js
79-
80-
//! moment.js
81-
82-
//! version : 0.5.34

dist/ccdb5.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"cypress": "9.1.1",
7676
"d3": "^5.12.0",
7777
"d3-selection": "^1.4.0",
78+
"dayjs": "^1.10.7",
7879
"detect-port": "1.2.1",
7980
"dotenv": "6.2.0",
8081
"dotenv-expand": "4.2.0",
@@ -106,8 +107,6 @@
106107
"less-loader": "^4.0.5",
107108
"lodash": "^4.17.21",
108109
"mini-css-extract-plugin": "0.5.0",
109-
"moment": "^2.29.1",
110-
"moment-timezone": "^0.5.33",
111110
"object-assign": "4.1.1",
112111
"optimize-css-assets-webpack-plugin": "5.0.3",
113112
"pnp-webpack-plugin": "1.5.0",

src/__tests__/utils.spec.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '../utils'
77
import Analytics from '../actions/analytics'
88
import { DATE_RANGE_MIN } from '../constants'
9-
import moment from 'moment'
9+
import dayjs from 'dayjs'
1010

1111
describe('module::utils', () => {
1212
describe( 'ariaReadoutNumbers', () => {
@@ -127,13 +127,13 @@ describe('module::utils', () => {
127127
} );
128128

129129
it( 'returns 3y', () => {
130-
start = new Date( moment( end ).subtract( 3, 'years' ).calendar() )
130+
start = new Date( dayjs( end ).subtract( 3, 'years' ).calendar() )
131131
let actual = calculateDateRange( start, end )
132132
expect( actual ).toEqual( '3y' )
133133
} );
134134

135135
it( 'returns 6m', () => {
136-
start = new Date( moment( end ).subtract( 6, 'months' ).calendar() )
136+
start = new Date( dayjs( end ).subtract( 6, 'months' ).calendar() )
137137
let actual = calculateDateRange( start, end )
138138
expect( actual ).toEqual( '6m' )
139139
} )

src/components/DateInput/index.jsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import './DateInput.less'
44
import { bindAll, debounce, shortFormat } from '../../utils'
5+
import dayjs from 'dayjs'
56
import iconMap from '../iconMap';
6-
import moment from 'moment'
77
import PropTypes from 'prop-types'
88
import React from 'react'
99

@@ -161,11 +161,11 @@ export default class DateInput extends React.Component {
161161
return ERROR
162162
}
163163

164-
if ( props.min && moment( d ).isBefore( props.min, 'day' ) ) {
164+
if ( props.min && dayjs( d ).isBefore( props.min, 'day' ) ) {
165165
return TOO_LOW
166166
}
167167

168-
if ( props.max && moment( d ).isAfter( props.max, 'day' ) ) {
168+
if ( props.max && dayjs( d ).isAfter( props.max, 'day' ) ) {
169169
return TOO_HIGH
170170
}
171171

@@ -180,7 +180,7 @@ export default class DateInput extends React.Component {
180180
}
181181

182182
if ( state.phase === READY ) {
183-
state.asDate = moment( v, FORMAT )
183+
state.asDate = dayjs( v, FORMAT )
184184
state.phase = this._validateAsDate( props, state.asDate )
185185
}
186186

src/components/Filters/DateFilter.jsx

+11-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import CollapsibleFilter from './CollapsibleFilter'
55
import { connect } from 'react-redux'
66
import { DATE_RANGE_MIN } from '../../constants'
77
import DateInput from '../DateInput'
8+
import dayjs from 'dayjs'
9+
import dayjsIsBetween from 'dayjs/plugin/isBetween'
810
import iconMap from '../iconMap'
9-
import moment from 'moment'
1011
import PropTypes from 'prop-types'
1112
import React from 'react'
1213

14+
dayjs.extend( dayjsIsBetween )
15+
1316
const WARN_SERIES_BREAK = 'CFPB updated product and issue options' +
1417
' available to consumers in April 2017 ';
1518

@@ -39,10 +42,10 @@ export class DateFilter extends React.Component {
3942
}
4043

4144
render() {
42-
const from = moment( this.state.from, 'MM-DD-YYYY' )
43-
const through = moment( this.state.through, 'MM-DD-YYYY' )
45+
const from = dayjs( this.state.from, 'MM-DD-YYYY' )
46+
const through = dayjs( this.state.through, 'MM-DD-YYYY' )
4447

45-
const showWarning = moment( '2017-04-23' ).isBetween( from, through, 'day' )
48+
const showWarning = dayjs( '2017-04-23' ).isBetween( from, through, 'day' )
4649

4750
return (
4851
<CollapsibleFilter title={ this.props.title }
@@ -131,8 +134,8 @@ export class DateFilter extends React.Component {
131134
_validate( state ) {
132135

133136
// Check for range errors
134-
const from = moment( state.from, 'MM-DD-YYYY' )
135-
const through = moment( state.through, 'MM-DD-YYYY' )
137+
const from = dayjs( state.from, 'MM-DD-YYYY' )
138+
const through = dayjs( state.through, 'MM-DD-YYYY' )
136139
if ( from && through && from > through ) {
137140
state.messages.ordered = "'From' must be less than 'Through'"
138141
} else {
@@ -168,8 +171,8 @@ export class DateFilter extends React.Component {
168171
if ( this._hasMessages( state.messages ) === false &&
169172
this._isChanged( this.props, state ) ) {
170173

171-
const from = moment( state.from, 'MM-DD-YYYY' )
172-
const through = moment( state.through, 'MM-DD-YYYY' )
174+
const from = dayjs( state.from, 'MM-DD-YYYY' )
175+
const through = dayjs( state.through, 'MM-DD-YYYY' )
173176
const dateFrom = from.isValid() ? from.toDate() : null
174177
const dateThrough = through.isValid() ? through.toDate() : null
175178

src/reducers/__tests__/query.spec.jsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import target, {
44
import actions from '../../actions'
55
import * as types from '../../constants'
66

7-
import moment from 'moment'
7+
import dayjs from 'dayjs'
88
import { startOfToday } from '../../utils'
99

1010
const maxDate = startOfToday()
@@ -565,7 +565,7 @@ describe( 'reducer:query', () => {
565565

566566
it( 'clears the default range if the dates are not 3 years apart', () => {
567567
state.date_received_min = new Date(
568-
moment( maxDate ).subtract( 2, 'years' )
568+
dayjs( maxDate ).subtract( 2, 'years' )
569569
)
570570
expected.dateRange = ''
571571
expected.date_received_min = state.date_received_min
@@ -585,7 +585,7 @@ describe( 'reducer:query', () => {
585585

586586
it( 'sets the 3m range if the dates are right', () => {
587587
state.date_received_min = new Date(
588-
moment( maxDate ).subtract( 3, 'months' )
588+
dayjs( maxDate ).subtract( 3, 'months' )
589589
)
590590
expected.dateRange = '3m'
591591
expected.date_received_min = state.date_received_min
@@ -596,7 +596,7 @@ describe( 'reducer:query', () => {
596596

597597
it( 'sets the 6m range if the dates are right', () => {
598598
state.date_received_min = new Date(
599-
moment( maxDate ).subtract( 6, 'months' )
599+
dayjs( maxDate ).subtract( 6, 'months' )
600600
)
601601
expected.dateRange = '6m'
602602
expected.date_received_min = state.date_received_min
@@ -607,7 +607,7 @@ describe( 'reducer:query', () => {
607607

608608
it( 'sets the 1y range if the dates are right', () => {
609609
state.date_received_min = new Date(
610-
moment( maxDate ).subtract( 1, 'year' )
610+
dayjs( maxDate ).subtract( 1, 'year' )
611611
)
612612
expected.dateRange = '1y'
613613
expected.date_received_min = state.date_received_min
@@ -1116,7 +1116,7 @@ describe( 'reducer:query', () => {
11161116
} )
11171117

11181118
it( 'adds dateRange', () => {
1119-
const min = new Date( moment( maxDate ).subtract( 3, 'months' ) )
1119+
const min = new Date( dayjs( maxDate ).subtract( 3, 'months' ) )
11201120
action.maxDate = maxDate
11211121
action.minDate = min
11221122
result = target( {}, action )

src/reducers/query.jsx

+18-18
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
} from '../utils'
1212
import { enforceValues, validateTrendsReducer } from '../utils/reducers'
1313
import actions from '../actions'
14+
import dayjs from 'dayjs'
1415
import { isGreaterThanYear } from '../utils/trends'
15-
import moment from 'moment'
1616

1717
const queryString = require( 'query-string' )
1818

@@ -25,7 +25,7 @@ export const defaultQuery = {
2525
dateRange: '3y',
2626
date_received_max: startOfToday(),
2727
date_received_min: new Date(
28-
moment( startOfToday() ).subtract( 3, 'years' )
28+
dayjs( startOfToday() ).subtract( 3, 'years' )
2929
),
3030
enablePer1000: true,
3131
focus: '',
@@ -83,26 +83,26 @@ export function alignDateRange( state ) {
8383
const dateMin = state.date_received_min
8484

8585
// All
86-
if ( moment( dateMax ).isSame( defaultQuery.date_received_max ) &&
87-
moment( dateMin ).isSame( types.DATE_RANGE_MIN )
86+
if ( dayjs( dateMax ).isSame( defaultQuery.date_received_max ) &&
87+
dayjs( dateMin ).isSame( types.DATE_RANGE_MIN )
8888
) {
8989
state.dateRange = 'All'
9090
return state
9191
}
9292

9393
const rangeMap = {
94-
'3y': new Date( moment( dateMax ).subtract( 3, 'years' ) ),
95-
'3m': new Date( moment( dateMax ).subtract( 3, 'months' ) ),
96-
'6m': new Date( moment( dateMax ).subtract( 6, 'months' ) ),
97-
'1y': new Date( moment( dateMax ).subtract( 1, 'year' ) )
94+
'3y': new Date( dayjs( dateMax ).subtract( 3, 'years' ) ),
95+
'3m': new Date( dayjs( dateMax ).subtract( 3, 'months' ) ),
96+
'6m': new Date( dayjs( dateMax ).subtract( 6, 'months' ) ),
97+
'1y': new Date( dayjs( dateMax ).subtract( 1, 'year' ) )
9898
}
9999
const ranges = Object.keys( rangeMap )
100100
let matched = false
101101

102102
for ( let i = 0; i < ranges.length && !matched; i++ ) {
103103
const range = ranges[i]
104104

105-
if ( moment( dateMin ).isSame( rangeMap[range], 'day' ) ) {
105+
if ( dayjs( dateMin ).isSame( rangeMap[range], 'day' ) ) {
106106
state.dateRange = range
107107
matched = true
108108
}
@@ -255,10 +255,10 @@ export function changeDateRange( state, action ) {
255255

256256
const res = {
257257
'All': new Date( types.DATE_RANGE_MIN ),
258-
'3m': new Date( moment( maxDate ).subtract( 3, 'months' ) ),
259-
'6m': new Date( moment( maxDate ).subtract( 6, 'months' ) ),
260-
'1y': new Date( moment( maxDate ).subtract( 1, 'year' ) ),
261-
'3y': new Date( moment( maxDate ).subtract( 3, 'years' ) )
258+
'3m': new Date( dayjs( maxDate ).subtract( 3, 'months' ) ),
259+
'6m': new Date( dayjs( maxDate ).subtract( 6, 'months' ) ),
260+
'1y': new Date( dayjs( maxDate ).subtract( 1, 'year' ) ),
261+
'3y': new Date( dayjs( maxDate ).subtract( 3, 'years' ) )
262262
}
263263

264264
/* istanbul ignore else */
@@ -286,10 +286,10 @@ export function changeDates( state, action ) {
286286

287287
let { maxDate, minDate } = action
288288

289-
minDate = moment( minDate ).isValid() ?
290-
new Date( moment( minDate ).startOf( 'day' ) ) : null
291-
maxDate = moment( maxDate ).isValid() ?
292-
new Date( moment( maxDate ).startOf( 'day' ) ) : null
289+
minDate = dayjs( minDate ).isValid() ?
290+
new Date( dayjs( minDate ).startOf( 'day' ) ) : null
291+
maxDate = dayjs( maxDate ).isValid() ?
292+
new Date( dayjs( maxDate ).startOf( 'day' ) ) : null
293293

294294

295295
const newState = {
@@ -554,7 +554,7 @@ export function removeAllFilters( state ) {
554554
newState.dateRange = '3y'
555555
/* eslint-disable camelcase */
556556
newState.date_received_min =
557-
new Date( moment( startOfToday() ).subtract( 3, 'years' ) )
557+
new Date( dayjs( startOfToday() ).subtract( 3, 'years' ) )
558558
newState.date_received_max = startOfToday()
559559

560560
newState.focus = ''

0 commit comments

Comments
 (0)