pretty-format
Advanced tools
Comparing version 19.0.0 to 19.1.0-alpha.eed82034
@@ -7,2 +7,4 @@ /** | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* | ||
*/ | ||
@@ -13,4 +15,27 @@ /* eslint-disable max-len */ | ||
const style = require('ansi-styles'); | ||
const toString = Object.prototype.toString; | ||
@@ -28,16 +53,14 @@ const toISOString = Date.prototype.toISOString; | ||
function isToStringedArrayType(toStringed) { | ||
return ( | ||
toStringed === '[object Array]' || | ||
toStringed === '[object ArrayBuffer]' || | ||
toStringed === '[object DataView]' || | ||
toStringed === '[object Float32Array]' || | ||
toStringed === '[object Float64Array]' || | ||
toStringed === '[object Int8Array]' || | ||
toStringed === '[object Int16Array]' || | ||
toStringed === '[object Int32Array]' || | ||
toStringed === '[object Uint8Array]' || | ||
toStringed === '[object Uint8ClampedArray]' || | ||
toStringed === '[object Uint16Array]' || | ||
toStringed === '[object Uint32Array]'); | ||
return toStringed === '[object Array]' || | ||
toStringed === '[object ArrayBuffer]' || | ||
toStringed === '[object DataView]' || | ||
toStringed === '[object Float32Array]' || | ||
toStringed === '[object Float64Array]' || | ||
toStringed === '[object Int8Array]' || | ||
toStringed === '[object Int16Array]' || | ||
toStringed === '[object Int32Array]' || | ||
toStringed === '[object Uint8Array]' || | ||
toStringed === '[object Uint8ClampedArray]' || | ||
toStringed === '[object Uint16Array]' || | ||
toStringed === '[object Uint32Array]'; | ||
} | ||
@@ -71,3 +94,7 @@ | ||
function printBasicValue(val, printFunctionName, escapeRegex) { | ||
function printBasicValue( | ||
val, | ||
printFunctionName, | ||
escapeRegex) | ||
{ | ||
if (val === true || val === false) { | ||
@@ -106,3 +133,6 @@ return '' + val; | ||
} | ||
if (toStringed === '[object Function]' || toStringed === '[object GeneratorFunction]') { | ||
if ( | ||
toStringed === '[object Function]' || | ||
toStringed === '[object GeneratorFunction]') | ||
{ | ||
return printFunction(val, printFunctionName); | ||
@@ -137,6 +167,21 @@ } | ||
return false; | ||
return null; | ||
} | ||
function printList(list, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors) { | ||
function printList( | ||
list, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors) | ||
{ | ||
let body = ''; | ||
@@ -150,4 +195,20 @@ | ||
for (let i = 0; i < list.length; i++) { | ||
body += innerIndent + print(list[i], indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
body += innerIndent + | ||
print( | ||
list[i], | ||
indent, | ||
innerIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
if (i < list.length - 1) { | ||
@@ -164,11 +225,88 @@ body += ',' + spacing; | ||
function printArguments(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors) { | ||
return (min ? '' : 'Arguments ') + printList(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
function printArguments( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors) | ||
{ | ||
return (min ? '' : 'Arguments ') + | ||
printList( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
} | ||
function printArray(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors) { | ||
return (min ? '' : val.constructor.name + ' ') + printList(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
function printArray( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors) | ||
{ | ||
return (min ? '' : val.constructor.name + ' ') + | ||
printList( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
} | ||
function printMap(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors) { | ||
function printMap( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors) | ||
{ | ||
let result = 'Map {'; | ||
@@ -184,5 +322,35 @@ const iterator = val.entries(); | ||
while (!current.done) { | ||
const key = print(current.value[0], indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
const value = print(current.value[1], indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
const key = print( | ||
current.value[0], | ||
indent, | ||
innerIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
const value = print( | ||
current.value[1], | ||
indent, | ||
innerIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
result += innerIndent + key + ' => ' + value; | ||
@@ -203,4 +371,21 @@ | ||
function printObject(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors) { | ||
const constructor = min ? '' : val.constructor ? val.constructor.name + ' ' : 'Object '; | ||
function printObject( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors) | ||
{ | ||
const constructor = min ? | ||
'' : | ||
val.constructor ? val.constructor.name + ' ' : 'Object '; | ||
let result = constructor + '{'; | ||
@@ -212,3 +397,8 @@ let keys = Object.keys(val).sort(); | ||
keys = keys. | ||
filter(key => !(typeof key === 'symbol' || toString.call(key) === '[object Symbol]')). | ||
filter( | ||
key => | ||
// $FlowFixMe string literal `symbol`. This value is not a valid `typeof` return value | ||
!(typeof key === 'symbol' || | ||
toString.call(key) === '[object Symbol]')). | ||
concat(symbols); | ||
@@ -224,5 +414,35 @@ } | ||
const key = keys[i]; | ||
const name = print(key, indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
const value = print(val[key], indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
const name = print( | ||
key, | ||
indent, | ||
innerIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
const value = print( | ||
val[key], | ||
indent, | ||
innerIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
result += innerIndent + name + ': ' + value; | ||
@@ -241,3 +461,18 @@ | ||
function printSet(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors) { | ||
function printSet( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors) | ||
{ | ||
let result = 'Set {'; | ||
@@ -253,4 +488,20 @@ const iterator = val.entries(); | ||
while (!current.done) { | ||
result += innerIndent + print(current.value[1], indent, innerIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
result += innerIndent + | ||
print( | ||
current.value[1], | ||
indent, | ||
innerIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
current = iterator.next(); | ||
@@ -269,3 +520,18 @@ | ||
function printComplexValue(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors) { | ||
function printComplexValue( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors) | ||
{ | ||
refs = refs.slice(); | ||
@@ -282,4 +548,21 @@ if (refs.indexOf(val) > -1) { | ||
if (callToJSON && !hitMaxDepth && val.toJSON && typeof val.toJSON === 'function') { | ||
return print(val.toJSON(), indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
if ( | ||
callToJSON && !hitMaxDepth && val.toJSON && typeof val.toJSON === 'function') | ||
{ | ||
return print( | ||
val.toJSON(), | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
} | ||
@@ -289,23 +572,120 @@ | ||
if (toStringed === '[object Arguments]') { | ||
return hitMaxDepth ? '[Arguments]' : printArguments(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
return hitMaxDepth ? | ||
'[Arguments]' : | ||
printArguments( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
} else if (isToStringedArrayType(toStringed)) { | ||
return hitMaxDepth ? '[Array]' : printArray(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
return hitMaxDepth ? | ||
'[Array]' : | ||
printArray( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
} else if (toStringed === '[object Map]') { | ||
return hitMaxDepth ? '[Map]' : printMap(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
return hitMaxDepth ? | ||
'[Map]' : | ||
printMap( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
} else if (toStringed === '[object Set]') { | ||
return hitMaxDepth ? '[Set]' : printSet(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
return hitMaxDepth ? | ||
'[Set]' : | ||
printSet( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
} | ||
return hitMaxDepth ? '[Object]' : printObject(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
return hitMaxDepth ? | ||
'[Object]' : | ||
printObject( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
} | ||
function printPlugin(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors) { | ||
let match = false; | ||
function printPlugin( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors) | ||
{ | ||
let plugin; | ||
for (let p = 0; p < plugins.length; p++) { | ||
plugin = plugins[p]; | ||
if (plugin.test(val)) { | ||
match = true; | ||
if (plugins[p].test(val)) { | ||
plugin = plugins[p]; | ||
break; | ||
@@ -315,8 +695,23 @@ } | ||
if (!match) { | ||
return false; | ||
if (!plugin) { | ||
return null; | ||
} | ||
function boundPrint(val) { | ||
return print(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
return print( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
} | ||
@@ -337,14 +732,59 @@ | ||
function print(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors) { | ||
const basic = printBasicValue(val, printFunctionName, escapeRegex); | ||
if (basic) { | ||
return basic; | ||
function print( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors) | ||
{ | ||
const pluginsResult = printPlugin( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
if (typeof pluginsResult === 'string') { | ||
return pluginsResult; | ||
} | ||
const plugin = printPlugin(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
if (plugin) { | ||
return plugin; | ||
const basicResult = printBasicValue(val, printFunctionName, escapeRegex); | ||
if (basicResult !== null) { | ||
return basicResult; | ||
} | ||
return printComplexValue(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors); | ||
return printComplexValue( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
maxDepth, | ||
currentDepth, | ||
plugins, | ||
min, | ||
callToJSON, | ||
printFunctionName, | ||
escapeRegex, | ||
colors); | ||
} | ||
@@ -354,2 +794,3 @@ | ||
callToJSON: true, | ||
edgeSpacing: '\n', | ||
escapeRegex: false, | ||
@@ -362,2 +803,3 @@ highlight: false, | ||
printFunctionName: true, | ||
spacing: '\n', | ||
theme: { | ||
@@ -374,3 +816,3 @@ content: 'reset', | ||
if (!DEFAULTS.hasOwnProperty(key)) { | ||
throw new Error('prettyFormat: Invalid option: ' + key); | ||
throw new Error(`pretty-format: Unknown option "${key}".`); | ||
} | ||
@@ -380,3 +822,5 @@ }); | ||
if (opts.min && opts.indent !== undefined && opts.indent !== 0) { | ||
throw new Error('prettyFormat: Cannot run with min option and indent'); | ||
throw new Error( | ||
'pretty-format: Options "min" and "indent" cannot be used together.'); | ||
} | ||
@@ -388,4 +832,7 @@ } | ||
Object.keys(DEFAULTS).forEach(key => | ||
result[key] = opts.hasOwnProperty(key) ? opts[key] : DEFAULTS[key]); | ||
Object.keys(DEFAULTS).forEach( | ||
key => | ||
result[key] = opts.hasOwnProperty(key) ? | ||
key === 'theme' ? normalizeTheme(opts.theme) : opts[key] : | ||
DEFAULTS[key]); | ||
@@ -397,5 +844,31 @@ | ||
// $FlowFixMe the type cast below means YOU are responsible to verify the code above. | ||
return result; | ||
} | ||
function normalizeTheme(themeOption) { | ||
if (!themeOption) { | ||
throw new Error(`pretty-format: Option "theme" must not be null.`); | ||
} | ||
if (typeof themeOption !== 'object') { | ||
throw new Error( | ||
`pretty-format: Option "theme" must be of type "object" but instead received "${typeof themeOption}".`); | ||
} | ||
// Silently ignore any keys in `theme` that are not in defaults. | ||
const themeRefined = themeOption; | ||
const themeDefaults = DEFAULTS.theme; | ||
return Object.keys(themeDefaults).reduce( | ||
(theme, key) => { | ||
theme[key] = Object.prototype.hasOwnProperty.call(themeOption, key) ? | ||
themeRefined[key] : | ||
themeDefaults[key]; | ||
return theme; | ||
}, | ||
{}); | ||
} | ||
function createIndent(indent) { | ||
@@ -405,8 +878,9 @@ return new Array(indent + 1).join(' '); | ||
function prettyFormat(val, opts) { | ||
if (!opts) { | ||
function prettyFormat(val, initialOptions) { | ||
let opts; | ||
if (!initialOptions) { | ||
opts = DEFAULTS; | ||
} else { | ||
validateOptions(opts); | ||
opts = normalizeOptions(opts); | ||
validateOptions(initialOptions); | ||
opts = normalizeOptions(initialOptions); | ||
} | ||
@@ -417,3 +891,12 @@ | ||
if (opts.highlight) { | ||
colors[key] = style[opts.theme[key]]; | ||
const color = colors[key] = style[opts.theme[key]]; | ||
if ( | ||
!color || | ||
typeof color.close !== 'string' || | ||
typeof color.open !== 'string') | ||
{ | ||
throw new Error( | ||
`pretty-format: Option "theme" has a key "${key}" whose value "${opts.theme[key]}" is undefined in ansi-styles.`); | ||
} | ||
} else { | ||
@@ -434,4 +917,19 @@ colors[key] = { close: '', open: '' }; | ||
refs = []; | ||
const pluginsResult = printPlugin(val, indent, prevIndent, spacing, edgeSpacing, refs, opts.maxDepth, currentDepth, opts.plugins, opts.min, opts.callToJSON, opts.printFunctionName, opts.escapeRegex, colors); | ||
if (pluginsResult) { | ||
const pluginsResult = printPlugin( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
opts.maxDepth, | ||
currentDepth, | ||
opts.plugins, | ||
opts.min, | ||
opts.callToJSON, | ||
opts.printFunctionName, | ||
opts.escapeRegex, | ||
colors); | ||
if (typeof pluginsResult === 'string') { | ||
return pluginsResult; | ||
@@ -441,4 +939,8 @@ } | ||
const basicResult = printBasicValue(val, opts.printFunctionName, opts.escapeRegex); | ||
if (basicResult) { | ||
const basicResult = printBasicValue( | ||
val, | ||
opts.printFunctionName, | ||
opts.escapeRegex); | ||
if (basicResult !== null) { | ||
return basicResult; | ||
@@ -453,5 +955,20 @@ } | ||
} | ||
return printComplexValue(val, indent, prevIndent, spacing, edgeSpacing, refs, opts.maxDepth, currentDepth, opts.plugins, opts.min, opts.callToJSON, opts.printFunctionName, opts.escapeRegex, colors); | ||
return printComplexValue( | ||
val, | ||
indent, | ||
prevIndent, | ||
spacing, | ||
edgeSpacing, | ||
refs, | ||
opts.maxDepth, | ||
currentDepth, | ||
opts.plugins, | ||
opts.min, | ||
opts.callToJSON, | ||
opts.printFunctionName, | ||
opts.escapeRegex, | ||
colors); | ||
} | ||
module.exports = prettyFormat; |
@@ -12,2 +12,4 @@ /** | ||
const asymmetricMatcher = Symbol.for('jest.asymmetricMatcher'); | ||
@@ -19,3 +21,3 @@ const SPACE = ' '; | ||
const printAsymmetricMatcher = ( | ||
const print = ( | ||
val, | ||
@@ -54,4 +56,4 @@ print, | ||
module.exports = { | ||
print: printAsymmetricMatcher, | ||
test: object => object && object.$$typeof === asymmetricMatcher }; | ||
const test = object => object && object.$$typeof === asymmetricMatcher; | ||
module.exports = { print, test }; |
@@ -11,4 +11,6 @@ /** | ||
const escapeHTML = require('./escapeHTML'); | ||
const escapeHTML = require('./lib/escapeHTML'); | ||
const reactElement = Symbol.for('react.element'); | ||
@@ -25,5 +27,6 @@ | ||
function printChildren(flatChildren, print, indent, colors, opts) { | ||
return flatChildren.map(node => { | ||
return flatChildren. | ||
map(node => { | ||
if (typeof node === 'object') { | ||
return printElement(node, print, indent, colors, opts); | ||
return print(node, print, indent, colors, opts); | ||
} else if (typeof node === 'string') { | ||
@@ -34,7 +37,10 @@ return colors.content.open + escapeHTML(node) + colors.content.close; | ||
} | ||
}).join(opts.edgeSpacing); | ||
}). | ||
join(opts.edgeSpacing); | ||
} | ||
function printProps(props, print, indent, colors, opts) { | ||
return Object.keys(props).sort().map(name => { | ||
return Object.keys(props). | ||
sort(). | ||
map(name => { | ||
if (name === 'children') { | ||
@@ -49,3 +55,5 @@ return ''; | ||
if (printed.indexOf('\n') !== -1) { | ||
printed = '{' + opts.edgeSpacing + indent(indent(printed) + opts.edgeSpacing + '}'); | ||
printed = '{' + | ||
opts.edgeSpacing + | ||
indent(indent(printed) + opts.edgeSpacing + '}'); | ||
} else { | ||
@@ -56,7 +64,18 @@ printed = '{' + printed + '}'; | ||
return opts.spacing + indent(colors.prop.open + name + colors.prop.close + '=') + colors.value.open + printed + colors.value.close; | ||
}).join(''); | ||
return opts.spacing + | ||
indent(colors.prop.open + name + colors.prop.close + '=') + | ||
colors.value.open + | ||
printed + | ||
colors.value.close; | ||
}). | ||
join(''); | ||
} | ||
function printElement(element, print, indent, colors, opts) { | ||
const print = ( | ||
element, | ||
print, | ||
indent, | ||
opts, | ||
colors) => | ||
{ | ||
let result = colors.tag.open + '<'; | ||
@@ -75,4 +94,4 @@ let elementName; | ||
const opaqueChildren = element.props.children; | ||
const hasProps = !!Object.keys(element.props). | ||
filter(propName => propName !== 'children'). | ||
const hasProps = !!Object.keys(element.props).filter( | ||
propName => propName !== 'children'). | ||
length; | ||
@@ -87,16 +106,26 @@ const closeInNewLine = hasProps && !opts.min; | ||
const children = printChildren(flatChildren, print, indent, colors, opts); | ||
result += colors.tag.open + (closeInNewLine ? '\n' : '') + '>' + colors.tag.close + opts.edgeSpacing + indent(children) + opts.edgeSpacing + colors.tag.open + '</' + elementName + '>' + colors.tag.close; | ||
result += colors.tag.open + ( | ||
closeInNewLine ? '\n' : '') + | ||
'>' + | ||
colors.tag.close + | ||
opts.edgeSpacing + | ||
indent(children) + | ||
opts.edgeSpacing + | ||
colors.tag.open + | ||
'</' + | ||
elementName + | ||
'>' + | ||
colors.tag.close; | ||
} else { | ||
result += colors.tag.open + (closeInNewLine ? '\n' : ' ') + '/>' + colors.tag.close; | ||
result += colors.tag.open + ( | ||
closeInNewLine ? '\n' : ' ') + | ||
'/>' + | ||
colors.tag.close; | ||
} | ||
return result; | ||
} | ||
}; | ||
module.exports = { | ||
print(val, print, indent, opts, colors) { | ||
return printElement(val, print, indent, colors, opts); | ||
}, | ||
test(object) { | ||
return object && object.$$typeof === reactElement; | ||
} }; | ||
const test = object => object && object.$$typeof === reactElement; | ||
module.exports = { print, test }; |
@@ -13,5 +13,3 @@ /** | ||
const escapeHTML = require('./escapeHTML'); | ||
const reactTestInstance = Symbol.for('react.test.json'); | ||
@@ -24,11 +22,24 @@ | ||
// Child can be `number` in Stack renderer but not in Fiber renderer. | ||
function printChildren(children, print, indent, colors, opts) { | ||
return children.map(child => printInstance(child, print, indent, colors, opts)).join(opts.edgeSpacing); | ||
const escapeHTML = require('./lib/escapeHTML'); | ||
const reactTestInstance = Symbol.for('react.test.json'); | ||
function printChildren( | ||
children, | ||
print, | ||
indent, | ||
colors, | ||
opts) | ||
{ | ||
return children. | ||
map(child => printInstance(child, print, indent, colors, opts)). | ||
join(opts.edgeSpacing); | ||
} | ||
function printProps(props, print, indent, colors, opts) { | ||
return Object.keys(props).sort().map(name => { | ||
return Object.keys(props). | ||
sort(). | ||
map(name => { | ||
const prop = props[name]; | ||
@@ -39,3 +50,5 @@ let printed = print(prop); | ||
if (printed.indexOf('\n') !== -1) { | ||
printed = '{' + opts.edgeSpacing + indent(indent(printed) + opts.edgeSpacing + '}'); | ||
printed = '{' + | ||
opts.edgeSpacing + | ||
indent(indent(printed) + opts.edgeSpacing + '}'); | ||
} else { | ||
@@ -46,4 +59,9 @@ printed = '{' + printed + '}'; | ||
return opts.spacing + indent(colors.prop.open + name + colors.prop.close + '=') + colors.value.open + printed + colors.value.close; | ||
}).join(''); | ||
return opts.spacing + | ||
indent(colors.prop.open + name + colors.prop.close + '=') + | ||
colors.value.open + | ||
printed + | ||
colors.value.close; | ||
}). | ||
join(''); | ||
} | ||
@@ -69,6 +87,26 @@ | ||
if (instance.children) { | ||
const children = printChildren(instance.children, print, indent, colors, opts); | ||
result += colors.tag.open + (closeInNewLine ? '\n' : '') + '>' + colors.tag.close + opts.edgeSpacing + indent(children) + opts.edgeSpacing + colors.tag.open + '</' + instance.type + '>' + colors.tag.close; | ||
const children = printChildren( | ||
instance.children, | ||
print, | ||
indent, | ||
colors, | ||
opts); | ||
result += colors.tag.open + ( | ||
closeInNewLine ? '\n' : '') + | ||
'>' + | ||
colors.tag.close + | ||
opts.edgeSpacing + | ||
indent(children) + | ||
opts.edgeSpacing + | ||
colors.tag.open + | ||
'</' + | ||
instance.type + | ||
'>' + | ||
colors.tag.close; | ||
} else { | ||
result += colors.tag.open + (closeInNewLine ? '\n' : ' ') + '/>' + colors.tag.close; | ||
result += colors.tag.open + ( | ||
closeInNewLine ? '\n' : ' ') + | ||
'/>' + | ||
colors.tag.close; | ||
} | ||
@@ -79,8 +117,13 @@ | ||
module.exports = { | ||
print(val, print, indent, opts, colors) { | ||
return printInstance(val, print, indent, colors, opts); | ||
}, | ||
test(object) { | ||
return object && object.$$typeof === reactTestInstance; | ||
} }; | ||
const print = ( | ||
val, | ||
print, | ||
indent, | ||
opts, | ||
colors) => | ||
printInstance(val, print, indent, colors, opts); | ||
const test = object => | ||
object && object.$$typeof === reactTestInstance; | ||
module.exports = { print, test }; |
{ | ||
"name": "pretty-format", | ||
"version": "19.0.0", | ||
"version": "19.1.0-alpha.eed82034", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
299370
21
2583
2