From bc0014d25d4c00acb908b4325e5ef5493d60588e Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Thu, 4 Nov 2021 16:05:45 -0700 Subject: [PATCH] feat: More compact formatting for 1-item arrays and 1-key objects Change-Id: I22ba740d3dc02facdc9d02a021d98aff619e512d --- log-window.js | 11 +++++++++++ spec/log-window-tests.js | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/log-window.js b/log-window.js index 3b6c8c8..7792a57 100644 --- a/log-window.js +++ b/log-window.js @@ -267,9 +267,14 @@ function prettyPrint(obj, indentation = '') { } if (Array.isArray(obj)) { + // More compact representations for empty or 1-element arrays. if (obj.length == 0) { return '[]'; } + if (obj.length == 1) { + return `[${prettyPrint(obj[0], indentation)}]`; + } + let insides = ''; for (const entry of obj) { insides += indentation + ' '; @@ -280,9 +285,15 @@ function prettyPrint(obj, indentation = '') { if (obj.constructor == Object) { const keys = Object.keys(obj); + + // More compact representations for empty or 1-element objects. if (keys.length == 0) { return '{}'; } + if (keys.length == 1) { + return `{${keys[0]}: ${prettyPrint(obj[keys[0]], indentation)}}`; + } + let insides = ''; for (const key of keys) { insides += indentation + ' ' + key + ': '; diff --git a/spec/log-window-tests.js b/spec/log-window-tests.js index c837d7f..b66634c 100644 --- a/spec/log-window-tests.js +++ b/spec/log-window-tests.js @@ -253,6 +253,17 @@ describe('Log window', () => { expect(JSON5.parse(arrayText)).toEqual(array); }); + it('builds a compact formatted string from a 1-element array', () => { + const array = ['abc']; + logResult(array); + + const text = mockLogElement.querySelector('.data').textContent; + expect(text).toContain('=> ["abc"]'); + + const arrayText = text.split('=> ')[1]; + expect(JSON5.parse(arrayText)).toEqual(array); + }); + it('builds a formatted string from a Uint8Array', () => { const array = [12, 34, 12, 65, 34, 634, 78, 324, 54, 23, 53]; logResult(fakeObjectWithType( @@ -267,7 +278,8 @@ describe('Log window', () => { it('builds a formatted string from an Object', () => { const object = { - persistantStateRequired: true, + persistentState: 'required', + distinctiveIdentifier: 'required', }; logResult(object); @@ -278,17 +290,31 @@ describe('Log window', () => { expect(JSON5.parse(objectText)).toEqual(object); }); + it('builds a compact formatted string from a 1-field Object', () => { + const object = { + keySystem: 'com.widevine.alpha', + }; + logResult(object); + + const text = mockLogElement.querySelector('.data').textContent; + expect(text).toContain('=> {keySystem: "com.widevine.alpha"}'); + + const objectText = text.split('=> ')[1]; + expect(JSON5.parse(objectText)).toEqual(object); + }); + it('builds a formatted string from an Object with a type', () => { const fields = { - keySystem: 'test.com', + sessionId: 'abc', + expiration: Infinity, }; - const object = fakeObjectWithType('MediaKeys', fields); + const object = fakeObjectWithType('MediaKeySession', fields); logResult(object); const text = mockLogElement.querySelector('.data').textContent; - expect(text).toContain('=> MediaKeys instance {\n'); + expect(text).toContain('=> MediaKeySession instance {\n'); - const objectText = text.split('=> MediaKeys instance ')[1]; + const objectText = text.split('=> MediaKeySession instance ')[1]; expect(JSON5.parse(objectText)).toEqual(fields); }); });