pretty-format2
Advanced tools
Comparing version 1.2.0 to 2.0.0
61
index.js
@@ -15,7 +15,5 @@ // @flow | ||
export type PluginFunction = (value: mixed, stack: Stack, env: Env, refs: Refs) => void; | ||
export type PluginObject = { | ||
export type Plugin = { | ||
test(value: mixed): boolean, | ||
print( | ||
printOptimized?: ( | ||
val: any, | ||
@@ -26,7 +24,12 @@ serialize: mixed => string, | ||
colors: Colors, | ||
): string, | ||
) => string, | ||
print?: ( | ||
val: any, | ||
serialize: mixed => string, | ||
indent: string => string, | ||
opts: Options, | ||
colors: Colors, | ||
) => string, | ||
}; | ||
export type Plugin = PluginFunction | PluginObject; | ||
export type Plugins = Array<Plugin>; | ||
@@ -439,24 +442,5 @@ | ||
function printPlugin(value, stack, env, refs, depth) { | ||
let plugins = env.opts.plugins; | ||
function printLegacyPlugin(plugin, value, env, refs, depth) { | ||
let colors = env.colors; | ||
let plugin; | ||
for (let p = 0; p < plugins.length; p++) { | ||
let current = plugins[p]; | ||
if (typeof current === 'function') { | ||
let result = current(value, stack, env, refs); | ||
if (result) return current; | ||
} else { | ||
if (plugins[p].test(value)) { | ||
plugin = plugins[p]; | ||
break; | ||
} | ||
} | ||
} | ||
if (!plugin) { | ||
return null; | ||
} | ||
function print(value) { | ||
@@ -480,2 +464,25 @@ return printStack(value, depth + 1, refs, env); | ||
function printPlugin(value, stack, env, refs, depth) { | ||
let plugins = env.opts.plugins; | ||
let plugin; | ||
for (let p = 0; p < plugins.length; p++) { | ||
let current = plugins[p]; | ||
if (current.test(value)) { | ||
plugin = current; | ||
break; | ||
} | ||
} | ||
if (!plugin) return null; | ||
if (plugin.printOptimized) { | ||
return plugin.printOptimized(value, stack, env, refs); | ||
} else if (plugin.print) { | ||
return printLegacyPlugin(plugin, value, env, refs, depth); | ||
} else { | ||
throw new Error('Plugin must have either printOptimized() or print() method'); | ||
} | ||
} | ||
function printValue(value, stack, env, refs, depth) { | ||
@@ -482,0 +489,0 @@ if (env.opts.plugins.length) { |
{ | ||
"name": "pretty-format2", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Stringify any value. Better, faster, prettier.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
18307
581