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): add the Lower function #1970

Merged
merged 1 commit into from
Apr 24, 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
feat(formula): implement formula lower
  • Loading branch information
maihantang committed Apr 22, 2024
commit a6482792f05301832cd96251db7c53c8775fa7f7
2 changes: 2 additions & 0 deletions packages/engine-formula/src/functions/text/function-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import { FUNCTION_NAMES_TEXT } from './function-names';
import { Len } from './len';
import { Lenb } from './lenb';
import { Text } from './text';
import { Lower } from './lower';

export const functionText = [
[Concatenate, FUNCTION_NAMES_TEXT.CONCATENATE],
[Len, FUNCTION_NAMES_TEXT.LEN],
[Lenb, FUNCTION_NAMES_TEXT.LENB],
[Text, FUNCTION_NAMES_TEXT.TEXT],
[Lower, FUNCTION_NAMES_TEXT.LOWER],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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 { FUNCTION_NAMES_TEXT } from '../../function-names';
import { Lower } from '../index';
import { BooleanValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../../basics/error-type';

describe('Test lower function', () => {
const textFunction = new Lower(FUNCTION_NAMES_TEXT.LOWER);

describe('Lower', () => {
it('Value is normal', () => {
const value = StringValueObject.create('Apt. 2B');
const result = textFunction.calculate(value);
expect(result.getValue()).toBe('apt. 2b');
});

it('Value is number', () => {
const value = NumberValueObject.create(1);
const result = textFunction.calculate(value);
expect(result.getValue()).toBe('1');
});

it('Value is boolean', () => {
const value = BooleanValueObject.create(true);
const result = textFunction.calculate(value);
expect(result.getValue()).toBe('true');
});

it('Value is array', () => {
const text = new ArrayValueObject({
calculateValueList: transformToValueObject([
[1, ' ', 1.23, true, false, null, 'Univer表格シート繁體한국인'],
[0, '100', '2.34', 'TEST', -3, ErrorType.VALUE, null],
]),
rowCount: 2,
columnCount: 7,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const result = textFunction.calculate(text);
expect(transformToValue(result.getArrayValue())).toStrictEqual([
['1', ' ', '1.23', 'true', 'false', '', 'univer表格シート繁體한국인'],
['0', '100', '2.34', 'test', '-3', '#VALUE!', ''],
]);
});
});
});
57 changes: 57 additions & 0 deletions packages/engine-formula/src/functions/text/lower/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* 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 { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { StringValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';

export class Lower extends BaseFunction {
override calculate(text: BaseValueObject) {
if (text == null) {
return ErrorValueObject.create(ErrorType.NA);
}

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

if (text.isArray()) {
return text.mapValue((textValue: BaseValueObject) => {
return this._handleSingleText(textValue);
});
}

return this._handleSingleText(text);
}

private _handleSingleText(text: BaseValueObject) {
if (text.isError()) {
return text;
}

if (text.isNull()) {
return StringValueObject.create('');
}

if (text.isString() || text.isBoolean() || text.isNumber()) {
const textValue = text.getValue().toString().toLowerCase();
return StringValueObject.create(textValue);
}
jackpk235 marked this conversation as resolved.
Show resolved Hide resolved

return ErrorValueObject.create(ErrorType.VALUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export default {
},
},
LOWER: {
description: 'Converts text to lowercase',
description: 'Converts text to lowercase.',
abstract: 'Converts text to lowercase',
links: [
{
Expand All @@ -275,8 +275,10 @@ export default {
},
],
functionParameter: {
number1: { name: 'number1', detail: 'first' },
number2: { name: 'number2', detail: 'second' },
text: {
name: 'text',
detail: 'The text you want to convert to lowercase. LOWER does not change characters in text that are not letters.',
},
},
},
MID: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,18 @@ export default {
},
LOWER: {
description: '文字列に含まれる英字をすべて小文字に変換します。',
abstract: '文字列に含まれる英字をすべて小文字に変換します',
abstract: '文字列に含まれる英字をすべて小文字に変換します',
links: [
{
title: '指導',
url: 'https://support.microsoft.com/ja-jp/office/lower-%E9%96%A2%E6%95%B0-3f21df02-a80c-44b2-afaf-81358f9fdeb4',
},
],
functionParameter: {
number1: { name: 'number1', detail: 'first' },
number2: { name: 'number2', detail: 'second' },
text: {
name: '文字列',
detail: '小文字に変換する文字列を指定します。 それ以外の文字は変換されません。',
},
},
},
MID: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export default {
},
},
LOWER: {
description: '将文本转换为小写',
description: '将文本转换为小写',
abstract: '将文本转换为小写',
links: [
{
Expand All @@ -275,8 +275,10 @@ export default {
},
],
functionParameter: {
number1: { name: 'number1', detail: 'first' },
number2: { name: 'number2', detail: 'second' },
text: {
name: '文本',
detail: '要转换为小写字母的文本。 LOWER 不改变文本中的非字母字符。',
},
},
},
MID: {
Expand Down
13 changes: 3 additions & 10 deletions packages/sheets-formula/src/services/function-list/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,9 @@ export const FUNCTION_LIST_TEXT: IFunctionInfo[] = [
abstract: 'formula.functionList.LOWER.abstract',
functionParameter: [
{
name: 'formula.functionList.LOWER.functionParameter.number1.name',
detail: 'formula.functionList.LOWER.functionParameter.number1.detail',
example: 'A1:A20',
require: 1,
repeat: 0,
},
{
name: 'formula.functionList.LOWER.functionParameter.number2.name',
detail: 'formula.functionList.LOWER.functionParameter.number2.detail',
example: 'A1:A20',
name: 'formula.functionList.LOWER.functionParameter.text.name',
detail: 'formula.functionList.LOWER.functionParameter.text.detail',
example: '"Univer"',
require: 1,
repeat: 0,
},
Expand Down
Loading