Skip to content

Commit cc9d77c

Browse files
committed
refactor(uiDateMask): replace moment.js with date-fns
After replacing the lib the generated bundle is 30kb smaller Fix #310
1 parent 9c36d17 commit cc9d77c

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

gulpfile.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ gulp.task('build-dependencies', function() {
3333
.require('string-mask', {
3434
expose: 'string-mask'
3535
})
36-
.require('moment', {
37-
expose: 'moment'
36+
.require('date-fns/format', {
37+
expose: 'date-fns/format'
38+
})
39+
.require('date-fns/parse', {
40+
expose: 'date-fns/parse'
41+
})
42+
.require('date-fns/isValid', {
43+
expose: 'date-fns/isValid'
3844
})
3945
.require('br-validations', {
4046
expose: 'br-validations'

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"license": "MIT",
3131
"dependencies": {
3232
"br-validations": "^0.3.1",
33+
"date-fns": "^2.0.0-alpha.7",
3334
"moment": "^2.8.4",
3435
"string-mask": "^0.3.0"
3536
},

src/global/date/date.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

3-
var moment = require('moment');
3+
var formatDate = require('date-fns/format');
4+
var parseDate = require('date-fns/parse');
5+
var isValidDate = require('date-fns/isValid');
46
var StringMask = require('string-mask');
57

68
function isISODateString(date) {
@@ -33,7 +35,7 @@ function DateMaskDirective($locale) {
3335

3436
var cleanValue = value;
3537
if (typeof value === 'object' || isISODateString(value)) {
36-
cleanValue = moment(value).format(dateFormat);
38+
cleanValue = formatDate(value, dateFormat);
3739
}
3840

3941
cleanValue = cleanValue.replace(/[^0-9]/g, '');
@@ -58,15 +60,15 @@ function DateMaskDirective($locale) {
5860

5961
return attrs.parse === 'false'
6062
? formatedValue
61-
: moment(formatedValue, dateFormat).toDate();
63+
: parseDate(formatedValue, dateFormat, new Date());
6264
});
6365

6466
ctrl.$validators.date = function validator(modelValue, viewValue) {
6567
if (ctrl.$isEmpty(modelValue)) {
6668
return true;
6769
}
6870

69-
return moment(viewValue, dateFormat).isValid() && viewValue.length === dateFormat.length;
71+
return isValidDate(parseDate(viewValue, dateFormat, new Date())) && viewValue.length === dateFormat.length;
7072
};
7173
}
7274
};

src/global/date/date.spec.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

3-
var StringMask = require('string-mask'),
4-
moment = require('moment');
3+
var StringMask = require('string-mask');
4+
var parseDate = require('date-fns/parse');
5+
var formatDate = require('date-fns/format');
56

67
describe('uiDateMask', function() {
78
describe('default ("YYYY-MM-DD") mask', function() {
@@ -26,7 +27,7 @@ describe('uiDateMask', function() {
2627
expect(input.getAttribute('value')).toEqual(formatedDateAsString);
2728
}
2829

29-
expect(value.evaluate('dateMask.toString()')).toEqual(moment(formatedDateAsString, 'YYYY-MM-DD').toDate().toString());
30+
expect(value.evaluate('dateMask.toString()')).toEqual(parseDate(formatedDateAsString, 'YYYY-MM-DD', new Date()).toString());
3031

3132
for (i = 7; i >= 0; i--) {
3233
input.sendKeys(protractor.Key.BACK_SPACE);
@@ -42,8 +43,8 @@ describe('uiDateMask', function() {
4243
var input = element(by.model('initializedDateMask')),
4344
value = element(by.exactBinding('initializedDateMask'));
4445

45-
value.evaluate('initializedDateMask').then((initialValue) => {
46-
var dateValue = moment(initialValue).format('YYYY-MM-DD');
46+
value.evaluate('initializedDateMask.toString()').then((initialValue) => {
47+
var dateValue = formatDate(new Date(initialValue), 'YYYY-MM-DD');
4748
expect(input.getAttribute('value')).toEqual(dateValue);
4849
});
4950
});
@@ -53,7 +54,7 @@ describe('uiDateMask', function() {
5354
value = element(by.exactBinding('initializedWithISOStringDateMask'));
5455

5556
value.getText().then((textValue) => {
56-
var dateValue = moment(new Date(textValue)).format('YYYY-MM-DD');
57+
var dateValue = formatDate(parseDate(textValue, 'YYYY-MM-DD', new Date()), 'YYYY-MM-DD');
5758
expect(input.getAttribute('value')).toEqual(dateValue);
5859
});
5960
});
@@ -105,7 +106,7 @@ describe('uiDateMask', function() {
105106
expect(input.getAttribute('value')).toEqual(formatedDateAsString);
106107
}
107108

108-
expect(value.evaluate('dateMask.toString()')).toEqual(moment(formatedDateAsString, 'DD/MM/YYYY').toDate().toString());
109+
expect(value.evaluate('dateMask.toString()')).toEqual(parseDate(formatedDateAsString, 'DD/MM/YYYY', new Date()).toString());
109110

110111
for (i = 7; i >= 0; i--) {
111112
input.sendKeys(protractor.Key.BACK_SPACE);
@@ -121,8 +122,8 @@ describe('uiDateMask', function() {
121122
var input = element(by.model('initializedDateMask')),
122123
value = element(by.exactBinding('initializedDateMask'));
123124

124-
value.evaluate('initializedDateMask').then((initialValue) => {
125-
var dateValue = moment(initialValue).format('DD/MM/YYYY');
125+
value.evaluate('initializedDateMask.toString()').then((initialValue) => {
126+
var dateValue = formatDate(new Date(initialValue), 'DD/MM/YYYY');
126127
expect(input.getAttribute('value')).toEqual(dateValue);
127128
});
128129
});
@@ -131,8 +132,8 @@ describe('uiDateMask', function() {
131132
var input = element(by.model('initializedWithISOStringDateMask')),
132133
value = element(by.exactBinding('initializedWithISOStringDateMask'));
133134

134-
value.getText().then((textValue) => {
135-
var dateValue = moment(new Date(textValue)).format('DD/MM/YYYY');
135+
value.evaluate('initializedDateMask.toString()').then((initialValue) => {
136+
var dateValue = formatDate(new Date(initialValue), 'DD/MM/YYYY');
136137
expect(input.getAttribute('value')).toEqual(dateValue);
137138
});
138139
});

0 commit comments

Comments
 (0)