fix application of suffixes (#133780)

This commit is contained in:
Joe Reuter 2022-06-08 14:03:57 +02:00 committed by GitHub
parent 1bfb02678f
commit 3c5733e6fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 115 additions and 78 deletions

View file

@ -160,6 +160,33 @@ describe('format_column', () => {
});
});
it('applies suffix formatter even if there is a parent format', async () => {
datatable.columns[0].meta.params = {
id: 'wrapper',
params: { wrapperParam: 0, id: 'myformatter', params: { innerParam: 456 } },
};
const result = await fn(datatable, {
columnId: 'test',
format: '',
suffix: 'abc',
parentFormat: JSON.stringify({ id: 'wrapper', params: { wrapperParam: 123 } }),
});
expect(result.columns[0].meta.params).toEqual({
id: 'suffix',
params: {
suffixString: 'abc',
id: 'wrapper',
params: {
wrapperParam: 123,
id: 'myformatter',
params: {
innerParam: 456,
},
},
},
});
});
it('double-nests suffix formatters', async () => {
datatable.columns[0].meta.params = {
id: 'suffix',

View file

@ -26,101 +26,111 @@ export const formatColumnFn: FormatColumnExpressionFunction['fn'] = (
{ format, columnId, decimals, suffix, parentFormat }: FormatColumnArgs
) => ({
...input,
columns: input.columns.map((col) => {
if (col.id === columnId) {
if (!parentFormat) {
if (supportedFormats[format]) {
let serializedFormat: SerializedFieldFormat = {
id: format,
params: { pattern: supportedFormats[format].decimalsToPattern(decimals) },
};
if (suffix) {
serializedFormat = {
id: 'suffix',
params: {
...serializedFormat,
suffixString: suffix,
},
columns: input.columns
.map((col) => {
if (col.id === columnId) {
if (!parentFormat) {
if (supportedFormats[format]) {
const serializedFormat: SerializedFieldFormat = {
id: format,
params: { pattern: supportedFormats[format].decimalsToPattern(decimals) },
};
return withParams(col, serializedFormat as Record<string, unknown>);
} else if (format) {
return withParams(col, { id: format });
} else {
return col;
}
return withParams(col, serializedFormat as Record<string, unknown>);
} else if (format) {
return withParams(col, { id: format });
} else {
}
const parsedParentFormat = JSON.parse(parentFormat);
const parentFormatId = parsedParentFormat.id;
const parentFormatParams = parsedParentFormat.params ?? {};
// Be careful here to check for undefined custom format
const isDuplicateParentFormatter = parentFormatId === col.meta.params?.id && format == null;
if (!parentFormatId || isDuplicateParentFormatter) {
return col;
}
}
const parsedParentFormat = JSON.parse(parentFormat);
const parentFormatId = parsedParentFormat.id;
const parentFormatParams = parsedParentFormat.params ?? {};
// Be careful here to check for undefined custom format
const isDuplicateParentFormatter = parentFormatId === col.meta.params?.id && format == null;
if (!parentFormatId || isDuplicateParentFormatter) {
return col;
}
if (format && supportedFormats[format]) {
const customParams = {
pattern: supportedFormats[format].decimalsToPattern(decimals),
};
// Some parent formatters are multi-fields and wrap the custom format into a "paramsPerField"
// property. Here the format is passed to this property to make it work properly
if ((col.meta.params?.params?.paramsPerField as SerializedFieldFormat[])?.length) {
if (format && supportedFormats[format]) {
const customParams = {
pattern: supportedFormats[format].decimalsToPattern(decimals),
};
// Some parent formatters are multi-fields and wrap the custom format into a "paramsPerField"
// property. Here the format is passed to this property to make it work properly
if ((col.meta.params?.params?.paramsPerField as SerializedFieldFormat[])?.length) {
return withParams(col, {
id: parentFormatId,
params: {
...col.meta.params?.params,
id: format,
...parentFormatParams,
// some wrapper formatters require params to be flatten out (i.e. terms) while others
// require them to be in the params property (i.e. ranges)
// so for now duplicate
paramsPerField: (
col.meta.params?.params?.paramsPerField as SerializedFieldFormat[]
).map((f) => ({
...f,
params: { ...f.params, ...customParams },
...customParams,
})),
},
});
}
return withParams(col, {
id: parentFormatId,
params: {
...col.meta.params?.params,
id: format,
...parentFormatParams,
// some wrapper formatters require params to be flatten out (i.e. terms) while others
// require them to be in the params property (i.e. ranges)
// so for now duplicate
paramsPerField: (
col.meta.params?.params?.paramsPerField as SerializedFieldFormat[]
).map((f) => ({
...f,
params: { ...f.params, ...customParams },
...customParams,
})),
...customParams,
params: customParams,
...parentFormatParams,
},
});
}
return withParams(col, {
id: parentFormatId,
params: {
...col.meta.params?.params,
id: format,
// some wrapper formatters require params to be flatten out (i.e. terms) while others
// require them to be in the params property (i.e. ranges)
// so for now duplicate
...customParams,
params: customParams,
...parentFormatParams,
},
});
}
if (parentFormatParams) {
// if original format is already a nested one, we are just replacing the wrapper params
// otherwise wrapping it inside parentFormatId/parentFormatParams
const isNested = isNestedFormat(col.meta.params);
const innerParams = isNested
? col.meta.params?.params
: { id: col.meta.params?.id, params: col.meta.params?.params };
if (parentFormatParams) {
// if original format is already a nested one, we are just replacing the wrapper params
// otherwise wrapping it inside parentFormatId/parentFormatParams
const isNested = isNestedFormat(col.meta.params);
const innerParams = isNested
? col.meta.params?.params
: { id: col.meta.params?.id, params: col.meta.params?.params };
const formatId = isNested ? col.meta.params?.id : parentFormatId;
const formatId = isNested ? col.meta.params?.id : parentFormatId;
return withParams(col, {
...col.meta.params,
id: formatId,
params: {
...innerParams,
...parentFormatParams,
},
});
return withParams(col, {
...col.meta.params,
id: formatId,
params: {
...innerParams,
...parentFormatParams,
},
});
}
}
}
return col;
}),
return col;
})
.map((col) => {
if (!suffix) return col;
if (col.id !== columnId) return col;
if (!col.meta.params) return col;
return {
...col,
meta: {
...col.meta,
params: {
id: 'suffix',
params: {
...col.meta.params,
suffixString: suffix,
},
},
},
};
}),
});