mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 23:47:33 +08:00
code refactor
This commit is contained in:
+60
-44
@@ -4,7 +4,8 @@ import { type unitOfTime } from 'moment/moment';
|
||||
import {
|
||||
type YearUnixTime,
|
||||
type YearQuarter,
|
||||
type YearMonth,
|
||||
type Year0BasedMonth,
|
||||
type Year1BasedMonth,
|
||||
type YearMonthRange,
|
||||
type TimeRange,
|
||||
type TimeRangeAndDateType,
|
||||
@@ -34,24 +35,24 @@ import {
|
||||
|
||||
type SupportedDate = Date | moment.Moment;
|
||||
|
||||
export function isYearMonthValid(year: number, month: number): boolean {
|
||||
if (!isNumber(year) || !isNumber(month)) {
|
||||
export function isYear0BasedMonthValid(year: number, month0base: number): boolean {
|
||||
if (!isNumber(year) || !isNumber(month0base)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return year > 0 && month >= 0 && month <= 11;
|
||||
return year > 0 && month0base >= 0 && month0base <= 11;
|
||||
}
|
||||
|
||||
export function getYearMonthObjectFromUnixTime(unixTime: number): YearMonth {
|
||||
export function getYear0BasedMonthObjectFromUnixTime(unixTime: number): Year0BasedMonth {
|
||||
const datetime = moment.unix(unixTime);
|
||||
|
||||
return {
|
||||
year: datetime.year(),
|
||||
month: datetime.month()
|
||||
month0base: datetime.month()
|
||||
};
|
||||
}
|
||||
|
||||
export function getYearMonthObjectFromString(yearMonth: string): YearMonth | null {
|
||||
export function getYear0BasedMonthObjectFromString(yearMonth: string): Year0BasedMonth | null {
|
||||
if (!isString(yearMonth)) {
|
||||
return null;
|
||||
}
|
||||
@@ -63,24 +64,24 @@ export function getYearMonthObjectFromString(yearMonth: string): YearMonth | nul
|
||||
}
|
||||
|
||||
const year = parseInt(items[0]);
|
||||
const month = parseInt(items[1]) - 1;
|
||||
const month0base = parseInt(items[1]) - 1;
|
||||
|
||||
if (!isYearMonthValid(year, month)) {
|
||||
if (!isYear0BasedMonthValid(year, month0base)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
year: year,
|
||||
month: month
|
||||
month0base: month0base
|
||||
};
|
||||
}
|
||||
|
||||
export function getYearMonthStringFromObject(yearMonth: YearMonth | null): string {
|
||||
if (!yearMonth || !isYearMonthValid(yearMonth.year, yearMonth.month)) {
|
||||
export function getYearMonthStringFromYear0BasedMonthObject(yearMonth: Year0BasedMonth | null): string {
|
||||
if (!yearMonth || !isYear0BasedMonthValid(yearMonth.year, yearMonth.month0base)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return `${yearMonth.year}-${yearMonth.month + 1}`;
|
||||
return `${yearMonth.year}-${yearMonth.month0base + 1}`;
|
||||
}
|
||||
|
||||
export function getTwoDigitsString(value: number): string {
|
||||
@@ -375,40 +376,55 @@ export function getQuarterLastUnixTime(yearQuarter: YearQuarter): number {
|
||||
return moment.unix(getQuarterFirstUnixTime(yearQuarter)).add(3, 'months').subtract(1, 'seconds').unix();
|
||||
}
|
||||
|
||||
export function getYearMonthFirstUnixTime(yearMonth: YearMonth | string): number {
|
||||
let yearMonthObj: YearMonth | null = null;
|
||||
export function getYearMonthFirstUnixTime(yearMonth: Year0BasedMonth | Year1BasedMonth | string): number {
|
||||
let yearMonthObj: Year0BasedMonth | null = null;
|
||||
|
||||
if (isString(yearMonth)) {
|
||||
yearMonthObj = getYearMonthObjectFromString(yearMonth);
|
||||
} else if (isObject(yearMonth) && isYearMonthValid(yearMonth.year, yearMonth.month)) {
|
||||
yearMonthObj = getYear0BasedMonthObjectFromString(yearMonth);
|
||||
} else if (isObject(yearMonth) && ('month0base' in yearMonth) && isYear0BasedMonthValid(yearMonth.year, yearMonth.month0base)) {
|
||||
yearMonthObj = yearMonth;
|
||||
} else if (isObject(yearMonth) && ('month1base' in yearMonth) && isYear0BasedMonthValid(yearMonth.year, yearMonth.month1base - 1)) {
|
||||
yearMonthObj = {
|
||||
year: yearMonth.year,
|
||||
month0base: yearMonth.month1base - 1
|
||||
};
|
||||
}
|
||||
|
||||
if (!yearMonthObj) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return moment().set({ year: yearMonthObj.year, month: yearMonthObj.month, date: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }).unix();
|
||||
return moment().set({ year: yearMonthObj.year, month: yearMonthObj.month0base, date: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }).unix();
|
||||
}
|
||||
|
||||
export function getYearMonthLastUnixTime(yearMonth: YearMonth | string): number {
|
||||
export function getYearMonthLastUnixTime(yearMonth: Year0BasedMonth | Year1BasedMonth | string): number {
|
||||
return moment.unix(getYearMonthFirstUnixTime(yearMonth)).add(1, 'months').subtract(1, 'seconds').unix();
|
||||
}
|
||||
|
||||
export function getStartEndYearMonthRange(startYearMonth: YearMonth | string, endYearMonth: YearMonth | string): YearMonthRange | null {
|
||||
let startYearMonthObj: YearMonth | null = null;
|
||||
let endYearMonthObj: YearMonth | null = null;
|
||||
export function getStartEndYearMonthRange(startYearMonth: Year0BasedMonth | Year1BasedMonth | string, endYearMonth: Year0BasedMonth | Year1BasedMonth | string): YearMonthRange | null {
|
||||
let startYearMonthObj: Year0BasedMonth | null = null;
|
||||
let endYearMonthObj: Year0BasedMonth | null = null;
|
||||
|
||||
if (isString(startYearMonth)) {
|
||||
startYearMonthObj = getYearMonthObjectFromString(startYearMonth);
|
||||
} else if (isObject(startYearMonth)) {
|
||||
startYearMonthObj = getYear0BasedMonthObjectFromString(startYearMonth);
|
||||
} else if (isObject(startYearMonth) && ('month0base' in startYearMonth)) {
|
||||
startYearMonthObj = startYearMonth;
|
||||
} else if (isObject(startYearMonth) && ('month1base' in startYearMonth)) {
|
||||
startYearMonthObj = {
|
||||
year: startYearMonth.year,
|
||||
month0base: startYearMonth.month1base - 1
|
||||
};
|
||||
}
|
||||
|
||||
if (isString(endYearMonth)) {
|
||||
endYearMonthObj = getYearMonthObjectFromString(endYearMonth);
|
||||
} else {
|
||||
endYearMonthObj = getYear0BasedMonthObjectFromString(endYearMonth);
|
||||
} else if (isObject(endYearMonth) && ('month0base' in endYearMonth)) {
|
||||
endYearMonthObj = endYearMonth;
|
||||
} else if (isObject(endYearMonth) && ('month1base' in endYearMonth)) {
|
||||
endYearMonthObj = {
|
||||
year: endYearMonth.year,
|
||||
month0base: endYearMonth.month1base - 1
|
||||
};
|
||||
}
|
||||
|
||||
if (!startYearMonthObj || !endYearMonthObj) {
|
||||
@@ -421,7 +437,7 @@ export function getStartEndYearMonthRange(startYearMonth: YearMonth | string, en
|
||||
};
|
||||
}
|
||||
|
||||
export function getAllYearsStartAndEndUnixTimes(startYearMonth: YearMonth | string, endYearMonth: YearMonth | string): YearUnixTime[] {
|
||||
export function getAllYearsStartAndEndUnixTimes(startYearMonth: Year0BasedMonth | Year1BasedMonth | string, endYearMonth: Year0BasedMonth | Year1BasedMonth | string): YearUnixTime[] {
|
||||
const allYearTimes: YearUnixTime[] = [];
|
||||
const range = getStartEndYearMonthRange(startYearMonth, endYearMonth);
|
||||
|
||||
@@ -442,7 +458,7 @@ export function getAllYearsStartAndEndUnixTimes(startYearMonth: YearMonth | stri
|
||||
return allYearTimes;
|
||||
}
|
||||
|
||||
export function getAllFiscalYearsStartAndEndUnixTimes(startYearMonth: YearMonth | string, endYearMonth: YearMonth | string, fiscalYearStartValue: number): FiscalYearUnixTime[] {
|
||||
export function getAllFiscalYearsStartAndEndUnixTimes(startYearMonth: Year0BasedMonth | Year1BasedMonth | string, endYearMonth: Year0BasedMonth | Year1BasedMonth | string, fiscalYearStartValue: number): FiscalYearUnixTime[] {
|
||||
// user selects date range: start=2024-01 and end=2026-12
|
||||
// result should be 4x FiscalYearUnixTime made up of:
|
||||
// - 2024-01->2024-06 (FY 24) - input start year-month->end of fiscal year in which the input start year-month falls
|
||||
@@ -468,7 +484,7 @@ export function getAllFiscalYearsStartAndEndUnixTimes(startYearMonth: YearMonth
|
||||
// Loop over 1 year before and 1 year after the input date range
|
||||
// to include fiscal years that start in the previous calendar year.
|
||||
for (let year = range.startYearMonth.year - 1; year <= range.endYearMonth.year + 1; year++) {
|
||||
const thisYearMonthUnixTime = getYearMonthFirstUnixTime({ year: year, month: fiscalYearStart.month - 1 });
|
||||
const thisYearMonthUnixTime = getYearMonthFirstUnixTime({ year: year, month1base: fiscalYearStart.month });
|
||||
const fiscalStartTime = getFiscalYearStartUnixTime(thisYearMonthUnixTime, fiscalYearStart.value);
|
||||
const fiscalEndTime = getFiscalYearEndUnixTime(thisYearMonthUnixTime, fiscalYearStart.value);
|
||||
|
||||
@@ -504,7 +520,7 @@ export function getAllFiscalYearsStartAndEndUnixTimes(startYearMonth: YearMonth
|
||||
return allFiscalYearTimes;
|
||||
}
|
||||
|
||||
export function getAllQuartersStartAndEndUnixTimes(startYearMonth: YearMonth | string, endYearMonth: YearMonth | string): YearQuarterUnixTime[] {
|
||||
export function getAllQuartersStartAndEndUnixTimes(startYearMonth: Year0BasedMonth | Year1BasedMonth | string, endYearMonth: Year0BasedMonth | Year1BasedMonth | string): YearQuarterUnixTime[] {
|
||||
const allYearQuarterTimes: YearQuarterUnixTime[] = [];
|
||||
const range = getStartEndYearMonthRange(startYearMonth, endYearMonth);
|
||||
|
||||
@@ -512,10 +528,10 @@ export function getAllQuartersStartAndEndUnixTimes(startYearMonth: YearMonth | s
|
||||
return allYearQuarterTimes;
|
||||
}
|
||||
|
||||
for (let year = range.startYearMonth.year, month = range.startYearMonth.month; year < range.endYearMonth.year || (year === range.endYearMonth.year && ((month / 3) <= (range.endYearMonth.month / 3))); ) {
|
||||
for (let year = range.startYearMonth.year, month0base = range.startYearMonth.month0base; year < range.endYearMonth.year || (year === range.endYearMonth.year && ((month0base / 3) <= (range.endYearMonth.month0base / 3))); ) {
|
||||
const yearQuarter: YearQuarter = {
|
||||
year: year,
|
||||
quarter: Math.floor((month / 3)) + 1
|
||||
quarter: Math.floor((month0base / 3)) + 1
|
||||
};
|
||||
|
||||
const minUnixTime = getQuarterFirstUnixTime(yearQuarter);
|
||||
@@ -523,22 +539,22 @@ export function getAllQuartersStartAndEndUnixTimes(startYearMonth: YearMonth | s
|
||||
|
||||
allYearQuarterTimes.push(YearQuarterUnixTime.of(yearQuarter, minUnixTime, maxUnixTime));
|
||||
|
||||
if (year === range.endYearMonth.year && month >= range.endYearMonth.month) {
|
||||
if (year === range.endYearMonth.year && month0base >= range.endYearMonth.month0base) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (month >= 9) {
|
||||
if (month0base >= 9) {
|
||||
year++;
|
||||
month = 0;
|
||||
month0base = 0;
|
||||
} else {
|
||||
month += 3;
|
||||
month0base += 3;
|
||||
}
|
||||
}
|
||||
|
||||
return allYearQuarterTimes;
|
||||
}
|
||||
|
||||
export function getAllMonthsStartAndEndUnixTimes(startYearMonth: YearMonth | string, endYearMonth: YearMonth | string): YearMonthUnixTime[] {
|
||||
export function getAllMonthsStartAndEndUnixTimes(startYearMonth: Year0BasedMonth | Year1BasedMonth | string, endYearMonth: Year0BasedMonth | Year1BasedMonth | string): YearMonthUnixTime[] {
|
||||
const allYearMonthTimes: YearMonthUnixTime[] = [];
|
||||
const range = getStartEndYearMonthRange(startYearMonth, endYearMonth);
|
||||
|
||||
@@ -546,10 +562,10 @@ export function getAllMonthsStartAndEndUnixTimes(startYearMonth: YearMonth | str
|
||||
return allYearMonthTimes;
|
||||
}
|
||||
|
||||
for (let year = range.startYearMonth.year, month = range.startYearMonth.month; year <= range.endYearMonth.year || month <= range.endYearMonth.month; ) {
|
||||
const yearMonth: YearMonth = {
|
||||
for (let year = range.startYearMonth.year, month0base = range.startYearMonth.month0base; year <= range.endYearMonth.year || month0base <= range.endYearMonth.month0base; ) {
|
||||
const yearMonth: Year0BasedMonth = {
|
||||
year: year,
|
||||
month: month
|
||||
month0base: month0base
|
||||
};
|
||||
|
||||
const minUnixTime = getYearMonthFirstUnixTime(yearMonth);
|
||||
@@ -557,15 +573,15 @@ export function getAllMonthsStartAndEndUnixTimes(startYearMonth: YearMonth | str
|
||||
|
||||
allYearMonthTimes.push(YearMonthUnixTime.of(yearMonth, minUnixTime, maxUnixTime));
|
||||
|
||||
if (year === range.endYearMonth.year && month === range.endYearMonth.month) {
|
||||
if (year === range.endYearMonth.year && month0base === range.endYearMonth.month0base) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (month >= 11) {
|
||||
if (month0base >= 11) {
|
||||
year++;
|
||||
month = 0;
|
||||
month0base = 0;
|
||||
} else {
|
||||
month++;
|
||||
month0base++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user