Skip to content

Commit

Permalink
style: modify code style of computed
Browse files Browse the repository at this point in the history
  • Loading branch information
Hzy0913 committed Feb 2, 2021
1 parent 22f1c0a commit 62bb075
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 62 deletions.
100 changes: 42 additions & 58 deletions src/components/timetable/computed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computedNextMonth, computedPrevMonth, getToday } from '../utils'
import { computedNextMonth, computedPrevMonth, getToday, computedNextYear } from '../utils';

function date2ymd(date: string): number[] {
const [y, m, d] = date.split('-');
Expand All @@ -13,13 +13,13 @@ function date2timeStamp(date: string): number {
function getLunarInfo(y: string, m: string, d: string, lunar: any) {
const date = `${y}-${m}-${d}`;
if (!lunar) {
return { date };
return { date };
}

const lunarInfo = lunar.solar2lunar(y, m, d) as any;
const { Term, lMonth, lDay, lYear } = lunarInfo || {};
const { lunarHoliday, gregorianHoliday } = lunar || {};
let lunarValue = lunarInfo.IDayCn;
const lunarValue = lunarInfo.IDayCn;
const yearEve = lMonth === 12 && lDay === lunar.monthDays(lYear, 12) ? '除夕' : undefined;

const lunarInfoObj = {
Expand All @@ -32,41 +32,35 @@ function getLunarInfo(y: string, m: string, d: string, lunar: any) {
return lunarInfoObj;
}

const setRemark = (function() {
const setRemark = (function () {
let remarksInfo: any = {};

return function() {
return function () {
return {
update(remarks: any = {}) {
remarksInfo = remarks;
},
getRemark(date: string) {
if (remarksInfo) return {
remark: remarksInfo[date]
};
if (remarksInfo) {
return {
remark: remarksInfo[date]
};
}
}
}
}
})()

function computedPrevYear(year: string | number, month: string | number): number {
if ((Number(month) - 1 - 1) < 0) {
return Number(year) - 1;
}

return +year;
}
};
};
}());

function computedPrevDay(year: string, month: string, day: string | number): string {
if ((Number(day) - 1) === 0) {
const prevMonth = computedPrevMonth(month);
if (prevMonth === 12) { //prev year
const prevYear = Number(year) - 1;
const prevDay = new Date(prevYear, prevMonth - 2, 0).getDate()
return `${prevYear}-${prevMonth}-${prevDay}`
const prevDay = new Date(prevYear, prevMonth - 2, 0).getDate();
return `${prevYear}-${prevMonth}-${prevDay}`;
} else { //current year and prev month
const prevDay = new Date(Number(year), prevMonth, 0).getDate()
return `${year}-${prevMonth}-${prevDay}`
const prevDay = new Date(Number(year), prevMonth, 0).getDate();
return `${year}-${prevMonth}-${prevDay}`;
}
} else {
return `${year}-${month}-${Number(day) - 1}`;
Expand All @@ -80,24 +74,17 @@ function computedNextDay(year: string, month: string, day: string): string {
const nextMonth = computedNextMonth(month);
if (nextMonth === 1) { //next year
const nextYear = computedNextYear(year, month);
const nextDay = new Date(nextYear, 0, 1).getDate()
const nextDay = new Date(nextYear, 0, 1).getDate();
return `${nextYear}-1-${nextDay}`;
} else { //current year and next month
const nextDay = new Date(Number(year), nextMonth - 1, 1).getDate()
return `${year}-${nextMonth}-${nextDay}`
const nextDay = new Date(Number(year), nextMonth - 1, 1).getDate();
return `${year}-${nextMonth}-${nextDay}`;
}
} else {
return `${year}-${month}-${Number(day) + 1}`;
}
}

function computedNextYear(year: string | number, month: string | number): number {
if ((Number(month) + 1) > 12) {
return Number(year) + 1;
}
return Number(year);
}

type rangeOptionType = {
date: string;
isWeekMode: boolean;
Expand All @@ -111,14 +98,14 @@ function isCurrentMonthToday(date: string) {
return todayString === date;
}

const rangeOption = function({selectDate, date}: any) {
function rangeOption({selectDate, date}: any) {
const { start, end } = selectDate;
if (start === date) {
const notCompleteClassName = end ? '' : ' selected-range-not-complete';
return 'vc-day-selected selected-range-start' + notCompleteClassName;
}
if (end === date) {
return 'vc-day-selected selected-range-end'
return 'vc-day-selected selected-range-end';
}

if (start && end && date) {
Expand All @@ -127,12 +114,12 @@ const rangeOption = function({selectDate, date}: any) {
const currentTimeStamp: number = date2timeStamp(date);

if (startTimeStamp < currentTimeStamp && currentTimeStamp < endTimeStamp) {
return 'vc-day-selected selected-range-includes'
return 'vc-day-selected selected-range-includes';
}
}
}

const multiRangeOption = function({selectDate = [], date}: any) {
function multiRangeOption({selectDate = [], date}: any) {
let className;
selectDate.some((selectItem: any) => {
const { start, end } = selectItem;
Expand All @@ -142,7 +129,7 @@ const multiRangeOption = function({selectDate = [], date}: any) {
return true;
}
if (end === date) {
className = 'vc-day-selected selected-range-end'
className = 'vc-day-selected selected-range-end';
return true;
}

Expand All @@ -160,13 +147,14 @@ const multiRangeOption = function({selectDate = [], date}: any) {
return className;
}

const multiOption = function({selectDate, date, begin, end, isWeekMode, rangeDate, playload, weekSwitch, getEvents, getLunarInfo, disabledDateHandle}: any) {
console.log(selectDate.includes(date), 'selectDateselectDate11222')
function multiOption({selectDate, date}: any) {
return selectDate.includes(date) ? 'vc-day-selected' : undefined;
}

function selectOption({date, selectDate}: any) {
return selectDate === date ? 'vc-day-selected' : undefined;
}

type selectOptionType = {
date: string;
playload: any;
Expand All @@ -177,13 +165,12 @@ type selectOptionType = {
getEvents: (year: number, month: number, day: number) => any;
}


const disabledDate = (function() {
let disabledDate: any = {};
return function() {
const disabledDate = (function () {
let disabledDates: any = {};
return function () {
return {
update(disabled: any[]) {
disabledDate = disabled.reduce((previousValue, currentValue) => {
disabledDates = disabled.reduce((previousValue, currentValue) => {
previousValue[currentValue] = true;
return previousValue;
}, {});
Expand All @@ -192,18 +179,16 @@ const disabledDate = (function() {
return disabledDate;
},
isDisabled(date: string) {
console.log(disabledDate, date,'disabledDatedisabledDate')

return !!disabledDate[date];
return !!disabledDates[date];
}
}
}
})();
};
};
}());

const setTileContent = (function() {
const setTileContent = (function () {
let tileContentInfo: any = {};

return function() {
return function () {
return {
update(tileContent: any) {
tileContentInfo = tileContent || [];
Expand All @@ -213,9 +198,9 @@ const setTileContent = (function() {
tileContent: (tileContentInfo || {})[date]
};
}
}
}
})();
};
};
}());

export {
getToday,
Expand All @@ -227,12 +212,11 @@ export {
setRemark,
setTileContent,
getLunarInfo,
computedPrevYear,
computedPrevMonth,
computedNextYear,
isCurrentMonthToday,
date2timeStamp,
computedNextDay,
computedPrevDay,
date2ymd,
}
};
4 changes: 2 additions & 2 deletions src/components/timetable/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function multiSelect(selectDate: string[] = [], date: string) {
return selectDate;
}

function rangeSelect(selectDate: { start: string, end: string } , date: string) {
function rangeSelect(selectDate: { start?: string, end?: string } , date: string) {
const { start, end } = selectDate;

if (start && end) {
Expand Down Expand Up @@ -45,7 +45,7 @@ function rangeSelect(selectDate: { start: string, end: string } , date: string)
}
}

function multiRange(selectDates: { start: string; end?: string }[], date: string) {
function multiRange(selectDates: { start?: string; end?: string }[], date: string) {
const selects = [...selectDates];
console.log(selects, 'selectsselects')
let deleteIndex;
Expand Down
4 changes: 2 additions & 2 deletions src/components/timetable/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@
import { ref, reactive, watch, toRefs } from 'vue';
import { disabledDate, computedPrevDay, date2ymd, selectOption, multiOption, rangeOption,
multiRangeOption, getLunarInfo, isCurrentMonthToday, setTileContent, setRemark,
computedPrevYear, computedPrevMonth, computedNextYear, computedNextDay
computedPrevMonth, computedNextYear, computedNextDay
} from './computed';
import { rangeSelect, singleSelect, multiSelect, multiRange } from './controller';
import { computedNextMonth } from '../utils';
import { computedNextMonth, computedPrevYear } from '../utils';
import { TimeTableInterface } from './declare';
import './style.less';
Expand Down

0 comments on commit 62bb075

Please sign in to comment.