Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(engine-formula): validate international weekend masks (#7876)
Lucien10d0d886272e
diff --git a/packages/engine-formula/src/basics/__tests__/date.spec.ts b/packages/engine-formula/src/basics/__tests__/date.spec.tsindex 55ff23dd72..358c4bd8e9 100644--- a/packages/engine-formula/src/basics/__tests__/date.spec.ts+++ b/packages/engine-formula/src/basics/__tests__/date.spec.ts@@ -22,11 +22,48 @@ import {formatDateDefault,getDateSerialNumberByObject,getTwoDateDaysByBasis,+ getWeekendArray,isValidDateStr,+ isValidWeekend,} from '../date';import { ErrorType } from '../error-type';describe('Test date', () => {+ it.each(['', '1', '000001', '00000110', '00000001', '00000|1', '0000011x', '11111111', '0000011\n'])('rejects invalid weekend mask %j', (weekend) => {+ expect(isValidWeekend(weekend)).toBe(false);+ expect(getWeekendArray(weekend)).toEqual([]);+ });++ it.each<[string, number[]]>([+ ['0000011', [6, 0]],+ ['1000000', [1]],+ ['0000000', []],+ ['1111111', [1, 2, 3, 4, 5, 6, 0]],+ ])('decodes valid weekend mask %s', (weekend, expected) => {+ expect(isValidWeekend(weekend)).toBe(true);+ expect(getWeekendArray(weekend)).toEqual(expected);+ });++ it.each<[number, number[]]>([+ [1, [6, 0]],+ [2, [0, 1]],+ [3, [1, 2]],+ [4, [2, 3]],+ [5, [3, 4]],+ [6, [4, 5]],+ [7, [5, 6]],+ [11, [0]],+ [12, [1]],+ [13, [2]],+ [14, [3]],+ [15, [4]],+ [16, [5]],+ [17, [6]],+ ])('decodes valid numeric weekend code %i', (weekend, expected) => {+ expect(isValidWeekend(weekend)).toBe(true);+ expect(getWeekendArray(weekend)).toEqual(expected);+ });+it('Function excelDateSerial', () => {expect(excelDateSerial(new Date(Date.UTC(1900, 1, 28)))).toBe(59);expect(excelDateSerial(new Date(Date.UTC(1900, 1, 29)))).toBe(61);diff --git a/packages/engine-formula/src/basics/date.ts b/packages/engine-formula/src/basics/date.tsindex 135a0841cf..65f0d9817e 100644--- a/packages/engine-formula/src/basics/date.ts+++ b/packages/engine-formula/src/basics/date.ts@@ -165,8 +165,8 @@ const weekendNumberMap: {export function isValidWeekend(weekend: number | string): boolean {// Weekend string values are seven characters long and each character in the string represents a day of the week, starting with Monday.- if (typeof weekend === 'string' && /^[0|1]{7}/.test(weekend)) {- return true;+ if (typeof weekend === 'string') {+ return weekend.length === 7 && /^[01]{7}$/.test(weekend);}if (weekendNumberMap[Number(weekend)]) {@@ -181,7 +181,7 @@ export function getWeekendArray(weekend: number | string): number[] {return [];}- if (typeof weekend === 'string' && /^[0|1]{7}/.test(weekend)) {+ if (typeof weekend === 'string') {// 1 represents a non-workday and 0 represents a workday. Only the characters 1 and 0 are permitted in the string. Using 1111111 will always return 0.const result = [];diff --git a/packages/engine-formula/src/functions/date/networkdays-intl/__tests__/index.spec.ts b/packages/engine-formula/src/functions/date/networkdays-intl/__tests__/index.spec.tsindex 320df6e5ab..222adbec53 100644--- a/packages/engine-formula/src/functions/date/networkdays-intl/__tests__/index.spec.ts+++ b/packages/engine-formula/src/functions/date/networkdays-intl/__tests__/index.spec.ts@@ -28,6 +28,22 @@ describe('Test networkdays.intl function', () => {const testFunction = new NetworkdaysIntl(FUNCTION_NAMES_DATE.NETWORKDAYS_INTL);describe('NetworkdaysIntl', () => {+ it.each(['1', '000001', '00000110', '00000001', '00000|1', '0000011x', '11111111', '0000011\n'])('rejects invalid weekend mask %j', (weekend) => {+ const result = testFunction.calculate(NumberValueObject.create(45000), NumberValueObject.create(45006), StringValueObject.create(weekend));+ expect(getObjectValue(result)).toBe(ErrorType.VALUE);+ });++ it('preserves valid masks and numeric weekend codes', () => {+ const startDate = NumberValueObject.create(45000);+ const endDate = NumberValueObject.create(45006);++ expect(getObjectValue(testFunction.calculate(startDate, endDate, StringValueObject.create('0000011')))).toBe(5);+ expect(getObjectValue(testFunction.calculate(startDate, endDate, NumberValueObject.create(1)))).toBe(5);+ expect(getObjectValue(testFunction.calculate(startDate, endDate, NumberValueObject.create(11)))).toBe(6);+ expect(getObjectValue(testFunction.calculate(startDate, endDate, StringValueObject.create('0000000')))).toBe(7);+ expect(getObjectValue(testFunction.calculate(startDate, endDate, StringValueObject.create('1111111')))).toBe(0);+ });+it('Value is all date string', () => {const startDate = StringValueObject.create('2012-10-1');const endDate = StringValueObject.create('2013-3-1');diff --git a/packages/engine-formula/src/functions/date/workday-intl/__tests__/index.spec.ts b/packages/engine-formula/src/functions/date/workday-intl/__tests__/index.spec.tsindex c2b381b85f..13c5515bc3 100644--- a/packages/engine-formula/src/functions/date/workday-intl/__tests__/index.spec.ts+++ b/packages/engine-formula/src/functions/date/workday-intl/__tests__/index.spec.ts@@ -27,6 +27,31 @@ describe('Test workday.intl function', () => {const testFunction = new WorkdayIntl(FUNCTION_NAMES_DATE.WORKDAY_INTL);describe('WorkdayIntl', () => {+ it.each(['1', '000001', '00000110', '00000001', '00000|1', '0000011x', '0000011\n'])('rejects invalid weekend mask %j', (weekend) => {+ const startDate = NumberValueObject.create(45000);++ for (const days of [1, -1]) {+ const result = testFunction.calculate(startDate, NumberValueObject.create(days), StringValueObject.create(weekend));+ expect(result.getValue()).toBe(ErrorType.VALUE);+ }+ });++ it('rejects an overlong all-rest mask for zero workdays', () => {+ const result = testFunction.calculate(NumberValueObject.create(45000), NumberValueObject.create(0), StringValueObject.create('11111111'));+ expect(result.getValue()).toBe(ErrorType.VALUE);+ });++ it('preserves valid masks and numeric weekend codes', () => {+ const startDate = NumberValueObject.create(45000);+ const days = NumberValueObject.create(3);++ expect(testFunction.calculate(startDate, days, StringValueObject.create('0000011')).getValue()).toBe(45005);+ expect(testFunction.calculate(startDate, days, NumberValueObject.create(1)).getValue()).toBe(45005);+ expect(testFunction.calculate(startDate, days, NumberValueObject.create(11)).getValue()).toBe(45003);+ expect(testFunction.calculate(startDate, days, StringValueObject.create('0000000')).getValue()).toBe(45003);+ expect(testFunction.calculate(startDate, days, StringValueObject.create('1111111')).getValue()).toBe(ErrorType.VALUE);+ });+it('Value is all normal', () => {const startDate = StringValueObject.create('2008-10-1');const days = NumberValueObject.create(151);