-
-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
182 additions
and
15 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
76 changes: 76 additions & 0 deletions
76
packages/engine-formula/src/functions/statistical/countif/__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,76 @@ | ||
/** | ||
* 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 | ||
* | ||
* https://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 { ArrayValueObject, transformToValue } from '../../../../engine/value-object/array-value-object'; | ||
import { FUNCTION_NAMES_STATISTICAL } from '../../function-names'; | ||
import { Countif } from '../index'; | ||
import { StringValueObject } from '../../../../engine/value-object/primitive-object'; | ||
|
||
describe('Test countif function', () => { | ||
const testFunction = new Countif(FUNCTION_NAMES_STATISTICAL.COUNTIF); | ||
|
||
describe('Countif', () => { | ||
it('Range and criteria', async () => { | ||
const range = ArrayValueObject.create(/*ts*/ `{ | ||
1; | ||
4; | ||
44; | ||
444; | ||
Univer | ||
}`); | ||
|
||
const criteria = StringValueObject.create('>40'); | ||
|
||
const resultObject = testFunction.calculate(range, criteria); | ||
expect(resultObject.getValue()).toBe(2); | ||
}); | ||
|
||
it('Average range with wildcard asterisk', async () => { | ||
const range = ArrayValueObject.create(/*ts*/ `{ | ||
Ada; | ||
test1; | ||
test12; | ||
Univer | ||
}`); | ||
|
||
const criteria = StringValueObject.create('test*'); | ||
|
||
const resultObject = testFunction.calculate(range, criteria); | ||
expect(resultObject.getValue()).toBe(2); | ||
}); | ||
|
||
it('ArrayValueObject range and ArrayValueObject criteria', async () => { | ||
const range = ArrayValueObject.create(/*ts*/ `{ | ||
1; | ||
4; | ||
44; | ||
444 | ||
}`); | ||
|
||
const criteria = ArrayValueObject.create(/*ts*/ `{ | ||
4; | ||
4; | ||
44; | ||
444 | ||
}`); | ||
|
||
const resultObject = testFunction.calculate(range, criteria); | ||
expect(transformToValue(resultObject.getArrayValue())).toStrictEqual([[1], [1], [1], [1]]); | ||
}); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
packages/engine-formula/src/functions/statistical/countif/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,64 @@ | ||
/** | ||
* 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 | ||
* | ||
* https://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 { isNumericComparison, valueObjectCompare } from '../../../engine/utils/object-compare'; | ||
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object'; | ||
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object'; | ||
import { BaseFunction } from '../../base-function'; | ||
|
||
export class Countif extends BaseFunction { | ||
override minParams = 2; | ||
|
||
override maxParams = 3; | ||
|
||
override calculate(range: BaseValueObject, criteria: BaseValueObject) { | ||
if (range.isError() || criteria.isError()) { | ||
return ErrorValueObject.create(ErrorType.NA); | ||
} | ||
|
||
if (!range.isArray()) { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
|
||
if (criteria.isArray()) { | ||
return criteria.map((criteriaItem) => this._handleSingleObject(range, criteriaItem)); | ||
} | ||
|
||
return this._handleSingleObject(range, criteria); | ||
} | ||
|
||
private _handleSingleObject(range: BaseValueObject, criteria: BaseValueObject, averageRange?: BaseValueObject) { | ||
const resultArrayObject = valueObjectCompare(range, criteria); | ||
|
||
// averageRange has the same dimensions as range | ||
const averageRangeArray = averageRange | ||
? (averageRange as ArrayValueObject).slice( | ||
[0, (range as ArrayValueObject).getRowCount()], | ||
[0, (range as ArrayValueObject).getColumnCount()] | ||
) | ||
: (range as ArrayValueObject); | ||
|
||
if (!averageRangeArray) { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
|
||
const picked = averageRangeArray.pick(resultArrayObject as ArrayValueObject); | ||
// If the condition is a numeric comparison, only numbers are counted, otherwise text is counted. | ||
const isNumeric = isNumericComparison(criteria.getValue()); | ||
return isNumeric ? picked.count() : picked.countA(); | ||
} | ||
} |
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