diff --git a/demos/html-demo/demo.ts b/demos/html-demo/demo.ts index ce06d266..a27a94cc 100644 --- a/demos/html-demo/demo.ts +++ b/demos/html-demo/demo.ts @@ -268,18 +268,20 @@ const getExampleJson = function () { const diffOptions = { objectHash: function (obj, index) { - const objRecord = obj as Record; - if (typeof objRecord._id !== 'undefined') { - return objRecord._id; - } - if (typeof objRecord.id !== 'undefined') { - return objRecord.id; - } - if (typeof objRecord.key !== 'undefined') { - return objRecord.key; - } - if (typeof objRecord.name !== 'undefined') { - return objRecord.name; + if (typeof obj === 'object' && obj !== null) { + const objRecord = obj as Record; + if (typeof objRecord._id !== 'undefined') { + return objRecord._id; + } + if (typeof objRecord.id !== 'undefined') { + return objRecord.id; + } + if (typeof objRecord.key !== 'undefined') { + return objRecord.key; + } + if (typeof objRecord.name !== 'undefined') { + return objRecord.name; + } } return '$$index:' + index; }, diff --git a/demos/html-demo/index.html b/demos/html-demo/index.html index 271dbc09..393b53e7 100644 --- a/demos/html-demo/index.html +++ b/demos/html-demo/index.html @@ -58,6 +58,21 @@ + + + + Features

- - diff --git a/packages/jsondiffpatch/src/formatters/html.ts b/packages/jsondiffpatch/src/formatters/html.ts index 2a3fe080..1a520fd4 100644 --- a/packages/jsondiffpatch/src/formatters/html.ts +++ b/packages/jsondiffpatch/src/formatters/html.ts @@ -30,7 +30,11 @@ class HtmlFormatter extends BaseFormatter { } formatValue(context: HtmlFormatterContext, value: unknown) { - context.out(`
${htmlEscape(JSON.stringify(value, null, 2))}
`); + const valueAsHtml = + typeof value === 'undefined' + ? 'undefined' + : htmlEscape(JSON.stringify(value, null, 2)); + context.out(`
${valueAsHtml}
`); } formatTextDiffString(context: HtmlFormatterContext, value: string) {