在JavaScript中重构上个月的第一天和最后一天的代码(node.js)
Refactoring code of first and last day of previous month in JavaScript (node.js)
我需要得到上个月的第一天和最后一天(例如startDate = "2016-04-01"
和endDate = "2016-04-30"
)。我提出了解决方案,但有点不清楚。有人知道我如何重构下面的代码吗?因为我不得不在几个地方使用这个,而且我不想违反规则——DRY。(目前var startDate
和var 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);
在请求响应中。这意味着您每次都从当前日期开始。
希望能有所帮助!
相关文章:
- 重构DRY代码的Node.JS回调
- 如何在不编写样板代码的情况下在Node中创建可重用函数
- 在JavaScript中重构上个月的第一天和最后一天的代码(node.js)
- Node.js-等待process.exit()在前面的代码完成后执行
- 即使被信号处理程序中断,node.js代码也会一直运行到完成吗
- WebStorm,使用Node Supervisor(因此不必在每次代码更改后重新启动)
- Node.js Promise - 代码不应该等到 .then 完成()
- 如何在 Node.js 中返回 JSON 后停止代码流
- 修改此node.js代码以输出.wav而不是.mp4
- Node.js:集群中具有差异代码的Worker
- 如何仅在通过node.js运行script.js时激发代码
- Node.js&两个请求之间的Express/Can上下文切换可以是同步代码
- 只有从Node运行时,Rsync才会失败,退出代码为23
- node.js nodeschool learnyounode需要帮助理解解决方案的代码
- 从node.js代码调用javascript代码
- 如何在node.js和MongoDb中混合javascript中的同步和异步代码
- node.js中的自修改代码可以集群工作
- 将JavaScript代码迁移到具有String.prototype扩展名的node.js模块
- node.js和Cygein,我该如何运行代码
- 解析服务器云代码Node.js兼容性