Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix column data appearing incorrectly when multiplier null #1194

Merged
merged 2 commits into from
Apr 3, 2023
Merged
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
9 changes: 4 additions & 5 deletions __mocks__/dh-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1812,11 +1812,10 @@ class NumberFormat {
}

static format(pattern, number) {
if (pattern.indexOf('.') >= 0) {
return number.toFixed(4);
} else {
return number.toFixed(0);
}
const decimalIndex = pattern.indexOf('.');
const decimalCount =
decimalIndex >= 0 ? pattern.length - decimalIndex - 1 : 0;
return number.toFixed(decimalCount);
}
}

Expand Down
41 changes: 41 additions & 0 deletions packages/jsapi-utils/src/formatters/DecimalColumnFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import DecimalColumnFormatter from './DecimalColumnFormatter';

describe('multiplier tests', () => {
const formatter = new DecimalColumnFormatter();
const value = 10.4;
it('handles null multiplier correctly', () => {
expect(
formatter.format(value, {
multiplier: null,
})
).toBe('10.4000');
});
it('handles undefined multiplier correctly', () => {
expect(
formatter.format(value, {
multiplier: undefined,
})
).toBe('10.4000');
});
it('ignores 0 multiplier correctly', () => {
expect(
formatter.format(value, {
multiplier: 0,
})
).toBe('10.4000');
});
it('handles 1 multiplier correctly', () => {
expect(
formatter.format(value, {
multiplier: 1,
})
).toBe('10.4000');
});
it('handles 2 multiplier correctly', () => {
expect(
formatter.format(value, {
multiplier: 2,
})
).toBe('20.8000');
});
});
4 changes: 2 additions & 2 deletions packages/jsapi-utils/src/formatters/DecimalColumnFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import TableColumnFormatter, {
const log = Log.module('DecimalColumnFormatter');

export type DecimalColumnFormat = TableColumnFormat & {
multiplier?: number;
multiplier?: number | null;
};

export type DecimalColumnFormatterOptions = {
Expand Down Expand Up @@ -175,7 +175,7 @@ export class DecimalColumnFormatter extends TableColumnFormatter<number> {
? format.formatString
: this.defaultFormatString;
const value =
format.multiplier !== undefined && format.multiplier !== 0
format.multiplier != null && format.multiplier !== 0
? valueParam * format.multiplier
: valueParam;
try {
Expand Down
41 changes: 41 additions & 0 deletions packages/jsapi-utils/src/formatters/IntegerColumnFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import IntegerColumnFormatter from './IntegerColumnFormatter';

describe('multiplier tests', () => {
const formatter = new IntegerColumnFormatter();
const value = 10;
it('handles null multiplier correctly', () => {
expect(
formatter.format(value, {
multiplier: null,
})
).toBe('10');
});
it('handles undefined multiplier correctly', () => {
expect(
formatter.format(value, {
multiplier: undefined,
})
).toBe('10');
});
it('ignores 0 multiplier correctly', () => {
expect(
formatter.format(value, {
multiplier: 0,
})
).toBe('10');
});
it('handles 1 multiplier correctly', () => {
expect(
formatter.format(value, {
multiplier: 1,
})
).toBe('10');
});
it('handles 2 multiplier correctly', () => {
expect(
formatter.format(value, {
multiplier: 2,
})
).toBe('20');
});
});
4 changes: 2 additions & 2 deletions packages/jsapi-utils/src/formatters/IntegerColumnFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import TableColumnFormatter, {
const log = Log.module('IntegerColumnFormatter');

export type IntegerColumnFormat = TableColumnFormat & {
multiplier?: number;
multiplier?: number | null;
};

export type IntegerColumnFormatterOptions = {
Expand Down Expand Up @@ -150,7 +150,7 @@ export class IntegerColumnFormatter extends TableColumnFormatter<number> {
? format.formatString
: this.defaultFormatString;
const value =
format.multiplier !== undefined && format.multiplier !== 0
format.multiplier != null && format.multiplier !== 0
? valueParam * format.multiplier
: valueParam;
try {
Expand Down