Skip to content
Closed
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
10 changes: 7 additions & 3 deletions src/helpers/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,12 @@ function applyInternalNumberFormat(value: number, format: NumberInternalFormat,
}
const { integerDigits, decimalDigits } = splitNumber(Math.abs(value), maxDecimals);

const hasDigitInDecimalPart = !!format.decimalPart?.some((token) => token.type === "DIGIT");
let formattedValue = applyIntegerFormat(
integerDigits,
format,
format.thousandsSeparator ? locale.thousandsSeparator : undefined
format.thousandsSeparator ? locale.thousandsSeparator : undefined,
hasDigitInDecimalPart
);

if (format.decimalPart !== undefined) {
Expand All @@ -273,10 +275,12 @@ function applyInternalNumberFormat(value: number, format: NumberInternalFormat,
function applyIntegerFormat(
integerDigits: string,
internalFormat: NumberInternalFormat,
thousandsSeparator: string | undefined
thousandsSeparator: string | undefined,
hasDigitInDecimalPart: boolean
): string {
let tokens = internalFormat.integerPart;
if (!tokens.some((token) => token.type === "DIGIT")) {
// If the format has a decimal part with digits, we always want to display the integer digits
if (hasDigitInDecimalPart && !tokens.some((token) => token.type === "DIGIT")) {
tokens = [...tokens, { type: "DIGIT", value: "#" }];
}
if (integerDigits === "0") {
Expand Down
8 changes: 8 additions & 0 deletions tests/formats/format_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,14 @@ describe("formatValue on number", () => {
expect(() => formatValue(1234, { format: "# @", locale })).toThrow();
expect(() => formatValue(1234, { format: "@0", locale })).toThrow();
});

test("Format with no digit in them", () => {
expect(formatValue(200, { format: "[$Positive]", locale })).toBe("Positive");
expect(formatValue(-123, { format: '0;,"Negative"', locale })).toBe("Negative");
expect(formatValue(0, { format: '0;0;"Zero"', locale })).toBe("Zero");
expect(formatValue("test", { format: '"Smile"', locale })).toBe("test");
expect(formatValue("test", { format: '@"Smile"', locale })).toBe("testSmile");
Comment on lines +417 to +418
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c'est marrant ça

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah first one is a number format applied to a text, so it does nothing, second one is a text format applied to a text. Looks a bit strange indeed 😅

});
});

describe("formatValue on large numbers", () => {
Expand Down