-
-
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): add hour/minute/second function
- Loading branch information
panxp
authored and
panxp
committed
Jul 12, 2024
1 parent
52dc8b2
commit 7e8efa8
Showing
13 changed files
with
487 additions
and
54 deletions.
There are no files selected for viewing
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
68 changes: 68 additions & 0 deletions
68
packages/engine-formula/src/functions/date/hour/__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,68 @@ | ||
/** | ||
* 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 { Hour } from '../index'; | ||
import { NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object'; | ||
import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object'; | ||
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object'; | ||
import { ErrorType } from '../../../../basics/error-type'; | ||
|
||
describe('Test hour function', () => { | ||
const testFunction = new Hour(FUNCTION_NAMES_DATE.HOUR); | ||
|
||
describe('Hour', () => { | ||
it('Serial number is normal', () => { | ||
const serialNumber = NumberValueObject.create(43832.233); | ||
const result = testFunction.calculate(serialNumber); | ||
expect(result.getValue()).toStrictEqual(5); | ||
}); | ||
|
||
it('Serial number is error', () => { | ||
const serialNumber = ErrorValueObject.create(ErrorType.NAME); | ||
const result = testFunction.calculate(serialNumber); | ||
expect(result.getValue()).toStrictEqual(ErrorType.NAME); | ||
}); | ||
|
||
it('Serial number is date string', () => { | ||
const serialNumber = StringValueObject.create('2020-01-02 7:45'); | ||
const result = testFunction.calculate(serialNumber); | ||
expect(result.getValue()).toStrictEqual(7); | ||
}); | ||
|
||
it('Serial number is array', () => { | ||
const serialNumber = ArrayValueObject.create({ | ||
calculateValueList: transformToValueObject([ | ||
[ErrorType.NAME, 0.75, '2011-7-18 7:45', '2012-4-21'], | ||
[true, 'test', false, -1], | ||
]), | ||
rowCount: 2, | ||
columnCount: 4, | ||
unitId: '', | ||
sheetId: '', | ||
row: 0, | ||
column: 0, | ||
}); | ||
const result = testFunction.calculate(serialNumber); | ||
expect(transformToValue(result.getArrayValue())).toStrictEqual([ | ||
[ErrorType.NAME, 18, 7, 0], | ||
[0, ErrorType.VALUE, 0, ErrorType.NUM], | ||
]); | ||
}); | ||
}); | ||
}); |
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,79 @@ | ||
/** | ||
* 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 { excelSerialToDateTime, parseFormattedDate, parseFormattedTime } from '../../../basics/date'; | ||
import { ErrorType } from '../../../basics/error-type'; | ||
import type { BaseValueObject } from '../../../engine/value-object/base-value-object'; | ||
import { ErrorValueObject } from '../../../engine/value-object/base-value-object'; | ||
import { NumberValueObject } from '../../../engine/value-object/primitive-object'; | ||
import { BaseFunction } from '../../base-function'; | ||
|
||
export class Hour extends BaseFunction { | ||
override minParams = 1; | ||
|
||
override maxParams = 1; | ||
|
||
override calculate(serialNumber: BaseValueObject) { | ||
if (serialNumber.isError()) { | ||
return serialNumber; | ||
} | ||
|
||
if (serialNumber.isArray()) { | ||
return serialNumber.map((serialNumberObject) => { | ||
if (serialNumberObject.isError()) { | ||
return serialNumberObject; | ||
} | ||
|
||
return this._handleSingleObject(serialNumberObject); | ||
}); | ||
} | ||
|
||
return this._handleSingleObject(serialNumber); | ||
} | ||
|
||
private _handleSingleObject(serialNumberObject: BaseValueObject) { | ||
let date: Date; | ||
const dateValue = serialNumberObject.getValue(); | ||
|
||
if (serialNumberObject.isString()) { | ||
if (parseFormattedDate(`${dateValue}`)) { | ||
date = excelSerialToDateTime(parseFormattedDate(`${dateValue}`).v); | ||
} else if (parseFormattedTime(`${dateValue}`)) { | ||
date = excelSerialToDateTime(parseFormattedTime(`${dateValue}`).v); | ||
} else { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
} else { | ||
const dateSerial = +serialNumberObject.getValue(); | ||
|
||
if (dateSerial < 0) { | ||
return ErrorValueObject.create(ErrorType.NUM); | ||
} | ||
|
||
// Excel serial 0 is 1900-01-00 | ||
// Google Sheets serial 0 is 1899-12-30 | ||
if (dateSerial === 0) { | ||
return NumberValueObject.create(0); | ||
} | ||
|
||
date = excelSerialToDateTime(dateSerial); | ||
} | ||
|
||
const hour = date.getUTCHours(); | ||
|
||
return NumberValueObject.create(hour); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
packages/engine-formula/src/functions/date/minute/__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,68 @@ | ||
/** | ||
* 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 { Minute } from '../index'; | ||
import { NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object'; | ||
import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object'; | ||
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object'; | ||
import { ErrorType } from '../../../../basics/error-type'; | ||
|
||
describe('Test minute function', () => { | ||
const testFunction = new Minute(FUNCTION_NAMES_DATE.MINUTE); | ||
|
||
describe('Minute', () => { | ||
it('Serial number is normal', () => { | ||
const serialNumber = NumberValueObject.create(43832.233); | ||
const result = testFunction.calculate(serialNumber); | ||
expect(result.getValue()).toStrictEqual(35); | ||
}); | ||
|
||
it('Serial number is error', () => { | ||
const serialNumber = ErrorValueObject.create(ErrorType.NAME); | ||
const result = testFunction.calculate(serialNumber); | ||
expect(result.getValue()).toStrictEqual(ErrorType.NAME); | ||
}); | ||
|
||
it('Serial number is date string', () => { | ||
const serialNumber = StringValueObject.create('2020-01-02 7:45'); | ||
const result = testFunction.calculate(serialNumber); | ||
expect(result.getValue()).toStrictEqual(45); | ||
}); | ||
|
||
it('Serial number is array', () => { | ||
const serialNumber = ArrayValueObject.create({ | ||
calculateValueList: transformToValueObject([ | ||
[ErrorType.NAME, 0.75, '2011-7-18 7:45', '2012-4-21'], | ||
[true, 'test', false, -1], | ||
]), | ||
rowCount: 2, | ||
columnCount: 4, | ||
unitId: '', | ||
sheetId: '', | ||
row: 0, | ||
column: 0, | ||
}); | ||
const result = testFunction.calculate(serialNumber); | ||
expect(transformToValue(result.getArrayValue())).toStrictEqual([ | ||
[ErrorType.NAME, 0, 45, 0], | ||
[0, ErrorType.VALUE, 0, ErrorType.NUM], | ||
]); | ||
}); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
packages/engine-formula/src/functions/date/minute/index.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,79 @@ | ||
/** | ||
* 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 { excelSerialToDateTime, parseFormattedDate, parseFormattedTime } from '../../../basics/date'; | ||
import { ErrorType } from '../../../basics/error-type'; | ||
import type { BaseValueObject } from '../../../engine/value-object/base-value-object'; | ||
import { ErrorValueObject } from '../../../engine/value-object/base-value-object'; | ||
import { NumberValueObject } from '../../../engine/value-object/primitive-object'; | ||
import { BaseFunction } from '../../base-function'; | ||
|
||
export class Minute extends BaseFunction { | ||
override minParams = 1; | ||
|
||
override maxParams = 1; | ||
|
||
override calculate(serialNumber: BaseValueObject) { | ||
if (serialNumber.isError()) { | ||
return serialNumber; | ||
} | ||
|
||
if (serialNumber.isArray()) { | ||
return serialNumber.map((serialNumberObject) => { | ||
if (serialNumberObject.isError()) { | ||
return serialNumberObject; | ||
} | ||
|
||
return this._handleSingleObject(serialNumberObject); | ||
}); | ||
} | ||
|
||
return this._handleSingleObject(serialNumber); | ||
} | ||
|
||
private _handleSingleObject(serialNumberObject: BaseValueObject) { | ||
let date: Date; | ||
const dateValue = serialNumberObject.getValue(); | ||
|
||
if (serialNumberObject.isString()) { | ||
if (parseFormattedDate(`${dateValue}`)) { | ||
date = excelSerialToDateTime(parseFormattedDate(`${dateValue}`).v); | ||
} else if (parseFormattedTime(`${dateValue}`)) { | ||
date = excelSerialToDateTime(parseFormattedTime(`${dateValue}`).v); | ||
} else { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
} else { | ||
const dateSerial = +serialNumberObject.getValue(); | ||
|
||
if (dateSerial < 0) { | ||
return ErrorValueObject.create(ErrorType.NUM); | ||
} | ||
|
||
// Excel serial 0 is 1900-01-00 | ||
// Google Sheets serial 0 is 1899-12-30 | ||
if (dateSerial === 0) { | ||
return NumberValueObject.create(0); | ||
} | ||
|
||
date = excelSerialToDateTime(dateSerial); | ||
} | ||
|
||
const hour = date.getUTCMinutes(); | ||
|
||
return NumberValueObject.create(hour); | ||
} | ||
} |
Oops, something went wrong.