How to use the diff.diffJson function in diff

To help you get started, we’ve selected a few diff examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github luckymarmot / API-Flow / testing / e2e / internal-swagger2 / e2e.spec.js View on Github external
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'
    }
github fourlabsldn / fl-form-builder / __tests__ / UiSnapshots.js View on Github external
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));
  });
github Kinto / kinto-admin / src / utils.js View on Github external
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(["..."]);
github sinonjs / sinon / lib / sinon / spy-formatters.js View on Github external
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;
    },
github checkr / flagr / browser / flagr-ui / src / components / FlagHistory.vue View on Github external
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);
    }
  },
github sanity-io / sanity / packages / @sanity / desk-tool / src / components / Diff.js View on Github external
render() {
    const diff = diffJson(this.props.inputA, this.props.inputB)
    return (
      <pre>        {diff.map((part, index) =&gt; (
          <span>
            {part.value}
          </span>
        ))}
      </pre>
    )
  }
}
github coveooss / platform-client / src / controllers / PageController.ts View on Github external
_.map(oldVersion, (oldPage: Page) =&gt; {
        const newPage: Page | undefined = _.find(object, (e: Page) =&gt; {
          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(/&lt;\/script&gt;/g, '&lt;\\/script&gt;');
        const newPageFormatted = (newPage as Page)
          .getHTML()
          .replace(/\\n/g, '\n')
          .replace(/&lt;\/script&gt;/g, '&lt;\\/script&gt;');

        pageDiff.push({ [(newPage as Page).getName()]: jsDiff.diffJson(oldPageFormatted, newPageFormatted) });
      });
      return pageDiff;
github gchq / CyberChef / src / core / operations / legacy / Diff.js View on Github external
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 &lt; 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;