Skip to content

Commit f5a72d2

Browse files
committed
[New] add quoteStyle option
1 parent 99a008c commit f5a72d2

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ var inspectCustom = require('./util.inspect').custom;
1313
var inspectSymbol = (inspectCustom && isSymbol(inspectCustom)) ? inspectCustom : null;
1414

1515
module.exports = function inspect_ (obj, opts, depth, seen) {
16+
if (!opts) opts = {};
17+
18+
if (has(opts, 'quoteStyle') && (opts.quoteStyle !== 'single' && opts.quoteStyle !== 'double')) {
19+
throw new TypeError('option "quoteStyle" must be "single" or "double"');
20+
}
21+
1622
if (typeof obj === 'undefined') {
1723
return 'undefined';
1824
}
@@ -22,8 +28,9 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
2228
if (typeof obj === 'boolean') {
2329
return obj ? 'true' : 'false';
2430
}
31+
2532
if (typeof obj === 'string') {
26-
return inspectString(obj);
33+
return inspectString(obj, opts);
2734
}
2835
if (typeof obj === 'number') {
2936
if (obj === 0) {
@@ -32,8 +39,6 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
3239
return String(obj);
3340
}
3441

35-
if (!opts) opts = {};
36-
3742
var maxDepth = typeof opts.depth === 'undefined' ? 5 : opts.depth;
3843
if (typeof depth === 'undefined') depth = 0;
3944
if (depth >= maxDepth && maxDepth > 0 && typeof obj === 'object') {
@@ -65,7 +70,7 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
6570
var s = '<' + String(obj.nodeName).toLowerCase();
6671
var attrs = obj.attributes || [];
6772
for (var i = 0; i < attrs.length; i++) {
68-
s += ' ' + attrs[i].name + '="' + quote(attrs[i].value) + '"';
73+
s += ' ' + attrs[i].name + '=' + wrapQuotes(quote(attrs[i].value), 'double', opts);
6974
}
7075
s += '>';
7176
if (obj.childNodes && obj.childNodes.length) s += '...';
@@ -119,6 +124,11 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
119124
return String(obj);
120125
};
121126

127+
function wrapQuotes (s, defaultStyle, opts) {
128+
var quoteChar = (opts.quoteStyle || defaultStyle) === 'double' ? '"' : "'";
129+
return quoteChar + s + quoteChar;
130+
}
131+
122132
function quote (s) {
123133
return String(s).replace(/"/g, '&quot;');
124134
}
@@ -197,9 +207,9 @@ function isElement (x) {
197207
;
198208
}
199209

200-
function inspectString (str) {
210+
function inspectString (str, opts) {
201211
var s = str.replace(/(['\\])/g, '\\$1').replace(/[\x00-\x1f]/g, lowbyte);
202-
return "'" + s + "'";
212+
return wrapQuotes(s, 'single', opts);
203213
}
204214

205215
function lowbyte (c) {

readme.markdown

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ var inspect = require('object-inspect')
4343

4444
## var s = inspect(obj, opts={})
4545

46-
Return a string `s` with the string representation of `obj` up to a depth of
47-
`opts.depth`.
46+
Return a string `s` with the string representation of `obj` up to a depth of `opts.depth`.
47+
48+
Additional options:
49+
- `quoteStyle`: must be "single" or "double", if present
4850

4951
# install
5052

test/element.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var inspect = require('../');
22
var test = require('tape');
33

44
test('element', function (t) {
5-
t.plan(1);
5+
t.plan(3);
66
var elem = {
77
nodeName: 'div',
88
attributes: [ { name: 'class', value: 'row' } ],
@@ -11,6 +11,8 @@ test('element', function (t) {
1111
};
1212
var obj = [ 1, elem, 3 ];
1313
t.deepEqual(inspect(obj), '[ 1, <div class="row"></div>, 3 ]');
14+
t.deepEqual(inspect(obj, { quoteStyle: 'single' }), "[ 1, <div class='row'></div>, 3 ]");
15+
t.deepEqual(inspect(obj, { quoteStyle: 'double' }), '[ 1, <div class="row"></div>, 3 ]');
1416
});
1517

1618
test('element no attr', function (t) {

test/quoteStyle.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var inspect = require('../');
4+
var test = require('tape');
5+
6+
test('quoteStyle option', function (t) {
7+
t['throws'](function () { inspect(null, { quoteStyle: false }); }, 'false is not a valid value');
8+
t['throws'](function () { inspect(null, { quoteStyle: true }); }, 'true is not a valid value');
9+
t['throws'](function () { inspect(null, { quoteStyle: '' }); }, '"" is not a valid value');
10+
t['throws'](function () { inspect(null, { quoteStyle: {} }); }, '{} is not a valid value');
11+
t['throws'](function () { inspect(null, { quoteStyle: [] }); }, '[] is not a valid value');
12+
t['throws'](function () { inspect(null, { quoteStyle: 42 }); }, '42 is not a valid value');
13+
t['throws'](function () { inspect(null, { quoteStyle: NaN }); }, 'NaN is not a valid value');
14+
t['throws'](function () { inspect(null, { quoteStyle: function () {} }); }, 'a function is not a valid value');
15+
16+
t.end();
17+
});

test/values.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ test('Strings', function (t) {
107107
var str = 'abc';
108108

109109
t.equal(inspect(str), "'" + str + "'", 'primitive string shows as such');
110+
t.equal(inspect(str, { quoteStyle: 'single' }), "'" + str + "'", 'primitive string shows as such, single quoted');
111+
t.equal(inspect(str, { quoteStyle: 'double' }), '"' + str + '"', 'primitive string shows as such, double quoted');
110112
t.equal(inspect(Object(str)), 'Object(' + inspect(str) + ')', 'String object shows as such');
113+
t.equal(inspect(Object(str), { quoteStyle: 'single' }), 'Object(' + inspect(str, { quoteStyle: 'single' }) + ')', 'String object shows as such, single quoted');
114+
t.equal(inspect(Object(str), { quoteStyle: 'double' }), 'Object(' + inspect(str, { quoteStyle: 'double' }) + ')', 'String object shows as such, double quoted');
111115

112116
t.end();
113117
});

0 commit comments

Comments
 (0)