Skip to content

Commit 8962008

Browse files
committed
implement general inspect method
1 parent 02dde97 commit 8962008

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

packages/core/src/shared/utilities/collectionUtils.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
import { isWeb } from '../extensionGlobals'
7+
import { inspect as nodeInspect } from 'util'
68
import { AsyncCollection, toCollection } from './asyncCollection'
79
import { SharedProp, AccumulableKeys, Coalesce, isNonNullable } from './tsUtils'
810

@@ -297,7 +299,6 @@ export function assign<T extends Record<any, any>, U extends Partial<T>>(data: T
297299
* - depth=2 returns `obj` with its children and their children.
298300
* - and so on...
299301
*
300-
* TODO: node's `util.inspect()` function is better, but doesn't work in web browser?
301302
*
302303
* @param obj Object to clone.
303304
* @param depth
@@ -329,6 +330,33 @@ export function partialClone(obj: any, depth: number = 3, omitKeys: string[] = [
329330
return clonedObj
330331
}
331332

333+
type inspectOptions = Partial<{
334+
depth: number
335+
omitKeys: string[]
336+
replacement: any
337+
showHidden: boolean
338+
color: boolean
339+
}>
340+
341+
/**
342+
* Wrapper around nodes inspect function that works on web. Defaults to JSON.stringify on web.
343+
* @param obj object to show
344+
* @param opt options for showing (ex. depth, omitting keys)
345+
*/
346+
export function inspect(obj: any, opt?: inspectOptions): string {
347+
const options = {
348+
depth: opt?.depth ?? 3,
349+
omitKeys: opt?.omitKeys ?? [],
350+
replacement: opt?.replacement,
351+
showHidden: opt?.showHidden ?? false,
352+
color: opt?.color ?? false,
353+
}
354+
const objToShow = partialClone(obj, options.depth, options.omitKeys, options.replacement)
355+
return isWeb()
356+
? JSON.stringify(objToShow)
357+
: nodeInspect(objToShow, options.showHidden, options.depth, options.color)
358+
}
359+
332360
/** Recursively delete undefined key/value pairs */
333361
export function stripUndefined<T extends Record<string, any>>(
334362
obj: T

packages/core/src/test/shared/utilities/collectionUtils.test.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
joinAll,
3232
isPresent,
3333
partialClone,
34+
inspect,
3435
} from '../../../shared/utilities/collectionUtils'
3536

3637
import { asyncGenerator } from '../../../shared/utilities/collectionUtils'
@@ -511,7 +512,7 @@ describe('CollectionUtils', async function () {
511512
const requester = async (request: { next?: string }) => pages[request.next ?? 'page1']
512513

513514
it('creates a new AsyncCollection', async function () {
514-
const collection = pageableToCollection(requester, {}, 'next', 'data')
515+
const collection = pageableToCollection(requester, {}, 'next' as never, 'data')
515516
assert.deepStrictEqual(await collection.promise(), [[0, 1, 2], [3, 4], [5], []])
516517
})
517518

@@ -540,7 +541,7 @@ describe('CollectionUtils', async function () {
540541

541542
describe('last', function () {
542543
it('it persists last element when mapped', async function () {
543-
const collection = pageableToCollection(requester, {}, 'next', 'data')
544+
const collection = pageableToCollection(requester, {}, 'next' as never, 'data')
544545
const mapped = collection.map((i) => i[0] ?? -1)
545546
assert.strictEqual(await last(mapped), -1)
546547
})
@@ -679,6 +680,40 @@ describe('CollectionUtils', async function () {
679680
})
680681
})
681682

683+
describe('inspect', function () {
684+
let testData: any
685+
before(function () {
686+
testData = {
687+
root: {
688+
A: {
689+
B: {
690+
C: {
691+
D: {
692+
E: 'data',
693+
},
694+
},
695+
},
696+
},
697+
},
698+
}
699+
})
700+
701+
it('defaults to a depth of 3', function () {
702+
assert.strictEqual(inspect(testData), '{ root: { A: { B: {} } } }')
703+
})
704+
705+
it('allows depth to be set manually', function () {
706+
assert.strictEqual(
707+
inspect(testData, { depth: 6 }),
708+
"{\n root: {\n A: {\n B: { C: { D: { E: 'data' } } }\n }\n }\n}"
709+
)
710+
})
711+
712+
it('omits keys specified', function () {
713+
assert.strictEqual(inspect(testData, { omitKeys: ['D', 'C'] }), '{ root: { A: { B: {} } } }')
714+
})
715+
})
716+
682717
describe('partialClone', function () {
683718
it('omits properties by depth', function () {
684719
const testObj = {

0 commit comments

Comments
 (0)