Skip to content

Commit

Permalink
feat: More compact formatting for 1-item arrays and 1-key objects
Browse files Browse the repository at this point in the history
Change-Id: I22ba740d3dc02facdc9d02a021d98aff619e512d
  • Loading branch information
joeyparrish committed Nov 8, 2021
1 parent 0b30146 commit bc0014d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
11 changes: 11 additions & 0 deletions log-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ' ';
Expand All @@ -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 + ': ';
Expand Down
36 changes: 31 additions & 5 deletions spec/log-window-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);

Expand All @@ -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);
});
});
Expand Down

0 comments on commit bc0014d

Please sign in to comment.