-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
util: add util.table() #63984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
watilde
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
watilde:util-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+294
−120
Open
util: add util.table() #63984
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| 'use strict'; | ||
|
|
||
| const { | ||
| ArrayFrom, | ||
| ArrayIsArray, | ||
| ArrayPrototypePush, | ||
| ArrayPrototypeUnshift, | ||
| ObjectKeys, | ||
| ObjectPrototypeHasOwnProperty, | ||
| ObjectValues, | ||
| } = primordials; | ||
|
|
||
| const { previewEntries } = internalBinding('util'); | ||
| const { Buffer: { isBuffer } } = require('buffer'); | ||
| const { inspect } = require('internal/util/inspect'); | ||
| const { | ||
| isTypedArray, isSet, isMap, isSetIterator, isMapIterator, | ||
| } = require('internal/util/types'); | ||
|
|
||
| const keyKey = 'Key'; | ||
| const valuesKey = 'Values'; | ||
| const indexKey = '(index)'; | ||
| const iterKey = '(iteration index)'; | ||
|
|
||
| const isArray = (v) => ArrayIsArray(v) || isTypedArray(v) || isBuffer(v); | ||
|
|
||
| /** | ||
| * Builds the rows of a table from tabular data and renders them into a string. | ||
| * Shared by `console.table` and `util.table`. | ||
| * @param {any} tabularData The data to tabulate. | ||
| * @param {string[]} [properties] The subset of properties to include as columns. | ||
| * @param {object} [inspectOptions] Base options merged into each cell's inspect call. | ||
| * @returns {string} The rendered table. | ||
| */ | ||
| function table(tabularData, properties, inspectOptions) { | ||
| // Lazy require to avoid a cycle with the console bootstrap path. | ||
| const cliTable = require('internal/cli_table'); | ||
|
|
||
| const _inspect = (v) => { | ||
| const depth = v !== null && | ||
| typeof v === 'object' && | ||
| !isArray(v) && | ||
| ObjectKeys(v).length > 2 ? -1 : 0; | ||
| const opt = { | ||
| depth, | ||
| maxArrayLength: 3, | ||
| breakLength: Infinity, | ||
| ...inspectOptions, | ||
| }; | ||
| return inspect(v, opt); | ||
| }; | ||
| const getIndexArray = (length) => ArrayFrom( | ||
| { length }, (_, i) => _inspect(i)); | ||
|
|
||
| const mapIter = isMapIterator(tabularData); | ||
| let isKeyValue = false; | ||
| let i = 0; | ||
| if (mapIter) { | ||
| const res = previewEntries(tabularData, true); | ||
| tabularData = res[0]; | ||
| isKeyValue = res[1]; | ||
| } | ||
|
|
||
| if (isKeyValue || isMap(tabularData)) { | ||
| const keys = []; | ||
| const values = []; | ||
| let length = 0; | ||
| if (mapIter) { | ||
| for (; i < tabularData.length / 2; ++i) { | ||
| ArrayPrototypePush(keys, _inspect(tabularData[i * 2])); | ||
| ArrayPrototypePush(values, _inspect(tabularData[i * 2 + 1])); | ||
| length++; | ||
| } | ||
| } else { | ||
| for (const { 0: k, 1: v } of tabularData) { | ||
| ArrayPrototypePush(keys, _inspect(k)); | ||
| ArrayPrototypePush(values, _inspect(v)); | ||
| length++; | ||
| } | ||
| } | ||
| return cliTable([ | ||
| iterKey, keyKey, valuesKey, | ||
| ], [ | ||
| getIndexArray(length), | ||
| keys, | ||
| values, | ||
| ]); | ||
| } | ||
|
|
||
| const setIter = isSetIterator(tabularData); | ||
| if (setIter) | ||
| tabularData = previewEntries(tabularData); | ||
|
|
||
| const setlike = setIter || mapIter || isSet(tabularData); | ||
| if (setlike) { | ||
| const values = []; | ||
| let length = 0; | ||
| for (const v of tabularData) { | ||
| ArrayPrototypePush(values, _inspect(v)); | ||
| length++; | ||
| } | ||
| return cliTable([iterKey, valuesKey], [getIndexArray(length), values]); | ||
| } | ||
|
|
||
| const map = { __proto__: null }; | ||
| let hasPrimitives = false; | ||
| const valuesKeyArray = []; | ||
| const indexKeyArray = ObjectKeys(tabularData); | ||
|
|
||
| for (; i < indexKeyArray.length; i++) { | ||
| const item = tabularData[indexKeyArray[i]]; | ||
| const primitive = item === null || | ||
| (typeof item !== 'function' && typeof item !== 'object'); | ||
| if (properties === undefined && primitive) { | ||
| hasPrimitives = true; | ||
| valuesKeyArray[i] = _inspect(item); | ||
| } else { | ||
| const keys = properties || ObjectKeys(item); | ||
| for (const key of keys) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here |
||
| map[key] ??= []; | ||
| if ((primitive && properties) || | ||
| !ObjectPrototypeHasOwnProperty(item, key)) | ||
| map[key][i] = ''; | ||
| else | ||
| map[key][i] = _inspect(item[key]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const keys = ObjectKeys(map); | ||
| const values = ObjectValues(map); | ||
| if (hasPrimitives) { | ||
| ArrayPrototypePush(keys, valuesKey); | ||
| ArrayPrototypePush(values, valuesKeyArray); | ||
| } | ||
| ArrayPrototypeUnshift(keys, indexKey); | ||
| ArrayPrototypeUnshift(values, indexKeyArray); | ||
|
|
||
| return cliTable(keys, values); | ||
| } | ||
|
|
||
| module.exports = table; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be a normal for loop - both for perf and to avoid needing a primordial for iteration