Skip to content

Commit 0479a4d

Browse files
committed
Add support for list of nodes, non-nodes
1 parent 9e03d45 commit 0479a4d

File tree

4 files changed

+97
-10
lines changed

4 files changed

+97
-10
lines changed

index.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,31 @@ function formatNode(node) {
113113
* @return {string}
114114
*/
115115
function inspect(node, pad) {
116-
var result = [formatNode(node)];
117-
var children = node.children;
118-
var index = -1;
119-
var length = children && children.length;
116+
var result;
117+
var children;
118+
var index;
119+
var length;
120+
121+
if (node && node.length && typeof node !== 'string') {
122+
length = node.length;
123+
index = -1;
124+
result = [];
125+
126+
while (++index < length) {
127+
result[index] = inspect(node[index]);
128+
}
129+
130+
return result.join('\n');
131+
}
132+
133+
if (!node || !node.type) {
134+
return String(node);
135+
}
136+
137+
result = [formatNode(node)];
138+
children = node.children;
139+
length = children && children.length;
140+
index = -1;
120141

121142
if (!length) {
122143
return result[0];

test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module unist:util:inspect
6+
* @fileoverview Test suite for `unist-util-inspect`.
7+
*/
8+
19
'use strict';
210

3-
/* eslint-env mocha */
11+
/* eslint-env node, mocha */
412

513
/*
614
* Module dependencies.
@@ -101,6 +109,33 @@ describe('inspect()', function () {
101109
);
102110
});
103111

112+
it('should work with a list of nodes', function () {
113+
equal(strip(inspect([
114+
{
115+
'type': 'SymbolNode',
116+
'value': '$'
117+
},
118+
{
119+
'type': 'WordNode',
120+
'children': [{
121+
'type': 'text',
122+
'value': '5,00'
123+
}]
124+
}
125+
])), [
126+
'SymbolNode: \'$\'',
127+
'WordNode[1]',
128+
'└─ text: \'5,00\''
129+
].join('\n'));
130+
});
131+
132+
it('should work on non-nodes', function () {
133+
equal(strip(inspect('foo')), 'foo');
134+
equal(strip(inspect('null')), 'null');
135+
equal(strip(inspect(NaN)), 'NaN');
136+
equal(strip(inspect(3)), '3');
137+
});
138+
104139
it('should work with data attributes', function () {
105140
equal(strip(inspect({
106141
'type': 'SymbolNode',

unist-util-inspect.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.unistUtilInspect = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2+
/**
3+
* @author Titus Wormer
4+
* @copyright 2015 Titus Wormer
5+
* @license MIT
6+
* @module unist:util:inspect
7+
* @fileoverview Unist node inspector.
8+
*/
9+
210
'use strict';
311

12+
/* eslint-env commonjs */
13+
414
/*
515
* Dependencies.
616
*/
@@ -104,10 +114,31 @@ function formatNode(node) {
104114
* @return {string}
105115
*/
106116
function inspect(node, pad) {
107-
var result = [formatNode(node)];
108-
var children = node.children;
109-
var index = -1;
110-
var length = children && children.length;
117+
var result;
118+
var children;
119+
var index;
120+
var length;
121+
122+
if (node && node.length && typeof node !== 'string') {
123+
length = node.length;
124+
index = -1;
125+
result = [];
126+
127+
while (++index < length) {
128+
result[index] = inspect(node[index]);
129+
}
130+
131+
return result.join('\n');
132+
}
133+
134+
if (!node || !node.type) {
135+
return String(node);
136+
}
137+
138+
result = [formatNode(node)];
139+
children = node.children;
140+
length = children && children.length;
141+
index = -1;
111142

112143
if (!length) {
113144
return result[0];

unist-util-inspect.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)