Skip to content

Commit

Permalink
url: add custom inspection for URLSearchParams and its iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyGu committed Jan 15, 2017
1 parent 36e8820 commit 87f6c3a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function getPunycode() {
}
}
const punycode = getPunycode();
const util = require('util');
const binding = process.binding('url');
const context = Symbol('context');
const cannotBeBase = Symbol('cannot-be-base');
Expand Down Expand Up @@ -859,6 +860,32 @@ class URLSearchParams {
// https://heycam.github.io/webidl/#es-iterable-entries
URLSearchParams.prototype[Symbol.iterator] = URLSearchParams.prototype.entries;

URLSearchParams.prototype[util.inspect.custom] =
function inspect(recurseTimes, ctx) {
const innerOpts = Object.assign({}, ctx);
if (recurseTimes !== null) {
innerOpts.depth = recurseTimes - 1;
}
const innerInspect = (v) => util.inspect(v, innerOpts);

const list = this[searchParams];
const output = [];
for (var i = 0; i < list.length; i += 2)
output.push(`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);

const colorRe = /\u001b\[\d\d?m/g;
const length = output.reduce((prev, cur) => {
return prev + cur.replace(colorRe, '').length + ', '.length;
}, -', '.length);
if (length > ctx.breakLength) {
return `${this.constructor.name} {\n ${output.join(',\n ')} }`;
} else if (output.length) {
return `${this.constructor.name} { ${output.join(', ')} }`;
} else {
return `${this.constructor.name} {}`;
}
};

// https://heycam.github.io/webidl/#dfn-default-iterator-object
function createSearchParamsIterator(target, kind) {
const iterator = Object.create(URLSearchParamsIteratorPrototype);
Expand Down Expand Up @@ -909,6 +936,37 @@ const URLSearchParamsIteratorPrototype = Object.setPrototypeOf({
value: result,
done: false
};
},
[util.inspect.custom]: function inspect(recurseTimes, ctx) {
const innerOpts = Object.assign({}, ctx);
if (recurseTimes !== null) {
innerOpts.depth = recurseTimes - 1;
}
const {
target,
kind,
index
} = this[context];
const output = target[searchParams].slice(index).reduce((prev, cur, i) => {
const key = i % 2 === 0;
if (kind === 'key' && key) {
prev.push(cur);
} else if (kind === 'value' && !key) {
prev.push(cur);
} else if (kind === 'key+value' && !key) {
prev.push([target[searchParams][index + i - 1], cur]);
}
return prev;
}, []);
const breakLn = util.inspect(output, innerOpts).includes('\n');
const outputStrs = output.map((p) => util.inspect(p, innerOpts));
let outputStr;
if (breakLn) {
outputStr = `\n ${outputStrs.join(',\n ')}`;
} else {
outputStr = ` ${outputStrs.join(', ')}`;
}
return `${this[Symbol.toStringTag]} {${outputStr} }`;
}
}, IteratorPrototype);

Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-inspect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

require('../common');
const assert = require('assert');
const util = require('util');
const URL = require('url').URL;

// Until we export URLSearchParams
const m = new URL('http://example.org');
const URLSearchParams = m.searchParams.constructor;

const sp = new URLSearchParams('?a=a&b=b&b=c');
assert.strictEqual(util.inspect(sp),
"URLSearchParams { 'a' => 'a', 'b' => 'b', 'b' => 'c' }");
assert.strictEqual(util.inspect(sp.keys()),
"URLSearchParamsIterator { 'a', 'b', 'b' }");
assert.strictEqual(util.inspect(sp.values()),
"URLSearchParamsIterator { 'a', 'b', 'c' }");

const iterator = sp.entries();
assert.strictEqual(util.inspect(iterator),
"URLSearchParamsIterator { [ 'a', 'a' ], [ 'b', 'b' ], " +
"[ 'b', 'c' ] }");
iterator.next();
assert.strictEqual(util.inspect(iterator),
"URLSearchParamsIterator { [ 'b', 'b' ], [ 'b', 'c' ] }");
iterator.next();
iterator.next();
assert.strictEqual(util.inspect(iterator),
'URLSearchParamsIterator { }');

0 comments on commit 87f6c3a

Please sign in to comment.