Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const compareStrings = (text1, text2) => {
const jsdiff = require('diff');
const diffResult = jsdiff.diffSentences(text1, text2);
let runningText = '';
const diffEntries = diffResult.entries();
// debugger;
// console.log(diffResult);
for (var i = 0; i < diffResult.length; i++) {
const result = diffResult[i];
const next = diffResult[i+1];
if (result.removed && next && next.added) {
const removed = result.value;
const added = diffResult[i+1].value;
i++;
runningText = runningText + resolveSentence(removed, added);
} else if (result.removed) {
runningText = runningText + "+++++" + result.value + "+++++";
pubSchema.statics.generateDiffObject = function(oldPubObject, newPubObject) {
// Diff each item in object and store output
// Iterate over to calculate total additions, deletions
const outputObject = {};
outputObject.diffMarkdown = jsdiff.diffSentences(oldPubObject.markdown || '', newPubObject.markdown || '', {newlineIsToken: true});
outputObject.diffStyleDesktop = jsdiff.diffWords(oldPubObject.styleDesktop || '', newPubObject.styleDesktop || '', {newlineIsToken: true});
outputObject.diffStyleMobile = jsdiff.diffWords(oldPubObject.styleMobile || '', newPubObject.styleMobile || '', {newlineIsToken: true});
let additions = 0;
let deletions = 0;
for (const key in outputObject) {
if (outputObject.hasOwnProperty(key)) {
outputObject[key].map((diffArrayItem)=>{
if (diffArrayItem.added) {
additions += 1;
}
if (diffArrayItem.removed) {
deletions += 1;
}
});
}
}, function(err, response, responseBody) {
// get the status code
var statuscode = (response || {}).statusCode;
// if the response was >= 200 && < 300
// this is a error :(
if(statuscode >= 200 &&
statuscode < 300) {
// get the diff
var lines = diff.diffSentences(content || '',responseBody || '');
// count the diff
var count = 0;
// loop the diff
for(var i = 0; i < (lines || []).length; i++) {
// check the count
if( (lines[i] || {}).added == true ||
(lines[i] || {}).removed == true ) {
// add to count
count++;
// stop loop
break;
self.compare = function(old, current) {
var oldContent = old.content;
if (oldContent === undefined) {
oldContent = '';
}
var currentContent = current.content;
if (currentContent === undefined) {
currentContent = '';
}
var diff = jsDiff.diffSentences(self.apos.utils.htmlToPlaintext(oldContent).replace(/\s+/g, ' '), self.apos.utils.htmlToPlaintext(currentContent).replace(/\s+/g, ' '), { ignoreWhitespace: true });
var changes = _.map(_.filter(diff, function(diffChange) {
return diffChange.added || diffChange.removed;
}), function(diffChange) {
// Convert a jsDiff change object to an
// apos versions change object
if (diffChange.removed) {
return {
action: 'remove',
text: diffChange.value,
field: {
type: 'string',
label: 'Content'
}
};
} else {
let content;
let diff;
let hasDiff;
if (this.editorType === 'rich') {
const hasEnterContent = isObject(this.editorContent);
if (hasEnterContent) {
return;
}
content = toMarkdown(this.editorContent).toString();
diff = jsdiff.diffSentences(this.originContent, content);
hasDiff = diff && diff.length > 1;
} else {
content = this.state.markdown;
diff = jsdiff.diffSentences(this.originContent, content);
hasDiff = diff && diff.length > 1;
}
if (!hasDiff) {
return;
}
this.doCommit(content);
}
case "Word":
if (ignoreWhitespace) {
diff = JsDiff.diffWords(samples[0], samples[1]);
} else {
diff = JsDiff.diffWordsWithSpace(samples[0], samples[1]);
}
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>";
private renderCharterField(charterField: string, prevCharterField?: string): JSX.Element {
const { isDiffModeEnabled } = this.state;
let out = <>;
if (isDiffModeEnabled && (prevCharterField || this.isViewingFirstRevision())) {
const diff = diffSentences(prevCharterField || "", charterField);
out = (
<>
{diff.map(sentence => {
if (sentence.added) {
return renderPTagsFromLineBreaks(sentence.value, CharterTextAdded);
} else if (sentence.removed) {
return renderPTagsFromLineBreaks(sentence.value, CharterTextRemoved);
}
return renderPTagsFromLineBreaks(sentence.value);
})}
);
} else {
out = renderPTagsFromLineBreaks(charterField);
}