pretty-format
Advanced tools
Comparing version 3.0.0 to 3.1.0
26
index.js
@@ -167,3 +167,25 @@ var isArguments = require('lodash/isArguments'); | ||
function printWithPlugin(plugin, val, refs, opts, state) { | ||
function boundPrint(val) { | ||
return print(val, refs, opts, state); | ||
} | ||
function boundIndent(val) { | ||
return indent(val, opts); | ||
} | ||
return plugin.print(val, boundPrint, boundIndent); | ||
} | ||
function printValue(val, refs, opts, state) { | ||
var plugins = opts.plugins; | ||
for (var p = 0; p < plugins.length; p++) { | ||
var plugin = plugins[p]; | ||
if (plugin.test(val)) { | ||
return printWithPlugin(plugin, val, refs, opts, state); | ||
} | ||
} | ||
// Simple values | ||
@@ -212,3 +234,4 @@ if ( isBoolean (val) ) return Boolean.prototype.toString.call(val); | ||
indent: 2, | ||
maxDepth: Infinity | ||
maxDepth: Infinity, | ||
plugins: [] | ||
}; | ||
@@ -238,2 +261,3 @@ | ||
opts = normalizeOptions(opts) | ||
plugins = opts.plugins; | ||
@@ -240,0 +264,0 @@ return print(val, [], opts, { |
{ | ||
"name": "pretty-format", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Stringify any JavaScript value.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -48,1 +48,25 @@ # pretty-format [![Travis build status](http://img.shields.io/travis/thejameskyle/pretty-format.svg?style=flat)](https://travis-ci.org/thejameskyle/pretty-format) | ||
``` | ||
### Plugins | ||
Pretty format also supports adding plugins: | ||
```js | ||
var fooPlugin = { | ||
test: function(val) { | ||
return val && val.hasOwnProperty('foo'); | ||
}, | ||
print: function(val, print, indent) { | ||
return 'Foo: ' + print(val.foo); | ||
} | ||
}; | ||
var obj = { foo: { bar: {} } }; | ||
prettyFormat(obj, { | ||
plugins: [fooPlugin] | ||
}); | ||
// Foo: Object { | ||
// "bar": Object {} | ||
// } | ||
``` |
15
test.js
@@ -228,2 +228,17 @@ var assert = require('assert'); | ||
}); | ||
it('should support plugins', function() { | ||
function Foo() {}; | ||
assert.equal(prettyFormat(new Foo(), { | ||
plugins: [{ | ||
test: function(object) { | ||
return object.constructor.name === 'Foo'; | ||
}, | ||
print: function() { | ||
return 'class Foo' | ||
} | ||
}] | ||
}), 'class Foo'); | ||
}) | ||
}); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16782
404
72
1