forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.js
More file actions
42 lines (34 loc) · 1.07 KB
/
diff.js
File metadata and controls
42 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'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,
};