Skip to content

Commit

Permalink
Update date handling to use dayjs (#237)
Browse files Browse the repository at this point in the history
* Update date handling to use dayjs

* Update lib/utils/mercadopagoDate.js
  • Loading branch information
jlucaso1 authored Jul 17, 2023
1 parent fe57e91 commit 38a243c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
24 changes: 14 additions & 10 deletions lib/utils/mercadopagoDate.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
var moment = require('moment');
var dayjs = require("dayjs");
var utc = require('dayjs/plugin/utc')
dayjs.extend(utc)

var mercadopagoDate = function mercadopagoDate() {
if (arguments.length === 1) {
this.date = moment(arguments[0]);
this.date = dayjs(arguments[0]);
} else if (arguments.length === 2) {
this.date = moment(arguments[0], arguments[1]);
this.date = dayjs(arguments[0], arguments[1]);
} else {
this.date = moment();
this.date = dayjs();
}
};

/**
* Converts a mercadopagoDate to ISO_8601 String equivalent
*/
mercadopagoDate.prototype.toString = function (utc) {
if (utc !== undefined && (typeof utc === 'number')) return this.date.utc(utc).format(mercadopagoDate.ISO_8601);
if (utc !== undefined && typeof utc === "number") {
return this.date.utcOffset(utc).format(mercadopagoDate.ISO_8601);
}
return this.date.format(mercadopagoDate.ISO_8601);
};

Expand All @@ -32,24 +36,24 @@ mercadopagoDate.prototype.toDate = function () {
* @returns {mercadopagoDate}
*/
mercadopagoDate.prototype.add = function (days) {
this.date.add(days, 'day');
this.date = this.date.add(days, "day");
return this;
};

/**
* Substract days to a mercadopagoDate
* Subtract days from a mercadopagoDate
* @param days
* @returns {mercadopagoDate}
*/
mercadopagoDate.prototype.subtract = function (days) {
this.date.subtract(days, 'day');
this.date = this.date.subtract(days, "day");
return this;
};

/**
* ISO_8601 format for moment library
* ISO_8601 format for dayjs library
* @type {string}
*/
mercadopagoDate.ISO_8601 = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
mercadopagoDate.ISO_8601 = "YYYY-MM-DDTHH:mm:ss.SSSZ";

module.exports = mercadopagoDate;
25 changes: 11 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"dependencies": {
"ajv": "^6.12.3",
"bluebird": "3.4.7",
"moment": "^2.29.4",
"dayjs": "^1.11.7",
"request": "^2.88.0",
"request-etag": "2.0.3",
"uuid": "3.0.1"
Expand Down
6 changes: 4 additions & 2 deletions test/mercadopagoDate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-env node, mocha */
var chai = require('chai');
var moment = require('moment');
var dayjs = require('dayjs');
var utc = require('dayjs/plugin/utc');
dayjs.extend(utc);
var MercadopagoDate = require('../lib/utils/mercadopagoDate');
var assert = chai.assert;

Expand All @@ -9,7 +11,7 @@ describe('MercadopagoDate Class', function () {

// I'm getting the current offset, because if the test server has another TZ, this tests will crash
before(function () {
var currentOffset = (moment('2016-01-01').utcOffset() / 60);
var currentOffset = (dayjs('2016-01-01').utcOffset() / 60);

stringOffset = (currentOffset < 0) ? '-' : '+';

Expand Down
10 changes: 6 additions & 4 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-env node, mocha */
var chai = require('chai');
var moment = require('moment');
var dayjs = require('dayjs');
var utc = require('dayjs/plugin/utc');
dayjs.extend(utc);
var MercadopagoDate = require('../lib/utils/mercadopagoDate');
var assert = chai.assert;
var mp = require('../index.js');
Expand All @@ -10,7 +12,7 @@ describe('Utils Module', function () {

// I'm getting the current offset, because if the test server has another TZ, this tests will crash
before(function () {
var currentOffset = (moment('2016-01-01').utcOffset() / 60);
var currentOffset = (dayjs('2016-01-01').utcOffset() / 60);

stringOffset = (currentOffset < 0) ? '-' : '+';

Expand All @@ -34,11 +36,11 @@ describe('Utils Module', function () {
});

it('Invalid Date Format (string)', function () {
assert.throws(mp.utils.date.from.bind(mp.utils, '2016/01/01'), 'Invalid date sent');
assert.throws(mp.utils.date.from.bind(mp.utils.date, '2016/01/01'), 'Invalid date sent');
});

it('Invalid Date Format (empty object)', function () {
assert.throws(mp.utils.date.from.bind(mp.utils, {}), 'Invalid date sent');
assert.throws(mp.utils.date.from.bind(mp.utils.date, {}), 'Invalid date sent');
});

it('Valid date with Date Object', function () {
Expand Down

0 comments on commit 38a243c

Please sign in to comment.