-
-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formula): date function single number
- Loading branch information
Showing
7 changed files
with
112 additions
and
13 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
packages/engine-formula/src/functions/date/date/__tests__/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { FUNCTION_NAMES_DATE } from '../../function-names'; | ||
import { DateFunction } from '..'; | ||
import { NumberValueObject } from '../../../../engine/value-object/primitive-object'; | ||
|
||
describe('Test date function', () => { | ||
const textFunction = new DateFunction(FUNCTION_NAMES_DATE.DATE); | ||
|
||
describe('Date', () => { | ||
it('Value is normal', () => { | ||
const year = new NumberValueObject(2024); | ||
const month = new NumberValueObject(1); | ||
const day = new NumberValueObject(1); | ||
const result = textFunction.calculate(year, month, day); | ||
expect(result.getValue()).toBe(45292); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { BaseValueObject } from '../../..'; | ||
import { ErrorType, ErrorValueObject, NumberValueObject } from '../../..'; | ||
import { DEFFAULT_DATE_FORMAT, excelDateSerial } from '../../../basics/date'; | ||
import { BaseFunction } from '../../base-function'; | ||
|
||
export class DateFunction extends BaseFunction { | ||
override calculate(year: BaseValueObject, month: BaseValueObject, day: BaseValueObject) { | ||
if (year.isError()) { | ||
return year; | ||
} | ||
|
||
if (month.isError()) { | ||
return month; | ||
} | ||
|
||
if (day.isError()) { | ||
return day; | ||
} | ||
|
||
// TODO@Dushusir: array | ||
const yearValue = +year.getValue(); | ||
const monthValue = +month.getValue(); | ||
const dayValue = +day.getValue(); | ||
|
||
const date = new Date(yearValue, monthValue - 1, dayValue); | ||
|
||
if (date.getMonth() !== monthValue - 1 || date.getDate() !== dayValue) { | ||
return new ErrorValueObject(ErrorType.VALUE); | ||
} | ||
|
||
const currentSerial = excelDateSerial(date); | ||
const valueObject = new NumberValueObject(currentSerial); | ||
valueObject.setPattern(DEFFAULT_DATE_FORMAT); | ||
return valueObject; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters