Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Fix eslint for json_utils.ts #2911

Merged
merged 1 commit into from
Sep 21, 2020
Merged
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
52 changes: 37 additions & 15 deletions src/webui/src/static/json_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ function batchFormat(
width: number,
keyOrKeys?: string | string[]
): string[] {

let keys: string[]; // dict key as prefix string
let keys: string[]; // dict key as prefix string
if (keyOrKeys === undefined) {
keys = objects.map(() => '');
} else if (typeof keyOrKeys === 'string') {
Expand All @@ -50,7 +49,7 @@ function batchFormat(

const hasNested = nonNull.some(obj => detectNested(obj));

if (!hasNested && lines.every(line => (line.length + curIndent.length < width))) {
if (!hasNested && lines.every(line => line.length + curIndent.length < width)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I personally think this expression is really hard to read...

return lines;
}

Expand All @@ -62,7 +61,15 @@ function batchFormat(
if (obj === null) {
return keys[i] + 'null';
} else {
return keys[i] + createBlock(curIndent, indent, '[]', obj.map(() => iter.next().value));
return (
keys[i] +
createBlock(
curIndent,
indent,
'[]',
obj.map(() => iter.next().value)
)
);
}
});
}
Expand All @@ -79,20 +86,29 @@ function batchFormat(
if (obj === null) {
return keys[i] + 'null';
} else {
return keys[i] + createBlock(curIndent, indent, '{}', Object.keys(obj).map(() => iter.next().value));
return (
keys[i] +
createBlock(
curIndent,
indent,
'{}',
Object.keys(obj).map(() => iter.next().value)
)
);
}
});

} else {
// these objects look like class instances, so we will try to group their fields
const uniqueKeys = new Set(childrenKeys);
const iters = new Map();
for (const key of uniqueKeys) {
const fields = nonNull.map(obj => obj[key]).filter(v => v !== undefined);
let elements;
if (detectBatch(fields)) { // look like same field of class instances
if (detectBatch(fields)) {
// look like same field of class instances
elements = batchFormat(fields, curIndent + indent, indent, width, key);
} else { // no idea what these are, fallback to format them independently
} else {
// no idea what these are, fallback to format them independently
elements = fields.map(field => batchFormat([field], curIndent + indent, indent, width, key));
}
iters.set(key, elements[Symbol.iterator]());
Expand Down Expand Up @@ -138,18 +154,18 @@ function detectBatch(objects: any[]): boolean {
return sameType(concat(nonNull));
}

if (nonNull.every(obj => (typeof obj === 'object' && !Array.isArray(obj)))) {
if (nonNull.every(obj => typeof obj === 'object' && !Array.isArray(obj))) {
const totalKeys = new Set(concat(nonNull.map(obj => Object.keys(obj)))).size;
const missKeys = nonNull.map(obj => (totalKeys - Object.keys(obj).length));
const missKeys = nonNull.map(obj => totalKeys - Object.keys(obj).length);
const missSum = missKeys.reduce((a, b) => a + b, 0);
return missSum < (totalKeys * nonNull.length) * batchThreshold;
return missSum < totalKeys * nonNull.length * batchThreshold;
}

return sameType(nonNull);
}

function detectNested(obj: any): boolean {
return typeof(obj) == 'object' && Object.values(obj).some(child => typeof(child) == 'object');
return typeof obj == 'object' && Object.values(obj).some(child => typeof child == 'object');
}

function concat(arrays: any[][]): any[] {
Expand All @@ -168,7 +184,7 @@ function createBlock(curIndent: string, indent: string, brackets: string, elemen

function sameType(objects: any[]): boolean {
const nonNull = objects.filter(obj => obj !== undefined);
return nonNull.length > 0 ? nonNull.every(obj => (typeof obj === typeof nonNull[0])) : true;
return nonNull.length > 0 ? nonNull.every(obj => typeof obj === typeof nonNull[0]) : true;
}

function stringifySingleLine(obj: any): string {
Expand All @@ -179,9 +195,15 @@ function stringifySingleLine(obj: any): string {
} else if (typeof obj === 'string') {
return `"${obj}"`;
} else if (Array.isArray(obj)) {
return '[' + obj.map(x => stringifySingleLine(x)).join(', ') + ']'
return '[' + obj.map(x => stringifySingleLine(x)).join(', ') + ']';
} else {
return '{' + Object.keys(obj).map(key => `"${key}": ${stringifySingleLine(obj[key])}`).join(', ') + '}';
return (
'{' +
Object.keys(obj)
.map(key => `"${key}": ${stringifySingleLine(obj[key])}`)
.join(', ') +
'}'
);
}
}

Expand Down