Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const compare = (actual, expected = '{}') => {
const delta = diff.diffJson(actual, expected)
if (
delta.length === 1 &&
typeof delta[0].removed === 'undefined' &&
typeof delta[0].added === 'undefined'
) {
return true
}
/* eslint-disable no-console */
console.log('\x1b[42m' +
(new Array(6)).join('-------------\n') + '\x1b[0m')
delta.forEach(part => {
let color = 'grey'
if (part.added) {
color = 'green'
}
const logDifference = (one, other) => {
const diff = jsdiff.diffJson(one, other);
diff.forEach((part) => {
// green for additions, red for deletions
// grey for common parts
let color;
if (part.added) {
color = 'green';
} else if (part.removed) {
color = 'red';
} else {
color = 'grey';
}
process.stderr.write(colors[color](part.value));
});
export function diffJson(a: Object, b: Object, context: number = 3): string[] {
// Number of lines to show above/below changes in diffs.
const chunks = diff(a, b);
return chunks.map((chunk, i) => {
const isFirstChunk = i == 0;
const isLastChunk = i == chunks.length - 1;
// Remove empty lines.
let lines = chunk.value.split("\n").filter(part => part !== "");
if (!chunk.added && !chunk.removed) {
// Truncate beginning of first chunk if larger than context.
// The opening brace "{" does not count as context, hence +1.
if (isFirstChunk && lines.length > context + 1) {
lines = ["..."].concat(lines.slice(-context));
// Truncate end of last chunk if larger than context.
// The closing brace "}" does not count as context, hence +1.
} else if (isLastChunk && lines.length > context + 1) {
lines = lines.slice(0, context).concat(["..."]);
var message = "";
for (var i = 0, l = spyInstance.callCount; i < l; ++i) {
// describe multiple calls
if (l > 1) {
message += "\nCall " + (i + 1) + ":";
}
var calledArgs = spyInstance.getCall(i).args;
for (var j = 0; j < calledArgs.length || j < args.length; ++j) {
message += "\n";
var calledArgMessage = j < calledArgs.length ? sinonFormat(calledArgs[j]) : "";
if (match.isMatcher(args[j])) {
message += colorSinonMatchText(args[j], calledArgs[j], calledArgMessage);
} else {
var expectedArgMessage = j < args.length ? sinonFormat(args[j]) : "";
var diff = jsDiff.diffJson(calledArgMessage, expectedArgMessage);
message += colorDiffText(diff);
}
}
}
return message;
},
getDiff(newFlag, oldFlag) {
const o = JSON.parse(JSON.stringify(oldFlag));
const n = JSON.parse(JSON.stringify(newFlag));
const d = diffJson(o, n);
if (d.length === 1) {
return "No changes";
}
return convertChangesToXML(d);
}
},
render() {
const diff = diffJson(this.props.inputA, this.props.inputB)
return (
<pre> {diff.map((part, index) => (
<span>
{part.value}
</span>
))}
</pre>
)
}
}
_.map(oldVersion, (oldPage: Page) => {
const newPage: Page | undefined = _.find(object, (e: Page) => {
return e.getName() === oldPage.getName();
});
Assert.isNotUndefined(newPage, `Something went wrong in the page diff. Unable to find ${oldPage.getName()}`);
const oldPageFormatted = oldPage
.getHTML()
.replace(/\\n/g, '\n')
.replace(/<\/script>/g, '<\\/script>');
const newPageFormatted = (newPage as Page)
.getHTML()
.replace(/\\n/g, '\n')
.replace(/<\/script>/g, '<\\/script>');
pageDiff.push({ [(newPage as Page).getName()]: jsDiff.diffJson(oldPageFormatted, newPageFormatted) });
});
return pageDiff;
break;
case "Line":
if (ignoreWhitespace) {
diff = JsDiff.diffTrimmedLines(samples[0], samples[1]);
} else {
diff = JsDiff.diffLines(samples[0], samples[1]);
}
break;
case "Sentence":
diff = JsDiff.diffSentences(samples[0], samples[1]);
break;
case "CSS":
diff = JsDiff.diffCss(samples[0], samples[1]);
break;
case "JSON":
diff = JsDiff.diffJson(samples[0], samples[1]);
break;
default:
return "Invalid 'Diff by' option.";
}
for (let i = 0; i < diff.length; i++) {
if (diff[i].added) {
if (showAdded) output += "<span class="hl5">" + Utils.escapeHtml(diff[i].value) + "</span>";
} else if (diff[i].removed) {
if (showRemoved) output += "<span class="hl3">" + Utils.escapeHtml(diff[i].value) + "</span>";
} else {
output += Utils.escapeHtml(diff[i].value);
}
}
return output;