Skip to content

Commit

Permalink
[New] add quoteStyle option
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 26, 2017
1 parent 99a008c commit f5a72d2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
22 changes: 16 additions & 6 deletions index.js
Expand Up @@ -13,6 +13,12 @@ var inspectCustom = require('./util.inspect').custom;
var inspectSymbol = (inspectCustom && isSymbol(inspectCustom)) ? inspectCustom : null;

module.exports = function inspect_ (obj, opts, depth, seen) {
if (!opts) opts = {};

if (has(opts, 'quoteStyle') && (opts.quoteStyle !== 'single' && opts.quoteStyle !== 'double')) {
throw new TypeError('option "quoteStyle" must be "single" or "double"');
}

if (typeof obj === 'undefined') {
return 'undefined';
}
Expand All @@ -22,8 +28,9 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
if (typeof obj === 'boolean') {
return obj ? 'true' : 'false';
}

if (typeof obj === 'string') {
return inspectString(obj);
return inspectString(obj, opts);
}
if (typeof obj === 'number') {
if (obj === 0) {
Expand All @@ -32,8 +39,6 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
return String(obj);
}

if (!opts) opts = {};

var maxDepth = typeof opts.depth === 'undefined' ? 5 : opts.depth;
if (typeof depth === 'undefined') depth = 0;
if (depth >= maxDepth && maxDepth > 0 && typeof obj === 'object') {
Expand Down Expand Up @@ -65,7 +70,7 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
var s = '<' + String(obj.nodeName).toLowerCase();
var attrs = obj.attributes || [];
for (var i = 0; i < attrs.length; i++) {
s += ' ' + attrs[i].name + '="' + quote(attrs[i].value) + '"';
s += ' ' + attrs[i].name + '=' + wrapQuotes(quote(attrs[i].value), 'double', opts);
}
s += '>';
if (obj.childNodes && obj.childNodes.length) s += '...';
Expand Down Expand Up @@ -119,6 +124,11 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
return String(obj);
};

function wrapQuotes (s, defaultStyle, opts) {
var quoteChar = (opts.quoteStyle || defaultStyle) === 'double' ? '"' : "'";
return quoteChar + s + quoteChar;
}

function quote (s) {
return String(s).replace(/"/g, '&quot;');
}
Expand Down Expand Up @@ -197,9 +207,9 @@ function isElement (x) {
;
}

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

function lowbyte (c) {
Expand Down
6 changes: 4 additions & 2 deletions readme.markdown
Expand Up @@ -43,8 +43,10 @@ var inspect = require('object-inspect')

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

Return a string `s` with the string representation of `obj` up to a depth of
`opts.depth`.
Return a string `s` with the string representation of `obj` up to a depth of `opts.depth`.

Additional options:
- `quoteStyle`: must be "single" or "double", if present

# install

Expand Down
4 changes: 3 additions & 1 deletion test/element.js
Expand Up @@ -2,7 +2,7 @@ var inspect = require('../');
var test = require('tape');

test('element', function (t) {
t.plan(1);
t.plan(3);
var elem = {
nodeName: 'div',
attributes: [ { name: 'class', value: 'row' } ],
Expand All @@ -11,6 +11,8 @@ test('element', function (t) {
};
var obj = [ 1, elem, 3 ];
t.deepEqual(inspect(obj), '[ 1, <div class="row"></div>, 3 ]');
t.deepEqual(inspect(obj, { quoteStyle: 'single' }), "[ 1, <div class='row'></div>, 3 ]");
t.deepEqual(inspect(obj, { quoteStyle: 'double' }), '[ 1, <div class="row"></div>, 3 ]');
});

test('element no attr', function (t) {
Expand Down
17 changes: 17 additions & 0 deletions test/quoteStyle.js
@@ -0,0 +1,17 @@
'use strict';

var inspect = require('../');
var test = require('tape');

test('quoteStyle option', function (t) {
t['throws'](function () { inspect(null, { quoteStyle: false }); }, 'false is not a valid value');
t['throws'](function () { inspect(null, { quoteStyle: true }); }, 'true is not a valid value');
t['throws'](function () { inspect(null, { quoteStyle: '' }); }, '"" is not a valid value');
t['throws'](function () { inspect(null, { quoteStyle: {} }); }, '{} is not a valid value');
t['throws'](function () { inspect(null, { quoteStyle: [] }); }, '[] is not a valid value');
t['throws'](function () { inspect(null, { quoteStyle: 42 }); }, '42 is not a valid value');
t['throws'](function () { inspect(null, { quoteStyle: NaN }); }, 'NaN is not a valid value');
t['throws'](function () { inspect(null, { quoteStyle: function () {} }); }, 'a function is not a valid value');

t.end();
});
4 changes: 4 additions & 0 deletions test/values.js
Expand Up @@ -107,7 +107,11 @@ test('Strings', function (t) {
var str = 'abc';

t.equal(inspect(str), "'" + str + "'", 'primitive string shows as such');
t.equal(inspect(str, { quoteStyle: 'single' }), "'" + str + "'", 'primitive string shows as such, single quoted');
t.equal(inspect(str, { quoteStyle: 'double' }), '"' + str + '"', 'primitive string shows as such, double quoted');
t.equal(inspect(Object(str)), 'Object(' + inspect(str) + ')', 'String object shows as such');
t.equal(inspect(Object(str), { quoteStyle: 'single' }), 'Object(' + inspect(str, { quoteStyle: 'single' }) + ')', 'String object shows as such, single quoted');
t.equal(inspect(Object(str), { quoteStyle: 'double' }), 'Object(' + inspect(str, { quoteStyle: 'double' }) + ')', 'String object shows as such, double quoted');

t.end();
});
Expand Down

0 comments on commit f5a72d2

Please sign in to comment.