-
-
Notifications
You must be signed in to change notification settings - Fork 465
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
352 additions
and
21 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
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
161 changes: 161 additions & 0 deletions
161
packages/engine-formula/src/functions/statistical/minifs/__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,161 @@ | ||
/** | ||
* 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: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 { FUNCTION_NAMES_STATISTICAL } from '../../function-names'; | ||
import { Minifs } from '../index'; | ||
import { ArrayValueObject, transformToValue } from '../../../../engine/value-object/array-value-object'; | ||
import { NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object'; | ||
|
||
describe('Test minifs function', () => { | ||
const testFunction = new Minifs(FUNCTION_NAMES_STATISTICAL.MINIFS); | ||
|
||
describe('Minifs', () => { | ||
it('Range and criteria', async () => { | ||
const minRange = ArrayValueObject.create(`{ | ||
1; | ||
2; | ||
3 | ||
}`); | ||
const range = ArrayValueObject.create(`{ | ||
2; | ||
3; | ||
4 | ||
}`); | ||
|
||
const criteria = StringValueObject.create('>2'); | ||
const resultObject = testFunction.calculate(minRange, range, criteria); | ||
expect(transformToValue(resultObject.getArrayValue())).toStrictEqual([[2]]); | ||
}); | ||
|
||
it('Range and array criteria', async () => { | ||
const minRange = ArrayValueObject.create(`{ | ||
1; | ||
2; | ||
3 | ||
}`); | ||
|
||
const range = ArrayValueObject.create(`{ | ||
2; | ||
3; | ||
4 | ||
}`); | ||
|
||
const criteria = ArrayValueObject.create(`{ | ||
>2; | ||
>3; | ||
>4 | ||
}`); | ||
|
||
const resultObject = testFunction.calculate(minRange, range, criteria); | ||
expect(transformToValue(resultObject.getArrayValue())).toStrictEqual([[2], [3], [0]]); | ||
}); | ||
|
||
it('2 ranges and criteria', async () => { | ||
const minRange = ArrayValueObject.create(`{ | ||
1; | ||
2; | ||
3 | ||
}`); | ||
|
||
const range1 = ArrayValueObject.create(`{ | ||
2; | ||
3; | ||
4 | ||
}`); | ||
|
||
const criteria1 = StringValueObject.create('>2'); | ||
|
||
const range2 = ArrayValueObject.create(`{ | ||
3; | ||
4; | ||
5 | ||
}`); | ||
|
||
const criteria2 = StringValueObject.create('<5'); | ||
|
||
const resultObject = testFunction.calculate(minRange, range1, criteria1, range2, criteria2); | ||
expect(transformToValue(resultObject.getArrayValue())).toStrictEqual([[2]]); | ||
}); | ||
|
||
it('2 ranges and criteria, 1 array criteria', async () => { | ||
const minRange = ArrayValueObject.create(`{ | ||
1; | ||
2; | ||
3 | ||
}`); | ||
|
||
const range1 = ArrayValueObject.create(`{ | ||
2; | ||
3; | ||
4 | ||
}`); | ||
|
||
const criteria1 = ArrayValueObject.create(`{ | ||
>2; | ||
>3; | ||
>4 | ||
}`); | ||
|
||
const range2 = ArrayValueObject.create(`{ | ||
3; | ||
4; | ||
5 | ||
}`); | ||
|
||
const criteria2 = NumberValueObject.create(5); | ||
|
||
const resultObject = testFunction.calculate(minRange, range1, criteria1, range2, criteria2); | ||
expect(transformToValue(resultObject.getArrayValue())).toStrictEqual([[3], [3], [0]]); | ||
}); | ||
|
||
it('2 ranges and criteria, 2 array criteria', async () => { | ||
const minRange = ArrayValueObject.create(`{ | ||
1; | ||
2; | ||
3 | ||
}`); | ||
|
||
const range1 = ArrayValueObject.create(`{ | ||
2; | ||
3; | ||
4 | ||
}`); | ||
|
||
const criteria1 = ArrayValueObject.create(`{ | ||
>2; | ||
>3; | ||
>4 | ||
}`); | ||
|
||
const range2 = ArrayValueObject.create(`{ | ||
3; | ||
4; | ||
5 | ||
}`); | ||
|
||
const criteria2 = ArrayValueObject.create(`{ | ||
4; | ||
4; | ||
4; | ||
4 | ||
}`); | ||
|
||
const resultObject = testFunction.calculate(minRange, range1, criteria1, range2, criteria2); | ||
expect(transformToValue(resultObject.getArrayValue())).toStrictEqual([[2], [0], [0], [0]]); | ||
}); | ||
}); | ||
}); |
130 changes: 130 additions & 0 deletions
130
packages/engine-formula/src/functions/statistical/minifs/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,130 @@ | ||
/** | ||
* 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: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 { expandArrayValueObject } from '../../../engine/utils/array-object'; | ||
import { booleanObjectIntersection, valueObjectCompare } from '../../../engine/utils/object-compare'; | ||
import { ArrayValueObject } from '../../../engine/value-object/array-value-object'; | ||
import type { BaseValueObject, IArrayValueObject } from '../../../engine/value-object/base-value-object'; | ||
import { ErrorValueObject } from '../../../engine/value-object/base-value-object'; | ||
import { BaseFunction } from '../../base-function'; | ||
|
||
export class Minifs extends BaseFunction { | ||
override minParams = 3; | ||
|
||
override maxParams = 255; | ||
|
||
override calculate(minRange: BaseValueObject, ...variants: BaseValueObject[]) { | ||
if (minRange.isError()) { | ||
return ErrorValueObject.create(ErrorType.NA); | ||
} | ||
|
||
if (!minRange.isArray()) { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
|
||
// Range and criteria must be paired | ||
if (variants.length % 2 !== 0) { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
|
||
// Every range must be array | ||
if (variants.some((variant, i) => i % 2 === 0 && !variant.isArray())) { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
|
||
const sumRowLength = (minRange as ArrayValueObject).getRowCount(); | ||
const sumColumnLength = (minRange as ArrayValueObject).getColumnCount(); | ||
// The size of the extended range is determined by the maximum width and height of the criteria range. | ||
let maxRowLength = 0; | ||
let maxColumnLength = 0; | ||
|
||
variants.forEach((variant, i) => { | ||
if (i % 2 === 1) { | ||
if (variant.isArray()) { | ||
const arrayValue = variant as ArrayValueObject; | ||
maxRowLength = Math.max(maxRowLength, arrayValue.getRowCount()); | ||
maxColumnLength = Math.max(maxColumnLength, arrayValue.getColumnCount()); | ||
} else { | ||
maxRowLength = Math.max(maxRowLength, 1); | ||
maxColumnLength = Math.max(maxColumnLength, 1); | ||
} | ||
} | ||
}); | ||
|
||
const booleanResults: BaseValueObject[][] = []; | ||
|
||
for (let i = 0; i < variants.length; i++) { | ||
if (i % 2 === 1) continue; | ||
|
||
const range = variants[i] as ArrayValueObject; | ||
|
||
const rangeRowLength = range.getRowCount(); | ||
const rangeColumnLength = range.getColumnCount(); | ||
if (rangeRowLength !== sumRowLength || rangeColumnLength !== sumColumnLength) { | ||
return expandArrayValueObject(maxRowLength, maxColumnLength, ErrorValueObject.create(ErrorType.NA)); | ||
} | ||
|
||
const criteria = variants[i + 1]; | ||
const criteriaArray = expandArrayValueObject(maxRowLength, maxColumnLength, criteria, ErrorValueObject.create(ErrorType.NA)); | ||
|
||
criteriaArray.iterator((criteriaValueObject, rowIndex, columnIndex) => { | ||
if (!criteriaValueObject) { | ||
return; | ||
} | ||
|
||
const resultArrayObject = valueObjectCompare(range, criteriaValueObject); | ||
|
||
if (booleanResults[rowIndex] === undefined) { | ||
booleanResults[rowIndex] = []; | ||
} | ||
|
||
if (booleanResults[rowIndex][columnIndex] === undefined) { | ||
booleanResults[rowIndex][columnIndex] = resultArrayObject; | ||
} | ||
|
||
booleanResults[rowIndex][columnIndex] = booleanObjectIntersection(booleanResults[rowIndex][columnIndex], resultArrayObject); | ||
}); | ||
} | ||
|
||
return this._aggregateResults(minRange, booleanResults); | ||
} | ||
|
||
private _aggregateResults(minRange: BaseValueObject, booleanResults: BaseValueObject[][]): ArrayValueObject { | ||
const maxResults = booleanResults.map((row) => { | ||
return row.map((booleanResult) => { | ||
const picked = (minRange as ArrayValueObject).pick(booleanResult as ArrayValueObject); | ||
if (picked.getColumnCount() === 0) { | ||
return ArrayValueObject.create('0'); | ||
} | ||
|
||
return picked.min(); | ||
}); | ||
}); | ||
|
||
const arrayValueObjectData: IArrayValueObject = { | ||
calculateValueList: maxResults, | ||
rowCount: maxResults.length, | ||
columnCount: maxResults[0].length, | ||
unitId: this.unitId || '', | ||
sheetId: this.subUnitId || '', | ||
row: this.row, | ||
column: this.column, | ||
}; | ||
|
||
return ArrayValueObject.create(arrayValueObjectData); | ||
} | ||
} |
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
Oops, something went wrong.