diff
external fun diff(actual: String, expected: String): ERROR CLASS: Symbol not found for js.array.ReadonlyArray<{node/util/DiffEntry=} ERROR CLASS: Symbol not found for js.array.Tuple2<kotlin/Int, kotlin/String>>(source)
external fun diff(actual: String, expected: ERROR CLASS: Symbol not found for js.array.ReadonlyArray<kotlin/String>): ERROR CLASS: Symbol not found for js.array.ReadonlyArray<{node/util/DiffEntry=} ERROR CLASS: Symbol not found for js.array.Tuple2<kotlin/Int, kotlin/String>>(source)
external fun diff(actual: ERROR CLASS: Symbol not found for js.array.ReadonlyArray<kotlin/String>, expected: String): ERROR CLASS: Symbol not found for js.array.ReadonlyArray<{node/util/DiffEntry=} ERROR CLASS: Symbol not found for js.array.Tuple2<kotlin/Int, kotlin/String>>(source)
external fun diff(actual: ERROR CLASS: Symbol not found for js.array.ReadonlyArray<kotlin/String>, expected: ERROR CLASS: Symbol not found for js.array.ReadonlyArray<kotlin/String>): ERROR CLASS: Symbol not found for js.array.ReadonlyArray<{node/util/DiffEntry=} ERROR CLASS: Symbol not found for js.array.Tuple2<kotlin/Int, kotlin/String>>(source)
util.diff()
compares two string or array values and returns an array of difference entries. It uses the Myers diff algorithm to compute minimal differences, which is the same algorithm used internally by assertion error messages.
If the values are equal, an empty array is returned.
const { diff } = require('node:util');
// Comparing strings
const actualString = '12345678';
const expectedString = '12!!5!7!';
console.log(diff(actualString, expectedString));
// [
// [0, '1'],
// [0, '2'],
// [1, '3'],
// [1, '4'],
// [-1, '!'],
// [-1, '!'],
// [0, '5'],
// [1, '6'],
// [-1, '!'],
// [0, '7'],
// [1, '8'],
// [-1, '!'],
// ]
// Comparing arrays
const actualArray = ['1', '2', '3'];
const expectedArray = ['1', '3', '4'];
console.log(diff(actualArray, expectedArray));
// [
// [0, '1'],
// [1, '2'],
// [0, '3'],
// [-1, '4'],
// ]
// Equal values return empty array
console.log(diff('same', 'same'));
// []
Content copied to clipboard
Since
v22.15.0
Parameters
actual
The first value to compare
expected
The second value to compare