Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pretty-format

Package Overview
Dependencies
Maintainers
2
Versions
237
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pretty-format - npm Package Compare versions

Comparing version 3.2.0 to 3.3.0

15

index.js

@@ -54,2 +54,10 @@ var isArguments = require('lodash/isArguments');

function printFunction(val) {
if (val.name === '') {
return '[Function anonymous]'
} else {
return '[Function ' + val.name + ']';
}
}
function printList(list, refs, opts, state) {

@@ -173,5 +181,4 @@ var body = '';

function boundIndent(val, options) {
options = options || opts;
return indent(val, options);
function boundIndent(val) {
return indent(val, opts);
}

@@ -197,3 +204,3 @@

if ( isError (val) ) return '[' + Error.prototype.toString.call(val) + ']';
if ( isFunction (val) ) return Function.prototype.toString.call(val);
if ( isFunction (val) ) return printFunction(val);
if ( isInfinity (val) ) return Infinity.toString.call(val);

@@ -200,0 +207,0 @@ if ( isNaN (val) ) return 'NaN';

{
"name": "pretty-format",
"version": "3.2.0",
"version": "3.3.0",
"description": "Stringify any JavaScript value.",

@@ -13,3 +13,3 @@ "license": "MIT",

"scripts": {
"test": "mocha test.js test-plugins-ReactTestComponent.js"
"test": "mocha test.js"
},

@@ -16,0 +16,0 @@ "devDependencies": {

var reactTestInstance = Symbol.for('react.test.json');
function handleChildren(children, indentation, print, indent) {
var result = '';
if (children) {
children.forEach(function(child) {
result += '\n' + objectToJSX(child, indentation, print, indent);
});
result += '\n';
}
return result;
function printChildren(children, print, indent) {
return children.map(function(child) {
return printElement(child, print, indent);
}).join('\n');
}
function handleProps(node, indentation, print, indent) {
var props = node.props;
var result = '';
if (props) {
var indentOpts = {
indent: 2 * (indentation + 1)
};
Object.keys(props).forEach(function(prop) {
result += '\n' + indent(prop, indentOpts) + '=';
var value = props[prop];
if (typeof value === 'string') {
result += '"' + value + '"';
} else {
var formatted = indent(print(value), {indent: 2 * (indentation + 2)});
result += '{\n' + formatted + '\n' + indent('}', indentOpts);
}
});
}
return result;
function printProps(props, print, indent) {
return Object.keys(props).map(function(name) {
var prop = props[name];
var printed = print(prop);
if (typeof prop !== 'string') {
printed = '{\n' + indent(indent(printed) + '\n}');
}
return '\n' + indent(name + '=') + printed;
}).join('');
}
function objectToJSX(root, indentation, print, indent) {
indentation = indentation || 0;
var indentationOpts = {indent: indentation * 2};
var type = root.type;
if (!type && typeof root === 'string'){
return indent(root, indentationOpts);
function printElement(element, print, indent) {
if (typeof element === 'string') {
return element;
}
var result = '';
if (!root.children) {
result += indent('<', indentationOpts) + type;
result += handleProps(root, indentation, print, indent);
var result = '<' + element.type;
if (element.props) {
result += printProps(element.props, print, indent);
}
if (element.children) {
var children = printChildren(element.children, print, indent);
result += '>\n' + indent(children) + '\n</' + element.type + '>';
} else {
result += ' />';
} else {
result += indent('<', indentationOpts) + type;
result += handleProps(root, indentation, print, indent);
result += '>';
result += handleChildren(root.children, indentation + 1, print, indent);
result += indent('</', indentationOpts) + type + '>';
}
return result;

@@ -58,8 +44,8 @@ }

module.exports = {
test: function(object){
test: function(object) {
return object && object.$$typeof === reactTestInstance;
},
print: function(val, print, indent){
return objectToJSX(val, 0, print, indent);
print: function(val, print, indent) {
return printElement(val, print, indent);
}
};

@@ -72,1 +72,20 @@ # pretty-format [![Travis build status](http://img.shields.io/travis/thejameskyle/pretty-format.svg?style=flat)](https://travis-ci.org/thejameskyle/pretty-format)

```
#### ReactTestComponent plugin
```js
var prettyFormat = require('pretty-format');
var reactPlugin = require('pretty-format/plugins/ReactTestComponent');
var React = require('react');
var renderer = require('react/lib/ReactTestRenderer');
var jsx = React.createElement('h1', null, 'Hello World');
prettyFormat(renderer.create(jsx).toJSON(), {
plugins: [reactPlugin]
});
// <h1>
// Hello World
// </h1>
```
var assert = require('assert');
var prettyFormat = require('./');
var React = require('react');
var ReactTestComponent = require('./plugins/ReactTestComponent');
var renderer = require('react/lib/ReactTestRenderer');
function returnArguments() {

@@ -8,2 +12,11 @@ return arguments;

function assertPrintedJSX(actual, expected) {
assert.equal(
prettyFormat(renderer.create(actual).toJSON(), {
plugins: [ReactTestComponent]
}),
expected
);
}
describe('prettyFormat()', function() {

@@ -67,3 +80,3 @@ it('should print empty arguments', function() {

var val = new Function();
assert.equal(prettyFormat(val), 'function anonymous() {\n\n}');
assert.equal(prettyFormat(val), '[Function anonymous]');
});

@@ -73,3 +86,3 @@

var val = function() {};
assert.equal(prettyFormat(val), 'function () {}');
assert.equal(prettyFormat(val), '[Function anonymous]');
});

@@ -79,3 +92,3 @@

var val = function named() {};
assert.equal(prettyFormat(val), 'function named() {}');
assert.equal(prettyFormat(val), '[Function named]');
});

@@ -246,3 +259,86 @@

}), 'class Foo');
})
});
describe('ReactTestComponent plugin', function() {
var Mouse = React.createClass({
getInitialState: function() {
return { mouse: 'mouse' };
},
handleMoose: function() {
this.setState({ mouse: 'moose' });
},
render: function() {
return React.createElement('div', null, this.state.mouse);
}
});
it('should support a single element with no props or children', function() {
assertPrintedJSX(
React.createElement('Mouse'),
'<Mouse />'
);
});
it('should support a single element with no props or children', function() {
assertPrintedJSX(
React.createElement('Mouse', null, 'Hello World'),
'<Mouse>\n Hello World\n</Mouse>'
);
});
it('should support props with strings', function() {
assertPrintedJSX(
React.createElement('Mouse', { style: 'color:red' }),
'<Mouse\n style="color:red" />'
);
});
it('should support a single element with a function prop', function() {
assertPrintedJSX(
React.createElement('Mouse', { onclick: function onclick(){} }),
'<Mouse\n onclick={\n [Function onclick]\n } />'
);
});
it('should support a single element with a object prop', function() {
assertPrintedJSX(
React.createElement('Mouse', { customProp: { one: '1', two: 2 } }),
'<Mouse\n customProp={\n Object {\n "one": "1",\n "two": 2\n }\n } />'
);
});
it('should support an element with and object prop and children', function() {
assertPrintedJSX(
React.createElement('Mouse', { customProp: { one: '1', two: 2 } },
React.createElement('Mouse')
),
'<Mouse\n customProp={\n Object {\n "one": "1",\n "two": 2\n }\n }>\n <Mouse />\n</Mouse>'
);
});
it('should support an element with complex props and mixed children', function() {
assertPrintedJSX(
React.createElement('Mouse', { customProp: { one: '1', two: 2 }, onclick: function(){} },
'HELLO',
React.createElement('Mouse'), 'CIAO'
),
'<Mouse\n customProp={\n Object {\n "one": "1",\n "two": 2\n }\n }\n onclick={\n [Function anonymous]\n }>\n HELLO\n <Mouse />\n CIAO\n</Mouse>'
);
});
it('should support everything all together', function() {
assertPrintedJSX(
React.createElement('Mouse', { customProp: { one: '1', two: 2 }, onclick: function(){} },
'HELLO',
React.createElement('Mouse', { customProp: { one: '1', two: 2 }, onclick: function(){} },
'HELLO',
React.createElement('Mouse'),
'CIAO'
),
'CIAO'
),
'<Mouse\n customProp={\n Object {\n "one": "1",\n "two": 2\n }\n }\n onclick={\n [Function anonymous]\n }>\n HELLO\n <Mouse\n customProp={\n Object {\n "one": "1",\n "two": 2\n }\n }\n onclick={\n [Function anonymous]\n }>\n HELLO\n <Mouse />\n CIAO\n </Mouse>\n CIAO\n</Mouse>'
);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc