add a undefined JSON replacer for diff

This commit is contained in:
Karol Wypchlo 2020-10-10 16:04:13 +02:00
parent bcaa64bda1
commit 6228671048
2 changed files with 13 additions and 2 deletions

View File

@ -2,7 +2,7 @@ const superagent = require("superagent");
const hash = require("object-hash");
const { detailedDiff } = require("deep-object-diff");
const { isEqual } = require("lodash");
const { calculateElapsedTime, getResponseContent } = require("../utils");
const { calculateElapsedTime, ensureValidJSON, getResponseContent } = require("../utils");
// audioExampleCheck returns the result of trying to download the skylink
// for the Example audio file on siasky.net
@ -918,7 +918,7 @@ function skylinkVerification(done, { name, skylink, bodyHash, metadata }) {
if (!isEqual(currentMetadata, metadata)) {
entry.up = false;
info.metadata = detailedDiff(metadata, currentMetadata);
info.metadata = ensureValidJSON(detailedDiff(metadata, currentMetadata));
}
if (Object.keys(info).length) entry.info = info; // add info only if it exists

View File

@ -29,8 +29,19 @@ function getResponseContent(response) {
}
}
/**
* Ensures that the object serializes to JSON properly
*/
function ensureValidJSON(object) {
const replacer = (key, value) => (value === undefined ? "--undefined--" : value);
const stringified = JSON.stringify(object, replacer);
return JSON.parse(stringified);
}
module.exports = {
calculateElapsedTime,
getYesterdayISOString,
getResponseContent,
ensureValidJSON,
};