├── .npmignore ├── index.js ├── README.md ├── test └── inspectReactElement-test.js ├── package.json ├── .gitignore ├── LICENSE └── src └── inspectReactElement.js /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | test -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/inspectReactElement'); 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # inspect-react-element 2 | Prettyprints ReactElements and their children 3 | 4 | ```js 5 | var React = require('react'); 6 | var inspectReactElement = require('inspect-react-element'); 7 | 8 | console.log(inspectReactElement(React.createElement('div'))); // outputs '
' 9 | ``` 10 | -------------------------------------------------------------------------------- /test/inspectReactElement-test.js: -------------------------------------------------------------------------------- 1 | var tap = require('tap'); 2 | 3 | tap.test('inspectReactElement produces desired output', function (t) { 4 | t.plan(1); 5 | var inspectReactElement = require('../'); 6 | var React = require('react'); 7 | 8 | t.equal(inspectReactElement(React.createElement('div')), '
'); 9 | }); 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "inspect-react-element", 3 | "version": "1.1.1", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "prepublish": "babel src --out-dir lib", 8 | "test": "npm run prepublish && tap test" 9 | }, 10 | "author": "", 11 | "license": "MIT", 12 | "dependencies": { 13 | "indent-string": "^2.1.0", 14 | "lodash.omit": "^3.1.0", 15 | "react": ">=0.13.0" 16 | }, 17 | "devDependencies": { 18 | "babel": "^5.8.23", 19 | "tap": "^1.4.1" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | 29 | lib 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 James Friend 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/inspectReactElement.js: -------------------------------------------------------------------------------- 1 | import util from 'util'; 2 | import React from 'react'; 3 | import omit from 'lodash.omit'; 4 | import indentString from 'indent-string'; 5 | 6 | function inspectReactProp(propName, propValue) { 7 | if (typeof propValue == 'string') return `${propName}="${propValue}"`; 8 | return `${propName}={${util.inspect(propValue)}}`; 9 | } 10 | 11 | function inspectReactType(type) { 12 | if (!type) return '' + type; 13 | return typeof type == 'string' ? type : type.name || type.displayName; 14 | } 15 | 16 | function inspectReactNode(node, depth = 0) { 17 | if (!React.isValidElement(node)) { 18 | const childInspected = util.inspect(node); 19 | const childText = depth > 0 ? `{${childInspected}}` : childInspected; 20 | return childText; 21 | } 22 | 23 | const props = node.props || {}; 24 | const propNames = Object.keys(omit(props, 'children')); 25 | const propsText = propNames.length && propNames 26 | .map(propName => inspectReactProp(propName, props[propName])) 27 | .join(' '); 28 | 29 | let childrenText; 30 | if (props.children) { 31 | const childrenInspected = []; 32 | React.Children.forEach(props.children, (node) => childrenInspected.push(inspectReactNode(node, depth + 1))); 33 | childrenText = childrenInspected.join('\n'); 34 | } 35 | 36 | let nodeText = '<'; 37 | nodeText += inspectReactType(node.type); 38 | if (propsText) nodeText += ` ${propsText}`; 39 | 40 | if (childrenText) { 41 | nodeText += '>\n'; 42 | nodeText += indentString(childrenText, ' ', 1); 43 | nodeText += `\n`; 44 | } else { 45 | nodeText += ' />'; 46 | } 47 | return nodeText; 48 | } 49 | 50 | export default function inspectReactElement(element) { 51 | return inspectReactNode(element); 52 | } 53 | --------------------------------------------------------------------------------