Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(engine-formula): add iseven isodd (#1873)
* feat(engine-formula): add iseven isodd * feat: register isodd * chore: fix typo * feat: deal with i18n * test: add tests for not convertable situations * feat: deal with boolean values * fix: test * fix: fix array value
- Loading branch information
Showing
9 changed files
with
203 additions
and
32 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
49 changes: 49 additions & 0 deletions
49
packages/engine-formula/src/functions/information/iseven/iseven.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,49 @@ | ||
/** | ||
* 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_INFORMATION } from '../function-names'; | ||
import { BooleanValueObject, NumberValueObject, StringValueObject } from '../../../engine/value-object/primitive-object'; | ||
import { ErrorType } from '../../../basics/error-type'; | ||
import { ArrayValueObject } from '../../../engine/value-object/array-value-object'; | ||
import { Iseven } from './iseven'; | ||
|
||
describe('Test iseven function', () => { | ||
const testFunction = new Iseven(FUNCTION_NAMES_INFORMATION.ISEVEN); | ||
|
||
it('should work with different kind of number values', () => { | ||
expect(testFunction.calculate(NumberValueObject.create(-1)).getValue()).toBeFalsy(); | ||
expect(testFunction.calculate(NumberValueObject.create(2.5)).getValue()).toBeTruthy(); | ||
expect(testFunction.calculate(NumberValueObject.create(5)).getValue()).toBeFalsy(); | ||
expect(testFunction.calculate(NumberValueObject.create(0)).getValue()).toBeTruthy(); | ||
}); | ||
|
||
it('should convert value first if is it not a number value', () => { | ||
expect(testFunction.calculate(StringValueObject.create('123')).getValue()).toBeFalsy(); | ||
expect(testFunction.calculate(StringValueObject.create('122')).getValue()).toBeTruthy(); | ||
}); | ||
|
||
it('should throw error when value is not convertable to number', () => { | ||
expect(testFunction.calculate(StringValueObject.create('not')).getValue()).toBe(ErrorType.VALUE); | ||
expect(testFunction.calculate(BooleanValueObject.create(true)).getValue()).toBe(ErrorType.VALUE); | ||
expect(testFunction.calculate(BooleanValueObject.create(false)).getValue()).toBe(ErrorType.VALUE); | ||
}); | ||
|
||
it('should throw error for array values', () => { | ||
expect(testFunction.calculate(ArrayValueObject.create('A1:C1')).getValue()).toBe(ErrorType.VALUE); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
packages/engine-formula/src/functions/information/iseven/iseven.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,44 @@ | ||
/** | ||
* 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 { 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 { BooleanValueObject } from '../../../engine/value-object/primitive-object'; | ||
import { BaseFunction } from '../../base-function'; | ||
|
||
export class Iseven extends BaseFunction { | ||
override calculate(value: BaseValueObject) { | ||
if (value == null) { | ||
return ErrorValueObject.create(ErrorType.NA); | ||
} | ||
|
||
if (value.isArray() || value.isBoolean()) { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
|
||
if (!value.isNumber()) { | ||
value = value.convertToNumberObjectValue(); | ||
if (!value.isNumber()) { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
} | ||
|
||
const val = value.getValue() as number; | ||
const floored = Math.floor(Math.abs(val)); | ||
return BooleanValueObject.create(floored % 2 === 0); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/engine-formula/src/functions/information/isodd/isodd.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,50 @@ | ||
/** | ||
* 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_INFORMATION } from '../function-names'; | ||
|
||
import { BooleanValueObject, NumberValueObject, StringValueObject } from '../../../engine/value-object/primitive-object'; | ||
import { ErrorType } from '../../../basics/error-type'; | ||
import { ArrayValueObject } from '../../../engine/value-object/array-value-object'; | ||
import { Isodd } from './isodd'; | ||
|
||
|
||
describe('Test isodd function', () => { | ||
const testFunction = new Isodd(FUNCTION_NAMES_INFORMATION.ISODD); | ||
|
||
it('should work with different kind of number values', () => { | ||
expect(testFunction.calculate(NumberValueObject.create(-1)).getValue()).toBeTruthy(); | ||
expect(testFunction.calculate(NumberValueObject.create(2.5)).getValue()).toBeFalsy(); | ||
expect(testFunction.calculate(NumberValueObject.create(5)).getValue()).toBeTruthy(); | ||
expect(testFunction.calculate(NumberValueObject.create(0)).getValue()).toBeFalsy(); | ||
}); | ||
|
||
it('should convert value first if is it not a number value', () => { | ||
expect(testFunction.calculate(StringValueObject.create('123')).getValue()).toBeTruthy(); | ||
expect(testFunction.calculate(StringValueObject.create('122')).getValue()).toBeFalsy(); | ||
}); | ||
|
||
it('should throw error when value is not convertable to number', () => { | ||
expect(testFunction.calculate(StringValueObject.create('not')).getValue()).toBe(ErrorType.VALUE); | ||
expect(testFunction.calculate(BooleanValueObject.create(true)).getValue()).toBe(ErrorType.VALUE); | ||
expect(testFunction.calculate(BooleanValueObject.create(false)).getValue()).toBe(ErrorType.VALUE); | ||
}); | ||
|
||
it('should throw error for array values', () => { | ||
expect(testFunction.calculate(ArrayValueObject.create('A1:C1')).getValue()).toBe(ErrorType.VALUE); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
packages/engine-formula/src/functions/information/isodd/isodd.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,44 @@ | ||
/** | ||
* 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 { 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 { BooleanValueObject } from '../../../engine/value-object/primitive-object'; | ||
import { BaseFunction } from '../../base-function'; | ||
|
||
export class Isodd extends BaseFunction { | ||
override calculate(value: BaseValueObject) { | ||
if (value == null) { | ||
return ErrorValueObject.create(ErrorType.NA); | ||
} | ||
|
||
if (value.isArray() || value.isBoolean()) { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
|
||
if (!value.isNumber()) { | ||
value = value.convertToNumberObjectValue(); | ||
if (!value.isNumber()) { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
} | ||
|
||
const val = value.getValue() as number; | ||
const floored = Math.floor(Math.abs(val)); | ||
return BooleanValueObject.create(floored % 2 !== 0); | ||
} | ||
} |
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