Skip to content

Commit

Permalink
fix(formula): fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored and wpxp123456 committed Sep 3, 2024
1 parent 95f5dbb commit e3142bd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 38 deletions.
49 changes: 30 additions & 19 deletions packages/engine-formula/src/functions/text/textafter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,40 +197,51 @@ export class Textafter extends BaseFunction {
ifNotFoundObject: BaseValueObject,
onlyThreeVariant: boolean
): BaseValueObject {
const matchNum = textValue.match(new RegExp(delimiterValue, `g${!matchModeValue ? '' : 'i'}`));

// only three variant and if instance_num is greater than the number of occurrences of delimiter. returns a #N/A error
if (matchNum && matchNum.length < Math.abs(instanceNumValue) && onlyThreeVariant) {
return ErrorValueObject.create(ErrorType.NA);
}

if (!matchNum || matchNum.length < Math.abs(instanceNumValue)) {
if (matchEndValue) {
if (instanceNumValue > 0) {
return StringValueObject.create('');
} else {
return StringValueObject.create(textValue);
}
}

return ifNotFoundObject;
}

let substrText = !matchModeValue ? textValue : textValue.toLocaleLowerCase();
const _delimiterValue = !matchModeValue ? delimiterValue : delimiterValue.toLocaleLowerCase();

let resultIndex = 0;
let matchNum = 0;

for (let i = 0; i < Math.abs(instanceNumValue); i++) {
if (instanceNumValue < 0) {
const index = substrText.lastIndexOf(_delimiterValue);

if (index === -1) {
break;
}

resultIndex = index;
substrText = substrText.substr(0, index);
matchNum++;
} else {
const index = substrText.indexOf(_delimiterValue);

if (index === -1) {
break;
}

resultIndex += (index + i * _delimiterValue.length);
substrText = substrText.substr(index + _delimiterValue.length);
matchNum++;
}
}

// only three variant and if instance_num is greater than the number of occurrences of delimiter. returns a #N/A error
if (matchNum && matchNum < Math.abs(instanceNumValue) && onlyThreeVariant) {
return ErrorValueObject.create(ErrorType.NA);
}

if (!matchNum || matchNum < Math.abs(instanceNumValue)) {
if (matchEndValue) {
if (instanceNumValue > 0) {
return StringValueObject.create('');
} else {
return StringValueObject.create(textValue);
}
}

return ifNotFoundObject;
}

const result = textValue.substr(resultIndex + _delimiterValue.length);
Expand Down
49 changes: 30 additions & 19 deletions packages/engine-formula/src/functions/text/textbefore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,42 +197,53 @@ export class Textbefore extends BaseFunction {
ifNotFoundObject: BaseValueObject,
onlyThreeVariant: boolean
): BaseValueObject {
const matchNum = textValue.match(new RegExp(delimiterValue, `g${!matchModeValue ? '' : 'i'}`));

// only three variant and if instance_num is greater than the number of occurrences of delimiter. returns a #N/A error
if (matchNum && matchNum.length < Math.abs(instanceNumValue) && onlyThreeVariant) {
return ErrorValueObject.create(ErrorType.NA);
}

if (!matchNum || matchNum.length < Math.abs(instanceNumValue)) {
if (matchEndValue) {
if (instanceNumValue > 0) {
return StringValueObject.create(textValue);
} else {
return StringValueObject.create('');
}
}

return ifNotFoundObject;
}

let substrText = !matchModeValue ? textValue : textValue.toLocaleLowerCase();
const _delimiterValue = !matchModeValue ? delimiterValue : delimiterValue.toLocaleLowerCase();

let resultIndex = 0;
let matchNum = 0;

for (let i = 0; i < Math.abs(instanceNumValue); i++) {
if (instanceNumValue < 0) {
const index = substrText.lastIndexOf(_delimiterValue);

if (index === -1) {
break;
}

resultIndex = index;
substrText = substrText.substr(0, index);
matchNum++;
} else {
const index = substrText.indexOf(_delimiterValue);

if (index === -1) {
break;
}

resultIndex += (index + i * _delimiterValue.length);
substrText = substrText.substr(index + _delimiterValue.length);
matchNum++;
}
}

// only three variant and if instance_num is greater than the number of occurrences of delimiter. returns a #N/A error
if (matchNum && matchNum < Math.abs(instanceNumValue) && onlyThreeVariant) {
return ErrorValueObject.create(ErrorType.NA);
}

if (!matchNum || matchNum < Math.abs(instanceNumValue)) {
if (matchEndValue) {
if (instanceNumValue > 0) {
return StringValueObject.create(textValue);
} else {
return StringValueObject.create('');
}
}

return ifNotFoundObject;
}

const result = textValue.substr(0, resultIndex);

return StringValueObject.create(result);
Expand Down

0 comments on commit e3142bd

Please sign in to comment.