Skip to content

Commit

Permalink
Rewrite some read bumps in pretty-format (#4093)
Browse files Browse the repository at this point in the history
* Rewrite some read bumps in pretty-format

* Replace ternary with conditional or

* Rename recently refactored helper functions
  • Loading branch information
pedrottimark authored and cpojer committed Jul 20, 2017
1 parent 00ea3ac commit d2b7c2d
Showing 1 changed file with 29 additions and 24 deletions.
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

0 comments on commit d2b7c2d

Please sign in to comment.