From f5a72d26edb3959b048f74c056ca7100a6b091e4 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 25 Dec 2017 22:56:23 -0800 Subject: [PATCH] [New] add `quoteStyle` option --- index.js | 22 ++++++++++++++++------ readme.markdown | 6 ++++-- test/element.js | 4 +++- test/quoteStyle.js | 17 +++++++++++++++++ test/values.js | 4 ++++ 5 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 test/quoteStyle.js diff --git a/index.js b/index.js index 137905f..ae78858 100644 --- a/index.js +++ b/index.js @@ -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'; } @@ -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) { @@ -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') { @@ -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 += '...'; @@ -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, '"'); } @@ -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) { diff --git a/readme.markdown b/readme.markdown index 41959a4..744eeb5 100644 --- a/readme.markdown +++ b/readme.markdown @@ -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 diff --git a/test/element.js b/test/element.js index 66df4b8..a36f465 100644 --- a/test/element.js +++ b/test/element.js @@ -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' } ], @@ -11,6 +11,8 @@ test('element', function (t) { }; var obj = [ 1, elem, 3 ]; t.deepEqual(inspect(obj), '[ 1,
, 3 ]'); + t.deepEqual(inspect(obj, { quoteStyle: 'single' }), "[ 1,
, 3 ]"); + t.deepEqual(inspect(obj, { quoteStyle: 'double' }), '[ 1,
, 3 ]'); }); test('element no attr', function (t) { diff --git a/test/quoteStyle.js b/test/quoteStyle.js new file mode 100644 index 0000000..ae4d734 --- /dev/null +++ b/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(); +}); diff --git a/test/values.js b/test/values.js index 1fcd998..3e1954b 100644 --- a/test/values.js +++ b/test/values.js @@ -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(); });