Skip to content

Commit

Permalink
fix quotes around non-json strings; test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
smanolloff committed May 27, 2020
1 parent 6c76e93 commit 801a528
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ describe('<KeyValuesTable>', () => {

const data = [
{ key: 'span.kind', value: 'client' },
{ key: 'omg', value: 'mos-def' },
{ key: 'numericString', value: '12345678901234567890' },
{ key: 'omg', value: '{not-a-json' },
{ key: 'numeric', value: 123456789 },
{ key: 'jsonkey', value: JSON.stringify({ hello: 'world' }) },
];

Expand Down Expand Up @@ -138,7 +138,7 @@ describe('<KeyValuesTable>', () => {
expect(el.length).toBe(data.length);
el.forEach((valueDiv, i) => {
if (data[i].key !== 'jsonkey') {
expect(valueDiv.text()).toBe(data[i].value);
expect(valueDiv.html()).toMatch(data[i].value.toString());
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ import './KeyValuesTable.css';
const jsonObjectOrArrayStartRegex = /^(\[|\{)/;

function tryParseJson(value: string) {
// if the value is a string representing actual json object or array, then use json-markup
// otherwise just return as is
try {
return JSON.parse(value);
return jsonObjectOrArrayStartRegex.test(value)
? JSON.parse(value)
: value
} catch (_) {
return value;
}
Expand All @@ -51,12 +55,11 @@ function _jsonMarkup(value: any) {
function formatValue(value: any) {
let content;

// if the value is a string representing actual json object or array, then use json-markup
if (typeof value === 'string') {
// otherwise just return as is
content = jsonObjectOrArrayStartRegex.test(value)
? _jsonMarkup(tryParseJson(value))
: stringMarkup(value);
let parsed = tryParseJson(value);
content = (typeof parsed === 'string')
? stringMarkup(parsed)
: _jsonMarkup(parsed);
} else {
content = _jsonMarkup(value);
}
Expand Down

0 comments on commit 801a528

Please sign in to comment.