From 72a19d03969c6974a862d89fb8849583dc78d0fe Mon Sep 17 00:00:00 2001 From: izulin Date: Tue, 11 May 2021 16:33:44 +0200 Subject: [PATCH 1/7] filter implementation --- src/error-message.ts | 2 + src/i18n/languages/csCZ.ts | 1 + src/i18n/languages/daDK.ts | 1 + src/i18n/languages/deDE.ts | 1 + src/i18n/languages/enGB.ts | 1 + src/i18n/languages/esES.ts | 1 + src/i18n/languages/fiFI.ts | 1 + src/i18n/languages/frFR.ts | 1 + src/i18n/languages/huHU.ts | 1 + src/i18n/languages/itIT.ts | 1 + src/i18n/languages/nbNO.ts | 1 + src/i18n/languages/nlNL.ts | 1 + src/i18n/languages/plPL.ts | 1 + src/i18n/languages/ptPT.ts | 1 + src/i18n/languages/ruRU.ts | 1 + src/i18n/languages/svSE.ts | 1 + src/i18n/languages/trTR.ts | 1 + src/interpreter/plugin/ArrayFormulaPlugin.ts | 54 +++++++++++++++++++- 18 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/error-message.ts b/src/error-message.ts index 34c72d6511..5593fc7370 100644 --- a/src/error-message.ts +++ b/src/error-message.ts @@ -16,6 +16,7 @@ export class ErrorMessage { public static BadCriterion = 'Incorrect criterion.' public static RangeManySheets = 'Range spans more than one sheet.' public static CellRangeExpected = 'Cell range expected.' + public static WrongDimension = 'Wrong range dimension.' public static ScalarExpected = 'Cell range not allowed.' public static NumberCoercion = 'Value cannot be coerced to number.' public static NumberExpected = 'Number argument expected.' @@ -32,6 +33,7 @@ export class ErrorMessage { public static NotHex = 'String does not represent a hexadecimal number.' public static EndStartPeriod = 'End period needs to be at least start period.' public static CellRefExpected = 'Cell reference expected.' + public static EmptyRange = 'Empty range not allowed.' public static BadRef = 'Address is not correct.' public static NumberRange = 'Number-only range expected.' public static ValueNotFound = 'Value not found.' diff --git a/src/i18n/languages/csCZ.ts b/src/i18n/languages/csCZ.ts index 76bdab8bc8..0791abe4d6 100644 --- a/src/i18n/languages/csCZ.ts +++ b/src/i18n/languages/csCZ.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#HODNOTA!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ARCCOS', diff --git a/src/i18n/languages/daDK.ts b/src/i18n/languages/daDK.ts index 93eff0f6d5..9c67ff62ba 100644 --- a/src/i18n/languages/daDK.ts +++ b/src/i18n/languages/daDK.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#VÆRDI!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ARCCOS', diff --git a/src/i18n/languages/deDE.ts b/src/i18n/languages/deDE.ts index 3aa8e3f468..54d0a4515c 100644 --- a/src/i18n/languages/deDE.ts +++ b/src/i18n/languages/deDE.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#WERT!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ARCCOS', diff --git a/src/i18n/languages/enGB.ts b/src/i18n/languages/enGB.ts index f25f722ccc..b405ea2f0e 100644 --- a/src/i18n/languages/enGB.ts +++ b/src/i18n/languages/enGB.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#VALUE!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ACOS', diff --git a/src/i18n/languages/esES.ts b/src/i18n/languages/esES.ts index b8622a6dd1..1a8e2f2ecb 100644 --- a/src/i18n/languages/esES.ts +++ b/src/i18n/languages/esES.ts @@ -18,6 +18,7 @@ export const dictionary: RawTranslationPackage = { VALUE: '#¡VALOR!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ACOS', diff --git a/src/i18n/languages/fiFI.ts b/src/i18n/languages/fiFI.ts index 9cbb1ea988..cc45b19768 100644 --- a/src/i18n/languages/fiFI.ts +++ b/src/i18n/languages/fiFI.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#ARVO!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ITSEISARVO', ACOS: 'ACOS', diff --git a/src/i18n/languages/frFR.ts b/src/i18n/languages/frFR.ts index 3ca195050d..24b8484bba 100644 --- a/src/i18n/languages/frFR.ts +++ b/src/i18n/languages/frFR.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#VALEUR!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ACOS', diff --git a/src/i18n/languages/huHU.ts b/src/i18n/languages/huHU.ts index 968d7d3c34..f3e37475c2 100644 --- a/src/i18n/languages/huHU.ts +++ b/src/i18n/languages/huHU.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#ÉRTÉK!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ARCCOS', diff --git a/src/i18n/languages/itIT.ts b/src/i18n/languages/itIT.ts index 87dd871177..0ccc0f3079 100644 --- a/src/i18n/languages/itIT.ts +++ b/src/i18n/languages/itIT.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#VALORE!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ASS', ACOS: 'ARCCOS', diff --git a/src/i18n/languages/nbNO.ts b/src/i18n/languages/nbNO.ts index db96792487..26b7128a99 100644 --- a/src/i18n/languages/nbNO.ts +++ b/src/i18n/languages/nbNO.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#VERDI!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ARCCOS', diff --git a/src/i18n/languages/nlNL.ts b/src/i18n/languages/nlNL.ts index e01a972252..3396d94b21 100644 --- a/src/i18n/languages/nlNL.ts +++ b/src/i18n/languages/nlNL.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#WAARDE!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'BOOGCOS', diff --git a/src/i18n/languages/plPL.ts b/src/i18n/languages/plPL.ts index 54750ce876..5d23722d17 100644 --- a/src/i18n/languages/plPL.ts +++ b/src/i18n/languages/plPL.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#ARG!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'WART.BEZWGL', ACOS: 'ACOS', diff --git a/src/i18n/languages/ptPT.ts b/src/i18n/languages/ptPT.ts index a44e97a8ef..4f98ccbe4b 100644 --- a/src/i18n/languages/ptPT.ts +++ b/src/i18n/languages/ptPT.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#VALOR!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ACOS', diff --git a/src/i18n/languages/ruRU.ts b/src/i18n/languages/ruRU.ts index 8d9531d6fa..c7b2f34264 100644 --- a/src/i18n/languages/ruRU.ts +++ b/src/i18n/languages/ruRU.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#ЗНАЧ!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ACOS', diff --git a/src/i18n/languages/svSE.ts b/src/i18n/languages/svSE.ts index f1dd13a2fe..88877848ce 100644 --- a/src/i18n/languages/svSE.ts +++ b/src/i18n/languages/svSE.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#VÄRDEFEL!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'ABS', ACOS: 'ARCCOS', diff --git a/src/i18n/languages/trTR.ts b/src/i18n/languages/trTR.ts index 9994fa4afe..6a3fe7f371 100644 --- a/src/i18n/languages/trTR.ts +++ b/src/i18n/languages/trTR.ts @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = { VALUE: '#DEĞER!', }, functions: { + FILTER: 'FILTER', ARRAYFORMULA: 'ARRAYFORMULA', ABS: 'MUTLAK', ACOS: 'ACOS', diff --git a/src/interpreter/plugin/ArrayFormulaPlugin.ts b/src/interpreter/plugin/ArrayFormulaPlugin.ts index 2738ff51c5..1a40701f50 100644 --- a/src/interpreter/plugin/ArrayFormulaPlugin.ts +++ b/src/interpreter/plugin/ArrayFormulaPlugin.ts @@ -3,9 +3,13 @@ * Copyright (c) 2021 Handsoncode. All rights reserved. */ +import {CellError, ErrorType} from '../../Cell' +import {ErrorMessage} from '../../error-message' import {ProcedureAst} from '../../parser' +import {coerceScalarToBoolean} from '../ArithmeticHelper' import {InterpreterState} from '../InterpreterState' -import {InternalScalarValue} from '../InterpreterValue' +import {InternalScalarValue, InterpreterValue} from '../InterpreterValue' +import {SimpleRangeValue} from '../SimpleRangeValue' import {ArgumentTypes, FunctionPlugin, FunctionPluginTypecheck} from './FunctionPlugin' export class ArrayFormulaPlugin extends FunctionPlugin implements FunctionPluginTypecheck{ @@ -17,9 +21,57 @@ export class ArrayFormulaPlugin extends FunctionPlugin implements FunctionPlugin {argumentType: ArgumentTypes.ANY} ], }, + 'FILTER': { + method: 'filter', + arrayFunction: true, + parameters: [ + {argumentType: ArgumentTypes.RANGE}, + {argumentType: ArgumentTypes.RANGE}, + ], + repeatLastArgs: 1, + } } public arrayformula(ast: ProcedureAst, state: InterpreterState): InternalScalarValue { return this.runFunction(ast.args, state, this.metadata('ARRAYFORMULA'), (value) => value) } + + public filter(ast: ProcedureAst, state: InterpreterState): InterpreterValue { + return this.runMatrixFunction(ast.args, state, this.metadata('FILTER'), (rangeVals: SimpleRangeValue, ...rangeFilters: SimpleRangeValue[]) => { + for(const filter of rangeFilters) { + if (rangeVals.width() !== filter.width() || rangeVals.height() !== filter.height()) { + return new CellError(ErrorType.NA, ErrorMessage.EqualLength) + } + } + if(rangeVals.width()>1 && rangeVals.height()>1) { + return new CellError(ErrorType.NA, ErrorMessage.WrongDimension) + } + const vals = rangeVals.data + const ret = [] + for(let i=0;i0) { + ret.push(row) + } + } + if(ret.length>0) { + return SimpleRangeValue.onlyValues(ret) + } else { + return new CellError(ErrorType.NA, ErrorMessage.EmptyRange) + } + }) + } } From ec949e5fa1b208266c7bb48171db2a6a42d8be62 Mon Sep 17 00:00:00 2001 From: izulin Date: Tue, 11 May 2021 16:50:57 +0200 Subject: [PATCH 2/7] test file --- test/interpreter/function-filter.spec.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/interpreter/function-filter.spec.ts diff --git a/test/interpreter/function-filter.spec.ts b/test/interpreter/function-filter.spec.ts new file mode 100644 index 0000000000..e69de29bb2 From 12ec7f3c94f560219b01ba3ef8b4bb5a2c5e87ea Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 12 May 2021 00:57:39 +0200 Subject: [PATCH 3/7] fix --- src/MatrixSize.ts | 13 +++++++++ test/interpreter/function-filter.spec.ts | 35 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/MatrixSize.ts b/src/MatrixSize.ts index a51e226ea4..4dffa5497b 100644 --- a/src/MatrixSize.ts +++ b/src/MatrixSize.ts @@ -137,6 +137,19 @@ export class MatrixSizePredictor { } return subChecks[0] } + case 'FILTER': { + if(ast.args.length <= 1) { + return undefined + } + for(const subcheck of subChecks) { + if(subcheck === undefined) { + return undefined + } + } + const width = Math.max(...(subChecks as MatrixSize[]).map(val => val.width)) + const height = Math.max(...(subChecks as MatrixSize[]).map(val => val.height)) + return new MatrixSize(width, height) + } default: { return new MatrixSize(1, 1) } diff --git a/test/interpreter/function-filter.spec.ts b/test/interpreter/function-filter.spec.ts index e69de29bb2..3cc6b8678e 100644 --- a/test/interpreter/function-filter.spec.ts +++ b/test/interpreter/function-filter.spec.ts @@ -0,0 +1,35 @@ +import {ErrorType, HyperFormula} from '../../src' +import {ErrorMessage} from '../../src/error-message' +import {adr, detailedError} from '../testUtils' + +describe('Function FALSE', () => { + it('validates input #1', () => { + const engine = HyperFormula.buildFromArray([['=FILTER(A2:B3, A2:B3)']]) + + expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongDimension)) + }) + + it('validates input #2', () => { + const engine = HyperFormula.buildFromArray([['=FILTER(A2:A3, A2:A3, A2:A4)']]) + + expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.EqualLength)) + }) + + it('validates input #3', () => { + const engine = HyperFormula.buildFromArray([['=FILTER(1, FALSE())']]) + + expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.EmptyRange)) + }) + + it('works #1', () => { + const engine = HyperFormula.buildFromArray([['=FILTER(A2:C2,A3:C3)'],[1,2,3],[true,false,true]]) + + expect(engine.getSheetValues(0)).toEqual([[1,3],[1,2,3],[true,false,true]]) + }) + + it('is 0-arity', () => { + const engine = HyperFormula.buildFromArray([['=FALSE(1)']]) + + expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongArgNumber)) + }) +}) From 2f8b3c01af9606a64e395db8f7a18551afb76bd0 Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 12 May 2021 15:35:31 +0200 Subject: [PATCH 4/7] bugfix --- src/DependencyGraph/MatrixVertex.ts | 5 +++-- src/Evaluator.ts | 6 ++++++ src/Matrix.ts | 25 +++++++++++++++++++----- test/cruds/adding-columns.spec.ts | 8 ++++---- test/cruds/adding-row.spec.ts | 6 +++--- test/interpreter/function-filter.spec.ts | 24 +++++++++++++++++------ test/matrix.spec.ts | 9 +++++---- 7 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/DependencyGraph/MatrixVertex.ts b/src/DependencyGraph/MatrixVertex.ts index f116959819..03964e7785 100644 --- a/src/DependencyGraph/MatrixVertex.ts +++ b/src/DependencyGraph/MatrixVertex.ts @@ -35,6 +35,7 @@ export class MatrixVertex { } public setCellValue(matrix: Matrix) { + matrix.resize(this.matrix.size) this.matrix = matrix } @@ -42,11 +43,11 @@ export class MatrixVertex { this.matrix = new ErroredMatrix(error, this.matrix.size) } - public getCellValue(): Matrix | CellError { + public getCellValue(): Matrix { if (this.matrix instanceof NotComputedMatrix) { throw Error('Matrix not computed yet.') } - return this.matrix as (Matrix | CellError) + return this.matrix as Matrix } public getMatrixCellValue(address: SimpleCellAddress): InternalScalarValue { diff --git a/src/Evaluator.ts b/src/Evaluator.ts index 3b03b59bfe..2cbc065d9d 100644 --- a/src/Evaluator.ts +++ b/src/Evaluator.ts @@ -105,6 +105,10 @@ export class Evaluator { const error = new CellError(ErrorType.CYCLE, undefined, vertex.address) vertex.setCellValue(error) changes.addChange(error, vertex.address) + } else if (vertex instanceof MatrixVertex && vertex.isFormula()) { + const error = new CellError(ErrorType.CYCLE, undefined, vertex.cellAddress) + vertex.setErrorValue(error) + changes.addChange(error, vertex.cellAddress) } }, ) @@ -144,6 +148,8 @@ export class Evaluator { cycled.forEach((vertex: Vertex) => { if (vertex instanceof FormulaCellVertex) { vertex.setCellValue(new CellError(ErrorType.CYCLE, undefined, vertex.address)) + } else if (vertex instanceof MatrixVertex && vertex.isFormula()) { + vertex.setErrorValue(new CellError(ErrorType.CYCLE, undefined, vertex.cellAddress)) } }) sorted.forEach((vertex: Vertex) => { diff --git a/src/Matrix.ts b/src/Matrix.ts index b860fd23b1..f7e6976f12 100644 --- a/src/Matrix.ts +++ b/src/Matrix.ts @@ -4,7 +4,7 @@ */ import {CellError, SimpleCellAddress, simpleCellAddress} from './Cell' -import {InternalScalarValue} from './interpreter/InterpreterValue' +import {EmptyValue, InternalScalarValue} from './interpreter/InterpreterValue' import {MatrixSize} from './MatrixSize' export interface IMatrix { @@ -45,13 +45,13 @@ export class Matrix implements IMatrix { } public addRows(aboveRow: number, numberOfRows: number) { - this.matrix.splice(aboveRow, 0, ...this.zeroArrays(numberOfRows, this.width())) + this.matrix.splice(aboveRow, 0, ...this.nullArrays(numberOfRows, this.width())) this.size.height += numberOfRows } public addColumns(aboveColumn: number, numberOfColumns: number) { for (let i = 0; i < this.height(); i++) { - this.matrix[i].splice(aboveColumn, 0, ...new Array(numberOfColumns).fill(0)) + this.matrix[i].splice(aboveColumn, 0, ...new Array(numberOfColumns).fill(EmptyValue)) } this.size.width += numberOfColumns } @@ -76,10 +76,10 @@ export class Matrix implements IMatrix { this.size.width -= numberOfColumns } - public zeroArrays(count: number, size: number) { + public nullArrays(count: number, size: number) { const result = [] for (let i = 0; i < count; ++i) { - result.push(new Array(size).fill(0)) + result.push(new Array(size).fill(EmptyValue)) } return result } @@ -118,6 +118,21 @@ export class Matrix implements IMatrix { } } + public resize(newSize: MatrixSize) { + if(this.height() < newSize.height) { + this.addRows(this.height(), newSize.height-this.height()) + } + if(this.height() > newSize.height) { + throw "Resizing to smaller matrix" + } + if(this.width() < newSize.width) { + this.addColumns(this.width(), newSize.width-this.width()) + } + if(this.width() > newSize.width) { + throw "Resizing to smaller matrix" + } + } + private outOfBound(col: number, row: number): boolean { return col < 0 || row < 0 || row > this.size.height - 1 || col > this.size.width - 1 } diff --git a/test/cruds/adding-columns.spec.ts b/test/cruds/adding-columns.spec.ts index 91cb3cc9f4..f7d6f86d2d 100644 --- a/test/cruds/adding-columns.spec.ts +++ b/test/cruds/adding-columns.spec.ts @@ -255,8 +255,8 @@ describe('Adding column - MatrixVertex', () => { }) }) -describe('Adding column - numeric matrix', () => { - it('add column inside numeric matrix, expand matrix', () => { +describe('Adding column - matrix', () => { + it('add column inside matrix, expand matrix', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], @@ -266,8 +266,8 @@ describe('Adding column - numeric matrix', () => { engine.addColumns(0, [1, 2]) - expect(engine.getCellValue(adr('B1'))).toEqual(0) - expect(engine.getCellValue(adr('C1'))).toEqual(0) + expect(engine.getCellValue(adr('B1'))).toEqual(null) + expect(engine.getCellValue(adr('C1'))).toEqual(null) expect(engine.getCellValue(adr('D1'))).toEqual(2) }) }) diff --git a/test/cruds/adding-row.spec.ts b/test/cruds/adding-row.spec.ts index f89109c2b8..22d40dfeb9 100644 --- a/test/cruds/adding-row.spec.ts +++ b/test/cruds/adding-row.spec.ts @@ -287,7 +287,7 @@ describe('Adding row - FormulaCellVertex#address update', () => { }) describe('Adding row - matrices adjustments', () => { - it('add row inside numeric matrix, expand matrix', () => { + it('add row inside matrix, expand matrix', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], @@ -297,8 +297,8 @@ describe('Adding row - matrices adjustments', () => { engine.addRows(0, [1, 2]) - expect(engine.getCellValue(adr('A2'))).toEqual(0) - expect(engine.getCellValue(adr('A3'))).toEqual(0) + expect(engine.getCellValue(adr('A2'))).toEqual(null) + expect(engine.getCellValue(adr('A3'))).toEqual(null) expect(engine.getCellValue(adr('A4'))).toEqual(3) }) }) diff --git a/test/interpreter/function-filter.spec.ts b/test/interpreter/function-filter.spec.ts index 3cc6b8678e..3224a81e55 100644 --- a/test/interpreter/function-filter.spec.ts +++ b/test/interpreter/function-filter.spec.ts @@ -2,15 +2,15 @@ import {ErrorType, HyperFormula} from '../../src' import {ErrorMessage} from '../../src/error-message' import {adr, detailedError} from '../testUtils' -describe('Function FALSE', () => { +describe('Function FILTER', () => { it('validates input #1', () => { - const engine = HyperFormula.buildFromArray([['=FILTER(A2:B3, A2:B3)']]) + const engine = HyperFormula.buildFromArray([['=FILTER(D2:E3, D2:E3)']]) expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongDimension)) }) it('validates input #2', () => { - const engine = HyperFormula.buildFromArray([['=FILTER(A2:A3, A2:A3, A2:A4)']]) + const engine = HyperFormula.buildFromArray([['=FILTER(D2:D3, D2:D3, D2:D4)']]) expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.EqualLength)) }) @@ -27,9 +27,21 @@ describe('Function FALSE', () => { expect(engine.getSheetValues(0)).toEqual([[1,3],[1,2,3],[true,false,true]]) }) - it('is 0-arity', () => { - const engine = HyperFormula.buildFromArray([['=FALSE(1)']]) + it('works #2', () => { + const engine = HyperFormula.buildFromArray([['=FILTER(A2:C2,A3:C3,A4:C4)'],[1,2,3],[true,false,true], [true,true,false]]) - expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongArgNumber)) + expect(engine.getSheetValues(0)).toEqual([[1],[1,2,3],[true,false,true],[true,true,false]]) + }) + + it('works #3', () => { + const engine = HyperFormula.buildFromArray([['=FILTER(B1:B3,C1:C3)',1,true],[undefined,2,false],[undefined,3,true]]) + + expect(engine.getSheetValues(0)).toEqual([[1,1,true],[3,2,false],[null,3,true]]) + }) + + it('enables array arithmetic', () => { + const engine = HyperFormula.buildFromArray([['=FILTER(2*A2:C2,A3:C3)'],[1,2,3],[true,true,true]]) + + expect(engine.getSheetValues(0)).toEqual([[2,4,6],[1,2,3],[true,true,true]]) }) }) diff --git a/test/matrix.spec.ts b/test/matrix.spec.ts index e38671e691..00bbb09fbe 100644 --- a/test/matrix.spec.ts +++ b/test/matrix.spec.ts @@ -1,3 +1,4 @@ +import {EmptyValue} from '../src/interpreter/InterpreterValue' import {Matrix} from '../src/Matrix' describe('Matrix', () => { @@ -6,7 +7,7 @@ describe('Matrix', () => { expect(matrix.raw()).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) }) - it('add zero rows', () => { + it('add rows', () => { const matrix = new Matrix([ [1, 2, 3], [4, 5, 6], @@ -15,9 +16,9 @@ describe('Matrix', () => { matrix.addRows(1, 3) expect(matrix.raw()).toEqual([ [1, 2, 3], - [0, 0, 0], - [0, 0, 0], - [0, 0, 0], + [EmptyValue, EmptyValue, EmptyValue], + [EmptyValue, EmptyValue, EmptyValue], + [EmptyValue, EmptyValue, EmptyValue], [4, 5, 6], [7, 8, 9], ]) From b8c7474091dbe0bd88eb9f727ef35c4dea03c62a Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 12 May 2021 15:59:31 +0200 Subject: [PATCH 5/7] doc --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fc033d5ec..88bde307b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added support for array arithmetic. (#628) - Added performance improvements for array handling. (#629) - Added ARRAYFORMULA function. (#630) +- Added FILTER function. (#668) ### Fixed - Fixed an issue with arrays and cruds. (#651) From 439a330f1c9af0741844ad86bdab515e0fa77a86 Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 12 May 2021 16:14:40 +0200 Subject: [PATCH 6/7] doc --- docs/guide/built-in-functions.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/guide/built-in-functions.md b/docs/guide/built-in-functions.md index 175ed5be38..ee9cbcab6f 100644 --- a/docs/guide/built-in-functions.md +++ b/docs/guide/built-in-functions.md @@ -68,6 +68,7 @@ Total number of functions: **{{ $page.functionsCount }}** | Function ID | Description | Syntax | | :--- | :--- | :--- | | ARRAYFORMULA | Enables array arithmetic inside. | ARRAYFORMULA(Formula) | +| FILTER | Filter input array based on boolean arrays. | FILTER(Array; BoolArray1[; BoolArray2[; ...]]) ### Date and time From a7c91692eca5fea6f7eab7999cf983b5008bc2c3 Mon Sep 17 00:00:00 2001 From: izulin Date: Fri, 14 May 2021 21:23:52 +0200 Subject: [PATCH 7/7] linter --- src/Matrix.ts | 4 ++-- src/interpreter/FunctionRegistry.ts | 2 +- test/interpreter/function-filter.spec.ts | 16 ++++++++-------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Matrix.ts b/src/Matrix.ts index f7e6976f12..0329b056c3 100644 --- a/src/Matrix.ts +++ b/src/Matrix.ts @@ -123,13 +123,13 @@ export class Matrix implements IMatrix { this.addRows(this.height(), newSize.height-this.height()) } if(this.height() > newSize.height) { - throw "Resizing to smaller matrix" + throw 'Resizing to smaller matrix' } if(this.width() < newSize.width) { this.addColumns(this.width(), newSize.width-this.width()) } if(this.width() > newSize.width) { - throw "Resizing to smaller matrix" + throw 'Resizing to smaller matrix' } } diff --git a/src/interpreter/FunctionRegistry.ts b/src/interpreter/FunctionRegistry.ts index 0802d2e12c..686ef6161c 100644 --- a/src/interpreter/FunctionRegistry.ts +++ b/src/interpreter/FunctionRegistry.ts @@ -164,7 +164,7 @@ export class FunctionRegistry { private readonly arrayFunctions: Set = new Set() private readonly structuralChangeFunctions: Set = new Set() private readonly functionsWhichDoesNotNeedArgumentsToBeComputed: Set = new Set() - private readonly functionsMetadata: Map = new Map() + private readonly functionsMetadata: Map = new Map() constructor(private config: Config) { if (config.functionPlugins.length > 0) { diff --git a/test/interpreter/function-filter.spec.ts b/test/interpreter/function-filter.spec.ts index 3224a81e55..750f0477bf 100644 --- a/test/interpreter/function-filter.spec.ts +++ b/test/interpreter/function-filter.spec.ts @@ -22,26 +22,26 @@ describe('Function FILTER', () => { }) it('works #1', () => { - const engine = HyperFormula.buildFromArray([['=FILTER(A2:C2,A3:C3)'],[1,2,3],[true,false,true]]) + const engine = HyperFormula.buildFromArray([['=FILTER(A2:C2,A3:C3)'], [1, 2, 3], [true, false, true]]) - expect(engine.getSheetValues(0)).toEqual([[1,3],[1,2,3],[true,false,true]]) + expect(engine.getSheetValues(0)).toEqual([[1, 3], [1, 2, 3], [true, false, true]]) }) it('works #2', () => { - const engine = HyperFormula.buildFromArray([['=FILTER(A2:C2,A3:C3,A4:C4)'],[1,2,3],[true,false,true], [true,true,false]]) + const engine = HyperFormula.buildFromArray([['=FILTER(A2:C2,A3:C3,A4:C4)'], [1, 2, 3], [true, false, true], [true, true, false]]) - expect(engine.getSheetValues(0)).toEqual([[1],[1,2,3],[true,false,true],[true,true,false]]) + expect(engine.getSheetValues(0)).toEqual([[1], [1, 2, 3], [true, false, true], [true, true, false]]) }) it('works #3', () => { - const engine = HyperFormula.buildFromArray([['=FILTER(B1:B3,C1:C3)',1,true],[undefined,2,false],[undefined,3,true]]) + const engine = HyperFormula.buildFromArray([['=FILTER(B1:B3,C1:C3)', 1, true], [undefined, 2, false], [undefined, 3, true]]) - expect(engine.getSheetValues(0)).toEqual([[1,1,true],[3,2,false],[null,3,true]]) + expect(engine.getSheetValues(0)).toEqual([[1, 1, true], [3, 2, false], [null, 3, true]]) }) it('enables array arithmetic', () => { - const engine = HyperFormula.buildFromArray([['=FILTER(2*A2:C2,A3:C3)'],[1,2,3],[true,true,true]]) + const engine = HyperFormula.buildFromArray([['=FILTER(2*A2:C2,A3:C3)'], [1, 2, 3], [true, true, true]]) - expect(engine.getSheetValues(0)).toEqual([[2,4,6],[1,2,3],[true,true,true]]) + expect(engine.getSheetValues(0)).toEqual([[2, 4, 6], [1, 2, 3], [true, true, true]]) }) })