Skip to content

diffWords produces duplicate spaces with oneChangePerToken #698

Description

@aforemendude

When diffWords is called with oneChangePerToken set to true, the output contains duplicate spaces. The other text diff methods don't output duplicate separators.

Reproduced on the latest v9.0.0. Happening to the latest master as well.

Reproduction:

https://github.com/aforemendude/bugs-reproduction/tree/main/jsdiff-one-change-per-token

import {
  diffChars,
  diffLines,
  diffSentences,
  diffWords,
  diffWordsWithSpace,
} from 'diff'

const options = { oneChangePerToken: true }

const diffCases = [
  ['diffChars', diffChars, 'cat dog', 'cat dig'],
  ['diffWords', diffWords, 'a b', 'a c'],
  ['diffWordsWithSpace', diffWordsWithSpace, 'red blue', 'red green'],
  ['diffLines', diffLines, 'red\nblue\n', 'red\ngreen\n'],
  [
    'diffSentences',
    diffSentences,
    'The cat sleeps. The dog runs.',
    'The cat rests. The dog runs.',
  ],
]

for (const [name, diffMethod, oldText, newText] of diffCases) {
  const changes = diffMethod(oldText, newText, options)
  console.log(`${name} (${JSON.stringify(oldText)} -> ${JSON.stringify(newText)}):`)
  console.log('changes:', JSON.stringify(changes, null, 2))
  console.log('------------------------------')
}

diffWords outputs a (a + space), b (space + b), and c (space + c). The input only has 1 space, but the output has 2 (after a, and before b/c).

diffWords ("a b" -> "a c"):
changes: [
  {
    "count": 1,
    "added": false,
    "removed": false,
    "value": "a "
  },
  {
    "count": 1,
    "added": false,
    "removed": true,
    "value": " b"
  },
  {
    "count": 1,
    "added": true,
    "removed": false,
    "value": " c"
  }
]

All other text diff methods (diffChars, diffLines, diffSentences, diffWordsWithSpace) are working properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions