Skip to content

Unexpected diffArrays result on a basic example #474

Closed as not planned
Closed as not planned
@pbmallinson

Description

@pbmallinson

diffArrays() from diff@5.1.0 reports misleading change data in the following case.

  • specifically, the second and third instances of [ ...'words', 'to', ... ] in the array are reported as unchanged then added, where the expected behaviour is that they should be reported as added then unchanged.
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const diff_1 = require("diff");
const a = ["words", "to", "start", "Words", "To", "Change", "words", "to", "end",];
const b = ["words", "to", "start", "words", "to", "change", "words", "to", "end",];
console.log("diffArrays result:", (0, diff_1.diffArrays)(a, b));

yields unexpected:

diffArrays result: [
  { count: 3, value: [ 'words', 'to', 'start' ] },
  {
    count: 3,
    added: undefined,
    removed: true,
    value: [ 'Words', 'To', 'Change' ]
  },
  { count: 2, value: [ 'words', 'to' ] },
  {
    count: 3,
    added: true,
    removed: undefined,
    value: [ 'change', 'words', 'to' ]
  },
  { count: 1, value: [ 'end' ] }
]

expected:

diffArrays result: [
  { count: 3, value: [ 'words', 'to', 'start' ] },
  {
    count: 3,
    added: undefined,
    removed: true,
    value: [ 'Words', 'To', 'Change' ]
  },
  {
    count: 3,
    added: true,
    removed: undefined,
    value: [ 'words', 'to', 'change' ]
  },
  { count: 3, value: [ 'words', 'to', 'end' ] }
]

By contrast, the diffWords() implementation produces expected output.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const diff_1 = require("diff");
const orginalString = "words to start Words To Change words to end";
const updatedString = "words to start words to change words to end";
console.log("diffWords result:", (0, diff_1.diffWords)(orginalString, updatedString));

yields expected:

diffWords result: [
  { count: 6, value: 'words to start ' },
  { count: 1, added: undefined, removed: true, value: 'Words' },
  { count: 1, added: true, removed: undefined, value: 'words' },
  { count: 1, value: ' ' },
  { count: 1, added: undefined, removed: true, value: 'To' },
  { count: 1, added: true, removed: undefined, value: 'to' },
  { count: 1, value: ' ' },
  { count: 1, added: undefined, removed: true, value: 'Change' },
  { count: 1, added: true, removed: undefined, value: 'change' },
  { count: 6, value: ' words to end' }
]

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