Skip to content

Commit c4fe04f

Browse files
committed
[FIX] format: large number format with no digits
Since e8b38a a format with no date part and no digit is (correctly) detected as a number format. But `_createLargeNumberFormat` would crash if there were no digits. Task: 6010376
1 parent aa16066 commit c4fe04f

4 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/helpers/format/format.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ function _createLargeNumberFormat<T extends InternalFormat>(
813813

814814
const lastDigitIndex = newIntegerPart.findLastIndex((token) => token.type === "DIGIT");
815815
if (lastDigitIndex === -1) {
816-
throw new Error("Cannot create a large number format from a format with no digit.");
816+
return format;
817817
}
818818
while (newIntegerPart[lastDigitIndex + 1]?.type === "THOUSANDS_SEPARATOR") {
819819
newIntegerPart = removeIndexesFromArray(newIntegerPart, [lastDigitIndex + 1]);

tests/formats/format_helpers.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,7 @@ describe("rounding format", () => {
10911091
test("Round multi part format", () => {
10921092
expect(roundFormat("0.00\\€;$0.#; 0.00 ;@")).toBe("0\\€;$0; 0 ;@");
10931093
expect(roundFormat("dd/mm/yyyy;0.#")).toBe("dd/mm/yyyy;0");
1094+
expect(roundFormat(" - ; - ; - ; - ")).toBe(" - ; - ; - ; - ");
10941095
});
10951096
});
10961097

tests/formats/formatting_plugin.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ describe("formatting values (with formatters)", () => {
249249
setFormat(model, "A1", ";;;@");
250250
setDecimal(model, "A1", -1);
251251
expect(getCell(model, "A1")?.format).toBe(";;;@");
252+
253+
setFormat(model, "A1", " - ; - ; - ; - ");
254+
setDecimal(model, "A1", -1);
255+
expect(getCell(model, "A1")?.format).toBe(" - ; - ; - ; - ");
252256
});
253257

254258
test("SET_DECIMAL on scientific format", () => {

tests/functions/module_custom.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Model } from "../../src";
22
import { setCellContent, setCellFormat, setFormat } from "../test_helpers/commands_helpers";
3-
import { getCellContent, getCellError } from "../test_helpers/getters_helpers";
3+
import { getCellContent, getCellError, getEvaluatedCell } from "../test_helpers/getters_helpers";
44
import { evaluateCellText } from "../test_helpers/helpers";
55

66
describe("FORMAT.LARGE.NUMBER formula", () => {
@@ -178,4 +178,12 @@ describe("FORMAT.LARGE.NUMBER formula", () => {
178178
setCellContent(model, "A2", "=FORMAT.LARGE.NUMBER(A1)");
179179
expect(getCellContent(model, "A2")).toBe("10,000k%");
180180
});
181+
182+
test("FORMAT.LARGE.NUMBER does not crash on format without digits", () => {
183+
const model = new Model();
184+
setCellContent(model, "A1", "100000");
185+
setFormat(model, "A1", " - ; - ; - ; - ");
186+
setCellContent(model, "A2", "=FORMAT.LARGE.NUMBER(A1)");
187+
expect(getEvaluatedCell(model, "A2")?.format).toBe(" - ; - ; - ; - ");
188+
});
181189
});

0 commit comments

Comments
 (0)