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

Rewrite some read bumps in pretty-format #4093

Merged
merged 3 commits into from
Jul 20, 2017
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
53 changes: 29 additions & 24 deletions packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,8 @@ function printNumber(val: number): string {
function printFunction(val: Function, printFunctionName: boolean): string {
if (!printFunctionName) {
return '[Function]';
} else if (val.name === '') {
return '[Function anonymous]';
} else {
return '[Function ' + val.name + ']';
}
return '[Function ' + (val.name || 'anonymous') + ']';
}

function printSymbol(val: Symbol): string {
Expand Down Expand Up @@ -155,7 +152,7 @@ function printBasicValue(
return null;
}

function printList(
function printListItems(
list: any,
config: Config,
indentation: string,
Expand Down Expand Up @@ -186,7 +183,7 @@ function printList(
return result;
}

function printMap(
function printMapEntries(
val: Map<any, any>,
config: Config,
indentation: string,
Expand All @@ -203,7 +200,13 @@ function printMap(
const indentationNext = indentation + config.indent;

while (!current.done) {
const key = print(current.value[0], config, indentationNext, depth, refs);
const name = print(
current.value[0],
config,
indentationNext,
depth,
refs,
);
const value = print(
current.value[1],
config,
Expand All @@ -212,7 +215,7 @@ function printMap(
refs,
);

result += indentationNext + key + ' => ' + value;
result += indentationNext + name + ' => ' + value;

current = iterator.next();

Expand All @@ -229,7 +232,7 @@ function printMap(
return result;
}

function printObject(
function printObjectProperties(
val: Object,
config: Config,
indentation: string,
Expand Down Expand Up @@ -269,15 +272,15 @@ function printObject(
return result;
}

function printSet(
function printSetValues(
val: Set<any>,
config: Config,
indentation: string,
depth: number,
refs: Refs,
): string {
let result = '';
const iterator = val.entries();
const iterator = val.values();
let current = iterator.next();

if (!current.done) {
Expand All @@ -288,7 +291,7 @@ function printSet(
while (!current.done) {
result +=
indentationNext +
print(current.value[1], config, indentationNext, depth, refs);
print(current.value, config, indentationNext, depth, refs);

current = iterator.next();

Expand All @@ -312,12 +315,11 @@ function printComplexValue(
depth: number,
refs: Refs,
): string {
refs = refs.slice();
if (refs.indexOf(val) > -1) {
if (refs.indexOf(val) !== -1) {
return '[Circular]';
} else {
refs.push(val);
}
refs = refs.slice();
refs.push(val);

const hitMaxDepth = ++depth > config.maxDepth;
const min = config.min;
Expand All @@ -337,30 +339,33 @@ function printComplexValue(
? '[Arguments]'
: (min ? '' : 'Arguments ') +
'[' +
printList(val, config, indentation, depth, refs) +
printListItems(val, config, indentation, depth, refs) +
']';
} else if (isToStringedArrayType(toStringed)) {
}
if (isToStringedArrayType(toStringed)) {
return hitMaxDepth
? '[' + val.constructor.name + ']'
: (min ? '' : val.constructor.name + ' ') +
'[' +
printList(val, config, indentation, depth, refs) +
printListItems(val, config, indentation, depth, refs) +
']';
} else if (toStringed === '[object Map]') {
}
if (toStringed === '[object Map]') {
return hitMaxDepth
? '[Map]'
: 'Map {' + printMap(val, config, indentation, depth, refs) + '}';
} else if (toStringed === '[object Set]') {
: 'Map {' + printMapEntries(val, config, indentation, depth, refs) + '}';
}
if (toStringed === '[object Set]') {
return hitMaxDepth
? '[Set]'
: 'Set {' + printSet(val, config, indentation, depth, refs) + '}';
: 'Set {' + printSetValues(val, config, indentation, depth, refs) + '}';
}

return hitMaxDepth
? '[' + (val.constructor ? val.constructor.name : 'Object') + ']'
: (min ? '' : (val.constructor ? val.constructor.name : 'Object') + ' ') +
'{' +
printObject(val, config, indentation, depth, refs) +
printObjectProperties(val, config, indentation, depth, refs) +
'}';
}

Expand Down