| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fcf059a commit 5b14066
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -462,13 +462,6 @@ function getPrefix(constructor, tag, fallback) { | |||
| 462 | 462 | return ''; | |
| 463 | 463 | } | |
| 464 | 464 | ||
| 465 | - function addExtraKeys(source, target, keys) { | ||
| 466 | - for (const key of keys) { | ||
| 467 | - target[key] = source[key]; | ||
| 468 | - } | ||
| 469 | - return target; | ||
| 470 | - } | ||
| 471 | - | ||
| 472 | 465 | function findTypedConstructor(value) { | |
| 473 | 466 | for (const [check, clazz] of [ | |
| 474 | 467 | [isUint8Array, Uint8Array], | |
@@ -484,14 +477,36 @@ function findTypedConstructor(value) { | |||
| 484 | 477 | [isBigUint64Array, BigUint64Array] | |
| 485 | 478 | ]) { | |
| 486 | 479 | if (check(value)) { | |
| 487 | - return new clazz(value); | ||
| 480 | + return clazz; | ||
| 488 | 481 | } | |
| 489 | 482 | } | |
| 490 | - return value; | ||
| 491 | 483 | } | |
| 492 | 484 | ||
| 493 | 485 | const getBoxedValue = formatPrimitive.bind(null, stylizeNoColor); | |
| 494 | 486 | ||
| 487 | + function noPrototypeIterator(ctx, value, recurseTimes) { | ||
| 488 | + let newVal; | ||
| 489 | + // TODO: Create a Subclass in case there's no prototype and show | ||
| 490 | + // `null-prototype`. | ||
| 491 | + if (isSet(value)) { | ||
| 492 | + const clazz = Object.getPrototypeOf(value) || Set; | ||
| 493 | + newVal = new clazz(setValues(value)); | ||
| 494 | + } else if (isMap(value)) { | ||
| 495 | + const clazz = Object.getPrototypeOf(value) || Map; | ||
| 496 | + newVal = new clazz(mapEntries(value)); | ||
| 497 | + } else if (Array.isArray(value)) { | ||
| 498 | + const clazz = Object.getPrototypeOf(value) || Array; | ||
| 499 | + newVal = new clazz(value.length || 0); | ||
| 500 | + } else if (isTypedArray(value)) { | ||
| 501 | + const clazz = findTypedConstructor(value) || Uint8Array; | ||
| 502 | + newVal = new clazz(value); | ||
| 503 | + } | ||
| 504 | + if (newVal) { | ||
| 505 | + Object.defineProperties(newVal, Object.getOwnPropertyDescriptors(value)); | ||
| 506 | + return formatValue(ctx, newVal, recurseTimes); | ||
| 507 | + } | ||
| 508 | + } | ||
| 509 | + | ||
| 495 | 510 | // Note: using `formatValue` directly requires the indentation level to be | |
| 496 | 511 | // corrected by setting `ctx.indentationLvL += diff` and then to decrease the | |
| 497 | 512 | // value afterwards again. | |
@@ -757,39 +772,25 @@ function formatValue(ctx, value, recurseTimes) { | |||
| 757 | 772 | braces = ['{', '}']; | |
| 758 | 773 | // The input prototype got manipulated. Special handle these. | |
| 759 | 774 | // We have to rebuild the information so we are able to display everything. | |
| 760 | - } else if (isSet(value)) { | ||
| 761 | - const newVal = addExtraKeys(value, new Set(setValues(value)), keys); | ||
| 762 | - return formatValue(ctx, newVal, recurseTimes); | ||
| 763 | - } else if (isMap(value)) { | ||
| 764 | - const newVal = addExtraKeys(value, new Map(mapEntries(value)), keys); | ||
| 765 | - return formatValue(ctx, newVal, recurseTimes); | ||
| 766 | - } else if (Array.isArray(value)) { | ||
| 767 | - // The prefix is not always possible to fully reconstruct. | ||
| 768 | - const prefix = getPrefix(constructor, tag); | ||
| 769 | - braces = [`${prefix === 'Array ' ? '' : prefix}[`, ']']; | ||
| 770 | - formatter = formatArray; | ||
| 771 | - const newValue = []; | ||
| 772 | - newValue.length = value.length; | ||
| 773 | - value = addExtraKeys(value, newValue, keys); | ||
| 774 | - } else if (isTypedArray(value)) { | ||
| 775 | - const newValue = findTypedConstructor(value); | ||
| 776 | - value = addExtraKeys(value, newValue, keys.slice(newValue.length)); | ||
| 777 | - // The prefix is not always possible to fully reconstruct. | ||
| 778 | - braces = [`${getPrefix(getConstructorName(value), tag)}[`, ']']; | ||
| 779 | - formatter = formatTypedArray; | ||
| 780 | - } else if (isMapIterator(value)) { | ||
| 781 | - braces = [`[${tag || 'Map Iterator'}] {`, '}']; | ||
| 782 | - formatter = formatMapIterator; | ||
| 783 | - } else if (isSetIterator(value)) { | ||
| 784 | - braces = [`[${tag || 'Set Iterator'}] {`, '}']; | ||
| 785 | - formatter = formatSetIterator; | ||
| 786 | - // Handle other regular objects again. | ||
| 787 | - } else if (keyLength === 0) { | ||
| 788 | - if (isExternal(value)) | ||
| 789 | - return ctx.stylize('[External]', 'special'); | ||
| 790 | - return `${getPrefix(constructor, tag)}{}`; | ||
| 791 | 775 | } else { | |
| 792 | - braces[0] = `${getPrefix(constructor, tag)}{`; | ||
| 776 | + const specialIterator = noPrototypeIterator(ctx, value, recurseTimes); | ||
| 777 | + if (specialIterator) { | ||
| 778 | + return specialIterator; | ||
| 779 | + } | ||
| 780 | + if (isMapIterator(value)) { | ||
| 781 | + braces = [`[${tag || 'Map Iterator'}] {`, '}']; | ||
| 782 | + formatter = formatMapIterator; | ||
| 783 | + } else if (isSetIterator(value)) { | ||
| 784 | + braces = [`[${tag || 'Set Iterator'}] {`, '}']; | ||
| 785 | + formatter = formatSetIterator; | ||
| 786 | + // Handle other regular objects again. | ||
| 787 | + } else if (keyLength === 0) { | ||
| 788 | + if (isExternal(value)) | ||
| 789 | + return ctx.stylize('[External]', 'special'); | ||
| 790 | + return `${getPrefix(constructor, tag)}{}`; | ||
| 791 | + } else { | ||
| 792 | + braces[0] = `${getPrefix(constructor, tag)}{`; | ||
| 793 | + } | ||
| 793 | 794 | } | |
| 794 | 795 | } | |
| 795 | 796 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1600,24 +1600,6 @@ util.inspect(process); | |||
| 1600 | 1600 | 'prematurely. Maximum call stack size exceeded.]')); | |
| 1601 | 1601 | } | |
| 1602 | 1602 | ||
| 1603 | - // Manipulating the Symbol.iterator should still produce nice results. | ||
| 1604 | - [ | ||
| 1605 | - [[1, 2], '[ 1, 2 ]'], | ||
| 1606 | - [[, , 5, , , , ], '[ <2 empty items>, 5, <3 empty items> ]'], | ||
| 1607 | - [new Set([1, 2]), 'Set { 1, 2 }'], | ||
| 1608 | - [new Map([[1, 2]]), 'Map { 1 => 2 }'], | ||
| 1609 | - [new Uint8Array(2), 'Uint8Array [ 0, 0 ]'], | ||
| 1610 | - // It seems like the following can not be fully restored :( | ||
| 1611 | - [new Set([1, 2]).entries(), 'Object [Set Iterator] {}'], | ||
| 1612 | - [new Map([[1, 2]]).keys(), 'Object [Map Iterator] {}'], | ||
| 1613 | - ].forEach(([value, expected]) => { | ||
| 1614 | - // "Remove the Symbol.iterator" | ||
| 1615 | - Object.defineProperty(value, Symbol.iterator, { | ||
| 1616 | - value: false | ||
| 1617 | - }); | ||
| 1618 | - assert.strictEqual(util.inspect(value), expected); | ||
| 1619 | - }); | ||
| 1620 | - | ||
| 1621 | 1603 | // Verify the output in case the value has no prototype. | |
| 1622 | 1604 | // Sadly, these cases can not be fully inspected :( | |
| 1623 | 1605 | [ | |
@@ -1673,6 +1655,9 @@ util.inspect(process); | |||
| 1673 | 1655 | ); | |
| 1674 | 1656 | value.foo = 'bar'; | |
| 1675 | 1657 | assert.notStrictEqual(util.inspect(value), expected); | |
| 1658 | + delete value.foo; | ||
| 1659 | + value[Symbol('foo')] = 'yeah'; | ||
| 1660 | + assert.notStrictEqual(util.inspect(value), expected); | ||
| 1676 | 1661 | }); | |
| 1677 | 1662 | ||
| 1678 | 1663 | assert.strictEqual(inspect(1n), '1n'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments