-
-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(formula): add OR/TEXT/LEN functions (#1593)
* fix(formula): date default format * feat(formula): add or function * fix(formula): create StringValueObject * feat(formual): add text function * feat(formula): add len,lenb functions * fix(formula): index row number
- Loading branch information
Showing
27 changed files
with
739 additions
and
82 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
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
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. | ||
*/ | ||
|
||
/** | ||
* Korean in Excel does not count as two characters. Here we calculate all Chinese, Japanese and Korean characters as two characters. | ||
* | ||
* ā -> 1 | ||
* ー -> 2 | ||
* | ||
* @param str | ||
* @returns | ||
*/ | ||
export function charLenByte(str: string) { | ||
let byteCount = 0; | ||
|
||
for (let i = 0; i < str.length; i++) { | ||
const charCode = str.charCodeAt(i); | ||
|
||
if ( | ||
(charCode >= 0x3040 && charCode <= 0x30FF) || // Japanese hiragana and katakana | ||
(charCode >= 0x4E00 && charCode <= 0x9FFF) || // Chinese (simplified and traditional) | ||
(charCode >= 0xAC00 && charCode <= 0xD7AF) // Korean language | ||
) { | ||
byteCount += 2; | ||
} else { | ||
byteCount += 1; | ||
} | ||
} | ||
|
||
return byteCount; | ||
} |
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
140 changes: 140 additions & 0 deletions
140
packages/engine-formula/src/functions/logical/or/__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,140 @@ | ||
/** | ||
* 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_LOGICAL } from '../../function-names'; | ||
import { Or } from '..'; | ||
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object'; | ||
import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object'; | ||
import { ErrorType } from '../../../..'; | ||
|
||
describe('Test or function', () => { | ||
const textFunction = new Or(FUNCTION_NAMES_LOGICAL.OR); | ||
|
||
describe('Or', () => { | ||
it('logical1 string', () => { | ||
const logical1 = StringValueObject.create('a1'); | ||
const result = textFunction.calculate(logical1); | ||
expect(result.getValue()).toBe(ErrorType.VALUE); | ||
}); | ||
it('logical1 number 1', () => { | ||
const logical1 = NumberValueObject.create(1); | ||
const result = textFunction.calculate(logical1); | ||
expect(result.getValue()).toBe(true); | ||
}); | ||
|
||
it('logical1 number 0', () => { | ||
const logical1 = NumberValueObject.create(0); | ||
const result = textFunction.calculate(logical1); | ||
expect(result.getValue()).toBe(false); | ||
}); | ||
|
||
it('logical1 null', () => { | ||
const logical1 = NullValueObject.create(); | ||
const result = textFunction.calculate(logical1); | ||
expect(result.getValue()).toBe(ErrorType.VALUE); | ||
}); | ||
|
||
it('logical1 true', () => { | ||
const logical1 = BooleanValueObject.create(true); | ||
const result = textFunction.calculate(logical1); | ||
expect(result.getValue()).toBe(true); | ||
}); | ||
|
||
it('logical2 false', () => { | ||
const logical1 = BooleanValueObject.create(true); | ||
const logical2 = BooleanValueObject.create(false); | ||
const result = textFunction.calculate(logical1, logical2); | ||
expect(result.getValue()).toBe(true); | ||
}); | ||
|
||
it('logical1 is array, no logical value', () => { | ||
const logical1 = ArrayValueObject.create({ | ||
calculateValueList: transformToValueObject([ | ||
['a1'], | ||
['a2'], | ||
]), | ||
rowCount: 2, | ||
columnCount: 1, | ||
unitId: '', | ||
sheetId: '', | ||
row: 0, | ||
column: 0, | ||
}); | ||
const result = textFunction.calculate(logical1); | ||
expect(result.getValue()).toBe(ErrorType.VALUE); | ||
}); | ||
|
||
it('logical1 is array and logical2 is array', () => { | ||
const logical1 = ArrayValueObject.create({ | ||
calculateValueList: transformToValueObject([ | ||
['a1'], | ||
['a2'], | ||
]), | ||
rowCount: 2, | ||
columnCount: 1, | ||
unitId: '', | ||
sheetId: '', | ||
row: 0, | ||
column: 0, | ||
}); | ||
const logical2 = ArrayValueObject.create({ | ||
calculateValueList: transformToValueObject([ | ||
[true], | ||
['a4'], | ||
]), | ||
rowCount: 2, | ||
columnCount: 1, | ||
unitId: '', | ||
sheetId: '', | ||
row: 0, | ||
column: 0, | ||
}); | ||
const result = textFunction.calculate(logical1, logical2); | ||
expect(result.getValue()).toBe(true); | ||
}); | ||
|
||
it('logical1 is array and logical2 is array, error value', () => { | ||
const logical1 = ArrayValueObject.create({ | ||
calculateValueList: transformToValueObject([ | ||
['a1'], | ||
['a2'], | ||
]), | ||
rowCount: 2, | ||
columnCount: 1, | ||
unitId: '', | ||
sheetId: '', | ||
row: 0, | ||
column: 0, | ||
}); | ||
const logical2 = ArrayValueObject.create({ | ||
calculateValueList: transformToValueObject([ | ||
[false], | ||
['#NAME?'], | ||
]), | ||
rowCount: 2, | ||
columnCount: 1, | ||
unitId: '', | ||
sheetId: '', | ||
row: 0, | ||
column: 0, | ||
}); | ||
const result = textFunction.calculate(logical1, logical2); | ||
expect(result.getValue()).toBe('#NAME?'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.