Unverified Commit 36042435 authored by Yuge Zhang's avatar Yuge Zhang Committed by GitHub
Browse files

Fix eslint for json_utils.ts (#2911)

parent 6e967f50
...@@ -29,7 +29,6 @@ function batchFormat( ...@@ -29,7 +29,6 @@ function batchFormat(
width: number, width: number,
keyOrKeys?: string | string[] keyOrKeys?: string | string[]
): string[] { ): string[] {
let keys: string[]; // dict key as prefix string let keys: string[]; // dict key as prefix string
if (keyOrKeys === undefined) { if (keyOrKeys === undefined) {
keys = objects.map(() => ''); keys = objects.map(() => '');
...@@ -50,7 +49,7 @@ function batchFormat( ...@@ -50,7 +49,7 @@ function batchFormat(
const hasNested = nonNull.some(obj => detectNested(obj)); 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)) {
return lines; return lines;
} }
...@@ -62,7 +61,15 @@ function batchFormat( ...@@ -62,7 +61,15 @@ function batchFormat(
if (obj === null) { if (obj === null) {
return keys[i] + 'null'; return keys[i] + 'null';
} else { } else {
return keys[i] + createBlock(curIndent, indent, '[]', obj.map(() => iter.next().value)); return (
keys[i] +
createBlock(
curIndent,
indent,
'[]',
obj.map(() => iter.next().value)
)
);
} }
}); });
} }
...@@ -79,10 +86,17 @@ function batchFormat( ...@@ -79,10 +86,17 @@ function batchFormat(
if (obj === null) { if (obj === null) {
return keys[i] + 'null'; return keys[i] + 'null';
} else { } 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 { } else {
// these objects look like class instances, so we will try to group their fields // these objects look like class instances, so we will try to group their fields
const uniqueKeys = new Set(childrenKeys); const uniqueKeys = new Set(childrenKeys);
...@@ -90,9 +104,11 @@ function batchFormat( ...@@ -90,9 +104,11 @@ function batchFormat(
for (const key of uniqueKeys) { for (const key of uniqueKeys) {
const fields = nonNull.map(obj => obj[key]).filter(v => v !== undefined); const fields = nonNull.map(obj => obj[key]).filter(v => v !== undefined);
let elements; 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); 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)); elements = fields.map(field => batchFormat([field], curIndent + indent, indent, width, key));
} }
iters.set(key, elements[Symbol.iterator]()); iters.set(key, elements[Symbol.iterator]());
...@@ -138,18 +154,18 @@ function detectBatch(objects: any[]): boolean { ...@@ -138,18 +154,18 @@ function detectBatch(objects: any[]): boolean {
return sameType(concat(nonNull)); 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 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); const missSum = missKeys.reduce((a, b) => a + b, 0);
return missSum < (totalKeys * nonNull.length) * batchThreshold; return missSum < totalKeys * nonNull.length * batchThreshold;
} }
return sameType(nonNull); return sameType(nonNull);
} }
function detectNested(obj: any): boolean { 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[] { function concat(arrays: any[][]): any[] {
...@@ -168,7 +184,7 @@ function createBlock(curIndent: string, indent: string, brackets: string, elemen ...@@ -168,7 +184,7 @@ function createBlock(curIndent: string, indent: string, brackets: string, elemen
function sameType(objects: any[]): boolean { function sameType(objects: any[]): boolean {
const nonNull = objects.filter(obj => obj !== undefined); 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 { function stringifySingleLine(obj: any): string {
...@@ -179,9 +195,15 @@ function stringifySingleLine(obj: any): string { ...@@ -179,9 +195,15 @@ function stringifySingleLine(obj: any): string {
} else if (typeof obj === 'string') { } else if (typeof obj === 'string') {
return `"${obj}"`; return `"${obj}"`;
} else if (Array.isArray(obj)) { } else if (Array.isArray(obj)) {
return '[' + obj.map(x => stringifySingleLine(x)).join(', ') + ']' return '[' + obj.map(x => stringifySingleLine(x)).join(', ') + ']';
} else { } else {
return '{' + Object.keys(obj).map(key => `"${key}": ${stringifySingleLine(obj[key])}`).join(', ') + '}'; return (
'{' +
Object.keys(obj)
.map(key => `"${key}": ${stringifySingleLine(obj[key])}`)
.join(', ') +
'}'
);
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment