Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(formula): support cubeValueObject #1536

Merged
merged 4 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/engine-formula/src/basics/token-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ export const DEFAULT_TOKEN_TYPE_LAMBDA_OMIT_PARAMETER = 'LO_1';
export const DEFAULT_TOKEN_LET_FUNCTION_NAME = 'LET';

export const DEFAULT_TOKEN_LAMBDA_FUNCTION_NAME = 'LAMBDA';

export const DEFAULT_TOKEN_CUBE_FUNCTION_NAME = 'CUBE';
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ describe('lexer nodeMaker test', () => {
const node = lexerTreeBuilder.treeBuilder('=-(2)+*9') as LexerNode;
expect(JSON.stringify(node.serialize())).toStrictEqual('{"token":"R_1","st":-1,"ed":-1,"children":["0","2","-","9","+"]}');
});

it('ref:ref parser', () => {
const node = lexerTreeBuilder.treeBuilder('=SUM(A6:B6:C6:D7,1,1,2)') as LexerNode;
expect(JSON.stringify(node.serialize())).toStrictEqual('{"token":"R_1","st":-1,"ed":-1,"children":[{"token":"SUM","st":0,"ed":2,"children":[{"token":"P_1","st":0,"ed":2,"children":[{"token":":","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"A6","st":-1,"ed":-1,"children":[]}]},{"token":"P_1","st":-1,"ed":-1,"children":[{"token":":","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"B6","st":-1,"ed":-1,"children":[]}]},{"token":"P_1","st":-1,"ed":-1,"children":[{"token":":","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"C6","st":-1,"ed":-1,"children":[]}]},{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"D7","st":-1,"ed":-1,"children":[]}]}]}]}]}]}]}]},{"token":"P_1","st":12,"ed":14,"children":["1"]},{"token":"P_1","st":14,"ed":16,"children":["1"]},{"token":"P_1","st":16,"ed":18,"children":["2"]}]}]}');
});

it('cube parser', () => {
const node = lexerTreeBuilder.treeBuilder('=INDEX((A6:B6,C6:D7),1,1,2)') as LexerNode;
expect(JSON.stringify(node.serialize())).toStrictEqual('{"token":"R_1","st":-1,"ed":-1,"children":[{"token":"INDEX","st":0,"ed":4,"children":[{"token":"P_1","st":2,"ed":4,"children":[{"token":"CUBE","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":":","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"A6","st":-1,"ed":-1,"children":[]}]},{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"B6","st":-1,"ed":-1,"children":[]}]}]}]},{"token":"P_1","st":9,"ed":11,"children":[{"token":":","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"C6","st":-1,"ed":-1,"children":[]}]},{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"D7","st":-1,"ed":-1,"children":[]}]}]}]}]}]},{"token":"P_1","st":16,"ed":18,"children":["1"]},{"token":"P_1","st":18,"ed":20,"children":["1"]},{"token":"P_1","st":20,"ed":22,"children":["2"]}]}]}');
});
});

describe('checkIfAddBracket', () => {
Expand Down Expand Up @@ -232,5 +242,54 @@ describe('lexer nodeMaker test', () => {
},
]);
});

it('cube', () => {
expect(lexerTreeBuilder.sequenceNodesBuilder('=INDEX((A6:B6,C6:D7),1,1,2)')).toStrictEqual([
{
endIndex: 4,
nodeType: 3,
startIndex: 0,
token: 'INDEX',
},
'(',
'(',
{
endIndex: 11,
nodeType: 4,
startIndex: 7,
token: 'A6:B6',
},
',',
{
endIndex: 17,
nodeType: 4,
startIndex: 13,
token: 'C6:D7',
},
')',
',',
{
endIndex: 20,
nodeType: 1,
startIndex: 20,
token: '1',
},
',',
{
endIndex: 22,
nodeType: 1,
startIndex: 22,
token: '1',
},
',',
{
endIndex: 24,
nodeType: 1,
startIndex: 24,
token: '2',
},
')',
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
suffixToken,
} from '../../basics/token';
import {
DEFAULT_TOKEN_CUBE_FUNCTION_NAME,
DEFAULT_TOKEN_LAMBDA_FUNCTION_NAME,
DEFAULT_TOKEN_TYPE_LAMBDA_PARAMETER,
DEFAULT_TOKEN_TYPE_PARAMETER,
Expand Down Expand Up @@ -701,6 +702,11 @@ export class LexerTreeBuilder extends Disposable {
return bracketState[bracketState.length - 1];
}

private _changeCurrentBracket(type: bracketType) {
const bracketState = this._bracketState;
bracketState[bracketState.length - 1] = type;
}

private _openBraces() {
this._bracesState += 1;
}
Expand Down Expand Up @@ -1138,7 +1144,35 @@ export class LexerTreeBuilder extends Disposable {
// this.setCurrentLexerNode(subLexerNode);
this._newAndPushCurrentLexerNode(DEFAULT_TOKEN_TYPE_PARAMETER, cur);
} else {
return ErrorType.VALUE;
/**
* support cubeValueObject for =INDEX((A6:B6,C6:D7),1,1,2)
*/
const cubeNode = new LexerNode();
cubeNode.setToken(DEFAULT_TOKEN_CUBE_FUNCTION_NAME);
const cubeParamNode = new LexerNode();
cubeParamNode.setToken(DEFAULT_TOKEN_TYPE_PARAMETER);
cubeParamNode.changeToParent(cubeNode);

const colonNode = this._currentLexerNode.getParent();

if (colonNode && colonNode.getToken() === matchToken.COLON) {
const colonNodeParent = colonNode.getParent();
if (!colonNodeParent) {
return ErrorType.VALUE;
}

colonNode.changeToParent(cubeParamNode);
colonNodeParent.setChildren([]);
cubeNode.changeToParent(colonNodeParent);
} else {
return ErrorType.VALUE;
}

this._changeCurrentBracket(bracketType.FUNCTION);
this._pushNodeToChildren(this._segment);
this._resetSegment();
this._currentLexerNode = cubeNode;
this._newAndPushCurrentLexerNode(DEFAULT_TOKEN_TYPE_PARAMETER, cur);
}
} else if (
currentString === matchToken.COLON &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class BaseValueObject extends ObjectClassType {
/** abstract */
}

isCube() {
return false;
}

isArray() {
return false;
}
Expand Down
DR-Univer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* 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 type { ArrayValueObject } from './array-value-object';
import { BaseValueObject } from './base-value-object';
import { NumberValueObject } from './primitive-object';

export class CubeValueObject extends BaseValueObject {
override isCube() {
return true;
}

private _values: ArrayValueObject[] = [];

constructor(values: ArrayValueObject[]) {
super('');
this._values = values;
}

override sum() {
const result = new NumberValueObject(0);
this._values.forEach((arr) => {
result.plus(arr.sum());
});
return result;
}

override max() {
let result = new NumberValueObject(Number.NEGATIVE_INFINITY);
this._values.forEach((arr) => {
const compare = arr.max() as NumberValueObject;
if (result.isLessThan(compare)) {
result = compare;
}
});
return result;
}

override min() {
let result = new NumberValueObject(Number.POSITIVE_INFINITY);
this._values.forEach((arr) => {
const compare = arr.max() as NumberValueObject;
if (result.isGreaterThan(compare)) {
result = compare;
}
});
return result;
}

override count() {
const count = new NumberValueObject(0);
this._values.forEach((arr) => {
count.plus(arr.count());
});
return count;
}

override countA() {
const count = new NumberValueObject(0);
this._values.forEach((arr) => {
count.plus(arr.countA());
});
return count;
}

override countBlank() {
const count = new NumberValueObject(0);
this._values.forEach((arr) => {
count.plus(arr.countBlank());
});
return count;
}
}
48 changes: 48 additions & 0 deletions packages/engine-formula/src/functions/meta/cube/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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 { BaseFunction } from '../../base-function';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { CubeValueObject } from '../../../engine/value-object/cube-value-object';

export class Cube extends BaseFunction {
override calculate(...variants: BaseValueObject[]) {
if (variants.length === 0) {
return new ErrorValueObject(ErrorType.VALUE);
}

const values: ArrayValueObject[] = [];

for (let i = 0; i < variants.length; i++) {
const variant = variants[i];

if (variant.isError()) {
return variant;
}

if (!variant.isArray()) {
return new ErrorValueObject(ErrorType.VALUE);
}

values.push(variant as ArrayValueObject);
}

return new CubeValueObject(values);
}
}
2 changes: 2 additions & 0 deletions packages/engine-formula/src/functions/meta/function-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { Compare } from './compare';
import { Cube } from './cube';
import { Divided } from './divided';
import { FUNCTION_NAMES_META } from './function-names';
import { Minus } from './minus';
Expand All @@ -27,4 +28,5 @@ export const functionMeta = [
[Minus, FUNCTION_NAMES_META.MINUS],
[Multiply, FUNCTION_NAMES_META.MULTIPLY],
[Plus, FUNCTION_NAMES_META.PLUS],
[Cube, FUNCTION_NAMES_META.CUBE],
];
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export enum FUNCTION_NAMES_META {
MULTIPLY = 'MULTIPLY',
PLUS = 'PLUS',
UNION = 'UNION',
CUBE = 'CUBE',
}
Loading