diff --git a/packages/pretty-format/src/index.js b/packages/pretty-format/src/index.js index a7e5fdb1a02e..b2a25ffc9737 100644 --- a/packages/pretty-format/src/index.js +++ b/packages/pretty-format/src/index.js @@ -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 { @@ -155,7 +152,7 @@ function printBasicValue( return null; } -function printList( +function printListItems( list: any, config: Config, indentation: string, @@ -186,7 +183,7 @@ function printList( return result; } -function printMap( +function printMapEntries( val: Map, config: Config, indentation: string, @@ -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, @@ -212,7 +215,7 @@ function printMap( refs, ); - result += indentationNext + key + ' => ' + value; + result += indentationNext + name + ' => ' + value; current = iterator.next(); @@ -229,7 +232,7 @@ function printMap( return result; } -function printObject( +function printObjectProperties( val: Object, config: Config, indentation: string, @@ -269,7 +272,7 @@ function printObject( return result; } -function printSet( +function printSetValues( val: Set, config: Config, indentation: string, @@ -277,7 +280,7 @@ function printSet( refs: Refs, ): string { let result = ''; - const iterator = val.entries(); + const iterator = val.values(); let current = iterator.next(); if (!current.done) { @@ -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(); @@ -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; @@ -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) + '}'; }