fix: use json replacer for both util functions

This commit is contained in:
William Oldham 2025-09-19 22:53:50 +01:00
parent 40a0308088
commit f8fca8f082
2 changed files with 13 additions and 11 deletions

View File

@ -20,8 +20,8 @@ const listCmd = new Command('ls')
});
logOutputList(cmd.format, files.map(v => ({
...v,
size: Number(v.size),
dataId: Number(v.dataId)
size: v.size,
dataId: v.dataId
}), {
dataId: 'Data ID',
name: 'Name',
@ -48,10 +48,10 @@ const viewCmd = new Command('view')
return;
}
logOutputObject(cmd.format, {
dataId: Number(file.dataId),
dataId: file.dataId,
name: file.name,
type: file.type,
size: Number(file.size),
size: file.size,
hash: file.hash,
supportedCountries: file.supportedCountries,
supportedLanguages: file.supportedLanguages,

View File

@ -11,14 +11,16 @@ function mapOutputObject(obj: FormattableObject, mapping: FieldMapping): Formatt
return Object.fromEntries(mappedEntries);
}
function jsonReplacer(key: string, value: any): any {
if (typeof value === 'bigint') {
return Number(value);
}
return value;
}
export function logOutputList<T extends FormattableObject>(format: FormatOption, items: T[], mapping: FieldMapping = {}): void {
if (format === 'json') {
console.log(JSON.stringify(items, (_, v) => {
if (typeof v === 'bigint') {
return v.toString();
}
return v;
}, 2));
console.log(JSON.stringify(items, jsonReplacer, 2));
return;
}
const mappedItems = items.map(item => mapOutputObject(item, mapping));
@ -27,7 +29,7 @@ export function logOutputList<T extends FormattableObject>(format: FormatOption,
export function logOutputObject<T extends FormattableObject>(format: FormatOption, obj: T, mapping: FieldMapping = {}): void {
if (format === 'json') {
console.log(JSON.stringify(obj, null, 2));
console.log(JSON.stringify(obj, jsonReplacer, 2));
return;
}
console.log(mapOutputObject(obj, mapping));