Skip to content

Commit

Permalink
feat(formula): fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored and wpxp123456 committed Oct 18, 2024
1 parent 8071978 commit 256522c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 80 deletions.
55 changes: 55 additions & 0 deletions packages/engine-formula/src/basics/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,58 @@ function adjoint(matrix: number[][]): number[][] {

return adj;
}

export const romanToArabicMap = new Map<string, number>([
['I', 1],
['V', 5],
['X', 10],
['L', 50],
['C', 100],
['D', 500],
['M', 1000],
]);

export const arabicToRomanMap = new Map<number, string>([
[1, 'I'],
[4, 'IV'],
[5, 'V'],
[9, 'IX'],
[10, 'X'],
[40, 'XL'],
[45, 'VL'],
[49, 'IL'],
[50, 'L'],
[90, 'XC'],
[95, 'VC'],
[99, 'IC'],
[100, 'C'],
[400, 'CD'],
[450, 'LD'],
[490, 'XD'],
[495, 'VD'],
[499, 'ID'],
[500, 'D'],
[900, 'CM'],
[950, 'LM'],
[990, 'XM'],
[995, 'VM'],
[999, 'IM'],
[1000, 'M'],
]);

/**
* form: A number specifying the type of roman numeral you want.
* The roman numeral style ranges from Classic to Simplified, becoming more concise as the value of form increases
* 0 Classic
* 1 More concise
* 2 More concise
* 3 More concise
* 4 Simplified
*/
export const romanFormArray: number[][] = [
[1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000, 4000],
[1, 4, 5, 9, 10, 40, 45, 50, 90, 95, 100, 400, 450, 500, 900, 950, 1000, 4000],
[1, 4, 5, 9, 10, 40, 45, 49, 50, 90, 95, 99, 100, 400, 450, 490, 500, 900, 950, 990, 1000, 4000],
[1, 4, 5, 9, 10, 40, 45, 49, 50, 90, 95, 99, 100, 400, 450, 490, 495, 500, 900, 950, 990, 995, 1000, 4000],
[1, 4, 5, 9, 10, 40, 45, 49, 50, 90, 95, 99, 100, 400, 450, 490, 495, 499, 500, 900, 950, 990, 995, 999, 1000, 4000],
];
21 changes: 6 additions & 15 deletions packages/engine-formula/src/functions/math/arabic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
* limitations under the License.
*/

import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../basics/error-type';
import { romanToArabicMap } from '../../../basics/math';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';

export class Arabic extends BaseFunction {
override minParams = 1;
Expand Down Expand Up @@ -68,10 +69,10 @@ export class Arabic extends BaseFunction {
let result = 0;

for (let i = 0; i < textValue.length; i++) {
const currentCharValue = this._romanToNumberMap.get(textValue[i]) || 0;
const nextCharValue = this._romanToNumberMap.get(textValue[i + 1]) || 0;
const nextnextCharValue = this._romanToNumberMap.get(textValue[i + 2]) || 0;
const nextnextnextCharValue = this._romanToNumberMap.get(textValue[i + 3]) || 0;
const currentCharValue = romanToArabicMap.get(textValue[i]) || 0;
const nextCharValue = romanToArabicMap.get(textValue[i + 1]) || 0;
const nextnextCharValue = romanToArabicMap.get(textValue[i + 2]) || 0;
const nextnextnextCharValue = romanToArabicMap.get(textValue[i + 3]) || 0;

if (
!currentCharValue ||
Expand All @@ -91,14 +92,4 @@ export class Arabic extends BaseFunction {

return NumberValueObject.create(isNegtive ? -result : result);
}

private _romanToNumberMap = new Map<string, number>([
['I', 1],
['V', 5],
['X', 10],
['L', 50],
['C', 100],
['D', 500],
['M', 1000],
]);
}
43 changes: 4 additions & 39 deletions packages/engine-formula/src/functions/math/roman/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
* limitations under the License.
*/

import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../basics/error-type';
import { arabicToRomanMap, romanFormArray } from '../../../basics/math';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject, StringValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';

export class Roman extends BaseFunction {
override minParams = 1;
Expand Down Expand Up @@ -95,7 +96,7 @@ export class Roman extends BaseFunction {
return ErrorValueObject.create(ErrorType.VALUE);
}

const formArray = this._formArray[formValue];
const formArray = romanFormArray[formValue];

let index = formArray.length - 1;
let result = '';
Expand All @@ -107,48 +108,12 @@ export class Roman extends BaseFunction {

numberValue -= number;

result += this._numberToRomanMap.get(number);
result += arabicToRomanMap.get(number);
}

return StringValueObject.create(result);
}

private _formArray: Array<Array<number>> = [
[1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000, 4000],
[1, 4, 5, 9, 10, 40, 45, 50, 90, 95, 100, 400, 450, 500, 900, 950, 1000, 4000],
[1, 4, 5, 9, 10, 40, 45, 49, 50, 90, 95, 99, 100, 400, 450, 490, 500, 900, 950, 990, 1000, 4000],
[1, 4, 5, 9, 10, 40, 45, 49, 50, 90, 95, 99, 100, 400, 450, 490, 495, 500, 900, 950, 990, 995, 1000, 4000],
[1, 4, 5, 9, 10, 40, 45, 49, 50, 90, 95, 99, 100, 400, 450, 490, 495, 499, 500, 900, 950, 990, 995, 999, 1000, 4000],
];

private _numberToRomanMap = new Map<number, string>([
[1, 'I'],
[4, 'IV'],
[5, 'V'],
[9, 'IX'],
[10, 'X'],
[40, 'XL'],
[45, 'VL'],
[49, 'IL'],
[50, 'L'],
[90, 'XC'],
[95, 'VC'],
[99, 'IC'],
[100, 'C'],
[400, 'CD'],
[450, 'LD'],
[490, 'XD'],
[495, 'VD'],
[499, 'ID'],
[500, 'D'],
[900, 'CM'],
[950, 'LM'],
[990, 'XM'],
[995, 'VM'],
[999, 'IM'],
[1000, 'M'],
]);

private _binarySearch(target: number, left: number, right: number, array: Array<number>): number {
let _left = left;
let _right = right;
Expand Down
29 changes: 16 additions & 13 deletions packages/engine-formula/src/functions/text/char/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ import { ErrorValueObject } from '../../../engine/value-object/base-value-object
import { StringValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';

// special case for some characters causing doc stream issues
const filterCodeArray = Object.values(DataStreamTreeTokenType).filter((value) => {
return [
DataStreamTreeTokenType.TABLE_START,
DataStreamTreeTokenType.TABLE_ROW_START,
DataStreamTreeTokenType.TABLE_CELL_START,
DataStreamTreeTokenType.TABLE_CELL_END,
DataStreamTreeTokenType.TABLE_ROW_END,
DataStreamTreeTokenType.TABLE_END,
DataStreamTreeTokenType.CUSTOM_RANGE_START,
DataStreamTreeTokenType.CUSTOM_RANGE_END,
DataStreamTreeTokenType.CUSTOM_BLOCK,
].includes(value as DataStreamTreeTokenType);
});

export class Char extends BaseFunction {
override minParams = 1;

Expand Down Expand Up @@ -60,19 +75,7 @@ export class Char extends BaseFunction {
let result = String.fromCharCode(numberValue);

// special case for some characters causing doc stream issues
if (Object.values(DataStreamTreeTokenType).filter((value) => {
return [
DataStreamTreeTokenType.TABLE_START,
DataStreamTreeTokenType.TABLE_ROW_START,
DataStreamTreeTokenType.TABLE_CELL_START,
DataStreamTreeTokenType.TABLE_CELL_END,
DataStreamTreeTokenType.TABLE_ROW_END,
DataStreamTreeTokenType.TABLE_END,
DataStreamTreeTokenType.CUSTOM_RANGE_START,
DataStreamTreeTokenType.CUSTOM_RANGE_END,
DataStreamTreeTokenType.CUSTOM_BLOCK,
].includes(value as DataStreamTreeTokenType);
}).some((value) => value === result)) {
if (filterCodeArray.some((value) => value === result)) {
result = String.fromCharCode(1);
}

Expand Down
29 changes: 16 additions & 13 deletions packages/engine-formula/src/functions/text/unichar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ import { ErrorValueObject } from '../../../engine/value-object/base-value-object
import { StringValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';

// special case for some characters causing doc stream issues
const filterCodeArray = Object.values(DataStreamTreeTokenType).filter((value) => {
return [
DataStreamTreeTokenType.TABLE_START,
DataStreamTreeTokenType.TABLE_ROW_START,
DataStreamTreeTokenType.TABLE_CELL_START,
DataStreamTreeTokenType.TABLE_CELL_END,
DataStreamTreeTokenType.TABLE_ROW_END,
DataStreamTreeTokenType.TABLE_END,
DataStreamTreeTokenType.CUSTOM_RANGE_START,
DataStreamTreeTokenType.CUSTOM_RANGE_END,
DataStreamTreeTokenType.CUSTOM_BLOCK,
].includes(value as DataStreamTreeTokenType);
});

export class Unichar extends BaseFunction {
override minParams = 1;

Expand Down Expand Up @@ -65,19 +80,7 @@ export class Unichar extends BaseFunction {
let result = String.fromCharCode(numberValue);

// special case for some characters causing doc stream issues
if (Object.values(DataStreamTreeTokenType).filter((value) => {
return [
DataStreamTreeTokenType.TABLE_START,
DataStreamTreeTokenType.TABLE_ROW_START,
DataStreamTreeTokenType.TABLE_CELL_START,
DataStreamTreeTokenType.TABLE_CELL_END,
DataStreamTreeTokenType.TABLE_ROW_END,
DataStreamTreeTokenType.TABLE_END,
DataStreamTreeTokenType.CUSTOM_RANGE_START,
DataStreamTreeTokenType.CUSTOM_RANGE_END,
DataStreamTreeTokenType.CUSTOM_BLOCK,
].includes(value as DataStreamTreeTokenType);
}).some((value) => value === result)) {
if (filterCodeArray.some((value) => value === result)) {
result = String.fromCharCode(1);
}

Expand Down

0 comments on commit 256522c

Please sign in to comment.