pipr.tools

diff

Line-level diff of two text blocks separated by ---

Aa Text

Try it

stdin0 chars
stdout0 chars

Example

Compare two text blocks line by line

Usage
$ echo "the quick brown fox
jumps over
the lazy dog
---
the quick re..." | diff
View source
(input) => {
      const sep = input.indexOf("\n---\n");
      if (sep === -1)
        return "Error: separate two text blocks with a line containing only ---";
      const oldText = input.slice(0, sep);
      const newText = input.slice(sep + 5);
      const changes = diffLines(oldText, newText);
      const result = [];
      for (const part of changes) {
        const prefix = part.added ? "+ " : part.removed ? "- " : "  ";
        // diffLines includes trailing newlines in each part; split and mark each line
        const lines = part.value.replace(/\n$/, "").split("\n");
        for (const line of lines) result.push(prefix + line);
      }
      return result.join("\n");
    }

Dependencies

Suggested Pipelines

Related Tools