node/lib/internal/util/diff.js
Giovanni Bucci 6b42554342
util: expose diff function used by the assertion errors
fix: https://github.com/nodejs/node/issues/51740
PR-URL: https://github.com/nodejs/node/pull/57462
Fixes: https://github.com/nodejs/node/issues/51740
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2025-03-19 23:59:07 +00:00

42 lines
1.1 KiB
JavaScript

'use strict';
const {
ArrayIsArray,
ArrayPrototypeReverse,
} = primordials;
const { validateStringArray, validateString } = require('internal/validators');
const { myersDiff } = require('internal/assert/myers_diff');
function validateInput(value, name) {
if (!ArrayIsArray(value)) {
validateString(value, name);
return;
}
validateStringArray(value, name);
}
/**
* Generate a difference report between two values
* @param {Array | string} actual - The first value to compare
* @param {Array | string} expected - The second value to compare
* @returns {Array} - An array of differences between the two values.
* The returned data is an array of arrays, where each sub-array has two elements:
* 1. The operation to perform: -1 for delete, 0 for no-op, 1 for insert
* 2. The value to perform the operation on
*/
function diff(actual, expected) {
if (actual === expected) {
return [];
}
validateInput(actual, 'actual');
validateInput(expected, 'expected');
return ArrayPrototypeReverse(myersDiff(actual, expected));
}
module.exports = {
diff,
};