GeezerVison is showing github.com — util: improve display of iterators and weak entries · nodejs/node@b171fa2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/console.js +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -360,8 +360,11 @@ Console.prototype.table = function(tabularData, properties) { | 360 | 360 | const mapIter = isMapIterator(tabularData); | 361 | 361 | let isKeyValue = false; | 362 | 362 | let i = 0; | 363 | | - if (mapIter) | 364 | | - [ tabularData, isKeyValue ] = previewEntries(tabularData); | | 363 | + if (mapIter) { | | 364 | + const res = previewEntries(tabularData, true); | | 365 | + tabularData = res[0]; | | 366 | + isKeyValue = res[1]; | | 367 | + } | 365 | 368 |
| 366 | 369 | if (isKeyValue || isMap(tabularData)) { | 367 | 370 | const keys = []; | @@ -391,7 +394,7 @@ Console.prototype.table = function(tabularData, properties) { | 391 | 394 |
| 392 | 395 | const setIter = isSetIterator(tabularData); | 393 | 396 | if (setIter) | 394 | | - [ tabularData ] = previewEntries(tabularData); | | 397 | + tabularData = previewEntries(tabularData); | 395 | 398 |
| 396 | 399 | const setlike = setIter || (mapIter && !isKeyValue) || isSet(tabularData); | 397 | 400 | if (setlike) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/util.js +54-44Lines changed: 54 additions & 44 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,6 +115,10 @@ const meta = [ | 115 | 115 | '', '', '', '', '', '', '', '', '', '', | 116 | 116 | '', '', '', '', '', '', '', '\\\\' | 117 | 117 | ]; | | 118 | +// Constants to map the iterator state. | | 119 | +const kWeak = 0; | | 120 | +const kIterator = 1; | | 121 | +const kMapEntries = 2; | 118 | 122 |
| 119 | 123 | const escapeFn = (str) => meta[str.charCodeAt(0)]; | 120 | 124 |
| @@ -982,77 +986,83 @@ function formatMap(ctx, value, recurseTimes, keys) { | 982 | 986 | return output; | 983 | 987 | } | 984 | 988 |
| 985 | | -function formatWeakSet(ctx, value, recurseTimes, keys) { | | 989 | +function formatSetIterInner(ctx, value, recurseTimes, keys, entries, state) { | 986 | 990 | const maxArrayLength = Math.max(ctx.maxArrayLength, 0); | 987 | | - const [ entries ] = previewEntries(value).slice(0, maxArrayLength + 1); | 988 | 991 | const maxLength = Math.min(maxArrayLength, entries.length); | 989 | 992 | let output = new Array(maxLength); | 990 | 993 | for (var i = 0; i < maxLength; ++i) | 991 | 994 | output[i] = formatValue(ctx, entries[i], recurseTimes); | 992 | | - // Sort all entries to have a halfway reliable output (if more entries than | 993 | | - // retrieved ones exist, we can not reliably return the same output). | 994 | | - output = output.sort(); | 995 | | - if (entries.length > maxArrayLength) | 996 | | - output.push('... more items'); | | 995 | + if (state === kWeak) { | | 996 | + // Sort all entries to have a halfway reliable output (if more entries than | | 997 | + // retrieved ones exist, we can not reliably return the same output). | | 998 | + output = output.sort(); | | 999 | + } | | 1000 | + const remaining = entries.length - maxLength; | | 1001 | + if (remaining > 0) { | | 1002 | + output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); | | 1003 | + } | 997 | 1004 | for (i = 0; i < keys.length; i++) | 998 | 1005 | output.push(formatProperty(ctx, value, recurseTimes, keys[i], 0)); | 999 | 1006 | return output; | 1000 | 1007 | } | 1001 | 1008 |
| 1002 | | -function formatWeakMap(ctx, value, recurseTimes, keys) { | | 1009 | +function formatMapIterInner(ctx, value, recurseTimes, keys, entries, state) { | 1003 | 1010 | const maxArrayLength = Math.max(ctx.maxArrayLength, 0); | 1004 | | - const [ entries ] = previewEntries(value).slice(0, (maxArrayLength + 1) * 2); | 1005 | 1011 | // Entries exist as [key1, val1, key2, val2, ...] | 1006 | | - const remainder = entries.length / 2 > maxArrayLength; | 1007 | | - const len = entries.length / 2 - (remainder ? 1 : 0); | | 1012 | + const len = entries.length / 2; | | 1013 | + const remaining = len - maxArrayLength; | 1008 | 1014 | const maxLength = Math.min(maxArrayLength, len); | 1009 | 1015 | let output = new Array(maxLength); | 1010 | | - for (var i = 0; i < maxLength; i++) { | | 1016 | + let start = ''; | | 1017 | + let end = ''; | | 1018 | + let middle = ' => '; | | 1019 | + let i = 0; | | 1020 | + if (state === kMapEntries) { | | 1021 | + start = '[ '; | | 1022 | + end = ' ]'; | | 1023 | + middle = ', '; | | 1024 | + } | | 1025 | + for (; i < maxLength; i++) { | 1011 | 1026 | const pos = i * 2; | 1012 | | - output[i] = `${formatValue(ctx, entries[pos], recurseTimes)} => ` + | 1013 | | - formatValue(ctx, entries[pos + 1], recurseTimes); | | 1027 | + output[i] = `${start}${formatValue(ctx, entries[pos], recurseTimes)}` + | | 1028 | + `${middle}${formatValue(ctx, entries[pos + 1], recurseTimes)}${end}`; | | 1029 | + } | | 1030 | + if (state === kWeak) { | | 1031 | + // Sort all entries to have a halfway reliable output (if more entries | | 1032 | + // than retrieved ones exist, we can not reliably return the same output). | | 1033 | + output = output.sort(); | | 1034 | + } | | 1035 | + if (remaining > 0) { | | 1036 | + output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); | 1014 | 1037 | } | 1015 | | - // Sort all entries to have a halfway reliable output (if more entries than | 1016 | | - // retrieved ones exist, we can not reliably return the same output). | 1017 | | - output = output.sort(); | 1018 | | - if (remainder > 0) | 1019 | | - output.push('... more items'); | 1020 | 1038 | for (i = 0; i < keys.length; i++) | 1021 | 1039 | output.push(formatProperty(ctx, value, recurseTimes, keys[i], 0)); | 1022 | 1040 | return output; | 1023 | 1041 | } | 1024 | 1042 |
| 1025 | | -function zip2(list) { | 1026 | | - const ret = Array(list.length / 2); | 1027 | | - for (var i = 0; i < ret.length; ++i) | 1028 | | - ret[i] = [list[2 * i], list[2 * i + 1]]; | 1029 | | - return ret; | | 1043 | +function formatWeakSet(ctx, value, recurseTimes, keys) { | | 1044 | + const entries = previewEntries(value); | | 1045 | + return formatSetIterInner(ctx, value, recurseTimes, keys, entries, kWeak); | 1030 | 1046 | } | 1031 | 1047 |
| 1032 | | -function formatCollectionIterator(ctx, value, recurseTimes, keys) { | 1033 | | - const output = []; | 1034 | | - var [ entries, isKeyValue ] = previewEntries(value); | 1035 | | - if (isKeyValue) | 1036 | | - entries = zip2(entries); | 1037 | | - for (const entry of entries) { | 1038 | | - if (ctx.maxArrayLength === output.length) { | 1039 | | - output.push('... more items'); | 1040 | | - break; | 1041 | | - } | 1042 | | - output.push(formatValue(ctx, entry, recurseTimes)); | 1043 | | - } | 1044 | | - for (var n = 0; n < keys.length; n++) { | 1045 | | - output.push(formatProperty(ctx, value, recurseTimes, keys[n], 0)); | 1046 | | - } | 1047 | | - return output; | | 1048 | +function formatWeakMap(ctx, value, recurseTimes, keys) { | | 1049 | + const entries = previewEntries(value); | | 1050 | + return formatMapIterInner(ctx, value, recurseTimes, keys, entries, kWeak); | 1048 | 1051 | } | 1049 | 1052 |
| 1050 | | -function formatMapIterator(ctx, value, recurseTimes, keys) { | 1051 | | - return formatCollectionIterator(ctx, value, recurseTimes, keys); | | 1053 | +function formatSetIterator(ctx, value, recurseTimes, keys) { | | 1054 | + const entries = previewEntries(value); | | 1055 | + return formatSetIterInner(ctx, value, recurseTimes, keys, entries, kIterator); | 1052 | 1056 | } | 1053 | 1057 |
| 1054 | | -function formatSetIterator(ctx, value, recurseTimes, keys) { | 1055 | | - return formatCollectionIterator(ctx, value, recurseTimes, keys); | | 1058 | +function formatMapIterator(ctx, value, recurseTimes, keys) { | | 1059 | + const [entries, isKeyValue] = previewEntries(value, true); | | 1060 | + if (isKeyValue) { | | 1061 | + return formatMapIterInner( | | 1062 | + ctx, value, recurseTimes, keys, entries, kMapEntries); | | 1063 | + } | | 1064 | + | | 1065 | + return formatSetIterInner(ctx, value, recurseTimes, keys, entries, kIterator); | 1056 | 1066 | } | 1057 | 1067 |
| 1058 | 1068 | function formatPromise(ctx, value, recurseTimes, keys) { | |
Collapse file Copy file name to clipboardExpand all lines: src/node_util.cc +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,6 +57,9 @@ static void PreviewEntries(const FunctionCallbackInfo<Value>& args) { | 57 | 57 | Local<Array> entries; | 58 | 58 | if (!args[0].As<Object>()->PreviewEntries(&is_key_value).ToLocal(&entries)) | 59 | 59 | return; | | 60 | + // Fast path for WeakMap, WeakSet and Set iterators. | | 61 | + if (args.Length() == 1) | | 62 | + return args.GetReturnValue().Set(entries); | 60 | 63 | Local<Array> ret = Array::New(env->isolate(), 2); | 61 | 64 | ret->Set(env->context(), 0, entries).FromJust(); | 62 | 65 | ret->Set(env->context(), 1, v8::Boolean::New(env->isolate(), is_key_value)) | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js +26-23Lines changed: 26 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -447,13 +447,15 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324'); | 447 | 447 | { | 448 | 448 | const map = new Map(); | 449 | 449 | map.set(1, 2); | 450 | | - const [ vals ] = previewEntries(map.entries()); | 451 | | - const valsOutput = []; | 452 | | - for (const o of vals) { | 453 | | - valsOutput.push(o); | 454 | | - } | 455 | | - | 456 | | - assert.strictEqual(util.inspect(valsOutput), '[ 1, 2 ]'); | | 450 | + // Passing only a single argument to indicate a set iterator. | | 451 | + const valsSetIterator = previewEntries(map.entries()); | | 452 | + // Passing through true to indicate a map iterator. | | 453 | + const valsMapIterEntries = previewEntries(map.entries(), true); | | 454 | + const valsMapIterKeys = previewEntries(map.keys(), true); | | 455 | + | | 456 | + assert.strictEqual(util.inspect(valsSetIterator), '[ 1, 2 ]'); | | 457 | + assert.strictEqual(util.inspect(valsMapIterEntries), '[ [ 1, 2 ], true ]'); | | 458 | + assert.strictEqual(util.inspect(valsMapIterKeys), '[ [ 1 ], false ]'); | 457 | 459 | } | 458 | 460 |
| 459 | 461 | // Test for other constructors in different context. | @@ -965,18 +967,19 @@ if (typeof Symbol !== 'undefined') { | 965 | 967 | // Test Map iterators. | 966 | 968 | { | 967 | 969 | const map = new Map([['foo', 'bar']]); | 968 | | - assert.strictEqual(util.inspect(map.keys()), "[Map Iterator] { 'foo' }"); | 969 | | - assert.strictEqual(util.inspect(map.values()), "[Map Iterator] { 'bar' }"); | 970 | | - assert.strictEqual(util.inspect(map.entries()), | 971 | | - "[Map Iterator] { [ 'foo', 'bar' ] }"); | | 970 | + assert.strictEqual(util.inspect(map.keys()), '[Map Iterator] { \'foo\' }'); | | 971 | + assert.strictEqual(util.inspect(map.values()), '[Map Iterator] { \'bar\' }'); | | 972 | + map.set('A', 'B!'); | | 973 | + assert.strictEqual(util.inspect(map.entries(), { maxArrayLength: 1 }), | | 974 | + "[Map Iterator] { [ 'foo', 'bar' ], ... 1 more item }"); | 972 | 975 | // Make sure the iterator doesn't get consumed. | 973 | 976 | const keys = map.keys(); | 974 | | - assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo' }"); | 975 | | - assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo' }"); | | 977 | + assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo', 'A' }"); | | 978 | + assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo', 'A' }"); | 976 | 979 | keys.extra = true; | 977 | 980 | assert.strictEqual( | 978 | 981 | util.inspect(keys, { maxArrayLength: 0 }), | 979 | | - '[Map Iterator] { ... more items, extra: true }'); | | 982 | + '[Map Iterator] { ... 2 more items, extra: true }'); | 980 | 983 | } | 981 | 984 |
| 982 | 985 | // Test Set iterators. | @@ -992,7 +995,7 @@ if (typeof Symbol !== 'undefined') { | 992 | 995 | keys.extra = true; | 993 | 996 | assert.strictEqual( | 994 | 997 | util.inspect(keys, { maxArrayLength: 1 }), | 995 | | - '[Set Iterator] { 1, ... more items, extra: true }'); | | 998 | + '[Set Iterator] { 1, ... 1 more item, extra: true }'); | 996 | 999 | } | 997 | 1000 |
| 998 | 1001 | // Test alignment of items in container. | @@ -1413,17 +1416,17 @@ util.inspect(process); | 1413 | 1416 | assert.strictEqual(out, expect); | 1414 | 1417 |
| 1415 | 1418 | out = util.inspect(weakMap, { maxArrayLength: 0, showHidden: true }); | 1416 | | - expect = 'WeakMap { ... more items }'; | | 1419 | + expect = 'WeakMap { ... 2 more items }'; | 1417 | 1420 | assert.strictEqual(out, expect); | 1418 | 1421 |
| 1419 | 1422 | weakMap.extra = true; | 1420 | 1423 | out = util.inspect(weakMap, { maxArrayLength: 1, showHidden: true }); | 1421 | 1424 | // It is not possible to determine the output reliable. | 1422 | | - expect = 'WeakMap { [ [length]: 0 ] => {}, ... more items, extra: true }'; | 1423 | | - const expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... more items, ' + | | 1425 | + expect = 'WeakMap { [ [length]: 0 ] => {}, ... 1 more item, extra: true }'; | | 1426 | + const expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... 1 more item, ' + | 1424 | 1427 | 'extra: true }'; | 1425 | 1428 | assert(out === expect || out === expectAlt, | 1426 | | - `Found "${out}" rather than "${expect}" or "${expectAlt}"`); | | 1429 | + `Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`); | 1427 | 1430 | } | 1428 | 1431 |
| 1429 | 1432 | { // Test WeakSet | @@ -1437,17 +1440,17 @@ util.inspect(process); | 1437 | 1440 | assert.strictEqual(out, expect); | 1438 | 1441 |
| 1439 | 1442 | out = util.inspect(weakSet, { maxArrayLength: -2, showHidden: true }); | 1440 | | - expect = 'WeakSet { ... more items }'; | | 1443 | + expect = 'WeakSet { ... 2 more items }'; | 1441 | 1444 | assert.strictEqual(out, expect); | 1442 | 1445 |
| 1443 | 1446 | weakSet.extra = true; | 1444 | 1447 | out = util.inspect(weakSet, { maxArrayLength: 1, showHidden: true }); | 1445 | 1448 | // It is not possible to determine the output reliable. | 1446 | | - expect = 'WeakSet { {}, ... more items, extra: true }'; | 1447 | | - const expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... more items, ' + | | 1449 | + expect = 'WeakSet { {}, ... 1 more item, extra: true }'; | | 1450 | + const expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... 1 more item, ' + | 1448 | 1451 | 'extra: true }'; | 1449 | 1452 | assert(out === expect || out === expectAlt, | 1450 | | - `Found "${out}" rather than "${expect}" or "${expectAlt}"`); | | 1453 | + `Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`); | 1451 | 1454 | } | 1452 | 1455 |
| 1453 | 1456 | { // Test argument objects. | |
|