Skip to content

Commit

Permalink
releases 3.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Jan 3, 2025
1 parent e2c003c commit 986d479
Show file tree
Hide file tree
Showing 5 changed files with 667 additions and 65 deletions.
2 changes: 2 additions & 0 deletions func/getMonthWeek.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var helperCreateGetDateWeek = require('./helperCreateGetDateWeek')
*/
var getMonthWeek = helperCreateGetDateWeek(function (targetDate) {
return new Date(targetDate.getFullYear(), targetDate.getMonth(), 1)
}, function (date1, date2) {
return date1.getMonth() !== date2.getMonth()
})

module.exports = getMonthWeek
2 changes: 2 additions & 0 deletions func/getYearWeek.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var helperCreateGetDateWeek = require('./helperCreateGetDateWeek')
*/
var getYearWeek = helperCreateGetDateWeek(function (targetDate) {
return new Date(targetDate.getFullYear(), 0, 1)
}, function (date1, date2) {
return date1.getFullYear() !== date2.getFullYear()
})

module.exports = getYearWeek
58 changes: 48 additions & 10 deletions func/helperCreateGetDateWeek.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,66 @@
var setupDefaults = require('./setupDefaults')

var staticDayTime = require('./staticDayTime')
var staticWeekTime = require('./staticWeekTime')

var isNumber = require('./isNumber')
var includes = require('./includes')
var toStringDate = require('./toStringDate')
var isValidDate = require('./isValidDate')
var getWhatWeek = require('./getWhatWeek')
var range = require('./range')
var map = require('./map')

var helperGetDateTime = require('./helperGetDateTime')

function helperCreateGetDateWeek (getStartDate) {
var nextStartMaps = map(range(0, 7), function (day) {
return [(day + 1) % 7, (day + 2) % 7, (day + 3) % 7]
})

function matchWeekStartDay (time, viewStartDay) {
var day = new Date(time).getDay()
return includes(nextStartMaps[viewStartDay], day)
}

function helperCreateGetDateWeek (getStartDate, checkCrossDate) {
return function (date, firstDay) {
var viewStartDay = isNumber(firstDay) ? firstDay : setupDefaults.firstDayOfWeek
var targetDate = getWhatWeek(date, 0, viewStartDay, viewStartDay)
var targetDate = toStringDate(date)
if (isValidDate(targetDate)) {
var targetOffsetDate = new Date(targetDate.getFullYear(), targetDate.getMonth(), targetDate.getDate())
var targerStartDate = getStartDate(targetDate)
var targetFirstDay = targerStartDate.getDay()
if (targetFirstDay > viewStartDay) {
targerStartDate.setDate(7 - targetFirstDay + viewStartDay + 1)
var targetWeekStartDate = getWhatWeek(targetDate, 0, viewStartDay, viewStartDay)
var firstDate = getStartDate(targetWeekStartDate)
var firstTime = helperGetDateTime(firstDate)
var targetWeekStartTime = helperGetDateTime(targetWeekStartDate)
var targetWeekEndTime = targetWeekStartTime + staticDayTime * 6
var targetWeekEndDate = new Date(targetWeekEndTime)
var firstWeekStartDate = getWhatWeek(firstDate, 0, viewStartDay, viewStartDay)
var firstWeekStartTime = helperGetDateTime(firstWeekStartDate)
var tempTime
if (targetWeekStartTime === firstWeekStartTime) {
return 1
}
if (checkCrossDate(targetWeekStartDate, targetWeekEndDate)) {
tempTime = helperGetDateTime(getStartDate(targetWeekEndDate))
for (; tempTime < targetWeekEndTime; tempTime += staticDayTime) {
if (matchWeekStartDay(tempTime, viewStartDay)) {
return 1
}
}
}
if (targetFirstDay < viewStartDay) {
targerStartDate.setDate(viewStartDay - targetFirstDay + 1)
var firstWeekEndTime = firstWeekStartTime + staticDayTime * 6
var firstWeekEndDate = new Date(targetWeekEndTime)
var offsetNum = 1
if (checkCrossDate(firstWeekStartDate, firstWeekEndDate)) {
offsetNum = 0
tempTime = firstTime
for (; tempTime < firstWeekEndTime; tempTime += staticDayTime) {
if (matchWeekStartDay(tempTime, viewStartDay)) {
offsetNum++
break
}
}
}
return Math.floor((helperGetDateTime(targetOffsetDate) - helperGetDateTime(targerStartDate)) / staticWeekTime + 1)
return Math.floor((targetWeekStartTime - firstWeekStartTime) / staticWeekTime) + offsetNum
}
return NaN
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xe-utils",
"version": "3.6.0",
"version": "3.7.0",
"description": "JavaScript 函数库、工具类",
"main": "index.js",
"unpkg": "dist/xe-utils.umd.min.js",
Expand Down
Loading

0 comments on commit 986d479

Please sign in to comment.