在JavaScript中重构上个月的第一天和最后一天的代码(node.js)

Refactoring code of first and last day of previous month in JavaScript (node.js)

本文关键字:代码 node js 最后一天 第一天 JavaScript 重构 上个月      更新时间:2023-09-26

我需要得到上个月的第一天和最后一天(例如startDate = "2016-04-01"endDate = "2016-04-30")。我提出了解决方案,但有点不清楚。有人知道我如何重构下面的代码吗?因为我不得不在几个地方使用这个,而且我不想违反规则——DRY。(目前var startDatevar endDate在每个router.get中,正如你所看到的,这真的很复杂。代码将只在这个文件中使用,所以我的目标是简单形式的全局变量。

代码:

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
var lastDay = new Date(date.getFullYear(), date.getMonth(), 0);
var missingHours = ' 23:59:59'; // Because default date has format YYYY-MM-DD 00:00:00
router.get('/shop', function (req, res) {
    var startDate = req.query.startDate || firstDay.getFullYear() + '-' + ('0' + (firstDay.getMonth() + 1)).slice(-2) + '-' + ('0' + (firstDay.getDate())).slice(-2);
    var endDate = req.query.endDate || lastDay.getFullYear() + '-' + ('0' + (lastDay.getMonth() + 1)).slice(-2) + '-' + (lastDay.getDate());
    shop.prepareData(startDate, endDate + missingHours, function(err, data) {
    ...
    })
    }

在这种情况下,它不是关于Don't Repeat Yourself,而是关于标记您的代码正在做什么:-)

例如,这一行:

var startDate = req.query.startDate || firstDay.getFullYear() + '-' + ('0' + (firstDay.getMonth() + 1)).slice(-2) + '-' + ('0' + (firstDay.getDate())).slice(-2);

真的很难阅读。因此,我们可以提取一些辅助函数来为流程的每个阶段命名。

首先,我们在这里添加0个填充。

(firstDay.getMonth() + 1)).slice(-2)

因此,我们可以得出一个在短数字上加零的通用函数,例如:

function padNumber(number) {
  return ('0' + number).slice(-2);
}

这条线现在看起来是这样的:

var startDate = req.query.startDate || (
  firstDay.getFullYear() + '-' +
  padNumber(firstDay.getMonth() + 1) + '-' +
  padNumber(firstDay.getDate())
);

我们可以通过将这个串联的数字集变成一个数组来减少行中的噪声,然后使用array.prototype.join将数组中的项连接在一起。

var startDate = req.query.startDate || (
  [ firstDay.getFullYear(),
  padNumber(firstDay.getMonth() + 1),
  padNumber(firstDay.getDate()) ].join('-')
);

看看下一行,它正在做一件非常相似的事情,所以让我们再次使用一个通用函数。

function formatDate(date) {
  return [ date.getFullYear(), 
    padNumber(date.getMonth() + 1), 
    padNumber(date.getDate()) ].join('-')
}

这就整理了这两条长长的线:

var startDate = req.query.startDate || formatDate(firstDay);
var endDate = req.query.endDate || formatDate(lastDay);

此处的完整代码:

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
var lastDay = new Date(date.getFullYear(), date.getMonth(), 0);
var missingHours = ' 23:59:59'; // Because default date has format YYYY-MM-DD 00:00:00
function padNumber(number) {
  return ('0' + number).slice(-2);
}
function formatDate(date) {
  return [ date.getFullYear(),
           padNumber(date.getMonth() + 1), 
           padNumber(date.getDate()) ].join('-')
}
router.get('/shop', function (req, res) {
  var startDate = req.query.startDate || formatDate(firstDay);
  var endDate = req.query.endDate || formatDate(lastDay);
  shop.prepareData(startDate, endDate + missingHours, function(err, data) {
    ...
  })
}

顺便说一句,这会改变代码的功能,所以我不会把它包括在答案中,但我想你希望根据请求重新计算日期?否则,你总是会得到服务器启动的日期

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
var lastDay = new Date(date.getFullYear(), date.getMonth(), 0);

在请求响应中。这意味着您每次都从当前日期开始。

希望能有所帮助!