Skip to content
moxon home

Text Comparison

The /diff endpoint compares two texts and returns an ordered list of changes, line by line or word by word.

posthttps://api.moxon.dev/v5/diff

How it works?

The comparison relies on the Longest Common Subsequence (LCS) algorithm: it finds the largest set of segments shared by both texts in the same order, and everything outside that set is reported as added or removed. In line mode the texts are split on newlines, keeping empty lines; in word mode they are split on whitespace runs and empty segments are dropped.

Try itpost/v5/diff

Parameters

The request body must be JSON:

  • astringrequired

    Original text

  • bstringrequired

    Modified text, compared against a

  • mode"line" | "word"optional

    Comparison granularity: line or word. Default: line

Available Modes

ModeDescription
lineSplits both texts on newlines and compares line by line
wordSplits both texts on whitespace and compares word by word

Response Fields

  • modestring

    Granularity used: line or word

  • addednumber

    Number of segments present only in b

  • removednumber

    Number of segments present only in a

  • changesobject[]

    Ordered changes rebuilding b from a

changes entries:

  • typestring

    equal, add or del

  • valuestring

    The line or word this change applies to

Code Examples

Error Handling

If parameters are missing or invalid, the API will return an error:

  • 400Please provide a first text (a={text})The a parameter is missing or is not a string
  • 400Please provide a second text (b={text})The b parameter is missing or is not a string
  • 400Texts must be 10000 characters or fewera or b exceeds the maximum length
  • 400Mode must be one of: line, wordThe mode value is not line or word
  • 400Texts must contain 2000 lines or fewerA text has more lines than the limit in line mode
  • 400Texts must contain 2000 words or fewerA text has more words than the limit in word mode