pretty-format
Advanced tools
Comparing version 3.7.0 to 3.8.0
100
index.js
@@ -71,3 +71,3 @@ 'use strict'; | ||
if (toStringed === '[object WeakSet]') return 'WeakSet {}'; | ||
if (toStringed === '[object Function]' || toStringed === '[object GeneratorFunction]') return printFunction(val); | ||
if (toStringed === '[object Function]' || toStringed === '[object GeneratorFunction]') return printFunction(val, min); | ||
if (toStringed === '[object Symbol]') return printSymbol(val); | ||
@@ -85,7 +85,7 @@ if (toStringed === '[object Date]') return toISOString.call(val); | ||
function printList(list, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
function printList(list, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min) { | ||
let body = ''; | ||
if (list.length) { | ||
body += '\n'; | ||
body += edgeSpacing; | ||
@@ -95,10 +95,10 @@ const innerIndent = prevIndent + indent; | ||
for (let i = 0; i < list.length; i++) { | ||
body += innerIndent + print(list[i], indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
body += innerIndent + print(list[i], indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
if (i < list.length - 1) { | ||
body += ',\n'; | ||
body += ',' + spacing; | ||
} | ||
} | ||
body += '\n' + prevIndent; | ||
body += edgeSpacing + prevIndent; | ||
} | ||
@@ -109,11 +109,11 @@ | ||
function printArguments(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
return 'Arguments ' + printList(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
function printArguments(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min) { | ||
return (min ? '' : 'Arguments ') + printList(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
} | ||
function printArray(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
return val.constructor.name + ' ' + printList(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
function printArray(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min) { | ||
return (min ? '' : val.constructor.name + ' ') + printList(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
} | ||
function printMap(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
function printMap(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min) { | ||
let result = 'Map {'; | ||
@@ -124,3 +124,3 @@ const iterator = val.entries(); | ||
if (!current.done) { | ||
result += '\n'; | ||
result += edgeSpacing; | ||
@@ -130,4 +130,4 @@ const innerIndent = prevIndent + indent; | ||
while (!current.done) { | ||
const key = print(current.value[0], indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
const value = print(current.value[1], indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
const key = print(current.value[0], indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
const value = print(current.value[1], indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
@@ -139,7 +139,7 @@ result += innerIndent + key + ' => ' + value; | ||
if (!current.done) { | ||
result += ',\n'; | ||
result += ',' + spacing; | ||
} | ||
} | ||
result += '\n' + prevIndent; | ||
result += edgeSpacing + prevIndent; | ||
} | ||
@@ -150,4 +150,4 @@ | ||
function printObject(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
const constructor = val.constructor ? val.constructor.name + ' ' : 'Object '; | ||
function printObject(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min) { | ||
const constructor = min ? '' : (val.constructor ? val.constructor.name + ' ' : 'Object '); | ||
let result = constructor + '{'; | ||
@@ -164,3 +164,3 @@ let keys = Object.keys(val).sort(); | ||
if (keys.length) { | ||
result += '\n'; | ||
result += edgeSpacing; | ||
@@ -171,4 +171,4 @@ const innerIndent = prevIndent + indent; | ||
const key = keys[i]; | ||
const name = print(key, indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
const value = print(val[key], indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
const name = print(key, indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
const value = print(val[key], indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
@@ -178,7 +178,7 @@ result += innerIndent + name + ': ' + value; | ||
if (i < keys.length - 1) { | ||
result += ',\n'; | ||
result += ',' + spacing; | ||
} | ||
} | ||
result += '\n' + prevIndent; | ||
result += edgeSpacing + prevIndent; | ||
} | ||
@@ -189,3 +189,3 @@ | ||
function printSet(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
function printSet(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min) { | ||
let result = 'Set {'; | ||
@@ -196,3 +196,3 @@ const iterator = val.entries(); | ||
if (!current.done) { | ||
result += '\n'; | ||
result += edgeSpacing; | ||
@@ -202,3 +202,3 @@ const innerIndent = prevIndent + indent; | ||
while (!current.done) { | ||
result += innerIndent + print(current.value[1], indent, innerIndent, refs, maxDepth, currentDepth, plugins); | ||
result += innerIndent + print(current.value[1], indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
@@ -208,7 +208,7 @@ current = iterator.next(); | ||
if (!current.done) { | ||
result += ',\n'; | ||
result += ',' + spacing; | ||
} | ||
} | ||
result += '\n' + prevIndent; | ||
result += edgeSpacing + prevIndent; | ||
} | ||
@@ -219,3 +219,3 @@ | ||
function printComplexValue(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
function printComplexValue(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min) { | ||
refs = refs.slice(); | ||
@@ -233,3 +233,3 @@ if (refs.indexOf(val) > -1) { | ||
if (!hitMaxDepth && val.toJSON && typeof val.toJSON === 'function') { | ||
return print(val.toJSON(), indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
return print(val.toJSON(), indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
} | ||
@@ -239,15 +239,15 @@ | ||
if (toStringed === '[object Arguments]') { | ||
return hitMaxDepth ? '[Arguments]' : printArguments(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
return hitMaxDepth ? '[Arguments]' : printArguments(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
} else if (isToStringedArrayType(toStringed)) { | ||
return hitMaxDepth ? '[Array]' : printArray(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
return hitMaxDepth ? '[Array]' : printArray(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
} else if (toStringed === '[object Map]') { | ||
return hitMaxDepth ? '[Map]' : printMap(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
return hitMaxDepth ? '[Map]' : printMap(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
} else if (toStringed === '[object Set]') { | ||
return hitMaxDepth ? '[Set]' : printSet(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
return hitMaxDepth ? '[Set]' : printSet(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
} else if (typeof val === 'object') { | ||
return hitMaxDepth ? '[Object]' : printObject(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
return hitMaxDepth ? '[Object]' : printObject(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
} | ||
} | ||
function printPlugin(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
function printPlugin(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min) { | ||
let match = false; | ||
@@ -270,3 +270,3 @@ let plugin; | ||
function boundPrint(val) { | ||
return print(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
return print(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
} | ||
@@ -279,13 +279,16 @@ | ||
return plugin.print(val, boundPrint, boundIndent); | ||
return plugin.print(val, boundPrint, boundIndent, { | ||
edgeSpacing: edgeSpacing, | ||
spacing: spacing | ||
}); | ||
} | ||
function print(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) { | ||
function print(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min) { | ||
const basic = printBasicValue(val); | ||
if (basic) return basic; | ||
const plugin = printPlugin(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
const plugin = printPlugin(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
if (plugin) return plugin; | ||
return printComplexValue(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins); | ||
return printComplexValue(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min); | ||
} | ||
@@ -295,2 +298,3 @@ | ||
indent: 2, | ||
min: false, | ||
maxDepth: Infinity, | ||
@@ -306,2 +310,6 @@ plugins: [] | ||
}); | ||
if (opts.min && opts.indent !== undefined && opts.indent !== 0) { | ||
throw new Error('prettyFormat: Cannot run with min option and indent'); | ||
} | ||
} | ||
@@ -316,2 +324,6 @@ | ||
if (result.min) { | ||
result.indent = 0; | ||
} | ||
return result; | ||
@@ -336,2 +348,4 @@ } | ||
const currentDepth = 0; | ||
const spacing = opts.min ? ' ' : '\n'; | ||
const edgeSpacing = opts.min ? '' : '\n'; | ||
@@ -341,3 +355,3 @@ if (opts && opts.plugins.length) { | ||
refs = []; | ||
var pluginsResult = printPlugin(val, indent, prevIndent, refs, opts.maxDepth, currentDepth, opts.plugins); | ||
var pluginsResult = printPlugin(val, indent, prevIndent, spacing, edgeSpacing, refs, opts.maxDepth, currentDepth, opts.plugins, opts.min); | ||
if (pluginsResult) return pluginsResult; | ||
@@ -351,5 +365,5 @@ } | ||
if (!refs) refs = []; | ||
return printComplexValue(val, indent, prevIndent, refs, opts.maxDepth, currentDepth, opts.plugins); | ||
return printComplexValue(val, indent, prevIndent, spacing, edgeSpacing, refs, opts.maxDepth, currentDepth, opts.plugins, opts.min); | ||
} | ||
module.exports = prettyFormat; |
{ | ||
"name": "pretty-format", | ||
"version": "3.7.0", | ||
"version": "3.8.0", | ||
"description": "Stringify any JavaScript value.", | ||
@@ -17,3 +17,2 @@ "license": "MIT", | ||
"jest": { | ||
"automock": false, | ||
"testEnvironment": "node", | ||
@@ -24,3 +23,3 @@ "verbose": true | ||
"chalk": "^1.1.3", | ||
"jest": "^14.1.0", | ||
"jest": "^15.1.1", | ||
"left-pad": "^1.1.1", | ||
@@ -27,0 +26,0 @@ "react": "15.3.0" |
@@ -15,6 +15,6 @@ 'use strict'; | ||
function printChildren(flatChildren, print, indent) { | ||
function printChildren(flatChildren, print, indent, opts) { | ||
return flatChildren.map(node => { | ||
if (typeof node === 'object') { | ||
return printElement(node, print, indent); | ||
return printElement(node, print, indent, opts); | ||
} else if (typeof node === 'string') { | ||
@@ -25,6 +25,6 @@ return printString(node); | ||
} | ||
}).join('\n'); | ||
}).join(opts.edgeSpacing); | ||
} | ||
function printProps(props, print, indent) { | ||
function printProps(props, print, indent, opts) { | ||
return Object.keys(props).sort().map(name => { | ||
@@ -40,3 +40,3 @@ if (name === 'children') { | ||
if (printed.indexOf('\n') !== -1) { | ||
printed = '{\n' + indent(indent(printed) + '\n}'); | ||
printed = '{' + opts.edgeSpacing + indent(indent(printed) + opts.edgeSpacing + '}'); | ||
} else { | ||
@@ -47,9 +47,9 @@ printed = '{' + printed + '}'; | ||
return '\n' + indent(name + '=') + printed; | ||
return opts.spacing + indent(name + '=') + printed; | ||
}).join(''); | ||
} | ||
function printElement(element, print, indent) { | ||
function printElement(element, print, indent, opts) { | ||
let result = '<' + element.type; | ||
result += printProps(element.props, print, indent); | ||
result += printProps(element.props, print, indent, opts); | ||
@@ -62,4 +62,4 @@ const opaqueChildren = element.props.children; | ||
}); | ||
const children = printChildren(flatChildren, print, indent); | ||
result += '>\n' + indent(children) + '\n</' + element.type + '>'; | ||
const children = printChildren(flatChildren, print, indent, opts); | ||
result += '>' + opts.edgeSpacing + indent(children) + opts.edgeSpacing + '</' + element.type + '>'; | ||
} else { | ||
@@ -76,5 +76,5 @@ result += ' />'; | ||
}, | ||
print(val, print, indent) { | ||
return printElement(val, print, indent); | ||
print(val, print, indent, opts) { | ||
return printElement(val, print, indent, opts); | ||
} | ||
}; |
@@ -7,7 +7,7 @@ 'use strict'; | ||
function printChildren(children, print, indent) { | ||
return children.map(child => printInstance(child, print, indent)).join('\n'); | ||
function printChildren(children, print, indent, opts) { | ||
return children.map(child => printInstance(child, print, indent, opts)).join(opts.edgeSpacing); | ||
} | ||
function printProps(props, print, indent) { | ||
function printProps(props, print, indent, opts) { | ||
return Object.keys(props).sort().map(name => { | ||
@@ -19,3 +19,3 @@ const prop = props[name]; | ||
if (printed.indexOf('\n') !== -1) { | ||
printed = '{\n' + indent(indent(printed) + '\n}'); | ||
printed = '{' + opts.edgeSpacing + indent(indent(printed) + opts.edgeSpacing + '}'); | ||
} else { | ||
@@ -26,7 +26,7 @@ printed = '{' + printed + '}'; | ||
return '\n' + indent(name + '=') + printed; | ||
return opts.spacing + indent(name + '=') + printed; | ||
}).join(''); | ||
} | ||
function printInstance(instance, print, indent) { | ||
function printInstance(instance, print, indent, opts) { | ||
if (typeof instance == 'number') { | ||
@@ -41,8 +41,8 @@ return print(instance); | ||
if (instance.props) { | ||
result += printProps(instance.props, print, indent); | ||
result += printProps(instance.props, print, indent, opts); | ||
} | ||
if (instance.children) { | ||
const children = printChildren(instance.children, print, indent); | ||
result += '>\n' + indent(children) + '\n</' + instance.type + '>'; | ||
const children = printChildren(instance.children, print, indent, opts); | ||
result += '>' + opts.edgeSpacing + indent(children) + opts.edgeSpacing + '</' + instance.type + '>'; | ||
} else { | ||
@@ -59,5 +59,5 @@ result += ' />'; | ||
}, | ||
print(val, print, indent) { | ||
return printInstance(val, print, indent); | ||
print(val, print, indent, opts) { | ||
return printInstance(val, print, indent, opts); | ||
} | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18143
376