@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
172 lines (162 loc) • 5.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toDateObject = exports.serializeMode = exports.getMonthDays = exports.getDateBoundary = exports.formatDate = exports.COLUMN_SERIALIZE_TYPES = void 0;
var _isDate = _interopRequireDefault(require("lodash/isDate"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* 可选项序列数组
*/
const COLUMN_SERIALIZE_TYPES = exports.COLUMN_SERIALIZE_TYPES = ['Y', 'M', 'D', 'h', 'm', 's'];
const serializeMode = modes => {
if (modes.length === 1) {
return modes;
}
// 起点终点
const startIndex = COLUMN_SERIALIZE_TYPES.findIndex(k => k === modes[0]);
const endIndex = COLUMN_SERIALIZE_TYPES.findIndex(k => k === modes[1]);
const serializeModes = [];
for (let index = startIndex; index <= endIndex; index++) {
serializeModes.push(COLUMN_SERIALIZE_TYPES[index]);
}
return serializeModes;
};
exports.serializeMode = serializeMode;
const toDateObject = d => ({
Y: d.getFullYear(),
M: d.getMonth(),
D: d.getDate(),
h: d.getHours(),
m: d.getMinutes(),
s: d.getSeconds()
});
/**
* 获取某年的某个月有多少天
* @param year 年
* @param month 月 1~12
* @returns 天数
*/
exports.toDateObject = toDateObject;
const getMonthDays = (year, month) => {
const day = new Date();
day.setFullYear(year);
day.setDate(1);
day.setMonth(month, 0);
return day.getDate();
};
exports.getMonthDays = getMonthDays;
const getDateBoundary = (value, modes, {
defaultMin,
defaultMax,
min,
max
}) => {
const minDefined = (0, _isDate.default)(min);
const maxDefined = (0, _isDate.default)(max);
const minDateObject = toDateObject(minDefined ? min : defaultMin);
const maxDateObject = toDateObject(maxDefined ? max : defaultMax);
const valueDateObject = toDateObject(value);
// 边界值
const isInMinYear = valueDateObject.Y === minDateObject.Y;
const isInMaxYear = valueDateObject.Y === maxDateObject.Y;
const isInMinMonth = isInMinYear && valueDateObject.M <= minDateObject.M;
const isInMaxMonth = isInMaxYear && valueDateObject.M >= maxDateObject.M;
const isInMinDate = isInMinMonth && valueDateObject.D <= minDateObject.D;
const isInMaxDate = isInMaxMonth && valueDateObject.D >= maxDateObject.D;
const isInMinHour = isInMinDate && valueDateObject.h <= minDateObject.h;
const isInMaxHour = isInMaxDate && valueDateObject.h >= maxDateObject.h;
const isInMinMinute = isInMinHour && valueDateObject.m <= minDateObject.m;
const isInMaxMinute = isInMaxHour && valueDateObject.m >= maxDateObject.m;
const boundary = {
Y: [minDateObject.Y, maxDateObject.Y],
M: (() => {
if (modes[0] === 'M') {
// 从月开始选
// 有限制时间根据限制时间选择,没有默认所有月份都可以
const a = minDefined ? minDateObject.M : 0;
const b = maxDefined ? maxDateObject.M : 11;
// a < b
if (a >= b) {
return [0, 11];
}
return [a, b];
}
const a = isInMinYear ? minDateObject.M : 0;
const b = isInMaxYear ? maxDateObject.M : 11;
return [a, b];
})(),
D: (() => {
if (modes[0] === 'D') {
const a = minDefined ? minDateObject.D : 1;
const b = maxDefined ? maxDateObject.D : getMonthDays(valueDateObject.Y, valueDateObject.M + 1);
// a < b
if (a >= b) {
return [1, getMonthDays(valueDateObject.Y, valueDateObject.M + 1)];
}
return [a, b];
}
const a = isInMinMonth ? minDateObject.D : 1;
const b = isInMaxMonth ? maxDateObject.D : getMonthDays(valueDateObject.Y, valueDateObject.M + 1);
return [a, b];
})(),
h: (() => {
if (modes[0] === 'h') {
const a = minDefined ? minDateObject.h : 0;
const b = maxDefined ? maxDateObject.h : 23;
// a < b
if (a >= b) {
return [0, 23];
}
return [a, b];
}
const a = isInMinDate ? minDateObject.h : 0;
const b = isInMaxDate ? maxDateObject.h : 23;
return [a, b];
})(),
m: (() => {
if (modes[0] === 'm') {
const a = minDefined ? minDateObject.m : 0;
const b = maxDateObject ? maxDateObject.m : 59;
// a < b
if (a >= b) {
return [0, 59];
}
return [a, b];
}
const a = isInMinHour ? minDateObject.m : 0;
const b = isInMaxHour ? maxDateObject.m : 59;
return [a, b];
})(),
s: (() => {
if (modes[0] === 's') {
const a = minDefined ? minDateObject.s : 0;
const b = maxDateObject ? maxDateObject.s : 59;
// a < b
if (a >= b) {
return [0, 59];
}
return [a, b];
}
const a = isInMinMinute ? minDateObject.s : 0;
const b = isInMaxMinute ? maxDateObject.s : 59;
return [a, b];
})()
};
return boundary;
};
/**
* 格式化时间
*/
exports.getDateBoundary = getDateBoundary;
const formatDate = (mode, day) => {
const dayDateObject = toDateObject(day);
const modes = serializeMode(mode.split('-'));
const hasKey = k => modes.includes(k);
const padStart = n => `${n}`.padStart(2, '0');
const time1 = [hasKey('Y') ? dayDateObject.Y : null, hasKey('M') ? padStart(dayDateObject.M + 1) : null, hasKey('D') ? padStart(dayDateObject.D) : null].filter(Boolean).join('-');
const time2 = [hasKey('h') ? padStart(dayDateObject.h) : null, hasKey('m') ? padStart(dayDateObject.m) : null, hasKey('s') ? padStart(dayDateObject.s) : null].filter(Boolean).join(':');
return [time1, time2].filter(Boolean).join(' ');
};
exports.formatDate = formatDate;
//# sourceMappingURL=helper.js.map