code refactor
This commit is contained in:
@@ -1,37 +1,21 @@
|
||||
/**
|
||||
* For a detailed explanation regarding each configuration property, visit:
|
||||
* https://jestjs.io/docs/configuration
|
||||
*/
|
||||
import { type JestConfigWithTsJest, createDefaultEsmPreset } from 'ts-jest';
|
||||
|
||||
import type { Config } from 'jest';
|
||||
const presetConfig = createDefaultEsmPreset({
|
||||
tsconfig: '<rootDir>/tsconfig.jest.json'
|
||||
});
|
||||
|
||||
const config: Config = {
|
||||
|
||||
// Automatically clear mock calls, instances, contexts and results before every test
|
||||
const config: JestConfigWithTsJest = {
|
||||
...presetConfig,
|
||||
clearMocks: true,
|
||||
|
||||
// Indicates whether the coverage information should be collected while executing the test
|
||||
collectCoverage: false,
|
||||
|
||||
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
|
||||
moduleNameMapper: {
|
||||
"^@/(.*)$": "<rootDir>/src/$1"
|
||||
},
|
||||
|
||||
// The test environment that will be used for testing
|
||||
testEnvironment: "node",
|
||||
|
||||
// The glob patterns Jest uses to detect test files
|
||||
testMatch: [
|
||||
"**/__tests__/**/*.[jt]s?(x)",
|
||||
"!**/__tests__/*_gen.[jt]s?(x)"
|
||||
],
|
||||
|
||||
// A map from regular expressions to paths to transformers
|
||||
transform: {
|
||||
'^.+\\.m?tsx?$': ['ts-jest', { useESM: true, isolatedModules: true }],
|
||||
},
|
||||
|
||||
]
|
||||
};
|
||||
|
||||
module.exports = config;
|
||||
export default config;
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
"build": "cross-env NODE_ENV=production vite build",
|
||||
"serve:dist": "vite preview",
|
||||
"lint": "tsc --noEmit && eslint . --fix",
|
||||
"test": "jest"
|
||||
"test": "cross-env TS_NODE_PROJECT=\"./tsconfig.jest.json\" jest"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mdi/js": "^7.4.47",
|
||||
|
||||
@@ -17,11 +17,25 @@ const (
|
||||
FISCAL_YEAR_START_INVALID FiscalYearStart = 0xFFFF // Invalid
|
||||
)
|
||||
|
||||
var MONTH_MAX_DAYS = []uint8{
|
||||
uint8(31), // January
|
||||
uint8(28), // February (Disallow fiscal year start on leap day)
|
||||
uint8(31), // March
|
||||
uint8(30), // April
|
||||
uint8(31), // May
|
||||
uint8(30), // June
|
||||
uint8(31), // July
|
||||
uint8(31), // August
|
||||
uint8(30), // September
|
||||
uint8(31), // October
|
||||
uint8(30), // November
|
||||
uint8(31), // December
|
||||
}
|
||||
|
||||
// NewFiscalYearStart creates a new FiscalYearStart from month and day values
|
||||
func NewFiscalYearStart(month uint8, day uint8) (FiscalYearStart, error) {
|
||||
month, day, err := validateMonthDay(month, day)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
if !isValidMonthDay(month, day) {
|
||||
return 0, errs.ErrFormatInvalid
|
||||
}
|
||||
|
||||
return FiscalYearStart(uint16(month)<<8 | uint16(day)), nil
|
||||
@@ -37,39 +51,27 @@ func (f FiscalYearStart) GetMonthDay() (uint8, uint8, error) {
|
||||
month := uint8(f >> 8)
|
||||
day := uint8(f & 0xFF)
|
||||
|
||||
return validateMonthDay(month, day)
|
||||
if !isValidMonthDay(month, day) {
|
||||
return 0, 0, errs.ErrFormatInvalid
|
||||
}
|
||||
|
||||
return month, day, nil
|
||||
}
|
||||
|
||||
// String returns a string representation of FiscalYearStart in MM/DD format
|
||||
func (f FiscalYearStart) String() string {
|
||||
month, day, err := f.GetMonthDay()
|
||||
|
||||
if err != nil {
|
||||
return "Invalid"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%02d-%02d", month, day)
|
||||
}
|
||||
|
||||
// validateMonthDay validates a month and day and returns them if valid
|
||||
func validateMonthDay(month uint8, day uint8) (uint8, uint8, error) {
|
||||
if month < 1 || month > 12 || day < 1 {
|
||||
return 0, 0, errs.ErrFormatInvalid
|
||||
}
|
||||
|
||||
maxDays := uint8(31)
|
||||
switch month {
|
||||
case 1, 3, 5, 7, 8, 10, 12: // January, March, May, July, August, October, December
|
||||
maxDays = 31
|
||||
case 4, 6, 9, 11: // April, June, September, November
|
||||
maxDays = 30
|
||||
case 2: // February
|
||||
maxDays = 28 // Disallow fiscal year start on leap day
|
||||
}
|
||||
|
||||
if day > maxDays {
|
||||
return 0, 0, errs.ErrFormatInvalid
|
||||
}
|
||||
|
||||
return month, day, nil
|
||||
// isValidMonthDay returns whether the specified month and day is valid
|
||||
func isValidMonthDay(month uint8, day uint8) bool {
|
||||
return uint8(1) <= month && month <= uint8(12) && uint8(1) <= day && day <= MONTH_MAX_DAYS[int(month)-1]
|
||||
}
|
||||
|
||||
// FiscalYearFormat represents the fiscal year format as a uint8
|
||||
|
||||
@@ -3,8 +3,9 @@ package core
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
||||
)
|
||||
|
||||
func TestNewFiscalYearStart_ValidMonthDay(t *testing.T) {
|
||||
|
||||
@@ -1,88 +1,95 @@
|
||||
import { computed } from 'vue';
|
||||
import { ref, computed } from 'vue';
|
||||
|
||||
import { FiscalYearStart } from '@/core/fiscalyear.ts';
|
||||
|
||||
import { useUserStore } from '@/stores/user.ts';
|
||||
import { arrangeArrayWithNewStartIndex } from '@/lib/common.ts';
|
||||
|
||||
import { useI18n } from '@/locales/helpers.ts';
|
||||
|
||||
import { arrangeArrayWithNewStartIndex } from '@/lib/common';
|
||||
import { useUserStore } from '@/stores/user.ts';
|
||||
|
||||
export interface FiscalYearStartSelectionBaseProps {
|
||||
export interface CommonFiscalYearStartSelectionProps {
|
||||
modelValue?: number;
|
||||
disabled?: boolean;
|
||||
readonly?: boolean;
|
||||
label?: string;
|
||||
}
|
||||
|
||||
export interface FiscalYearStartSelectionBaseEmits {
|
||||
export interface CommonFiscalYearStartSelectionEmits {
|
||||
(e: 'update:modelValue', value: number): void;
|
||||
}
|
||||
|
||||
export function useFiscalYearStartSelectionBase(props: FiscalYearStartSelectionBaseProps, emit: FiscalYearStartSelectionBaseEmits) {
|
||||
const { getAllMinWeekdayNames, formatMonthDayToLongDay, getCurrentFiscalYearStartFormatted } = useI18n();
|
||||
|
||||
const dayNames = computed<string[]>(() => arrangeArrayWithNewStartIndex(getAllMinWeekdayNames(), firstDayOfWeek.value));
|
||||
|
||||
const displayName = computed<string>(() => {
|
||||
const fy = FiscalYearStart.fromNumber(selectedFiscalYearStart.value);
|
||||
|
||||
if ( fy ) {
|
||||
return formatMonthDayToLongDay(fy.toMonthDashDayString())
|
||||
function getFiscalYearStartFromProps(props: CommonFiscalYearStartSelectionProps): number {
|
||||
if (!props.modelValue) {
|
||||
return FiscalYearStart.Default.value;
|
||||
}
|
||||
|
||||
return formatMonthDayToLongDay(FiscalYearStart.strictFromNumber(userStore.currentUserFiscalYearStart).toMonthDashDayString());
|
||||
const fiscalYearStart = FiscalYearStart.valueOf(props.modelValue);
|
||||
|
||||
});
|
||||
if (!fiscalYearStart) {
|
||||
return FiscalYearStart.Default.value;
|
||||
}
|
||||
|
||||
return fiscalYearStart.value;
|
||||
}
|
||||
|
||||
export function useFiscalYearStartSelectionBase(props: CommonFiscalYearStartSelectionProps) {
|
||||
const {
|
||||
getAllMinWeekdayNames,
|
||||
formatMonthDayToLongDay
|
||||
} = useI18n();
|
||||
|
||||
const userStore = useUserStore();
|
||||
|
||||
const disabledDates = (date: Date) => {
|
||||
// Disable February 29 (leap day)
|
||||
return date.getMonth() === 1 && date.getDate() === 29;
|
||||
};
|
||||
|
||||
const firstDayOfWeek = computed<number>(() => userStore.currentUserFirstDayOfWeek);
|
||||
const selectedFiscalYearStart = ref<number>(getFiscalYearStartFromProps(props));
|
||||
|
||||
const selectedFiscalYearStart = computed<number>(() => {
|
||||
return props.modelValue !== undefined ? props.modelValue : userStore.currentUserFiscalYearStart;
|
||||
const selectedFiscalYearStartValue = computed<string>({
|
||||
get: () => {
|
||||
const fiscalYearStart = FiscalYearStart.valueOf(selectedFiscalYearStart.value);
|
||||
|
||||
if (fiscalYearStart) {
|
||||
return fiscalYearStart.toMonthDashDayString();
|
||||
} else {
|
||||
return FiscalYearStart.Default.toMonthDashDayString();
|
||||
}
|
||||
},
|
||||
set: (value: string) => {
|
||||
const fiscalYearStart = FiscalYearStart.parse(value);
|
||||
|
||||
if (fiscalYearStart) {
|
||||
selectedFiscalYearStart.value = fiscalYearStart.value;
|
||||
} else {
|
||||
selectedFiscalYearStart.value = FiscalYearStart.Default.value;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const userStore = useUserStore();
|
||||
const displayFiscalYearStartDate = computed<string>(() => {
|
||||
let fiscalYearStart = FiscalYearStart.valueOf(selectedFiscalYearStart.value);
|
||||
|
||||
function selectedDisplayName(dateString: string): string {
|
||||
const fy = FiscalYearStart.fromMonthDashDayString(dateString);
|
||||
if ( fy ) {
|
||||
return formatMonthDayToLongDay(fy.toMonthDashDayString());
|
||||
}
|
||||
return displayName.value;
|
||||
if (!fiscalYearStart) {
|
||||
fiscalYearStart = FiscalYearStart.Default;
|
||||
}
|
||||
|
||||
function getModelValueToDateString(): string {
|
||||
const input = selectedFiscalYearStart.value;
|
||||
return formatMonthDayToLongDay(fiscalYearStart.toMonthDashDayString());
|
||||
});
|
||||
|
||||
const fy = FiscalYearStart.fromNumber(input);
|
||||
|
||||
if ( fy ) {
|
||||
return fy.toMonthDashDayString();
|
||||
}
|
||||
|
||||
return getCurrentFiscalYearStartFormatted();
|
||||
}
|
||||
|
||||
function getDateStringToModelValue(input: string): number {
|
||||
const fyString = FiscalYearStart.fromMonthDashDayString(input);
|
||||
if (fyString) {
|
||||
return fyString.value;
|
||||
}
|
||||
return userStore.currentUserFiscalYearStart;
|
||||
}
|
||||
const firstDayOfWeek = computed<number>(() => userStore.currentUserFirstDayOfWeek);
|
||||
const dayNames = computed<string[]>(() => arrangeArrayWithNewStartIndex(getAllMinWeekdayNames(), firstDayOfWeek.value));
|
||||
|
||||
return {
|
||||
// functions
|
||||
getDateStringToModelValue,
|
||||
getModelValueToDateString,
|
||||
selectedDisplayName,
|
||||
// computed states
|
||||
dayNames,
|
||||
displayName,
|
||||
// constants
|
||||
disabledDates,
|
||||
firstDayOfWeek,
|
||||
// states,
|
||||
selectedFiscalYearStart,
|
||||
}
|
||||
// computed states
|
||||
selectedFiscalYearStartValue,
|
||||
displayFiscalYearStartDate,
|
||||
firstDayOfWeek,
|
||||
dayNames
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,18 +3,16 @@
|
||||
persistent-placeholder
|
||||
:readonly="readonly"
|
||||
:disabled="disabled"
|
||||
:clearable="modelValue ? clearable : false"
|
||||
:label="label"
|
||||
:menu-props="{ contentClass: 'fiscal-year-start-select-menu' }"
|
||||
v-model="selectedDate"
|
||||
v-model="selectedFiscalYearStartValue"
|
||||
>
|
||||
<template #selection>
|
||||
<span class="text-truncate cursor-pointer">{{ displayName }}</span>
|
||||
<span class="text-truncate cursor-pointer">{{ displayFiscalYearStartDate }}</span>
|
||||
</template>
|
||||
|
||||
<template #no-data>
|
||||
<vue-date-picker inline vertical auto-apply hide-offset-dates disable-year-select
|
||||
ref="datepicker"
|
||||
<vue-date-picker inline auto-apply hide-offset-dates disable-year-select
|
||||
month-name-format="long"
|
||||
model-type="MM-dd"
|
||||
:clearable="false"
|
||||
@@ -23,8 +21,7 @@
|
||||
:week-start="firstDayOfWeek"
|
||||
:day-names="dayNames"
|
||||
:disabled-dates="disabledDates"
|
||||
:start-date="selectedDate"
|
||||
v-model="selectedDate"
|
||||
v-model="selectedFiscalYearStartValue"
|
||||
>
|
||||
<template #month="{ text }">
|
||||
{{ getMonthShortName(text) }}
|
||||
@@ -38,51 +35,46 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue';
|
||||
import { computed, watch } from 'vue';
|
||||
import { useTheme } from 'vuetify';
|
||||
import { useUserStore } from '@/stores/user.ts';
|
||||
import { ThemeType } from '@/core/theme.ts';
|
||||
import { arrangeArrayWithNewStartIndex } from '@/lib/common.ts';
|
||||
import {
|
||||
type FiscalYearStartSelectionBaseProps,
|
||||
type FiscalYearStartSelectionBaseEmits,
|
||||
useFiscalYearStartSelectionBase
|
||||
} from '@/components/base/FiscalYearStartSelectionBase.ts';
|
||||
|
||||
import { useI18n } from '@/locales/helpers.ts';
|
||||
|
||||
interface FiscalYearStartSelectProps extends FiscalYearStartSelectionBaseProps {
|
||||
disabled?: boolean;
|
||||
readonly?: boolean;
|
||||
clearable?: boolean;
|
||||
label?: string;
|
||||
}
|
||||
import {
|
||||
type CommonFiscalYearStartSelectionProps,
|
||||
type CommonFiscalYearStartSelectionEmits,
|
||||
useFiscalYearStartSelectionBase
|
||||
} from '@/components/base/FiscalYearStartSelectionBase.ts';
|
||||
|
||||
const props = defineProps<FiscalYearStartSelectProps>();
|
||||
const emit = defineEmits<FiscalYearStartSelectionBaseEmits>();
|
||||
import { ThemeType } from '@/core/theme.ts';
|
||||
|
||||
const props = defineProps<CommonFiscalYearStartSelectionProps>();
|
||||
const emit = defineEmits<CommonFiscalYearStartSelectionEmits>();
|
||||
|
||||
const { getMonthShortName } = useI18n();
|
||||
|
||||
const theme = useTheme();
|
||||
|
||||
const isDarkMode = computed<boolean>(() => theme.global.name.value === ThemeType.Dark);
|
||||
|
||||
// Get all base functionality
|
||||
const {
|
||||
dayNames,
|
||||
displayName,
|
||||
disabledDates,
|
||||
selectedFiscalYearStart,
|
||||
selectedFiscalYearStartValue,
|
||||
displayFiscalYearStartDate,
|
||||
firstDayOfWeek,
|
||||
getModelValueToDateString,
|
||||
getDateStringToModelValue,
|
||||
} = useFiscalYearStartSelectionBase(props, emit);
|
||||
dayNames
|
||||
} = useFiscalYearStartSelectionBase(props);
|
||||
|
||||
const selectedDate = computed<string>({
|
||||
get: () => {
|
||||
return getModelValueToDateString();
|
||||
},
|
||||
set: (value: string) => {
|
||||
emit('update:modelValue', getDateStringToModelValue(value));
|
||||
watch(() => props.modelValue, (newValue) => {
|
||||
if (newValue) {
|
||||
selectedFiscalYearStart.value = newValue;
|
||||
}
|
||||
});
|
||||
|
||||
watch(selectedFiscalYearStart, (newValue) => {
|
||||
emit('update:modelValue', newValue);
|
||||
});
|
||||
</script>
|
||||
|
||||
<style>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<f7-toolbar>
|
||||
<div class="swipe-handler"></div>
|
||||
<div class="left">
|
||||
<f7-link :text="tt('Clear')" @click="clear"></f7-link>
|
||||
<f7-link :text="tt('Reset')" @click="reset"></f7-link>
|
||||
</div>
|
||||
<div class="right">
|
||||
<f7-link :text="tt('Done')" @click="confirm"></f7-link>
|
||||
@@ -17,14 +17,13 @@
|
||||
model-type="MM-dd"
|
||||
six-weeks="center"
|
||||
class="justify-content-center"
|
||||
:clearable="false"
|
||||
:enable-time-picker="false"
|
||||
:clearable="true"
|
||||
:dark="isDarkMode"
|
||||
:week-start="firstDayOfWeek"
|
||||
:day-names="dayNames"
|
||||
:disabled-dates="disabledDates"
|
||||
:start-date="selectedDate"
|
||||
v-model="selectedDate">
|
||||
v-model="selectedFiscalYearStartValue">
|
||||
<template #month="{ text }">
|
||||
{{ getMonthShortName(text) }}
|
||||
</template>
|
||||
@@ -38,71 +37,71 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue';
|
||||
import { computed } from 'vue';
|
||||
|
||||
import { useI18n } from '@/locales/helpers.ts';
|
||||
|
||||
import { useEnvironmentsStore } from '@/stores/environment.ts';
|
||||
import { useUserStore } from '@/stores/user.ts';
|
||||
|
||||
import {
|
||||
type FiscalYearStartSelectionBaseProps,
|
||||
type FiscalYearStartSelectionBaseEmits,
|
||||
type CommonFiscalYearStartSelectionProps,
|
||||
type CommonFiscalYearStartSelectionEmits,
|
||||
useFiscalYearStartSelectionBase
|
||||
} from '@/components/base/FiscalYearStartSelectionBase.ts';
|
||||
|
||||
interface FiscalYearStartSelectionSheetProps extends FiscalYearStartSelectionBaseProps {
|
||||
import { FiscalYearStart } from '@/core/fiscalyear.ts';
|
||||
|
||||
interface MobileFiscalYearStartSelectionSheetProps extends CommonFiscalYearStartSelectionProps {
|
||||
clearable?: boolean;
|
||||
disabled?: boolean;
|
||||
label?: string;
|
||||
readonly?: boolean;
|
||||
show: boolean;
|
||||
title?: string;
|
||||
}
|
||||
|
||||
const props = defineProps<FiscalYearStartSelectionSheetProps>();
|
||||
|
||||
interface FiscalYearStartSelectionSheetEmits extends FiscalYearStartSelectionBaseEmits {
|
||||
interface MobileFiscalYearStartSelectionSheetEmits extends CommonFiscalYearStartSelectionEmits {
|
||||
(e: 'update:show', value: boolean): void;
|
||||
(e: 'update:title', value: string): void;
|
||||
}
|
||||
|
||||
const emit = defineEmits<FiscalYearStartSelectionSheetEmits>();
|
||||
const props = defineProps<MobileFiscalYearStartSelectionSheetProps>();
|
||||
const emit = defineEmits<MobileFiscalYearStartSelectionSheetEmits>();
|
||||
|
||||
const { tt, getMonthShortName, getCurrentFiscalYearStartFormatted } = useI18n();
|
||||
const { tt, getMonthShortName } = useI18n();
|
||||
|
||||
const environmentsStore = useEnvironmentsStore();
|
||||
const userStore = useUserStore();
|
||||
|
||||
const {
|
||||
dayNames,
|
||||
disabledDates,
|
||||
selectedFiscalYearStart,
|
||||
selectedFiscalYearStartValue,
|
||||
firstDayOfWeek,
|
||||
getDateStringToModelValue,
|
||||
getModelValueToDateString,
|
||||
selectedDisplayName,
|
||||
} = useFiscalYearStartSelectionBase(props, emit);
|
||||
dayNames
|
||||
} = useFiscalYearStartSelectionBase(props);
|
||||
|
||||
const isDarkMode = computed<boolean>(() => environmentsStore.framework7DarkMode || false);
|
||||
|
||||
const selectedDate = ref<string>(getModelValueToDateString());
|
||||
function confirm(): void {
|
||||
emit('update:modelValue', selectedFiscalYearStart.value);
|
||||
emit('update:show', false);
|
||||
}
|
||||
|
||||
function reset(): void {
|
||||
selectedFiscalYearStart.value = userStore.currentUserFiscalYearStart;
|
||||
}
|
||||
|
||||
function onSheetOpen(): void {
|
||||
selectedDate.value = getModelValueToDateString();
|
||||
if (props.modelValue) {
|
||||
const fiscalYearStart = FiscalYearStart.valueOf(props.modelValue);
|
||||
|
||||
if (fiscalYearStart) {
|
||||
selectedFiscalYearStart.value = fiscalYearStart.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onSheetClosed(): void {
|
||||
emit('update:show', false);
|
||||
}
|
||||
|
||||
function clear(): void {
|
||||
selectedDate.value = getCurrentFiscalYearStartFormatted();
|
||||
}
|
||||
|
||||
function confirm(): void {
|
||||
emit('update:modelValue', getDateStringToModelValue(selectedDate.value));
|
||||
emit('update:show', false);
|
||||
emit('update:title', selectedDisplayName(selectedDate.value));
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<style>
|
||||
|
||||
@@ -1,197 +1,87 @@
|
||||
import type { TypeAndDisplayName } from '@/core/base.ts';
|
||||
import type { UnixTimeRange } from './datetime';
|
||||
import type { UnixTimeRange } from './datetime.ts';
|
||||
|
||||
export class FiscalYearStart {
|
||||
public static readonly Default = new FiscalYearStart(1, 1);
|
||||
public static readonly JanuaryFirstDay = new FiscalYearStart(1, 1);
|
||||
public static readonly Default = FiscalYearStart.JanuaryFirstDay;
|
||||
|
||||
private static readonly MONTH_MAX_DAYS: number[] = [
|
||||
31, // January
|
||||
28, // February (Disallow fiscal year start on leap day)
|
||||
31, // March
|
||||
30, // April
|
||||
31, // May
|
||||
30, // June
|
||||
31, // July
|
||||
31, // August
|
||||
30, // September
|
||||
31, // October
|
||||
30, // November
|
||||
31 // December
|
||||
];
|
||||
|
||||
public readonly day: number;
|
||||
public readonly month: number;
|
||||
public readonly day: number;
|
||||
public readonly value: number;
|
||||
|
||||
private constructor(month: number, day: number) {
|
||||
const [validMonth, validDay] = validateMonthDay(month, day);
|
||||
this.day = validDay;
|
||||
this.month = validMonth;
|
||||
this.value = (validMonth << 8) | validDay;
|
||||
this.month = month;
|
||||
this.day = day;
|
||||
this.value = (month << 8) | day;
|
||||
}
|
||||
|
||||
public static of(month: number, day: number): FiscalYearStart | undefined {
|
||||
if (!FiscalYearStart.isValidMonthDay(month, day)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public static of(month: number, day: number): FiscalYearStart {
|
||||
return new FiscalYearStart(month, day);
|
||||
}
|
||||
|
||||
public static valueOf(value: number): FiscalYearStart {
|
||||
return FiscalYearStart.strictFromNumber(value);
|
||||
}
|
||||
|
||||
public static valuesFromNumber(value: number): number[] {
|
||||
return FiscalYearStart.strictFromNumber(value).values();
|
||||
}
|
||||
|
||||
public values(): number[] {
|
||||
return [
|
||||
this.month,
|
||||
this.day
|
||||
];
|
||||
}
|
||||
|
||||
public static parse(valueString: string): FiscalYearStart | undefined {
|
||||
return FiscalYearStart.strictFromMonthDashDayString(valueString);
|
||||
}
|
||||
|
||||
public static isValidType(value: number): boolean {
|
||||
if (value < 0x0101 || value > 0x0C1F) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const month = (value >> 8) & 0xFF;
|
||||
const day = value & 0xFF;
|
||||
|
||||
try {
|
||||
validateMonthDay(month, day);
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public isValid(): boolean {
|
||||
try {
|
||||
FiscalYearStart.validateMonthDay(this.month, this.day);
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public isDefault(): boolean {
|
||||
return this.month === 1 && this.day === 1;
|
||||
}
|
||||
|
||||
public static validateMonthDay(month: number, day: number): [number, number] {
|
||||
return validateMonthDay(month, day);
|
||||
}
|
||||
|
||||
public static strictFromMonthDayValues(month: number, day: number): FiscalYearStart {
|
||||
return FiscalYearStart.of(month, day);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a FiscalYearStart from a uint16 value (two bytes - month high, day low)
|
||||
* @param value uint16 value (month in high byte, day in low byte)
|
||||
* @returns FiscalYearStart instance
|
||||
* @returns FiscalYearStart instance or undefined if the value is out of range
|
||||
*/
|
||||
public static strictFromNumber(value: number): FiscalYearStart {
|
||||
if (value < 0 || value > 0xFFFF) {
|
||||
throw new Error('Invalid uint16 value');
|
||||
public static valueOf(value: number): FiscalYearStart | undefined {
|
||||
if (value < 0x0101 || value > 0x0C1F) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const month = (value >> 8) & 0xFF; // high byte
|
||||
const day = value & 0xFF; // low byte
|
||||
|
||||
try {
|
||||
const [validMonth, validDay] = validateMonthDay(month, day);
|
||||
return FiscalYearStart.of(validMonth, validDay);
|
||||
} catch (error) {
|
||||
throw new Error('Invalid uint16 value');
|
||||
}
|
||||
return FiscalYearStart.of(month, day);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a FiscalYearStart from a month/day string
|
||||
* @param input MM-dd string (e.g. "04-01" = 1 April)
|
||||
* @returns FiscalYearStart instance
|
||||
* @param monthDay MM-dd string (e.g. "04-01" = 1 April)
|
||||
* @returns FiscalYearStart instance or undefined if the monthDay is invalid
|
||||
*/
|
||||
public static strictFromMonthDashDayString(input: string): FiscalYearStart {
|
||||
if (!input || !input.includes('-')) {
|
||||
throw new Error('Invalid input string');
|
||||
public static parse(monthDay: string): FiscalYearStart | undefined {
|
||||
if (!monthDay || !monthDay.includes('-')) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const parts = input.split('-');
|
||||
const parts = monthDay.split('-');
|
||||
|
||||
if (parts.length !== 2) {
|
||||
throw new Error('Invalid input string');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const month = parseInt(parts[0], 10);
|
||||
const day = parseInt(parts[1], 10);
|
||||
|
||||
if (isNaN(month) || isNaN(day)) {
|
||||
throw new Error('Invalid input string');
|
||||
}
|
||||
|
||||
try {
|
||||
const [validMonth, validDay] = validateMonthDay(month, day);
|
||||
return FiscalYearStart.of(validMonth, validDay);
|
||||
} catch (error) {
|
||||
throw new Error('Invalid input string');
|
||||
}
|
||||
}
|
||||
|
||||
public static fromMonthDashDayString(input: string): FiscalYearStart | null {
|
||||
try {
|
||||
return FiscalYearStart.strictFromMonthDashDayString(input);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static fromNumber(value: number): FiscalYearStart | null {
|
||||
try {
|
||||
return FiscalYearStart.strictFromNumber(value);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static fromMonthDayValues(month: number, day: number): FiscalYearStart | null {
|
||||
try {
|
||||
return FiscalYearStart.strictFromMonthDayValues(month, day);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
return FiscalYearStart.of(month, day);
|
||||
}
|
||||
|
||||
public toMonthDashDayString(): string {
|
||||
return `${this.month.toString().padStart(2, '0')}-${this.day.toString().padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
public toMonthDayValues(): [string, string] {
|
||||
return [
|
||||
`${this.month.toString().padStart(2, '0')}`,
|
||||
`${this.day.toString().padStart(2, '0')}`
|
||||
]
|
||||
private static isValidMonthDay(month: number, day: number): boolean {
|
||||
return 1 <= month && month <= 12 && 1 <= day && day <= FiscalYearStart.MONTH_MAX_DAYS[month - 1];
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
return this.toMonthDashDayString();
|
||||
}
|
||||
}
|
||||
|
||||
function validateMonthDay(month: number, day: number): [number, number] {
|
||||
if (month < 1 || month > 12 || day < 1) {
|
||||
throw new Error('Invalid month or day');
|
||||
}
|
||||
|
||||
let maxDays = 31;
|
||||
switch (month) {
|
||||
// January, March, May, July, August, October, December
|
||||
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
|
||||
maxDays = 31;
|
||||
break;
|
||||
// April, June, September, November
|
||||
case 4: case 6: case 9: case 11:
|
||||
maxDays = 30;
|
||||
break;
|
||||
// February
|
||||
case 2:
|
||||
maxDays = 28; // Disallow fiscal year start on leap day
|
||||
break;
|
||||
}
|
||||
|
||||
if (day > maxDays) {
|
||||
throw new Error('Invalid day for given month');
|
||||
}
|
||||
|
||||
return [month, day];
|
||||
}
|
||||
|
||||
export class FiscalYearUnixTime implements UnixTimeRange {
|
||||
@@ -212,7 +102,7 @@ export class FiscalYearUnixTime implements UnixTimeRange {
|
||||
|
||||
export const LANGUAGE_DEFAULT_FISCAL_YEAR_FORMAT_VALUE: number = 0;
|
||||
|
||||
export class FiscalYearFormat implements TypeAndDisplayName {
|
||||
export class FiscalYearFormat {
|
||||
private static readonly allInstances: FiscalYearFormat[] = [];
|
||||
private static readonly allInstancesByType: Record<number, FiscalYearFormat> = {};
|
||||
private static readonly allInstancesByTypeName: Record<string, FiscalYearFormat> = {};
|
||||
@@ -226,19 +116,15 @@ export class FiscalYearFormat implements TypeAndDisplayName {
|
||||
public static readonly Default = FiscalYearFormat.EndYYYY;
|
||||
|
||||
public readonly type: number;
|
||||
public readonly displayName: string;
|
||||
public readonly typeName: string;
|
||||
|
||||
private constructor(type: number, displayName: string) {
|
||||
private constructor(type: number, typeName: string) {
|
||||
this.type = type;
|
||||
this.displayName = displayName;
|
||||
this.typeName = typeName;
|
||||
|
||||
FiscalYearFormat.allInstances.push(this);
|
||||
FiscalYearFormat.allInstancesByType[type] = this;
|
||||
FiscalYearFormat.allInstancesByTypeName[displayName] = this;
|
||||
}
|
||||
|
||||
public static all(): Record<string, FiscalYearFormat> {
|
||||
return FiscalYearFormat.allInstancesByTypeName;
|
||||
FiscalYearFormat.allInstancesByTypeName[typeName] = this;
|
||||
}
|
||||
|
||||
public static values(): FiscalYearFormat[] {
|
||||
@@ -249,7 +135,7 @@ export class FiscalYearFormat implements TypeAndDisplayName {
|
||||
return FiscalYearFormat.allInstancesByType[type];
|
||||
}
|
||||
|
||||
public static parse(displayName: string): FiscalYearFormat | undefined {
|
||||
return FiscalYearFormat.allInstancesByTypeName[displayName];
|
||||
public static parse(typeName: string): FiscalYearFormat | undefined {
|
||||
return FiscalYearFormat.allInstancesByTypeName[typeName];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,29 @@
|
||||
// Unit tests for fiscal year functions
|
||||
import moment from 'moment-timezone';
|
||||
import { describe, expect, test, beforeAll } from '@jest/globals';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { describe, expect, test, beforeAll } from '@jest/globals';
|
||||
import moment from 'moment-timezone';
|
||||
|
||||
// Import all the fiscal year functions from the lib
|
||||
import { FiscalYearStart, FiscalYearUnixTime } from '@/core/fiscalyear.ts';
|
||||
|
||||
import {
|
||||
getFiscalYearFromUnixTime,
|
||||
getFiscalYearStartUnixTime,
|
||||
getFiscalYearEndUnixTime,
|
||||
getFiscalYearTimeRangeFromUnixTime,
|
||||
getAllFiscalYearsStartAndEndUnixTimes,
|
||||
getFiscalYearTimeRangeFromYear
|
||||
getFiscalYearTimeRangeFromYear,
|
||||
formatUnixTime
|
||||
} from '@/lib/datetime.ts';
|
||||
|
||||
import { formatUnixTime } from '@/lib/datetime.ts';
|
||||
import { FiscalYearStart, FiscalYearUnixTime } from '@/core/fiscalyear.ts';
|
||||
|
||||
// Set test environment timezone to UTC, since the test data constants are in UTC
|
||||
beforeAll(() => {
|
||||
moment.tz.setDefault('UTC');
|
||||
});
|
||||
|
||||
// UTILITIES
|
||||
|
||||
function importTestData(datasetName: string): any[] {
|
||||
function importTestData(datasetName: string): unknown[] {
|
||||
const data = JSON.parse(
|
||||
fs.readFileSync(path.join(__dirname, 'fiscal_year.data.json'), 'utf8')
|
||||
);
|
||||
@@ -74,13 +73,13 @@ const TEST_FISCAL_YEAR_START_PRESETS: Record<string, FiscalYearStartConfig> = {
|
||||
// VALIDATE FISCAL YEAR START PRESETS
|
||||
describe('validateFiscalYearStart', () => {
|
||||
Object.values(TEST_FISCAL_YEAR_START_PRESETS).forEach((testFiscalYearStart) => {
|
||||
test(`should return true if fiscal year start value (uint16) is valid: id: ${testFiscalYearStart.id}; value: 0x${testFiscalYearStart.value.toString(16)}`, () => {
|
||||
expect(FiscalYearStart.isValidType(testFiscalYearStart.value)).toBe(true);
|
||||
test(`should return fiscal year start object if fiscal year start value (uint16) is valid: id: ${testFiscalYearStart.id}; value: 0x${testFiscalYearStart.value.toString(16)}`, () => {
|
||||
expect(FiscalYearStart.valueOf(testFiscalYearStart.value)).toBeDefined();
|
||||
});
|
||||
|
||||
test(`returns same month-date string for valid fiscal year start value: id: ${testFiscalYearStart.id}; value: 0x${testFiscalYearStart.value.toString(16)}`, () => {
|
||||
const fiscalYearStart = FiscalYearStart.strictFromNumber(testFiscalYearStart.value);
|
||||
expect(fiscalYearStart.toString()).toStrictEqual(testFiscalYearStart.monthDateString);
|
||||
const fiscalYearStart = FiscalYearStart.valueOf(testFiscalYearStart.value);
|
||||
expect(fiscalYearStart?.toMonthDashDayString()).toStrictEqual(testFiscalYearStart.monthDateString);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -103,24 +102,20 @@ const TestCase_invalidFiscalYearValues = [
|
||||
|
||||
describe('validateFiscalYearStartInvalidValues', () => {
|
||||
TestCase_invalidFiscalYearValues.forEach((testCase) => {
|
||||
test(`should return false if fiscal year start value (uint16) is invalid: value: 0x${testCase.toString(16)}`, () => {
|
||||
expect(FiscalYearStart.isValidType(testCase)).toBe(false);
|
||||
test(`should return undefined if fiscal year start value (uint16) is invalid: value: 0x${testCase.toString(16)}`, () => {
|
||||
expect(FiscalYearStart.valueOf(testCase)).not.toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// VALIDATE LEAP DAY FEBRUARY 29 IS NOT VALID
|
||||
describe('validateFiscalYearStartLeapDay', () => {
|
||||
test(`should return false if fiscal year start value (uint16) for February 29 is invalid: value: 0x0229}`, () => {
|
||||
expect(FiscalYearStart.isValidType(0x0229)).toBe(false);
|
||||
test(`should return undefined if fiscal year start value (uint16) for February 29 is invalid: value: 0x0229}`, () => {
|
||||
expect(FiscalYearStart.valueOf(0x021D)).not.toBeDefined();
|
||||
});
|
||||
|
||||
test(`should return error if fiscal year month-day string "02-29" is used to create fiscal year start object`, () => {
|
||||
expect(() => FiscalYearStart.strictFromMonthDashDayString('02-29')).toThrow();
|
||||
});
|
||||
|
||||
test(`should return error if integers "02" and "29" are used to create fiscal year start object`, () => {
|
||||
expect(() => FiscalYearStart.validateMonthDay(2, 29)).toThrow();
|
||||
test(`should return undefined if fiscal year month-day string "02-29" is used to create fiscal year start object`, () => {
|
||||
expect(FiscalYearStart.parse('02-29')).not.toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -133,8 +128,8 @@ type TestCase_getFiscalYearFromUnixTime = {
|
||||
};
|
||||
};
|
||||
|
||||
let TEST_CASES_GET_FISCAL_YEAR_FROM_UNIX_TIME: TestCase_getFiscalYearFromUnixTime[];
|
||||
TEST_CASES_GET_FISCAL_YEAR_FROM_UNIX_TIME = importTestData('test_cases_getFiscalYearFromUnixTime') as TestCase_getFiscalYearFromUnixTime[];
|
||||
const TEST_CASES_GET_FISCAL_YEAR_FROM_UNIX_TIME: TestCase_getFiscalYearFromUnixTime[] =
|
||||
importTestData('test_cases_getFiscalYearFromUnixTime') as TestCase_getFiscalYearFromUnixTime[];
|
||||
|
||||
describe('getFiscalYearFromUnixTime', () => {
|
||||
Object.values(TEST_FISCAL_YEAR_START_PRESETS).forEach((testFiscalYearStart) => {
|
||||
@@ -161,8 +156,8 @@ type TestCase_getFiscalYearStartUnixTime = {
|
||||
};
|
||||
}
|
||||
|
||||
let TEST_CASES_GET_FISCAL_YEAR_START_UNIX_TIME: TestCase_getFiscalYearStartUnixTime[];
|
||||
TEST_CASES_GET_FISCAL_YEAR_START_UNIX_TIME = importTestData('test_cases_getFiscalYearStartUnixTime') as TestCase_getFiscalYearStartUnixTime[];
|
||||
const TEST_CASES_GET_FISCAL_YEAR_START_UNIX_TIME: TestCase_getFiscalYearStartUnixTime[] =
|
||||
importTestData('test_cases_getFiscalYearStartUnixTime') as TestCase_getFiscalYearStartUnixTime[];
|
||||
|
||||
describe('getFiscalYearStartUnixTime', () => {
|
||||
Object.values(TEST_FISCAL_YEAR_START_PRESETS).forEach((testFiscalYearStart) => {
|
||||
@@ -191,8 +186,8 @@ type TestCase_getFiscalYearEndUnixTime = {
|
||||
};
|
||||
}
|
||||
|
||||
let TEST_CASES_GET_FISCAL_YEAR_END_UNIX_TIME: TestCase_getFiscalYearEndUnixTime[];
|
||||
TEST_CASES_GET_FISCAL_YEAR_END_UNIX_TIME = importTestData('test_cases_getFiscalYearEndUnixTime') as TestCase_getFiscalYearEndUnixTime[];
|
||||
const TEST_CASES_GET_FISCAL_YEAR_END_UNIX_TIME: TestCase_getFiscalYearEndUnixTime[] =
|
||||
importTestData('test_cases_getFiscalYearEndUnixTime') as TestCase_getFiscalYearEndUnixTime[];
|
||||
|
||||
describe('getFiscalYearEndUnixTime', () => {
|
||||
Object.values(TEST_FISCAL_YEAR_START_PRESETS).forEach((testFiscalYearStart) => {
|
||||
@@ -218,8 +213,8 @@ type TestCase_getFiscalYearTimeRangeFromUnixTime = {
|
||||
}
|
||||
}
|
||||
|
||||
let TEST_CASES_GET_FISCAL_YEAR_UNIX_TIME_RANGE: TestCase_getFiscalYearTimeRangeFromUnixTime[];
|
||||
TEST_CASES_GET_FISCAL_YEAR_UNIX_TIME_RANGE = importTestData('test_cases_getFiscalYearTimeRangeFromUnixTime') as TestCase_getFiscalYearTimeRangeFromUnixTime[];
|
||||
const TEST_CASES_GET_FISCAL_YEAR_UNIX_TIME_RANGE: TestCase_getFiscalYearTimeRangeFromUnixTime[] =
|
||||
importTestData('test_cases_getFiscalYearTimeRangeFromUnixTime') as TestCase_getFiscalYearTimeRangeFromUnixTime[];
|
||||
|
||||
describe('getFiscalYearTimeRangeFromUnixTime', () => {
|
||||
Object.values(TEST_FISCAL_YEAR_START_PRESETS).forEach((testFiscalYearStart) => {
|
||||
@@ -242,14 +237,16 @@ type TestCase_getAllFiscalYearsStartAndEndUnixTimes = {
|
||||
expected: FiscalYearUnixTime[]
|
||||
}
|
||||
|
||||
let TEST_CASES_GET_ALL_FISCAL_YEARS_START_AND_END_UNIX_TIMES: TestCase_getAllFiscalYearsStartAndEndUnixTimes[];
|
||||
TEST_CASES_GET_ALL_FISCAL_YEARS_START_AND_END_UNIX_TIMES = importTestData('test_cases_getAllFiscalYearsStartAndEndUnixTimes') as TestCase_getAllFiscalYearsStartAndEndUnixTimes[];
|
||||
const TEST_CASES_GET_ALL_FISCAL_YEARS_START_AND_END_UNIX_TIMES: TestCase_getAllFiscalYearsStartAndEndUnixTimes[] =
|
||||
importTestData('test_cases_getAllFiscalYearsStartAndEndUnixTimes') as TestCase_getAllFiscalYearsStartAndEndUnixTimes[];
|
||||
|
||||
describe('getAllFiscalYearsStartAndEndUnixTimes', () => {
|
||||
TEST_CASES_GET_ALL_FISCAL_YEARS_START_AND_END_UNIX_TIMES.forEach((testCase) => {
|
||||
const fiscalYearStart = FiscalYearStart.strictFromMonthDashDayString(testCase.fiscalYearStart);
|
||||
const fiscalYearStart = FiscalYearStart.parse(testCase.fiscalYearStart);
|
||||
test(`returns correct fiscal year start and end unix times for ${getTestTitleFormatString(testCase.fiscalYearStartId, `${testCase.startYearMonth} to ${testCase.endYearMonth}`)}`, () => {
|
||||
const fiscalYearStartAndEndUnixTimes = getAllFiscalYearsStartAndEndUnixTimes(testCase.startYearMonth, testCase.endYearMonth, fiscalYearStart.value);
|
||||
expect(fiscalYearStart).toBeDefined();
|
||||
|
||||
const fiscalYearStartAndEndUnixTimes = getAllFiscalYearsStartAndEndUnixTimes(testCase.startYearMonth, testCase.endYearMonth, fiscalYearStart?.value || 0);
|
||||
|
||||
// Convert results to include ISO strings for better test output
|
||||
const resultWithISO = fiscalYearStartAndEndUnixTimes.map(data => ({
|
||||
@@ -277,14 +274,15 @@ type TestCase_getFiscalYearTimeRangeFromYear = {
|
||||
expected: FiscalYearUnixTime;
|
||||
}
|
||||
|
||||
let TEST_CASES_GET_FISCAL_YEAR_RANGE_FROM_YEAR: TestCase_getFiscalYearTimeRangeFromYear[];
|
||||
TEST_CASES_GET_FISCAL_YEAR_RANGE_FROM_YEAR = importTestData('test_cases_getFiscalYearTimeRangeFromYear') as TestCase_getFiscalYearTimeRangeFromYear[];
|
||||
const TEST_CASES_GET_FISCAL_YEAR_RANGE_FROM_YEAR: TestCase_getFiscalYearTimeRangeFromYear[] =
|
||||
importTestData('test_cases_getFiscalYearTimeRangeFromYear') as TestCase_getFiscalYearTimeRangeFromYear[];
|
||||
|
||||
describe('getFiscalYearTimeRangeFromYear', () => {
|
||||
TEST_CASES_GET_FISCAL_YEAR_RANGE_FROM_YEAR.forEach((testCase) => {
|
||||
const fiscalYearStart = FiscalYearStart.strictFromMonthDashDayString(testCase.fiscalYearStart);
|
||||
const fiscalYearStart = FiscalYearStart.parse(testCase.fiscalYearStart);
|
||||
test(`returns correct fiscal year unix time range for input year integer ${testCase.year} and FY_START: ${testCase.fiscalYearStart}`, () => {
|
||||
const fiscalYearRange = getFiscalYearTimeRangeFromYear(testCase.year, fiscalYearStart.value);
|
||||
expect(fiscalYearStart).toBeDefined();
|
||||
const fiscalYearRange = getFiscalYearTimeRangeFromYear(testCase.year, fiscalYearStart?.value || 0);
|
||||
expect(fiscalYearRange).toStrictEqual(testCase.expected);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -442,7 +442,7 @@ export function getAllYearsStartAndEndUnixTimes(startYearMonth: YearMonth | stri
|
||||
return allYearTimes;
|
||||
}
|
||||
|
||||
export function getAllFiscalYearsStartAndEndUnixTimes(startYearMonth: YearMonth | string, endYearMonth: YearMonth | string, fiscalYearStart: number): FiscalYearUnixTime[] {
|
||||
export function getAllFiscalYearsStartAndEndUnixTimes(startYearMonth: YearMonth | string, endYearMonth: YearMonth | 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
|
||||
@@ -459,16 +459,22 @@ export function getAllFiscalYearsStartAndEndUnixTimes(startYearMonth: YearMonth
|
||||
|
||||
const inputStartUnixTime = getYearMonthFirstUnixTime(range.startYearMonth);
|
||||
const inputEndUnixTime = getYearMonthLastUnixTime(range.endYearMonth);
|
||||
const fiscalYearStartMonth = FiscalYearStart.strictFromNumber(fiscalYearStart).month;
|
||||
let fiscalYearStart = FiscalYearStart.valueOf(fiscalYearStartValue);
|
||||
|
||||
if (!fiscalYearStart) {
|
||||
fiscalYearStart = FiscalYearStart.Default;
|
||||
}
|
||||
|
||||
const fiscalYearStartMonth = fiscalYearStart.month;
|
||||
|
||||
// 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: fiscalYearStartMonth });
|
||||
const fiscalStartTime = getFiscalYearStartUnixTime(thisYearMonthUnixTime, fiscalYearStart);
|
||||
const fiscalEndTime = getFiscalYearEndUnixTime(thisYearMonthUnixTime, fiscalYearStart);
|
||||
const fiscalStartTime = getFiscalYearStartUnixTime(thisYearMonthUnixTime, fiscalYearStart.value);
|
||||
const fiscalEndTime = getFiscalYearEndUnixTime(thisYearMonthUnixTime, fiscalYearStart.value);
|
||||
|
||||
const fiscalYear = getFiscalYearFromUnixTime(fiscalStartTime, fiscalYearStart);
|
||||
const fiscalYear = getFiscalYearFromUnixTime(fiscalStartTime, fiscalYearStart.value);
|
||||
|
||||
if (fiscalStartTime <= inputEndUnixTime && fiscalEndTime >= inputStartUnixTime) {
|
||||
let minUnixTime = fiscalStartTime;
|
||||
@@ -1010,11 +1016,11 @@ export function isDateRangeMatchOneMonth(minTime: number, maxTime: number): bool
|
||||
return isDateRangeMatchFullMonths(minTime, maxTime);
|
||||
}
|
||||
|
||||
export function getFiscalYearFromUnixTime(unixTime: number, fiscalYearStart: number): number {
|
||||
export function getFiscalYearFromUnixTime(unixTime: number, fiscalYearStartValue: number): number {
|
||||
const date = moment.unix(unixTime);
|
||||
|
||||
// For January 1 fiscal year start, fiscal year matches calendar year
|
||||
if (fiscalYearStart === 0x0101) {
|
||||
if (fiscalYearStartValue === FiscalYearStart.JanuaryFirstDay.value) {
|
||||
return date.year();
|
||||
}
|
||||
|
||||
@@ -1023,12 +1029,16 @@ export function getFiscalYearFromUnixTime(unixTime: number, fiscalYearStart: num
|
||||
const day = date.date();
|
||||
const year = date.year();
|
||||
|
||||
const [fiscalYearStartMonth, fiscalYearStartDay] = FiscalYearStart.strictFromNumber(fiscalYearStart).values();
|
||||
let fiscalYearStart = FiscalYearStart.valueOf(fiscalYearStartValue);
|
||||
|
||||
if (!fiscalYearStart) {
|
||||
fiscalYearStart = FiscalYearStart.Default;
|
||||
}
|
||||
|
||||
// For other fiscal year starts:
|
||||
// If input time comes before the fiscal year start day in the calendar year,
|
||||
// it belongs to the fiscal year that ends in the current calendar year
|
||||
if (month < fiscalYearStartMonth || (month === fiscalYearStartMonth && day < fiscalYearStartDay)) {
|
||||
if (month < fiscalYearStart.month || (month === fiscalYearStart.month && day < fiscalYearStart.day)) {
|
||||
return year;
|
||||
}
|
||||
|
||||
@@ -1037,15 +1047,20 @@ export function getFiscalYearFromUnixTime(unixTime: number, fiscalYearStart: num
|
||||
return year + 1;
|
||||
}
|
||||
|
||||
export function getFiscalYearStartUnixTime(unixTime: number, fiscalYearStart: number): number {
|
||||
export function getFiscalYearStartUnixTime(unixTime: number, fiscalYearStartValue: number): number {
|
||||
const date = moment.unix(unixTime);
|
||||
|
||||
// For January 1 fiscal year start, fiscal year start time is always January 1 in the input calendar year
|
||||
if (fiscalYearStart === 0x0101) {
|
||||
if (fiscalYearStartValue === FiscalYearStart.JanuaryFirstDay.value) {
|
||||
return moment().year(date.year()).month(0).date(1).hour(0).minute(0).second(0).millisecond(0).unix();
|
||||
}
|
||||
|
||||
const [fiscalYearStartMonth, fiscalYearStartDay] = FiscalYearStart.strictFromNumber(fiscalYearStart).values();
|
||||
let fiscalYearStart = FiscalYearStart.valueOf(fiscalYearStartValue);
|
||||
|
||||
if (!fiscalYearStart) {
|
||||
fiscalYearStart = FiscalYearStart.Default;
|
||||
}
|
||||
|
||||
const month = date.month() + 1; // 1-index
|
||||
const day = date.date();
|
||||
const year = date.year();
|
||||
@@ -1056,14 +1071,14 @@ export function getFiscalYearStartUnixTime(unixTime: number, fiscalYearStart: nu
|
||||
// If input time comes on or after the fiscal year start day in the calendar year,
|
||||
// the relevant fiscal year has a start date in Calendar Year = Input Year - 1, and end date in Calendar Year = Input Year.
|
||||
let startYear = year - 1;
|
||||
if (month > fiscalYearStartMonth || (month === fiscalYearStartMonth && day >= fiscalYearStartDay)) {
|
||||
if (month > fiscalYearStart.month || (month === fiscalYearStart.month && day >= fiscalYearStart.day)) {
|
||||
startYear = year;
|
||||
}
|
||||
|
||||
return moment().set({
|
||||
year: startYear,
|
||||
month: fiscalYearStartMonth - 1, // 0-index
|
||||
date: fiscalYearStartDay,
|
||||
month: fiscalYearStart.month - 1, // 0-index
|
||||
date: fiscalYearStart.day,
|
||||
hour: 0,
|
||||
minute: 0,
|
||||
second: 0,
|
||||
@@ -1073,7 +1088,7 @@ export function getFiscalYearStartUnixTime(unixTime: number, fiscalYearStart: nu
|
||||
|
||||
export function getFiscalYearEndUnixTime(unixTime: number, fiscalYearStart: number): number {
|
||||
const fiscalYearStartTime = moment.unix(getFiscalYearStartUnixTime(unixTime, fiscalYearStart));
|
||||
return fiscalYearStartTime.add(1, 'year').subtract(1, 'second').unix();
|
||||
return fiscalYearStartTime.add(1, 'years').subtract(1, 'seconds').unix();
|
||||
}
|
||||
|
||||
export function getCurrentFiscalYear(fiscalYearStart: number): number {
|
||||
@@ -1091,19 +1106,23 @@ export function getFiscalYearTimeRangeFromUnixTime(unixTime: number, fiscalYearS
|
||||
};
|
||||
}
|
||||
|
||||
export function getFiscalYearTimeRangeFromYear(year: number, fiscalYearStart: number): FiscalYearUnixTime {
|
||||
export function getFiscalYearTimeRangeFromYear(year: number, fiscalYearStartValue: number): FiscalYearUnixTime {
|
||||
const fiscalYear = year;
|
||||
const fiscalYearStartObj = FiscalYearStart.strictFromNumber(fiscalYearStart);
|
||||
let fiscalYearStart = FiscalYearStart.valueOf(fiscalYearStartValue);
|
||||
|
||||
if (!fiscalYearStart) {
|
||||
fiscalYearStart = FiscalYearStart.Default;
|
||||
}
|
||||
|
||||
// For a specified fiscal year (e.g., 2023), the start date is in the previous calendar year
|
||||
// unless fiscal year starts on January 1
|
||||
const calendarStartYear = fiscalYearStart === 0x0101 ? fiscalYear : fiscalYear - 1;
|
||||
const calendarStartYear = fiscalYearStartValue === FiscalYearStart.JanuaryFirstDay.value ? fiscalYear : fiscalYear - 1;
|
||||
|
||||
// Create the timestamp for the start of the fiscal year
|
||||
const fiscalYearStartUnixTime = moment().set({
|
||||
year: calendarStartYear,
|
||||
month: fiscalYearStartObj.month - 1, // 0-index
|
||||
date: fiscalYearStartObj.day,
|
||||
month: fiscalYearStart.month - 1, // 0-index
|
||||
date: fiscalYearStart.day,
|
||||
hour: 0,
|
||||
minute: 0,
|
||||
second: 0,
|
||||
@@ -1111,7 +1130,7 @@ export function getFiscalYearTimeRangeFromYear(year: number, fiscalYearStart: nu
|
||||
}).unix();
|
||||
|
||||
// Fiscal year end is one year after start minus 1 second
|
||||
const fiscalYearEndUnixTime = moment.unix(fiscalYearStartUnixTime).add(1, 'year').subtract(1, 'second').unix();
|
||||
const fiscalYearEndUnixTime = moment.unix(fiscalYearStartUnixTime).add(1, 'years').subtract(1, 'seconds').unix();
|
||||
|
||||
return {
|
||||
year: fiscalYear,
|
||||
@@ -1119,5 +1138,3 @@ export function getFiscalYearTimeRangeFromYear(year: number, fiscalYearStart: nu
|
||||
maxUnixTime: fiscalYearEndUnixTime,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -935,31 +935,36 @@ export function useI18n() {
|
||||
];
|
||||
}
|
||||
|
||||
function getAllFiscalYearFormats(): FiscalYearFormat[] {
|
||||
function getAllFiscalYearFormats(): TypeAndDisplayName[] {
|
||||
const now = getCurrentUnixTime();
|
||||
let fiscalYearStart = userStore.currentUserFiscalYearStart;
|
||||
|
||||
if (!fiscalYearStart) {
|
||||
fiscalYearStart = FiscalYearStart.Default.value;
|
||||
}
|
||||
const nowFiscalYearRange = getFiscalYearTimeRangeFromUnixTime(now, userStore.currentUserFiscalYearStart);
|
||||
|
||||
const ret: FiscalYearFormat[] = [];
|
||||
const currentFiscalYearRange = getFiscalYearTimeRangeFromUnixTime(now, fiscalYearStart);
|
||||
const ret: TypeAndDisplayName[] = [];
|
||||
|
||||
let defaultFiscalYearFormatType = FiscalYearFormat.parse(t('default.fiscalYearFormat'));
|
||||
if (!defaultFiscalYearFormatType) {
|
||||
defaultFiscalYearFormatType = FiscalYearFormat.Default;
|
||||
let fiscalYearFormat = FiscalYearFormat.parse(getDefaultFiscalYearFormat());
|
||||
|
||||
if (!fiscalYearFormat) {
|
||||
fiscalYearFormat = FiscalYearFormat.Default;
|
||||
}
|
||||
|
||||
ret.push({
|
||||
type: LANGUAGE_DEFAULT_FISCAL_YEAR_FORMAT_VALUE,
|
||||
displayName: `${t('Language Default')} (${formatTimeRangeToFiscalYearFormat(defaultFiscalYearFormatType, nowFiscalYearRange)})`
|
||||
displayName: `${t('Language Default')} (${formatTimeRangeToFiscalYearFormat(fiscalYearFormat, currentFiscalYearRange)})`
|
||||
});
|
||||
|
||||
const allFiscalYearFormats = FiscalYearFormat.values();
|
||||
|
||||
for (let i = 0; i < allFiscalYearFormats.length; i++) {
|
||||
const type = allFiscalYearFormats[i];
|
||||
const format = allFiscalYearFormats[i];
|
||||
|
||||
ret.push({
|
||||
type: type.type,
|
||||
displayName: formatTimeRangeToFiscalYearFormat(type, nowFiscalYearRange),
|
||||
type: format.type,
|
||||
displayName: formatTimeRangeToFiscalYearFormat(format, currentFiscalYearRange),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1366,7 +1371,7 @@ export function useI18n() {
|
||||
let fiscalYearFormat = FiscalYearFormat.valueOf(userStore.currentUserFiscalYearFormat);
|
||||
|
||||
if (!fiscalYearFormat) {
|
||||
const defaultFiscalYearFormatTypeName = t('default.fiscalYearFormat');
|
||||
const defaultFiscalYearFormatTypeName = getDefaultFiscalYearFormat();
|
||||
fiscalYearFormat = FiscalYearFormat.parse(defaultFiscalYearFormatTypeName);
|
||||
|
||||
if (!fiscalYearFormat) {
|
||||
@@ -1477,7 +1482,7 @@ export function useI18n() {
|
||||
format = FiscalYearFormat.Default;
|
||||
}
|
||||
|
||||
return t('format.fiscalYear.' + format.displayName, {
|
||||
return t('format.fiscalYear.' + format.typeName, {
|
||||
StartYYYY: formatUnixTime(timeRange.minUnixTime, 'YYYY'),
|
||||
StartYY: formatUnixTime(timeRange.minUnixTime, 'YY'),
|
||||
EndYYYY: formatUnixTime(timeRange.maxUnixTime, 'YYYY'),
|
||||
@@ -1493,7 +1498,6 @@ export function useI18n() {
|
||||
}
|
||||
|
||||
const timeRange = getFiscalYearTimeRangeFromUnixTime(unixTime, userStore.currentUserFiscalYearStart);
|
||||
|
||||
return formatTimeRangeToFiscalYearFormat(fiscalYearFormat, timeRange);
|
||||
}
|
||||
|
||||
@@ -1505,18 +1509,17 @@ export function useI18n() {
|
||||
}
|
||||
|
||||
const timeRange = getFiscalYearTimeRangeFromYear(year, userStore.currentUserFiscalYearStart);
|
||||
|
||||
return formatTimeRangeToFiscalYearFormat(fiscalYearFormat, timeRange);
|
||||
}
|
||||
|
||||
function formatFiscalYearStart(fiscalYearStart: number) {
|
||||
let fy = FiscalYearStart.fromNumber(fiscalYearStart);
|
||||
function formatFiscalYearStartToLongDay(fiscalYearStartValue: number) {
|
||||
let fiscalYearStart = FiscalYearStart.valueOf(fiscalYearStartValue);
|
||||
|
||||
if ( !fy ) {
|
||||
fy = FiscalYearStart.Default;
|
||||
if (!fiscalYearStart) {
|
||||
fiscalYearStart = FiscalYearStart.Default;
|
||||
}
|
||||
|
||||
return formatMonthDayToLongDay(fy.toMonthDashDayString());
|
||||
return formatMonthDayToLongDay(fiscalYearStart.toMonthDashDayString());
|
||||
}
|
||||
|
||||
function getTimezoneDifferenceDisplayText(utcOffset: number): string {
|
||||
@@ -1865,7 +1868,6 @@ export function useI18n() {
|
||||
getWeekdayLongName,
|
||||
getMultiMonthdayShortNames,
|
||||
getMultiWeekdayLongNames,
|
||||
getCurrentFiscalYearStartFormatted: () => formatMonthDayToLongDay(FiscalYearStart.strictFromNumber(userStore.currentUserFiscalYearStart).toMonthDashDayString()),
|
||||
getCurrentFiscalYearFormatType,
|
||||
getCurrentDecimalSeparator,
|
||||
getCurrentDigitGroupingSymbol,
|
||||
@@ -1896,7 +1898,7 @@ export function useI18n() {
|
||||
formatMonthDayToLongDay,
|
||||
formatYearQuarter,
|
||||
formatDateRange,
|
||||
formatFiscalYearStart,
|
||||
formatFiscalYearStartToLongDay,
|
||||
formatTimeRangeToFiscalYearFormat,
|
||||
formatUnixTimeToFiscalYear,
|
||||
formatYearToFiscalYear,
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
<f7-list-item class="list-item-with-header-and-title list-item-no-item-after" header="Default Language" title="Language" link="#"></f7-list-item>
|
||||
<f7-list-item class="list-item-with-header-and-title list-item-no-item-after" header="Default Currency" title="Currency" link="#"></f7-list-item>
|
||||
<f7-list-item class="list-item-with-header-and-title list-item-no-item-after" header="First Day of Week" title="Week Day" link="#"></f7-list-item>
|
||||
<f7-list-item class="list-item-with-header-and-title list-item-no-item-after" header="Fiscal Year Start Date" title="January 1" link="#"></f7-list-item>
|
||||
</f7-list>
|
||||
|
||||
<f7-list strong inset dividers class="margin-vertical skeleton-text" v-if="loading">
|
||||
@@ -32,6 +33,7 @@
|
||||
<f7-list-item class="list-item-with-header-and-title list-item-no-item-after" header="Short Date Format" title="YYYY-MM-DD" link="#"></f7-list-item>
|
||||
<f7-list-item class="list-item-with-header-and-title list-item-no-item-after" header="Long Time Format" title="HH:mm:ss" link="#"></f7-list-item>
|
||||
<f7-list-item class="list-item-with-header-and-title list-item-no-item-after" header="Short Time Format" title="HH:mm" link="#"></f7-list-item>
|
||||
<f7-list-item class="list-item-with-header-and-title list-item-no-item-after" header="Fiscal Year Format" title="FY YYYY" link="#"></f7-list-item>
|
||||
</f7-list>
|
||||
|
||||
<f7-list strong inset dividers class="margin-vertical skeleton-text" v-if="loading">
|
||||
@@ -207,7 +209,7 @@
|
||||
link="#"
|
||||
class="list-item-with-header-and-title list-item-no-item-after"
|
||||
:header="tt('Fiscal Year Start Date')"
|
||||
:title="currentFiscalYearStartDate"
|
||||
:title="formatFiscalYearStartToLongDay(newProfile.fiscalYearStart)"
|
||||
@click="showFiscalYearStartSheet = true"
|
||||
>
|
||||
<fiscal-year-start-selection-sheet
|
||||
@@ -516,7 +518,7 @@ const props = defineProps<{
|
||||
f7router: Router.Router;
|
||||
}>();
|
||||
|
||||
const { tt, getAllLanguageOptions, getAllCurrencies, getCurrencyName, formatFiscalYearStart } = useI18n();
|
||||
const { tt, getAllLanguageOptions, getAllCurrencies, getCurrencyName, formatFiscalYearStartToLongDay } = useI18n();
|
||||
const { showAlert, showToast, routeBackOnError } = useI18nUIComponents();
|
||||
|
||||
const {
|
||||
@@ -597,7 +599,6 @@ const currentLanguageName = computed<string>(() => {
|
||||
});
|
||||
|
||||
const currentDayOfWeekName = computed<string | null>(() => findDisplayNameByType(allWeekDays.value, newProfile.value.firstDayOfWeek));
|
||||
const currentFiscalYearStartDate = computed<string | null>( () => formatFiscalYearStart(newProfile.value.fiscalYearStart) );
|
||||
|
||||
function init(): void {
|
||||
loading.value = true;
|
||||
|
||||
7
tsconfig.jest.json
Normal file
7
tsconfig.jest.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"isolatedModules": true,
|
||||
"verbatimModuleSyntax": false
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
},
|
||||
"include": [
|
||||
"src/**/*",
|
||||
"vite.config.ts"
|
||||
"vite.config.ts",
|
||||
"jest.config.ts"
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user