@voidzero-dev/vite-plus-test 0.0.0-0bfcc90f.20260209-0731
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +691 -0
- package/browser/context.d.ts +4 -0
- package/browser/context.js +20 -0
- package/config.d.ts +3 -0
- package/coverage.d.ts +1 -0
- package/dist/@vitest/browser/client/.vite/manifest.json +24 -0
- package/dist/@vitest/browser/client/__vitest__/assets/index-BUCFJtth.js +57 -0
- package/dist/@vitest/browser/client/__vitest__/assets/index-DlhE0rqZ.css +1 -0
- package/dist/@vitest/browser/client/__vitest__/bg.png +0 -0
- package/dist/@vitest/browser/client/__vitest__/favicon.ico +0 -0
- package/dist/@vitest/browser/client/__vitest__/favicon.svg +5 -0
- package/dist/@vitest/browser/client/__vitest__/index.html +32 -0
- package/dist/@vitest/browser/client/__vitest_browser__/orchestrator-S_3e_uzt.js +345 -0
- package/dist/@vitest/browser/client/__vitest_browser__/tester-k74mgIRa.js +2171 -0
- package/dist/@vitest/browser/client/__vitest_browser__/utils-uxqdqUz8.js +223 -0
- package/dist/@vitest/browser/client/error-catcher.js +82 -0
- package/dist/@vitest/browser/client/esm-client-injector.js +67 -0
- package/dist/@vitest/browser/client/favicon.svg +5 -0
- package/dist/@vitest/browser/client/orchestrator.html +35 -0
- package/dist/@vitest/browser/client/tester/tester.html +13 -0
- package/dist/@vitest/browser/client.js +456 -0
- package/dist/@vitest/browser/context.d.ts +792 -0
- package/dist/@vitest/browser/context.js +541 -0
- package/dist/@vitest/browser/expect-element.js +32 -0
- package/dist/@vitest/browser/index-D6m36C6U.js +11 -0
- package/dist/@vitest/browser/index.d.ts +73 -0
- package/dist/@vitest/browser/index.js +3513 -0
- package/dist/@vitest/browser/jest-dom.d.ts +724 -0
- package/dist/@vitest/browser/locators.d.ts +354 -0
- package/dist/@vitest/browser/locators.js +1 -0
- package/dist/@vitest/browser/matchers.d.ts +29 -0
- package/dist/@vitest/browser/shared/screenshotMatcher/types.d.ts +22 -0
- package/dist/@vitest/browser/state.js +280 -0
- package/dist/@vitest/browser/types.d.ts +69 -0
- package/dist/@vitest/browser-playwright/context.d.ts +1 -0
- package/dist/@vitest/browser-playwright/index.d.ts +106 -0
- package/dist/@vitest/browser-playwright/index.js +1111 -0
- package/dist/@vitest/browser-playwright/locators.js +114 -0
- package/dist/@vitest/browser-preview/context.d.ts +1 -0
- package/dist/@vitest/browser-preview/index.d.ts +19 -0
- package/dist/@vitest/browser-preview/index.js +148 -0
- package/dist/@vitest/browser-preview/locators.js +79 -0
- package/dist/@vitest/browser-webdriverio/context.d.ts +1 -0
- package/dist/@vitest/browser-webdriverio/index.d.ts +63 -0
- package/dist/@vitest/browser-webdriverio/index.js +600 -0
- package/dist/@vitest/browser-webdriverio/locators.js +163 -0
- package/dist/@vitest/expect/index.d.ts +807 -0
- package/dist/@vitest/expect/index.js +1875 -0
- package/dist/@vitest/mocker/auto-register.d.ts +2 -0
- package/dist/@vitest/mocker/auto-register.js +9 -0
- package/dist/@vitest/mocker/automock.d.ts +12 -0
- package/dist/@vitest/mocker/automock.js +1 -0
- package/dist/@vitest/mocker/browser.d.ts +53 -0
- package/dist/@vitest/mocker/browser.js +91 -0
- package/dist/@vitest/mocker/chunk-automock.js +354 -0
- package/dist/@vitest/mocker/chunk-interceptor-native.js +15 -0
- package/dist/@vitest/mocker/chunk-mocker.js +521 -0
- package/dist/@vitest/mocker/chunk-pathe.M-eThtNZ.js +174 -0
- package/dist/@vitest/mocker/chunk-registry.js +185 -0
- package/dist/@vitest/mocker/chunk-utils.js +16 -0
- package/dist/@vitest/mocker/index.d-C-sLYZi-.d.ts +25 -0
- package/dist/@vitest/mocker/index.d.ts +2 -0
- package/dist/@vitest/mocker/index.js +185 -0
- package/dist/@vitest/mocker/mocker.d-TnKRhz7N.d.ts +81 -0
- package/dist/@vitest/mocker/node.d.ts +800 -0
- package/dist/@vitest/mocker/node.js +966 -0
- package/dist/@vitest/mocker/redirect.d.ts +3 -0
- package/dist/@vitest/mocker/redirect.js +79 -0
- package/dist/@vitest/mocker/register.d.ts +9 -0
- package/dist/@vitest/mocker/register.js +41 -0
- package/dist/@vitest/mocker/types.d-B8CCKmHt.d.ts +107 -0
- package/dist/@vitest/pretty-format/index.d.ts +124 -0
- package/dist/@vitest/pretty-format/index.js +1022 -0
- package/dist/@vitest/runner/chunk-tasks.js +340 -0
- package/dist/@vitest/runner/index.d.ts +180 -0
- package/dist/@vitest/runner/index.js +2114 -0
- package/dist/@vitest/runner/tasks.d-C7UxawJ9.d.ts +834 -0
- package/dist/@vitest/runner/types.d.ts +183 -0
- package/dist/@vitest/runner/types.js +1 -0
- package/dist/@vitest/runner/utils.d.ts +45 -0
- package/dist/@vitest/runner/utils.js +5 -0
- package/dist/@vitest/snapshot/environment.d-DHdQ1Csl.d.ts +22 -0
- package/dist/@vitest/snapshot/environment.d.ts +16 -0
- package/dist/@vitest/snapshot/environment.js +40 -0
- package/dist/@vitest/snapshot/index.d.ts +130 -0
- package/dist/@vitest/snapshot/index.js +1437 -0
- package/dist/@vitest/snapshot/manager.d.ts +18 -0
- package/dist/@vitest/snapshot/manager.js +73 -0
- package/dist/@vitest/snapshot/rawSnapshot.d-lFsMJFUd.d.ts +61 -0
- package/dist/@vitest/spy/index.d.ts +384 -0
- package/dist/@vitest/spy/index.js +433 -0
- package/dist/@vitest/utils/chunk-_commonjsHelpers.js +5 -0
- package/dist/@vitest/utils/chunk-pathe.M-eThtNZ.js +156 -0
- package/dist/@vitest/utils/constants.d.ts +21 -0
- package/dist/@vitest/utils/constants.js +49 -0
- package/dist/@vitest/utils/diff.d.ts +93 -0
- package/dist/@vitest/utils/diff.js +2199 -0
- package/dist/@vitest/utils/display.d.ts +29 -0
- package/dist/@vitest/utils/display.js +742 -0
- package/dist/@vitest/utils/error.d.ts +7 -0
- package/dist/@vitest/utils/error.js +42 -0
- package/dist/@vitest/utils/helpers.d.ts +73 -0
- package/dist/@vitest/utils/helpers.js +295 -0
- package/dist/@vitest/utils/highlight.d.ts +9 -0
- package/dist/@vitest/utils/highlight.js +538 -0
- package/dist/@vitest/utils/index.d.ts +5 -0
- package/dist/@vitest/utils/index.js +1 -0
- package/dist/@vitest/utils/offset.d.ts +5 -0
- package/dist/@vitest/utils/offset.js +32 -0
- package/dist/@vitest/utils/resolver.d.ts +7 -0
- package/dist/@vitest/utils/resolver.js +71 -0
- package/dist/@vitest/utils/serialize.d.ts +3 -0
- package/dist/@vitest/utils/serialize.js +118 -0
- package/dist/@vitest/utils/source-map.d.ts +55 -0
- package/dist/@vitest/utils/source-map.js +478 -0
- package/dist/@vitest/utils/timers.d.ts +33 -0
- package/dist/@vitest/utils/timers.js +49 -0
- package/dist/@vitest/utils/types.d-BCElaP-c.d.ts +53 -0
- package/dist/@vitest/utils/types.d.ts +34 -0
- package/dist/@vitest/utils/types.js +1 -0
- package/dist/browser-compat.js +3 -0
- package/dist/browser.d.ts +46 -0
- package/dist/browser.js +20 -0
- package/dist/chunks/_commonjsHelpers.D26ty3Ew.js +6 -0
- package/dist/chunks/base.CJ0Y4ePK.js +165 -0
- package/dist/chunks/benchmark.B3N2zMcH.js +40 -0
- package/dist/chunks/benchmark.d.DAaHLpsq.d.ts +24 -0
- package/dist/chunks/browser.d.ChKACdzH.d.ts +59 -0
- package/dist/chunks/cac.DVeoLl0M.js +1409 -0
- package/dist/chunks/cli-api.B7PN_QUv.js +13672 -0
- package/dist/chunks/config.d.Cy95HiCx.d.ts +210 -0
- package/dist/chunks/console.Cf-YriPC.js +146 -0
- package/dist/chunks/constants.D_Q9UYh-.js +36 -0
- package/dist/chunks/coverage.AVPTjMgw.js +3292 -0
- package/dist/chunks/coverage.D_JHT54q.js +25 -0
- package/dist/chunks/coverage.d.BZtK59WP.d.ts +37 -0
- package/dist/chunks/creator.DAmOKTvJ.js +673 -0
- package/dist/chunks/date.Bq6ZW5rf.js +73 -0
- package/dist/chunks/defaults.BOqNVLsY.js +74 -0
- package/dist/chunks/env.D4Lgay0q.js +8 -0
- package/dist/chunks/environment.d.CrsxCzP1.d.ts +29 -0
- package/dist/chunks/evaluatedModules.Dg1zASAC.js +17 -0
- package/dist/chunks/evaluatedModules.d.BxJ5omdx.d.ts +7 -0
- package/dist/chunks/git.Bm2pzPAa.js +71 -0
- package/dist/chunks/global.d.B15mdLcR.d.ts +99 -0
- package/dist/chunks/globals.DOayXfHP.js +30 -0
- package/dist/chunks/index.6Qv1eEA6.js +109 -0
- package/dist/chunks/index.C5r1PdPD.js +231 -0
- package/dist/chunks/index.Chj8NDwU.js +206 -0
- package/dist/chunks/index.CyBMJtT7.js +727 -0
- package/dist/chunks/index.D3XRDfWc.js +213 -0
- package/dist/chunks/index.D4KonVSU.js +6343 -0
- package/dist/chunks/index.M8mOzt4Y.js +3839 -0
- package/dist/chunks/index.Z5E_ObnR.js +37 -0
- package/dist/chunks/init-forks._y3TW739.js +41 -0
- package/dist/chunks/init-threads.DBO2kn-p.js +18 -0
- package/dist/chunks/init.B6MLFIaN.js +334 -0
- package/dist/chunks/inspector.CvyFGlXm.js +53 -0
- package/dist/chunks/modules.BJuCwlRJ.js +36 -0
- package/dist/chunks/node.Ce0vMQM7.js +14 -0
- package/dist/chunks/plugin.d.CtqpEehP.d.ts +38 -0
- package/dist/chunks/reporters.d.CWXNI2jG.d.ts +3271 -0
- package/dist/chunks/rpc.BoxB0q7B.js +76 -0
- package/dist/chunks/rpc.d.RH3apGEf.d.ts +64 -0
- package/dist/chunks/setup-common.Cm-kSBVi.js +60 -0
- package/dist/chunks/startModuleRunner.DEj0jb3e.js +861 -0
- package/dist/chunks/suite.d.BJWk38HB.d.ts +10 -0
- package/dist/chunks/test.B8ej_ZHS.js +254 -0
- package/dist/chunks/traces.CCmnQaNT.js +217 -0
- package/dist/chunks/traces.d.402V_yFI.d.ts +18 -0
- package/dist/chunks/utils.DvEY5TfP.js +52 -0
- package/dist/chunks/vi.2VT5v0um.js +3919 -0
- package/dist/chunks/vm.D3epNOPZ.js +744 -0
- package/dist/chunks/worker.d.Dyxm8DEL.d.ts +255 -0
- package/dist/cli.js +28 -0
- package/dist/client/.vite/manifest.json +24 -0
- package/dist/client/__vitest__/assets/index-BUCFJtth.js +57 -0
- package/dist/client/__vitest__/assets/index-DlhE0rqZ.css +1 -0
- package/dist/client/__vitest__/bg.png +0 -0
- package/dist/client/__vitest__/favicon.ico +0 -0
- package/dist/client/__vitest__/favicon.svg +5 -0
- package/dist/client/__vitest__/index.html +32 -0
- package/dist/client/__vitest_browser__/orchestrator-S_3e_uzt.js +345 -0
- package/dist/client/__vitest_browser__/tester-k74mgIRa.js +2171 -0
- package/dist/client/__vitest_browser__/utils-uxqdqUz8.js +223 -0
- package/dist/client/error-catcher.js +82 -0
- package/dist/client/esm-client-injector.js +67 -0
- package/dist/client/favicon.svg +5 -0
- package/dist/client/orchestrator.html +35 -0
- package/dist/client/tester/tester.html +13 -0
- package/dist/client.js +456 -0
- package/dist/config.cjs +94 -0
- package/dist/config.d.ts +104 -0
- package/dist/config.js +15 -0
- package/dist/context.js +541 -0
- package/dist/coverage.d.ts +118 -0
- package/dist/coverage.js +23 -0
- package/dist/dummy.js +2 -0
- package/dist/environments.d.ts +22 -0
- package/dist/environments.js +3 -0
- package/dist/expect-element.js +27 -0
- package/dist/index-D6m36C6U.js +6 -0
- package/dist/index-node.js +7 -0
- package/dist/index.d.ts +510 -0
- package/dist/index.js +19 -0
- package/dist/locators.d.ts +354 -0
- package/dist/locators.js +1 -0
- package/dist/mocker.d.ts +1 -0
- package/dist/mocker.js +1 -0
- package/dist/module-evaluator.d.ts +124 -0
- package/dist/module-evaluator.js +343 -0
- package/dist/module-runner-stub.js +44 -0
- package/dist/module-runner.js +17 -0
- package/dist/node.d.ts +251 -0
- package/dist/node.js +98 -0
- package/dist/path.js +7 -0
- package/dist/plugins/browser-client.mjs +2 -0
- package/dist/plugins/browser-context.mjs +2 -0
- package/dist/plugins/browser-locators.mjs +2 -0
- package/dist/plugins/browser-playwright.mjs +2 -0
- package/dist/plugins/browser-preview.mjs +2 -0
- package/dist/plugins/browser-webdriverio.mjs +2 -0
- package/dist/plugins/browser.mjs +2 -0
- package/dist/plugins/expect.mjs +2 -0
- package/dist/plugins/mocker-automock.mjs +2 -0
- package/dist/plugins/mocker-browser.mjs +2 -0
- package/dist/plugins/mocker-node.mjs +2 -0
- package/dist/plugins/mocker-redirect.mjs +2 -0
- package/dist/plugins/mocker-register.mjs +2 -0
- package/dist/plugins/mocker.mjs +2 -0
- package/dist/plugins/pretty-format.mjs +2 -0
- package/dist/plugins/runner-types.mjs +2 -0
- package/dist/plugins/runner-utils.mjs +2 -0
- package/dist/plugins/runner.mjs +2 -0
- package/dist/plugins/snapshot-environment.mjs +2 -0
- package/dist/plugins/snapshot-manager.mjs +2 -0
- package/dist/plugins/snapshot.mjs +2 -0
- package/dist/plugins/spy.mjs +2 -0
- package/dist/plugins/utils-constants.mjs +2 -0
- package/dist/plugins/utils-diff.mjs +2 -0
- package/dist/plugins/utils-display.mjs +2 -0
- package/dist/plugins/utils-error.mjs +2 -0
- package/dist/plugins/utils-helpers.mjs +2 -0
- package/dist/plugins/utils-highlight.mjs +2 -0
- package/dist/plugins/utils-offset.mjs +2 -0
- package/dist/plugins/utils-resolver.mjs +2 -0
- package/dist/plugins/utils-serialize.mjs +2 -0
- package/dist/plugins/utils-source-map.mjs +2 -0
- package/dist/plugins/utils-timers.mjs +2 -0
- package/dist/plugins/utils.mjs +2 -0
- package/dist/reporters.d.ts +27 -0
- package/dist/reporters.js +24 -0
- package/dist/runners.d.ts +50 -0
- package/dist/runners.js +19 -0
- package/dist/shared/screenshotMatcher/types.d.ts +22 -0
- package/dist/snapshot.d.ts +9 -0
- package/dist/snapshot.js +4 -0
- package/dist/spy.js +1 -0
- package/dist/state.js +280 -0
- package/dist/suite.d.ts +5 -0
- package/dist/suite.js +6 -0
- package/dist/types.d.ts +69 -0
- package/dist/vendor/chai.d.mts +1 -0
- package/dist/vendor/chai.mjs +3577 -0
- package/dist/vendor/es-module-lexer.d.mts +193 -0
- package/dist/vendor/es-module-lexer.mjs +79 -0
- package/dist/vendor/estree-walker.d.mts +583 -0
- package/dist/vendor/estree-walker.mjs +339 -0
- package/dist/vendor/expect-type.d.mts +1574 -0
- package/dist/vendor/expect-type.mjs +214 -0
- package/dist/vendor/magic-string.d.mts +261 -0
- package/dist/vendor/magic-string.mjs +1700 -0
- package/dist/vendor/obug.d.mts +56 -0
- package/dist/vendor/obug.mjs +276 -0
- package/dist/vendor/pathe.d.mts +46 -0
- package/dist/vendor/pathe.mjs +496 -0
- package/dist/vendor/picomatch.d.mts +1 -0
- package/dist/vendor/picomatch.mjs +1855 -0
- package/dist/vendor/shared-3g9mwCWP.mjs +31 -0
- package/dist/vendor/std-env.d.mts +88 -0
- package/dist/vendor/std-env.mjs +159 -0
- package/dist/vendor/tinybench.d.mts +317 -0
- package/dist/vendor/tinybench.mjs +504 -0
- package/dist/vendor/tinyexec.d.mts +72 -0
- package/dist/vendor/tinyexec.mjs +637 -0
- package/dist/vendor/tinyglobby.d.mts +157 -0
- package/dist/vendor/tinyglobby.mjs +832 -0
- package/dist/vendor/tinyrainbow.d.mts +60 -0
- package/dist/vendor/tinyrainbow.mjs +93 -0
- package/dist/vendor/vitest_browser.mjs +2 -0
- package/dist/vendor/vitest_internal_browser.mjs +2 -0
- package/dist/vendor/vitest_runner.mjs +2 -0
- package/dist/vendor/vitest_runners.mjs +2 -0
- package/dist/worker.d.ts +32 -0
- package/dist/worker.js +48 -0
- package/dist/workers/forks.js +54 -0
- package/dist/workers/runVmTests.js +95 -0
- package/dist/workers/threads.js +55 -0
- package/dist/workers/vmForks.js +36 -0
- package/dist/workers/vmThreads.js +37 -0
- package/environments.d.ts +1 -0
- package/globals.d.ts +20 -0
- package/import-meta.d.ts +5 -0
- package/importMeta.d.ts +4 -0
- package/index.cjs +5 -0
- package/index.d.cts +1 -0
- package/jsdom.d.ts +6 -0
- package/mocker.d.ts +1 -0
- package/node.d.ts +1 -0
- package/optional-types.d.ts +7 -0
- package/package.json +335 -0
- package/reporters.d.ts +1 -0
- package/runners.d.ts +1 -0
- package/snapshot.d.ts +1 -0
- package/suite.d.ts +1 -0
- package/suppress-warnings.cjs +21 -0
- package/vitest.mjs +2 -0
- package/worker.d.ts +1 -0
|
@@ -0,0 +1,2199 @@
|
|
|
1
|
+
import { plugins, format } from '../pretty-format/index.js';
|
|
2
|
+
import c from '../../vendor/tinyrainbow.mjs';
|
|
3
|
+
import { stringify } from './display.js';
|
|
4
|
+
import { deepClone, getOwnProperties, getType as getType$1 } from './helpers.js';
|
|
5
|
+
import { g as getDefaultExportFromCjs } from './chunk-_commonjsHelpers.js';
|
|
6
|
+
import './constants.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Diff Match and Patch
|
|
10
|
+
* Copyright 2018 The diff-match-patch Authors.
|
|
11
|
+
* https://github.com/google/diff-match-patch
|
|
12
|
+
*
|
|
13
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License.
|
|
15
|
+
* You may obtain a copy of the License at
|
|
16
|
+
*
|
|
17
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
*
|
|
19
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
20
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
21
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
22
|
+
* See the License for the specific language governing permissions and
|
|
23
|
+
* limitations under the License.
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* @fileoverview Computes the difference between two texts to create a patch.
|
|
27
|
+
* Applies the patch onto another text, allowing for errors.
|
|
28
|
+
* @author fraser@google.com (Neil Fraser)
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* CHANGES by pedrottimark to diff_match_patch_uncompressed.ts file:
|
|
32
|
+
*
|
|
33
|
+
* 1. Delete anything not needed to use diff_cleanupSemantic method
|
|
34
|
+
* 2. Convert from prototype properties to var declarations
|
|
35
|
+
* 3. Convert Diff to class from constructor and prototype
|
|
36
|
+
* 4. Add type annotations for arguments and return values
|
|
37
|
+
* 5. Add exports
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* The data structure representing a diff is an array of tuples:
|
|
41
|
+
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
|
|
42
|
+
* which means: delete 'Hello', add 'Goodbye' and keep ' world.'
|
|
43
|
+
*/
|
|
44
|
+
const DIFF_DELETE = -1;
|
|
45
|
+
const DIFF_INSERT = 1;
|
|
46
|
+
const DIFF_EQUAL = 0;
|
|
47
|
+
/**
|
|
48
|
+
* Class representing one diff tuple.
|
|
49
|
+
* Attempts to look like a two-element array (which is what this used to be).
|
|
50
|
+
* @param {number} op Operation, one of: DIFF_DELETE, DIFF_INSERT, DIFF_EQUAL.
|
|
51
|
+
* @param {string} text Text to be deleted, inserted, or retained.
|
|
52
|
+
* @constructor
|
|
53
|
+
*/
|
|
54
|
+
class Diff {
|
|
55
|
+
0;
|
|
56
|
+
1;
|
|
57
|
+
constructor(op, text) {
|
|
58
|
+
this[0] = op;
|
|
59
|
+
this[1] = text;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Determine the common prefix of two strings.
|
|
64
|
+
* @param {string} text1 First string.
|
|
65
|
+
* @param {string} text2 Second string.
|
|
66
|
+
* @return {number} The number of characters common to the start of each
|
|
67
|
+
* string.
|
|
68
|
+
*/
|
|
69
|
+
function diff_commonPrefix(text1, text2) {
|
|
70
|
+
// Quick check for common null cases.
|
|
71
|
+
if (!text1 || !text2 || text1.charAt(0) !== text2.charAt(0)) {
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
// Binary search.
|
|
75
|
+
// Performance analysis: https://neil.fraser.name/news/2007/10/09/
|
|
76
|
+
let pointermin = 0;
|
|
77
|
+
let pointermax = Math.min(text1.length, text2.length);
|
|
78
|
+
let pointermid = pointermax;
|
|
79
|
+
let pointerstart = 0;
|
|
80
|
+
while (pointermin < pointermid) {
|
|
81
|
+
if (text1.substring(pointerstart, pointermid) === text2.substring(pointerstart, pointermid)) {
|
|
82
|
+
pointermin = pointermid;
|
|
83
|
+
pointerstart = pointermin;
|
|
84
|
+
} else {
|
|
85
|
+
pointermax = pointermid;
|
|
86
|
+
}
|
|
87
|
+
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin);
|
|
88
|
+
}
|
|
89
|
+
return pointermid;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Determine the common suffix of two strings.
|
|
93
|
+
* @param {string} text1 First string.
|
|
94
|
+
* @param {string} text2 Second string.
|
|
95
|
+
* @return {number} The number of characters common to the end of each string.
|
|
96
|
+
*/
|
|
97
|
+
function diff_commonSuffix(text1, text2) {
|
|
98
|
+
// Quick check for common null cases.
|
|
99
|
+
if (!text1 || !text2 || text1.charAt(text1.length - 1) !== text2.charAt(text2.length - 1)) {
|
|
100
|
+
return 0;
|
|
101
|
+
}
|
|
102
|
+
// Binary search.
|
|
103
|
+
// Performance analysis: https://neil.fraser.name/news/2007/10/09/
|
|
104
|
+
let pointermin = 0;
|
|
105
|
+
let pointermax = Math.min(text1.length, text2.length);
|
|
106
|
+
let pointermid = pointermax;
|
|
107
|
+
let pointerend = 0;
|
|
108
|
+
while (pointermin < pointermid) {
|
|
109
|
+
if (text1.substring(text1.length - pointermid, text1.length - pointerend) === text2.substring(text2.length - pointermid, text2.length - pointerend)) {
|
|
110
|
+
pointermin = pointermid;
|
|
111
|
+
pointerend = pointermin;
|
|
112
|
+
} else {
|
|
113
|
+
pointermax = pointermid;
|
|
114
|
+
}
|
|
115
|
+
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin);
|
|
116
|
+
}
|
|
117
|
+
return pointermid;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Determine if the suffix of one string is the prefix of another.
|
|
121
|
+
* @param {string} text1 First string.
|
|
122
|
+
* @param {string} text2 Second string.
|
|
123
|
+
* @return {number} The number of characters common to the end of the first
|
|
124
|
+
* string and the start of the second string.
|
|
125
|
+
* @private
|
|
126
|
+
*/
|
|
127
|
+
function diff_commonOverlap_(text1, text2) {
|
|
128
|
+
// Cache the text lengths to prevent multiple calls.
|
|
129
|
+
const text1_length = text1.length;
|
|
130
|
+
const text2_length = text2.length;
|
|
131
|
+
// Eliminate the null case.
|
|
132
|
+
if (text1_length === 0 || text2_length === 0) {
|
|
133
|
+
return 0;
|
|
134
|
+
}
|
|
135
|
+
// Truncate the longer string.
|
|
136
|
+
if (text1_length > text2_length) {
|
|
137
|
+
text1 = text1.substring(text1_length - text2_length);
|
|
138
|
+
} else if (text1_length < text2_length) {
|
|
139
|
+
text2 = text2.substring(0, text1_length);
|
|
140
|
+
}
|
|
141
|
+
const text_length = Math.min(text1_length, text2_length);
|
|
142
|
+
// Quick check for the worst case.
|
|
143
|
+
if (text1 === text2) {
|
|
144
|
+
return text_length;
|
|
145
|
+
}
|
|
146
|
+
// Start by looking for a single character match
|
|
147
|
+
// and increase length until no match is found.
|
|
148
|
+
// Performance analysis: https://neil.fraser.name/news/2010/11/04/
|
|
149
|
+
let best = 0;
|
|
150
|
+
let length = 1;
|
|
151
|
+
while (true) {
|
|
152
|
+
const pattern = text1.substring(text_length - length);
|
|
153
|
+
const found = text2.indexOf(pattern);
|
|
154
|
+
if (found === -1) {
|
|
155
|
+
return best;
|
|
156
|
+
}
|
|
157
|
+
length += found;
|
|
158
|
+
if (found === 0 || text1.substring(text_length - length) === text2.substring(0, length)) {
|
|
159
|
+
best = length;
|
|
160
|
+
length++;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Reduce the number of edits by eliminating semantically trivial equalities.
|
|
166
|
+
* @param {!Array.<!diff_match_patch.Diff>} diffs Array of diff tuples.
|
|
167
|
+
*/
|
|
168
|
+
function diff_cleanupSemantic(diffs) {
|
|
169
|
+
let changes = false;
|
|
170
|
+
const equalities = [];
|
|
171
|
+
let equalitiesLength = 0;
|
|
172
|
+
/** @type {?string} */
|
|
173
|
+
let lastEquality = null;
|
|
174
|
+
// Always equal to diffs[equalities[equalitiesLength - 1]][1]
|
|
175
|
+
let pointer = 0;
|
|
176
|
+
// Number of characters that changed prior to the equality.
|
|
177
|
+
let length_insertions1 = 0;
|
|
178
|
+
let length_deletions1 = 0;
|
|
179
|
+
// Number of characters that changed after the equality.
|
|
180
|
+
let length_insertions2 = 0;
|
|
181
|
+
let length_deletions2 = 0;
|
|
182
|
+
while (pointer < diffs.length) {
|
|
183
|
+
if (diffs[pointer][0] === DIFF_EQUAL) {
|
|
184
|
+
// Equality found.
|
|
185
|
+
equalities[equalitiesLength++] = pointer;
|
|
186
|
+
length_insertions1 = length_insertions2;
|
|
187
|
+
length_deletions1 = length_deletions2;
|
|
188
|
+
length_insertions2 = 0;
|
|
189
|
+
length_deletions2 = 0;
|
|
190
|
+
lastEquality = diffs[pointer][1];
|
|
191
|
+
} else {
|
|
192
|
+
// An insertion or deletion.
|
|
193
|
+
if (diffs[pointer][0] === DIFF_INSERT) {
|
|
194
|
+
length_insertions2 += diffs[pointer][1].length;
|
|
195
|
+
} else {
|
|
196
|
+
length_deletions2 += diffs[pointer][1].length;
|
|
197
|
+
}
|
|
198
|
+
// Eliminate an equality that is smaller or equal to the edits on both
|
|
199
|
+
// sides of it.
|
|
200
|
+
if (lastEquality && lastEquality.length <= Math.max(length_insertions1, length_deletions1) && lastEquality.length <= Math.max(length_insertions2, length_deletions2)) {
|
|
201
|
+
// Duplicate record.
|
|
202
|
+
diffs.splice(equalities[equalitiesLength - 1], 0, new Diff(DIFF_DELETE, lastEquality));
|
|
203
|
+
// Change second copy to insert.
|
|
204
|
+
diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT;
|
|
205
|
+
// Throw away the equality we just deleted.
|
|
206
|
+
equalitiesLength--;
|
|
207
|
+
// Throw away the previous equality (it needs to be reevaluated).
|
|
208
|
+
equalitiesLength--;
|
|
209
|
+
pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1;
|
|
210
|
+
length_insertions1 = 0;
|
|
211
|
+
length_deletions1 = 0;
|
|
212
|
+
length_insertions2 = 0;
|
|
213
|
+
length_deletions2 = 0;
|
|
214
|
+
lastEquality = null;
|
|
215
|
+
changes = true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
pointer++;
|
|
219
|
+
}
|
|
220
|
+
// Normalize the diff.
|
|
221
|
+
if (changes) {
|
|
222
|
+
diff_cleanupMerge(diffs);
|
|
223
|
+
}
|
|
224
|
+
diff_cleanupSemanticLossless(diffs);
|
|
225
|
+
// Find any overlaps between deletions and insertions.
|
|
226
|
+
// e.g: <del>abcxxx</del><ins>xxxdef</ins>
|
|
227
|
+
// -> <del>abc</del>xxx<ins>def</ins>
|
|
228
|
+
// e.g: <del>xxxabc</del><ins>defxxx</ins>
|
|
229
|
+
// -> <ins>def</ins>xxx<del>abc</del>
|
|
230
|
+
// Only extract an overlap if it is as big as the edit ahead or behind it.
|
|
231
|
+
pointer = 1;
|
|
232
|
+
while (pointer < diffs.length) {
|
|
233
|
+
if (diffs[pointer - 1][0] === DIFF_DELETE && diffs[pointer][0] === DIFF_INSERT) {
|
|
234
|
+
const deletion = diffs[pointer - 1][1];
|
|
235
|
+
const insertion = diffs[pointer][1];
|
|
236
|
+
const overlap_length1 = diff_commonOverlap_(deletion, insertion);
|
|
237
|
+
const overlap_length2 = diff_commonOverlap_(insertion, deletion);
|
|
238
|
+
if (overlap_length1 >= overlap_length2) {
|
|
239
|
+
if (overlap_length1 >= deletion.length / 2 || overlap_length1 >= insertion.length / 2) {
|
|
240
|
+
// Overlap found. Insert an equality and trim the surrounding edits.
|
|
241
|
+
diffs.splice(pointer, 0, new Diff(DIFF_EQUAL, insertion.substring(0, overlap_length1)));
|
|
242
|
+
diffs[pointer - 1][1] = deletion.substring(0, deletion.length - overlap_length1);
|
|
243
|
+
diffs[pointer + 1][1] = insertion.substring(overlap_length1);
|
|
244
|
+
pointer++;
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
if (overlap_length2 >= deletion.length / 2 || overlap_length2 >= insertion.length / 2) {
|
|
248
|
+
// Reverse overlap found.
|
|
249
|
+
// Insert an equality and swap and trim the surrounding edits.
|
|
250
|
+
diffs.splice(pointer, 0, new Diff(DIFF_EQUAL, deletion.substring(0, overlap_length2)));
|
|
251
|
+
diffs[pointer - 1][0] = DIFF_INSERT;
|
|
252
|
+
diffs[pointer - 1][1] = insertion.substring(0, insertion.length - overlap_length2);
|
|
253
|
+
diffs[pointer + 1][0] = DIFF_DELETE;
|
|
254
|
+
diffs[pointer + 1][1] = deletion.substring(overlap_length2);
|
|
255
|
+
pointer++;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
pointer++;
|
|
259
|
+
}
|
|
260
|
+
pointer++;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
// Define some regex patterns for matching boundaries.
|
|
264
|
+
const nonAlphaNumericRegex_ = /[^a-z0-9]/i;
|
|
265
|
+
const whitespaceRegex_ = /\s/;
|
|
266
|
+
const linebreakRegex_ = /[\r\n]/;
|
|
267
|
+
const blanklineEndRegex_ = /\n\r?\n$/;
|
|
268
|
+
const blanklineStartRegex_ = /^\r?\n\r?\n/;
|
|
269
|
+
/**
|
|
270
|
+
* Look for single edits surrounded on both sides by equalities
|
|
271
|
+
* which can be shifted sideways to align the edit to a word boundary.
|
|
272
|
+
* e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
|
|
273
|
+
* @param {!Array.<!diff_match_patch.Diff>} diffs Array of diff tuples.
|
|
274
|
+
*/
|
|
275
|
+
function diff_cleanupSemanticLossless(diffs) {
|
|
276
|
+
let pointer = 1;
|
|
277
|
+
// Intentionally ignore the first and last element (don't need checking).
|
|
278
|
+
while (pointer < diffs.length - 1) {
|
|
279
|
+
if (diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL) {
|
|
280
|
+
// This is a single edit surrounded by equalities.
|
|
281
|
+
let equality1 = diffs[pointer - 1][1];
|
|
282
|
+
let edit = diffs[pointer][1];
|
|
283
|
+
let equality2 = diffs[pointer + 1][1];
|
|
284
|
+
// First, shift the edit as far left as possible.
|
|
285
|
+
const commonOffset = diff_commonSuffix(equality1, edit);
|
|
286
|
+
if (commonOffset) {
|
|
287
|
+
const commonString = edit.substring(edit.length - commonOffset);
|
|
288
|
+
equality1 = equality1.substring(0, equality1.length - commonOffset);
|
|
289
|
+
edit = commonString + edit.substring(0, edit.length - commonOffset);
|
|
290
|
+
equality2 = commonString + equality2;
|
|
291
|
+
}
|
|
292
|
+
// Second, step character by character right, looking for the best fit.
|
|
293
|
+
let bestEquality1 = equality1;
|
|
294
|
+
let bestEdit = edit;
|
|
295
|
+
let bestEquality2 = equality2;
|
|
296
|
+
let bestScore = diff_cleanupSemanticScore_(equality1, edit) + diff_cleanupSemanticScore_(edit, equality2);
|
|
297
|
+
while (edit.charAt(0) === equality2.charAt(0)) {
|
|
298
|
+
equality1 += edit.charAt(0);
|
|
299
|
+
edit = edit.substring(1) + equality2.charAt(0);
|
|
300
|
+
equality2 = equality2.substring(1);
|
|
301
|
+
const score = diff_cleanupSemanticScore_(equality1, edit) + diff_cleanupSemanticScore_(edit, equality2);
|
|
302
|
+
// The >= encourages trailing rather than leading whitespace on edits.
|
|
303
|
+
if (score >= bestScore) {
|
|
304
|
+
bestScore = score;
|
|
305
|
+
bestEquality1 = equality1;
|
|
306
|
+
bestEdit = edit;
|
|
307
|
+
bestEquality2 = equality2;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (diffs[pointer - 1][1] !== bestEquality1) {
|
|
311
|
+
// We have an improvement, save it back to the diff.
|
|
312
|
+
if (bestEquality1) {
|
|
313
|
+
diffs[pointer - 1][1] = bestEquality1;
|
|
314
|
+
} else {
|
|
315
|
+
diffs.splice(pointer - 1, 1);
|
|
316
|
+
pointer--;
|
|
317
|
+
}
|
|
318
|
+
diffs[pointer][1] = bestEdit;
|
|
319
|
+
if (bestEquality2) {
|
|
320
|
+
diffs[pointer + 1][1] = bestEquality2;
|
|
321
|
+
} else {
|
|
322
|
+
diffs.splice(pointer + 1, 1);
|
|
323
|
+
pointer--;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
pointer++;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Reorder and merge like edit sections. Merge equalities.
|
|
332
|
+
* Any edit section can move as long as it doesn't cross an equality.
|
|
333
|
+
* @param {!Array.<!diff_match_patch.Diff>} diffs Array of diff tuples.
|
|
334
|
+
*/
|
|
335
|
+
function diff_cleanupMerge(diffs) {
|
|
336
|
+
var _diffs$at;
|
|
337
|
+
// Add a dummy entry at the end.
|
|
338
|
+
diffs.push(new Diff(DIFF_EQUAL, ""));
|
|
339
|
+
let pointer = 0;
|
|
340
|
+
let count_delete = 0;
|
|
341
|
+
let count_insert = 0;
|
|
342
|
+
let text_delete = "";
|
|
343
|
+
let text_insert = "";
|
|
344
|
+
let commonlength;
|
|
345
|
+
while (pointer < diffs.length) {
|
|
346
|
+
switch (diffs[pointer][0]) {
|
|
347
|
+
case DIFF_INSERT:
|
|
348
|
+
count_insert++;
|
|
349
|
+
text_insert += diffs[pointer][1];
|
|
350
|
+
pointer++;
|
|
351
|
+
break;
|
|
352
|
+
case DIFF_DELETE:
|
|
353
|
+
count_delete++;
|
|
354
|
+
text_delete += diffs[pointer][1];
|
|
355
|
+
pointer++;
|
|
356
|
+
break;
|
|
357
|
+
case DIFF_EQUAL:
|
|
358
|
+
// Upon reaching an equality, check for prior redundancies.
|
|
359
|
+
if (count_delete + count_insert > 1) {
|
|
360
|
+
if (count_delete !== 0 && count_insert !== 0) {
|
|
361
|
+
// Factor out any common prefixes.
|
|
362
|
+
commonlength = diff_commonPrefix(text_insert, text_delete);
|
|
363
|
+
if (commonlength !== 0) {
|
|
364
|
+
if (pointer - count_delete - count_insert > 0 && diffs[pointer - count_delete - count_insert - 1][0] === DIFF_EQUAL) {
|
|
365
|
+
diffs[pointer - count_delete - count_insert - 1][1] += text_insert.substring(0, commonlength);
|
|
366
|
+
} else {
|
|
367
|
+
diffs.splice(0, 0, new Diff(DIFF_EQUAL, text_insert.substring(0, commonlength)));
|
|
368
|
+
pointer++;
|
|
369
|
+
}
|
|
370
|
+
text_insert = text_insert.substring(commonlength);
|
|
371
|
+
text_delete = text_delete.substring(commonlength);
|
|
372
|
+
}
|
|
373
|
+
// Factor out any common suffixes.
|
|
374
|
+
commonlength = diff_commonSuffix(text_insert, text_delete);
|
|
375
|
+
if (commonlength !== 0) {
|
|
376
|
+
diffs[pointer][1] = text_insert.substring(text_insert.length - commonlength) + diffs[pointer][1];
|
|
377
|
+
text_insert = text_insert.substring(0, text_insert.length - commonlength);
|
|
378
|
+
text_delete = text_delete.substring(0, text_delete.length - commonlength);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
// Delete the offending records and add the merged ones.
|
|
382
|
+
pointer -= count_delete + count_insert;
|
|
383
|
+
diffs.splice(pointer, count_delete + count_insert);
|
|
384
|
+
if (text_delete.length) {
|
|
385
|
+
diffs.splice(pointer, 0, new Diff(DIFF_DELETE, text_delete));
|
|
386
|
+
pointer++;
|
|
387
|
+
}
|
|
388
|
+
if (text_insert.length) {
|
|
389
|
+
diffs.splice(pointer, 0, new Diff(DIFF_INSERT, text_insert));
|
|
390
|
+
pointer++;
|
|
391
|
+
}
|
|
392
|
+
pointer++;
|
|
393
|
+
} else if (pointer !== 0 && diffs[pointer - 1][0] === DIFF_EQUAL) {
|
|
394
|
+
// Merge this equality with the previous one.
|
|
395
|
+
diffs[pointer - 1][1] += diffs[pointer][1];
|
|
396
|
+
diffs.splice(pointer, 1);
|
|
397
|
+
} else {
|
|
398
|
+
pointer++;
|
|
399
|
+
}
|
|
400
|
+
count_insert = 0;
|
|
401
|
+
count_delete = 0;
|
|
402
|
+
text_delete = "";
|
|
403
|
+
text_insert = "";
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (((_diffs$at = diffs.at(-1)) === null || _diffs$at === void 0 ? void 0 : _diffs$at[1]) === "") {
|
|
408
|
+
diffs.pop();
|
|
409
|
+
}
|
|
410
|
+
// Second pass: look for single edits surrounded on both sides by equalities
|
|
411
|
+
// which can be shifted sideways to eliminate an equality.
|
|
412
|
+
// e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC
|
|
413
|
+
let changes = false;
|
|
414
|
+
pointer = 1;
|
|
415
|
+
// Intentionally ignore the first and last element (don't need checking).
|
|
416
|
+
while (pointer < diffs.length - 1) {
|
|
417
|
+
if (diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL) {
|
|
418
|
+
// This is a single edit surrounded by equalities.
|
|
419
|
+
if (diffs[pointer][1].substring(diffs[pointer][1].length - diffs[pointer - 1][1].length) === diffs[pointer - 1][1]) {
|
|
420
|
+
// Shift the edit over the previous equality.
|
|
421
|
+
diffs[pointer][1] = diffs[pointer - 1][1] + diffs[pointer][1].substring(0, diffs[pointer][1].length - diffs[pointer - 1][1].length);
|
|
422
|
+
diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1];
|
|
423
|
+
diffs.splice(pointer - 1, 1);
|
|
424
|
+
changes = true;
|
|
425
|
+
} else if (diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) === diffs[pointer + 1][1]) {
|
|
426
|
+
// Shift the edit over the next equality.
|
|
427
|
+
diffs[pointer - 1][1] += diffs[pointer + 1][1];
|
|
428
|
+
diffs[pointer][1] = diffs[pointer][1].substring(diffs[pointer + 1][1].length) + diffs[pointer + 1][1];
|
|
429
|
+
diffs.splice(pointer + 1, 1);
|
|
430
|
+
changes = true;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
pointer++;
|
|
434
|
+
}
|
|
435
|
+
// If shifts were made, the diff needs reordering and another shift sweep.
|
|
436
|
+
if (changes) {
|
|
437
|
+
diff_cleanupMerge(diffs);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Given two strings, compute a score representing whether the internal
|
|
442
|
+
* boundary falls on logical boundaries.
|
|
443
|
+
* Scores range from 6 (best) to 0 (worst).
|
|
444
|
+
* Closure, but does not reference any external variables.
|
|
445
|
+
* @param {string} one First string.
|
|
446
|
+
* @param {string} two Second string.
|
|
447
|
+
* @return {number} The score.
|
|
448
|
+
* @private
|
|
449
|
+
*/
|
|
450
|
+
function diff_cleanupSemanticScore_(one, two) {
|
|
451
|
+
if (!one || !two) {
|
|
452
|
+
// Edges are the best.
|
|
453
|
+
return 6;
|
|
454
|
+
}
|
|
455
|
+
// Each port of this function behaves slightly differently due to
|
|
456
|
+
// subtle differences in each language's definition of things like
|
|
457
|
+
// 'whitespace'. Since this function's purpose is largely cosmetic,
|
|
458
|
+
// the choice has been made to use each language's native features
|
|
459
|
+
// rather than force total conformity.
|
|
460
|
+
const char1 = one.charAt(one.length - 1);
|
|
461
|
+
const char2 = two.charAt(0);
|
|
462
|
+
const nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex_);
|
|
463
|
+
const nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex_);
|
|
464
|
+
const whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex_);
|
|
465
|
+
const whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex_);
|
|
466
|
+
const lineBreak1 = whitespace1 && char1.match(linebreakRegex_);
|
|
467
|
+
const lineBreak2 = whitespace2 && char2.match(linebreakRegex_);
|
|
468
|
+
const blankLine1 = lineBreak1 && one.match(blanklineEndRegex_);
|
|
469
|
+
const blankLine2 = lineBreak2 && two.match(blanklineStartRegex_);
|
|
470
|
+
if (blankLine1 || blankLine2) {
|
|
471
|
+
// Five points for blank lines.
|
|
472
|
+
return 5;
|
|
473
|
+
} else if (lineBreak1 || lineBreak2) {
|
|
474
|
+
// Four points for line breaks.
|
|
475
|
+
return 4;
|
|
476
|
+
} else if (nonAlphaNumeric1 && !whitespace1 && whitespace2) {
|
|
477
|
+
// Three points for end of sentences.
|
|
478
|
+
return 3;
|
|
479
|
+
} else if (whitespace1 || whitespace2) {
|
|
480
|
+
// Two points for whitespace.
|
|
481
|
+
return 2;
|
|
482
|
+
} else if (nonAlphaNumeric1 || nonAlphaNumeric2) {
|
|
483
|
+
// One point for non-alphanumeric.
|
|
484
|
+
return 1;
|
|
485
|
+
}
|
|
486
|
+
return 0;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
491
|
+
*
|
|
492
|
+
* This source code is licensed under the MIT license found in the
|
|
493
|
+
* LICENSE file in the root directory of this source tree.
|
|
494
|
+
*/
|
|
495
|
+
const NO_DIFF_MESSAGE = "Compared values have no visual difference.";
|
|
496
|
+
const SIMILAR_MESSAGE = "Compared values serialize to the same structure.\n" + "Printing internal object structure without calling `toJSON` instead.";
|
|
497
|
+
|
|
498
|
+
var build = {};
|
|
499
|
+
|
|
500
|
+
var hasRequiredBuild;
|
|
501
|
+
|
|
502
|
+
function requireBuild () {
|
|
503
|
+
if (hasRequiredBuild) return build;
|
|
504
|
+
hasRequiredBuild = 1;
|
|
505
|
+
|
|
506
|
+
Object.defineProperty(build, '__esModule', {
|
|
507
|
+
value: true
|
|
508
|
+
});
|
|
509
|
+
build.default = diffSequence;
|
|
510
|
+
/**
|
|
511
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
512
|
+
*
|
|
513
|
+
* This source code is licensed under the MIT license found in the
|
|
514
|
+
* LICENSE file in the root directory of this source tree.
|
|
515
|
+
*
|
|
516
|
+
*/
|
|
517
|
+
|
|
518
|
+
// This diff-sequences package implements the linear space variation in
|
|
519
|
+
// An O(ND) Difference Algorithm and Its Variations by Eugene W. Myers
|
|
520
|
+
|
|
521
|
+
// Relationship in notation between Myers paper and this package:
|
|
522
|
+
// A is a
|
|
523
|
+
// N is aLength, aEnd - aStart, and so on
|
|
524
|
+
// x is aIndex, aFirst, aLast, and so on
|
|
525
|
+
// B is b
|
|
526
|
+
// M is bLength, bEnd - bStart, and so on
|
|
527
|
+
// y is bIndex, bFirst, bLast, and so on
|
|
528
|
+
// Δ = N - M is negative of baDeltaLength = bLength - aLength
|
|
529
|
+
// D is d
|
|
530
|
+
// k is kF
|
|
531
|
+
// k + Δ is kF = kR - baDeltaLength
|
|
532
|
+
// V is aIndexesF or aIndexesR (see comment below about Indexes type)
|
|
533
|
+
// index intervals [1, N] and [1, M] are [0, aLength) and [0, bLength)
|
|
534
|
+
// starting point in forward direction (0, 0) is (-1, -1)
|
|
535
|
+
// starting point in reverse direction (N + 1, M + 1) is (aLength, bLength)
|
|
536
|
+
|
|
537
|
+
// The “edit graph” for sequences a and b corresponds to items:
|
|
538
|
+
// in a on the horizontal axis
|
|
539
|
+
// in b on the vertical axis
|
|
540
|
+
//
|
|
541
|
+
// Given a-coordinate of a point in a diagonal, you can compute b-coordinate.
|
|
542
|
+
//
|
|
543
|
+
// Forward diagonals kF:
|
|
544
|
+
// zero diagonal intersects top left corner
|
|
545
|
+
// positive diagonals intersect top edge
|
|
546
|
+
// negative diagonals insersect left edge
|
|
547
|
+
//
|
|
548
|
+
// Reverse diagonals kR:
|
|
549
|
+
// zero diagonal intersects bottom right corner
|
|
550
|
+
// positive diagonals intersect right edge
|
|
551
|
+
// negative diagonals intersect bottom edge
|
|
552
|
+
|
|
553
|
+
// The graph contains a directed acyclic graph of edges:
|
|
554
|
+
// horizontal: delete an item from a
|
|
555
|
+
// vertical: insert an item from b
|
|
556
|
+
// diagonal: common item in a and b
|
|
557
|
+
//
|
|
558
|
+
// The algorithm solves dual problems in the graph analogy:
|
|
559
|
+
// Find longest common subsequence: path with maximum number of diagonal edges
|
|
560
|
+
// Find shortest edit script: path with minimum number of non-diagonal edges
|
|
561
|
+
|
|
562
|
+
// Input callback function compares items at indexes in the sequences.
|
|
563
|
+
|
|
564
|
+
// Output callback function receives the number of adjacent items
|
|
565
|
+
// and starting indexes of each common subsequence.
|
|
566
|
+
// Either original functions or wrapped to swap indexes if graph is transposed.
|
|
567
|
+
// Indexes in sequence a of last point of forward or reverse paths in graph.
|
|
568
|
+
// Myers algorithm indexes by diagonal k which for negative is bad deopt in V8.
|
|
569
|
+
// This package indexes by iF and iR which are greater than or equal to zero.
|
|
570
|
+
// and also updates the index arrays in place to cut memory in half.
|
|
571
|
+
// kF = 2 * iF - d
|
|
572
|
+
// kR = d - 2 * iR
|
|
573
|
+
// Division of index intervals in sequences a and b at the middle change.
|
|
574
|
+
// Invariant: intervals do not have common items at the start or end.
|
|
575
|
+
const pkg = 'diff-sequences'; // for error messages
|
|
576
|
+
const NOT_YET_SET = 0; // small int instead of undefined to avoid deopt in V8
|
|
577
|
+
|
|
578
|
+
// Return the number of common items that follow in forward direction.
|
|
579
|
+
// The length of what Myers paper calls a “snake” in a forward path.
|
|
580
|
+
const countCommonItemsF = (aIndex, aEnd, bIndex, bEnd, isCommon) => {
|
|
581
|
+
let nCommon = 0;
|
|
582
|
+
while (aIndex < aEnd && bIndex < bEnd && isCommon(aIndex, bIndex)) {
|
|
583
|
+
aIndex += 1;
|
|
584
|
+
bIndex += 1;
|
|
585
|
+
nCommon += 1;
|
|
586
|
+
}
|
|
587
|
+
return nCommon;
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
// Return the number of common items that precede in reverse direction.
|
|
591
|
+
// The length of what Myers paper calls a “snake” in a reverse path.
|
|
592
|
+
const countCommonItemsR = (aStart, aIndex, bStart, bIndex, isCommon) => {
|
|
593
|
+
let nCommon = 0;
|
|
594
|
+
while (aStart <= aIndex && bStart <= bIndex && isCommon(aIndex, bIndex)) {
|
|
595
|
+
aIndex -= 1;
|
|
596
|
+
bIndex -= 1;
|
|
597
|
+
nCommon += 1;
|
|
598
|
+
}
|
|
599
|
+
return nCommon;
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
// A simple function to extend forward paths from (d - 1) to d changes
|
|
603
|
+
// when forward and reverse paths cannot yet overlap.
|
|
604
|
+
const extendPathsF = (
|
|
605
|
+
d,
|
|
606
|
+
aEnd,
|
|
607
|
+
bEnd,
|
|
608
|
+
bF,
|
|
609
|
+
isCommon,
|
|
610
|
+
aIndexesF,
|
|
611
|
+
iMaxF // return the value because optimization might decrease it
|
|
612
|
+
) => {
|
|
613
|
+
// Unroll the first iteration.
|
|
614
|
+
let iF = 0;
|
|
615
|
+
let kF = -d; // kF = 2 * iF - d
|
|
616
|
+
let aFirst = aIndexesF[iF]; // in first iteration always insert
|
|
617
|
+
let aIndexPrev1 = aFirst; // prev value of [iF - 1] in next iteration
|
|
618
|
+
aIndexesF[iF] += countCommonItemsF(
|
|
619
|
+
aFirst + 1,
|
|
620
|
+
aEnd,
|
|
621
|
+
bF + aFirst - kF + 1,
|
|
622
|
+
bEnd,
|
|
623
|
+
isCommon
|
|
624
|
+
);
|
|
625
|
+
|
|
626
|
+
// Optimization: skip diagonals in which paths cannot ever overlap.
|
|
627
|
+
const nF = d < iMaxF ? d : iMaxF;
|
|
628
|
+
|
|
629
|
+
// The diagonals kF are odd when d is odd and even when d is even.
|
|
630
|
+
for (iF += 1, kF += 2; iF <= nF; iF += 1, kF += 2) {
|
|
631
|
+
// To get first point of path segment, move one change in forward direction
|
|
632
|
+
// from last point of previous path segment in an adjacent diagonal.
|
|
633
|
+
// In last possible iteration when iF === d and kF === d always delete.
|
|
634
|
+
if (iF !== d && aIndexPrev1 < aIndexesF[iF]) {
|
|
635
|
+
aFirst = aIndexesF[iF]; // vertical to insert from b
|
|
636
|
+
} else {
|
|
637
|
+
aFirst = aIndexPrev1 + 1; // horizontal to delete from a
|
|
638
|
+
|
|
639
|
+
if (aEnd <= aFirst) {
|
|
640
|
+
// Optimization: delete moved past right of graph.
|
|
641
|
+
return iF - 1;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// To get last point of path segment, move along diagonal of common items.
|
|
646
|
+
aIndexPrev1 = aIndexesF[iF];
|
|
647
|
+
aIndexesF[iF] =
|
|
648
|
+
aFirst +
|
|
649
|
+
countCommonItemsF(aFirst + 1, aEnd, bF + aFirst - kF + 1, bEnd, isCommon);
|
|
650
|
+
}
|
|
651
|
+
return iMaxF;
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
// A simple function to extend reverse paths from (d - 1) to d changes
|
|
655
|
+
// when reverse and forward paths cannot yet overlap.
|
|
656
|
+
const extendPathsR = (
|
|
657
|
+
d,
|
|
658
|
+
aStart,
|
|
659
|
+
bStart,
|
|
660
|
+
bR,
|
|
661
|
+
isCommon,
|
|
662
|
+
aIndexesR,
|
|
663
|
+
iMaxR // return the value because optimization might decrease it
|
|
664
|
+
) => {
|
|
665
|
+
// Unroll the first iteration.
|
|
666
|
+
let iR = 0;
|
|
667
|
+
let kR = d; // kR = d - 2 * iR
|
|
668
|
+
let aFirst = aIndexesR[iR]; // in first iteration always insert
|
|
669
|
+
let aIndexPrev1 = aFirst; // prev value of [iR - 1] in next iteration
|
|
670
|
+
aIndexesR[iR] -= countCommonItemsR(
|
|
671
|
+
aStart,
|
|
672
|
+
aFirst - 1,
|
|
673
|
+
bStart,
|
|
674
|
+
bR + aFirst - kR - 1,
|
|
675
|
+
isCommon
|
|
676
|
+
);
|
|
677
|
+
|
|
678
|
+
// Optimization: skip diagonals in which paths cannot ever overlap.
|
|
679
|
+
const nR = d < iMaxR ? d : iMaxR;
|
|
680
|
+
|
|
681
|
+
// The diagonals kR are odd when d is odd and even when d is even.
|
|
682
|
+
for (iR += 1, kR -= 2; iR <= nR; iR += 1, kR -= 2) {
|
|
683
|
+
// To get first point of path segment, move one change in reverse direction
|
|
684
|
+
// from last point of previous path segment in an adjacent diagonal.
|
|
685
|
+
// In last possible iteration when iR === d and kR === -d always delete.
|
|
686
|
+
if (iR !== d && aIndexesR[iR] < aIndexPrev1) {
|
|
687
|
+
aFirst = aIndexesR[iR]; // vertical to insert from b
|
|
688
|
+
} else {
|
|
689
|
+
aFirst = aIndexPrev1 - 1; // horizontal to delete from a
|
|
690
|
+
|
|
691
|
+
if (aFirst < aStart) {
|
|
692
|
+
// Optimization: delete moved past left of graph.
|
|
693
|
+
return iR - 1;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
// To get last point of path segment, move along diagonal of common items.
|
|
698
|
+
aIndexPrev1 = aIndexesR[iR];
|
|
699
|
+
aIndexesR[iR] =
|
|
700
|
+
aFirst -
|
|
701
|
+
countCommonItemsR(
|
|
702
|
+
aStart,
|
|
703
|
+
aFirst - 1,
|
|
704
|
+
bStart,
|
|
705
|
+
bR + aFirst - kR - 1,
|
|
706
|
+
isCommon
|
|
707
|
+
);
|
|
708
|
+
}
|
|
709
|
+
return iMaxR;
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
// A complete function to extend forward paths from (d - 1) to d changes.
|
|
713
|
+
// Return true if a path overlaps reverse path of (d - 1) changes in its diagonal.
|
|
714
|
+
const extendOverlappablePathsF = (
|
|
715
|
+
d,
|
|
716
|
+
aStart,
|
|
717
|
+
aEnd,
|
|
718
|
+
bStart,
|
|
719
|
+
bEnd,
|
|
720
|
+
isCommon,
|
|
721
|
+
aIndexesF,
|
|
722
|
+
iMaxF,
|
|
723
|
+
aIndexesR,
|
|
724
|
+
iMaxR,
|
|
725
|
+
division // update prop values if return true
|
|
726
|
+
) => {
|
|
727
|
+
const bF = bStart - aStart; // bIndex = bF + aIndex - kF
|
|
728
|
+
const aLength = aEnd - aStart;
|
|
729
|
+
const bLength = bEnd - bStart;
|
|
730
|
+
const baDeltaLength = bLength - aLength; // kF = kR - baDeltaLength
|
|
731
|
+
|
|
732
|
+
// Range of diagonals in which forward and reverse paths might overlap.
|
|
733
|
+
const kMinOverlapF = -baDeltaLength - (d - 1); // -(d - 1) <= kR
|
|
734
|
+
const kMaxOverlapF = -baDeltaLength + (d - 1); // kR <= (d - 1)
|
|
735
|
+
|
|
736
|
+
let aIndexPrev1 = NOT_YET_SET; // prev value of [iF - 1] in next iteration
|
|
737
|
+
|
|
738
|
+
// Optimization: skip diagonals in which paths cannot ever overlap.
|
|
739
|
+
const nF = d < iMaxF ? d : iMaxF;
|
|
740
|
+
|
|
741
|
+
// The diagonals kF = 2 * iF - d are odd when d is odd and even when d is even.
|
|
742
|
+
for (let iF = 0, kF = -d; iF <= nF; iF += 1, kF += 2) {
|
|
743
|
+
// To get first point of path segment, move one change in forward direction
|
|
744
|
+
// from last point of previous path segment in an adjacent diagonal.
|
|
745
|
+
// In first iteration when iF === 0 and kF === -d always insert.
|
|
746
|
+
// In last possible iteration when iF === d and kF === d always delete.
|
|
747
|
+
const insert = iF === 0 || (iF !== d && aIndexPrev1 < aIndexesF[iF]);
|
|
748
|
+
const aLastPrev = insert ? aIndexesF[iF] : aIndexPrev1;
|
|
749
|
+
const aFirst = insert
|
|
750
|
+
? aLastPrev // vertical to insert from b
|
|
751
|
+
: aLastPrev + 1; // horizontal to delete from a
|
|
752
|
+
|
|
753
|
+
// To get last point of path segment, move along diagonal of common items.
|
|
754
|
+
const bFirst = bF + aFirst - kF;
|
|
755
|
+
const nCommonF = countCommonItemsF(
|
|
756
|
+
aFirst + 1,
|
|
757
|
+
aEnd,
|
|
758
|
+
bFirst + 1,
|
|
759
|
+
bEnd,
|
|
760
|
+
isCommon
|
|
761
|
+
);
|
|
762
|
+
const aLast = aFirst + nCommonF;
|
|
763
|
+
aIndexPrev1 = aIndexesF[iF];
|
|
764
|
+
aIndexesF[iF] = aLast;
|
|
765
|
+
if (kMinOverlapF <= kF && kF <= kMaxOverlapF) {
|
|
766
|
+
// Solve for iR of reverse path with (d - 1) changes in diagonal kF:
|
|
767
|
+
// kR = kF + baDeltaLength
|
|
768
|
+
// kR = (d - 1) - 2 * iR
|
|
769
|
+
const iR = (d - 1 - (kF + baDeltaLength)) / 2;
|
|
770
|
+
|
|
771
|
+
// If this forward path overlaps the reverse path in this diagonal,
|
|
772
|
+
// then this is the middle change of the index intervals.
|
|
773
|
+
if (iR <= iMaxR && aIndexesR[iR] - 1 <= aLast) {
|
|
774
|
+
// Unlike the Myers algorithm which finds only the middle “snake”
|
|
775
|
+
// this package can find two common subsequences per division.
|
|
776
|
+
// Last point of previous path segment is on an adjacent diagonal.
|
|
777
|
+
const bLastPrev = bF + aLastPrev - (insert ? kF + 1 : kF - 1);
|
|
778
|
+
|
|
779
|
+
// Because of invariant that intervals preceding the middle change
|
|
780
|
+
// cannot have common items at the end,
|
|
781
|
+
// move in reverse direction along a diagonal of common items.
|
|
782
|
+
const nCommonR = countCommonItemsR(
|
|
783
|
+
aStart,
|
|
784
|
+
aLastPrev,
|
|
785
|
+
bStart,
|
|
786
|
+
bLastPrev,
|
|
787
|
+
isCommon
|
|
788
|
+
);
|
|
789
|
+
const aIndexPrevFirst = aLastPrev - nCommonR;
|
|
790
|
+
const bIndexPrevFirst = bLastPrev - nCommonR;
|
|
791
|
+
const aEndPreceding = aIndexPrevFirst + 1;
|
|
792
|
+
const bEndPreceding = bIndexPrevFirst + 1;
|
|
793
|
+
division.nChangePreceding = d - 1;
|
|
794
|
+
if (d - 1 === aEndPreceding + bEndPreceding - aStart - bStart) {
|
|
795
|
+
// Optimization: number of preceding changes in forward direction
|
|
796
|
+
// is equal to number of items in preceding interval,
|
|
797
|
+
// therefore it cannot contain any common items.
|
|
798
|
+
division.aEndPreceding = aStart;
|
|
799
|
+
division.bEndPreceding = bStart;
|
|
800
|
+
} else {
|
|
801
|
+
division.aEndPreceding = aEndPreceding;
|
|
802
|
+
division.bEndPreceding = bEndPreceding;
|
|
803
|
+
}
|
|
804
|
+
division.nCommonPreceding = nCommonR;
|
|
805
|
+
if (nCommonR !== 0) {
|
|
806
|
+
division.aCommonPreceding = aEndPreceding;
|
|
807
|
+
division.bCommonPreceding = bEndPreceding;
|
|
808
|
+
}
|
|
809
|
+
division.nCommonFollowing = nCommonF;
|
|
810
|
+
if (nCommonF !== 0) {
|
|
811
|
+
division.aCommonFollowing = aFirst + 1;
|
|
812
|
+
division.bCommonFollowing = bFirst + 1;
|
|
813
|
+
}
|
|
814
|
+
const aStartFollowing = aLast + 1;
|
|
815
|
+
const bStartFollowing = bFirst + nCommonF + 1;
|
|
816
|
+
division.nChangeFollowing = d - 1;
|
|
817
|
+
if (d - 1 === aEnd + bEnd - aStartFollowing - bStartFollowing) {
|
|
818
|
+
// Optimization: number of changes in reverse direction
|
|
819
|
+
// is equal to number of items in following interval,
|
|
820
|
+
// therefore it cannot contain any common items.
|
|
821
|
+
division.aStartFollowing = aEnd;
|
|
822
|
+
division.bStartFollowing = bEnd;
|
|
823
|
+
} else {
|
|
824
|
+
division.aStartFollowing = aStartFollowing;
|
|
825
|
+
division.bStartFollowing = bStartFollowing;
|
|
826
|
+
}
|
|
827
|
+
return true;
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
return false;
|
|
832
|
+
};
|
|
833
|
+
|
|
834
|
+
// A complete function to extend reverse paths from (d - 1) to d changes.
|
|
835
|
+
// Return true if a path overlaps forward path of d changes in its diagonal.
|
|
836
|
+
const extendOverlappablePathsR = (
|
|
837
|
+
d,
|
|
838
|
+
aStart,
|
|
839
|
+
aEnd,
|
|
840
|
+
bStart,
|
|
841
|
+
bEnd,
|
|
842
|
+
isCommon,
|
|
843
|
+
aIndexesF,
|
|
844
|
+
iMaxF,
|
|
845
|
+
aIndexesR,
|
|
846
|
+
iMaxR,
|
|
847
|
+
division // update prop values if return true
|
|
848
|
+
) => {
|
|
849
|
+
const bR = bEnd - aEnd; // bIndex = bR + aIndex - kR
|
|
850
|
+
const aLength = aEnd - aStart;
|
|
851
|
+
const bLength = bEnd - bStart;
|
|
852
|
+
const baDeltaLength = bLength - aLength; // kR = kF + baDeltaLength
|
|
853
|
+
|
|
854
|
+
// Range of diagonals in which forward and reverse paths might overlap.
|
|
855
|
+
const kMinOverlapR = baDeltaLength - d; // -d <= kF
|
|
856
|
+
const kMaxOverlapR = baDeltaLength + d; // kF <= d
|
|
857
|
+
|
|
858
|
+
let aIndexPrev1 = NOT_YET_SET; // prev value of [iR - 1] in next iteration
|
|
859
|
+
|
|
860
|
+
// Optimization: skip diagonals in which paths cannot ever overlap.
|
|
861
|
+
const nR = d < iMaxR ? d : iMaxR;
|
|
862
|
+
|
|
863
|
+
// The diagonals kR = d - 2 * iR are odd when d is odd and even when d is even.
|
|
864
|
+
for (let iR = 0, kR = d; iR <= nR; iR += 1, kR -= 2) {
|
|
865
|
+
// To get first point of path segment, move one change in reverse direction
|
|
866
|
+
// from last point of previous path segment in an adjacent diagonal.
|
|
867
|
+
// In first iteration when iR === 0 and kR === d always insert.
|
|
868
|
+
// In last possible iteration when iR === d and kR === -d always delete.
|
|
869
|
+
const insert = iR === 0 || (iR !== d && aIndexesR[iR] < aIndexPrev1);
|
|
870
|
+
const aLastPrev = insert ? aIndexesR[iR] : aIndexPrev1;
|
|
871
|
+
const aFirst = insert
|
|
872
|
+
? aLastPrev // vertical to insert from b
|
|
873
|
+
: aLastPrev - 1; // horizontal to delete from a
|
|
874
|
+
|
|
875
|
+
// To get last point of path segment, move along diagonal of common items.
|
|
876
|
+
const bFirst = bR + aFirst - kR;
|
|
877
|
+
const nCommonR = countCommonItemsR(
|
|
878
|
+
aStart,
|
|
879
|
+
aFirst - 1,
|
|
880
|
+
bStart,
|
|
881
|
+
bFirst - 1,
|
|
882
|
+
isCommon
|
|
883
|
+
);
|
|
884
|
+
const aLast = aFirst - nCommonR;
|
|
885
|
+
aIndexPrev1 = aIndexesR[iR];
|
|
886
|
+
aIndexesR[iR] = aLast;
|
|
887
|
+
if (kMinOverlapR <= kR && kR <= kMaxOverlapR) {
|
|
888
|
+
// Solve for iF of forward path with d changes in diagonal kR:
|
|
889
|
+
// kF = kR - baDeltaLength
|
|
890
|
+
// kF = 2 * iF - d
|
|
891
|
+
const iF = (d + (kR - baDeltaLength)) / 2;
|
|
892
|
+
|
|
893
|
+
// If this reverse path overlaps the forward path in this diagonal,
|
|
894
|
+
// then this is a middle change of the index intervals.
|
|
895
|
+
if (iF <= iMaxF && aLast - 1 <= aIndexesF[iF]) {
|
|
896
|
+
const bLast = bFirst - nCommonR;
|
|
897
|
+
division.nChangePreceding = d;
|
|
898
|
+
if (d === aLast + bLast - aStart - bStart) {
|
|
899
|
+
// Optimization: number of changes in reverse direction
|
|
900
|
+
// is equal to number of items in preceding interval,
|
|
901
|
+
// therefore it cannot contain any common items.
|
|
902
|
+
division.aEndPreceding = aStart;
|
|
903
|
+
division.bEndPreceding = bStart;
|
|
904
|
+
} else {
|
|
905
|
+
division.aEndPreceding = aLast;
|
|
906
|
+
division.bEndPreceding = bLast;
|
|
907
|
+
}
|
|
908
|
+
division.nCommonPreceding = nCommonR;
|
|
909
|
+
if (nCommonR !== 0) {
|
|
910
|
+
// The last point of reverse path segment is start of common subsequence.
|
|
911
|
+
division.aCommonPreceding = aLast;
|
|
912
|
+
division.bCommonPreceding = bLast;
|
|
913
|
+
}
|
|
914
|
+
division.nChangeFollowing = d - 1;
|
|
915
|
+
if (d === 1) {
|
|
916
|
+
// There is no previous path segment.
|
|
917
|
+
division.nCommonFollowing = 0;
|
|
918
|
+
division.aStartFollowing = aEnd;
|
|
919
|
+
division.bStartFollowing = bEnd;
|
|
920
|
+
} else {
|
|
921
|
+
// Unlike the Myers algorithm which finds only the middle “snake”
|
|
922
|
+
// this package can find two common subsequences per division.
|
|
923
|
+
// Last point of previous path segment is on an adjacent diagonal.
|
|
924
|
+
const bLastPrev = bR + aLastPrev - (insert ? kR - 1 : kR + 1);
|
|
925
|
+
|
|
926
|
+
// Because of invariant that intervals following the middle change
|
|
927
|
+
// cannot have common items at the start,
|
|
928
|
+
// move in forward direction along a diagonal of common items.
|
|
929
|
+
const nCommonF = countCommonItemsF(
|
|
930
|
+
aLastPrev,
|
|
931
|
+
aEnd,
|
|
932
|
+
bLastPrev,
|
|
933
|
+
bEnd,
|
|
934
|
+
isCommon
|
|
935
|
+
);
|
|
936
|
+
division.nCommonFollowing = nCommonF;
|
|
937
|
+
if (nCommonF !== 0) {
|
|
938
|
+
// The last point of reverse path segment is start of common subsequence.
|
|
939
|
+
division.aCommonFollowing = aLastPrev;
|
|
940
|
+
division.bCommonFollowing = bLastPrev;
|
|
941
|
+
}
|
|
942
|
+
const aStartFollowing = aLastPrev + nCommonF; // aFirstPrev
|
|
943
|
+
const bStartFollowing = bLastPrev + nCommonF; // bFirstPrev
|
|
944
|
+
|
|
945
|
+
if (d - 1 === aEnd + bEnd - aStartFollowing - bStartFollowing) {
|
|
946
|
+
// Optimization: number of changes in forward direction
|
|
947
|
+
// is equal to number of items in following interval,
|
|
948
|
+
// therefore it cannot contain any common items.
|
|
949
|
+
division.aStartFollowing = aEnd;
|
|
950
|
+
division.bStartFollowing = bEnd;
|
|
951
|
+
} else {
|
|
952
|
+
division.aStartFollowing = aStartFollowing;
|
|
953
|
+
division.bStartFollowing = bStartFollowing;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return true;
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
return false;
|
|
961
|
+
};
|
|
962
|
+
|
|
963
|
+
// Given index intervals and input function to compare items at indexes,
|
|
964
|
+
// divide at the middle change.
|
|
965
|
+
//
|
|
966
|
+
// DO NOT CALL if start === end, because interval cannot contain common items
|
|
967
|
+
// and because this function will throw the “no overlap” error.
|
|
968
|
+
const divide = (
|
|
969
|
+
nChange,
|
|
970
|
+
aStart,
|
|
971
|
+
aEnd,
|
|
972
|
+
bStart,
|
|
973
|
+
bEnd,
|
|
974
|
+
isCommon,
|
|
975
|
+
aIndexesF,
|
|
976
|
+
aIndexesR,
|
|
977
|
+
division // output
|
|
978
|
+
) => {
|
|
979
|
+
const bF = bStart - aStart; // bIndex = bF + aIndex - kF
|
|
980
|
+
const bR = bEnd - aEnd; // bIndex = bR + aIndex - kR
|
|
981
|
+
const aLength = aEnd - aStart;
|
|
982
|
+
const bLength = bEnd - bStart;
|
|
983
|
+
|
|
984
|
+
// Because graph has square or portrait orientation,
|
|
985
|
+
// length difference is minimum number of items to insert from b.
|
|
986
|
+
// Corresponding forward and reverse diagonals in graph
|
|
987
|
+
// depend on length difference of the sequences:
|
|
988
|
+
// kF = kR - baDeltaLength
|
|
989
|
+
// kR = kF + baDeltaLength
|
|
990
|
+
const baDeltaLength = bLength - aLength;
|
|
991
|
+
|
|
992
|
+
// Optimization: max diagonal in graph intersects corner of shorter side.
|
|
993
|
+
let iMaxF = aLength;
|
|
994
|
+
let iMaxR = aLength;
|
|
995
|
+
|
|
996
|
+
// Initialize no changes yet in forward or reverse direction:
|
|
997
|
+
aIndexesF[0] = aStart - 1; // at open start of interval, outside closed start
|
|
998
|
+
aIndexesR[0] = aEnd; // at open end of interval
|
|
999
|
+
|
|
1000
|
+
if (baDeltaLength % 2 === 0) {
|
|
1001
|
+
// The number of changes in paths is 2 * d if length difference is even.
|
|
1002
|
+
const dMin = (nChange || baDeltaLength) / 2;
|
|
1003
|
+
const dMax = (aLength + bLength) / 2;
|
|
1004
|
+
for (let d = 1; d <= dMax; d += 1) {
|
|
1005
|
+
iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
|
|
1006
|
+
if (d < dMin) {
|
|
1007
|
+
iMaxR = extendPathsR(d, aStart, bStart, bR, isCommon, aIndexesR, iMaxR);
|
|
1008
|
+
} else if (
|
|
1009
|
+
// If a reverse path overlaps a forward path in the same diagonal,
|
|
1010
|
+
// return a division of the index intervals at the middle change.
|
|
1011
|
+
extendOverlappablePathsR(
|
|
1012
|
+
d,
|
|
1013
|
+
aStart,
|
|
1014
|
+
aEnd,
|
|
1015
|
+
bStart,
|
|
1016
|
+
bEnd,
|
|
1017
|
+
isCommon,
|
|
1018
|
+
aIndexesF,
|
|
1019
|
+
iMaxF,
|
|
1020
|
+
aIndexesR,
|
|
1021
|
+
iMaxR,
|
|
1022
|
+
division
|
|
1023
|
+
)
|
|
1024
|
+
) {
|
|
1025
|
+
return;
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
} else {
|
|
1029
|
+
// The number of changes in paths is 2 * d - 1 if length difference is odd.
|
|
1030
|
+
const dMin = ((nChange || baDeltaLength) + 1) / 2;
|
|
1031
|
+
const dMax = (aLength + bLength + 1) / 2;
|
|
1032
|
+
|
|
1033
|
+
// Unroll first half iteration so loop extends the relevant pairs of paths.
|
|
1034
|
+
// Because of invariant that intervals have no common items at start or end,
|
|
1035
|
+
// and limitation not to call divide with empty intervals,
|
|
1036
|
+
// therefore it cannot be called if a forward path with one change
|
|
1037
|
+
// would overlap a reverse path with no changes, even if dMin === 1.
|
|
1038
|
+
let d = 1;
|
|
1039
|
+
iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
|
|
1040
|
+
for (d += 1; d <= dMax; d += 1) {
|
|
1041
|
+
iMaxR = extendPathsR(
|
|
1042
|
+
d - 1,
|
|
1043
|
+
aStart,
|
|
1044
|
+
bStart,
|
|
1045
|
+
bR,
|
|
1046
|
+
isCommon,
|
|
1047
|
+
aIndexesR,
|
|
1048
|
+
iMaxR
|
|
1049
|
+
);
|
|
1050
|
+
if (d < dMin) {
|
|
1051
|
+
iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
|
|
1052
|
+
} else if (
|
|
1053
|
+
// If a forward path overlaps a reverse path in the same diagonal,
|
|
1054
|
+
// return a division of the index intervals at the middle change.
|
|
1055
|
+
extendOverlappablePathsF(
|
|
1056
|
+
d,
|
|
1057
|
+
aStart,
|
|
1058
|
+
aEnd,
|
|
1059
|
+
bStart,
|
|
1060
|
+
bEnd,
|
|
1061
|
+
isCommon,
|
|
1062
|
+
aIndexesF,
|
|
1063
|
+
iMaxF,
|
|
1064
|
+
aIndexesR,
|
|
1065
|
+
iMaxR,
|
|
1066
|
+
division
|
|
1067
|
+
)
|
|
1068
|
+
) {
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/* istanbul ignore next */
|
|
1075
|
+
throw new Error(
|
|
1076
|
+
`${pkg}: no overlap aStart=${aStart} aEnd=${aEnd} bStart=${bStart} bEnd=${bEnd}`
|
|
1077
|
+
);
|
|
1078
|
+
};
|
|
1079
|
+
|
|
1080
|
+
// Given index intervals and input function to compare items at indexes,
|
|
1081
|
+
// return by output function the number of adjacent items and starting indexes
|
|
1082
|
+
// of each common subsequence. Divide and conquer with only linear space.
|
|
1083
|
+
//
|
|
1084
|
+
// The index intervals are half open [start, end) like array slice method.
|
|
1085
|
+
// DO NOT CALL if start === end, because interval cannot contain common items
|
|
1086
|
+
// and because divide function will throw the “no overlap” error.
|
|
1087
|
+
const findSubsequences = (
|
|
1088
|
+
nChange,
|
|
1089
|
+
aStart,
|
|
1090
|
+
aEnd,
|
|
1091
|
+
bStart,
|
|
1092
|
+
bEnd,
|
|
1093
|
+
transposed,
|
|
1094
|
+
callbacks,
|
|
1095
|
+
aIndexesF,
|
|
1096
|
+
aIndexesR,
|
|
1097
|
+
division // temporary memory, not input nor output
|
|
1098
|
+
) => {
|
|
1099
|
+
if (bEnd - bStart < aEnd - aStart) {
|
|
1100
|
+
// Transpose graph so it has portrait instead of landscape orientation.
|
|
1101
|
+
// Always compare shorter to longer sequence for consistency and optimization.
|
|
1102
|
+
transposed = !transposed;
|
|
1103
|
+
if (transposed && callbacks.length === 1) {
|
|
1104
|
+
// Lazily wrap callback functions to swap args if graph is transposed.
|
|
1105
|
+
const {foundSubsequence, isCommon} = callbacks[0];
|
|
1106
|
+
callbacks[1] = {
|
|
1107
|
+
foundSubsequence: (nCommon, bCommon, aCommon) => {
|
|
1108
|
+
foundSubsequence(nCommon, aCommon, bCommon);
|
|
1109
|
+
},
|
|
1110
|
+
isCommon: (bIndex, aIndex) => isCommon(aIndex, bIndex)
|
|
1111
|
+
};
|
|
1112
|
+
}
|
|
1113
|
+
const tStart = aStart;
|
|
1114
|
+
const tEnd = aEnd;
|
|
1115
|
+
aStart = bStart;
|
|
1116
|
+
aEnd = bEnd;
|
|
1117
|
+
bStart = tStart;
|
|
1118
|
+
bEnd = tEnd;
|
|
1119
|
+
}
|
|
1120
|
+
const {foundSubsequence, isCommon} = callbacks[transposed ? 1 : 0];
|
|
1121
|
+
|
|
1122
|
+
// Divide the index intervals at the middle change.
|
|
1123
|
+
divide(
|
|
1124
|
+
nChange,
|
|
1125
|
+
aStart,
|
|
1126
|
+
aEnd,
|
|
1127
|
+
bStart,
|
|
1128
|
+
bEnd,
|
|
1129
|
+
isCommon,
|
|
1130
|
+
aIndexesF,
|
|
1131
|
+
aIndexesR,
|
|
1132
|
+
division
|
|
1133
|
+
);
|
|
1134
|
+
const {
|
|
1135
|
+
nChangePreceding,
|
|
1136
|
+
aEndPreceding,
|
|
1137
|
+
bEndPreceding,
|
|
1138
|
+
nCommonPreceding,
|
|
1139
|
+
aCommonPreceding,
|
|
1140
|
+
bCommonPreceding,
|
|
1141
|
+
nCommonFollowing,
|
|
1142
|
+
aCommonFollowing,
|
|
1143
|
+
bCommonFollowing,
|
|
1144
|
+
nChangeFollowing,
|
|
1145
|
+
aStartFollowing,
|
|
1146
|
+
bStartFollowing
|
|
1147
|
+
} = division;
|
|
1148
|
+
|
|
1149
|
+
// Unless either index interval is empty, they might contain common items.
|
|
1150
|
+
if (aStart < aEndPreceding && bStart < bEndPreceding) {
|
|
1151
|
+
// Recursely find and return common subsequences preceding the division.
|
|
1152
|
+
findSubsequences(
|
|
1153
|
+
nChangePreceding,
|
|
1154
|
+
aStart,
|
|
1155
|
+
aEndPreceding,
|
|
1156
|
+
bStart,
|
|
1157
|
+
bEndPreceding,
|
|
1158
|
+
transposed,
|
|
1159
|
+
callbacks,
|
|
1160
|
+
aIndexesF,
|
|
1161
|
+
aIndexesR,
|
|
1162
|
+
division
|
|
1163
|
+
);
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
// Return common subsequences that are adjacent to the middle change.
|
|
1167
|
+
if (nCommonPreceding !== 0) {
|
|
1168
|
+
foundSubsequence(nCommonPreceding, aCommonPreceding, bCommonPreceding);
|
|
1169
|
+
}
|
|
1170
|
+
if (nCommonFollowing !== 0) {
|
|
1171
|
+
foundSubsequence(nCommonFollowing, aCommonFollowing, bCommonFollowing);
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
// Unless either index interval is empty, they might contain common items.
|
|
1175
|
+
if (aStartFollowing < aEnd && bStartFollowing < bEnd) {
|
|
1176
|
+
// Recursely find and return common subsequences following the division.
|
|
1177
|
+
findSubsequences(
|
|
1178
|
+
nChangeFollowing,
|
|
1179
|
+
aStartFollowing,
|
|
1180
|
+
aEnd,
|
|
1181
|
+
bStartFollowing,
|
|
1182
|
+
bEnd,
|
|
1183
|
+
transposed,
|
|
1184
|
+
callbacks,
|
|
1185
|
+
aIndexesF,
|
|
1186
|
+
aIndexesR,
|
|
1187
|
+
division
|
|
1188
|
+
);
|
|
1189
|
+
}
|
|
1190
|
+
};
|
|
1191
|
+
const validateLength = (name, arg) => {
|
|
1192
|
+
if (typeof arg !== 'number') {
|
|
1193
|
+
throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);
|
|
1194
|
+
}
|
|
1195
|
+
if (!Number.isSafeInteger(arg)) {
|
|
1196
|
+
throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);
|
|
1197
|
+
}
|
|
1198
|
+
if (arg < 0) {
|
|
1199
|
+
throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);
|
|
1200
|
+
}
|
|
1201
|
+
};
|
|
1202
|
+
const validateCallback = (name, arg) => {
|
|
1203
|
+
const type = typeof arg;
|
|
1204
|
+
if (type !== 'function') {
|
|
1205
|
+
throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);
|
|
1206
|
+
}
|
|
1207
|
+
};
|
|
1208
|
+
|
|
1209
|
+
// Compare items in two sequences to find a longest common subsequence.
|
|
1210
|
+
// Given lengths of sequences and input function to compare items at indexes,
|
|
1211
|
+
// return by output function the number of adjacent items and starting indexes
|
|
1212
|
+
// of each common subsequence.
|
|
1213
|
+
function diffSequence(aLength, bLength, isCommon, foundSubsequence) {
|
|
1214
|
+
validateLength('aLength', aLength);
|
|
1215
|
+
validateLength('bLength', bLength);
|
|
1216
|
+
validateCallback('isCommon', isCommon);
|
|
1217
|
+
validateCallback('foundSubsequence', foundSubsequence);
|
|
1218
|
+
|
|
1219
|
+
// Count common items from the start in the forward direction.
|
|
1220
|
+
const nCommonF = countCommonItemsF(0, aLength, 0, bLength, isCommon);
|
|
1221
|
+
if (nCommonF !== 0) {
|
|
1222
|
+
foundSubsequence(nCommonF, 0, 0);
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
// Unless both sequences consist of common items only,
|
|
1226
|
+
// find common items in the half-trimmed index intervals.
|
|
1227
|
+
if (aLength !== nCommonF || bLength !== nCommonF) {
|
|
1228
|
+
// Invariant: intervals do not have common items at the start.
|
|
1229
|
+
// The start of an index interval is closed like array slice method.
|
|
1230
|
+
const aStart = nCommonF;
|
|
1231
|
+
const bStart = nCommonF;
|
|
1232
|
+
|
|
1233
|
+
// Count common items from the end in the reverse direction.
|
|
1234
|
+
const nCommonR = countCommonItemsR(
|
|
1235
|
+
aStart,
|
|
1236
|
+
aLength - 1,
|
|
1237
|
+
bStart,
|
|
1238
|
+
bLength - 1,
|
|
1239
|
+
isCommon
|
|
1240
|
+
);
|
|
1241
|
+
|
|
1242
|
+
// Invariant: intervals do not have common items at the end.
|
|
1243
|
+
// The end of an index interval is open like array slice method.
|
|
1244
|
+
const aEnd = aLength - nCommonR;
|
|
1245
|
+
const bEnd = bLength - nCommonR;
|
|
1246
|
+
|
|
1247
|
+
// Unless one sequence consists of common items only,
|
|
1248
|
+
// therefore the other trimmed index interval consists of changes only,
|
|
1249
|
+
// find common items in the trimmed index intervals.
|
|
1250
|
+
const nCommonFR = nCommonF + nCommonR;
|
|
1251
|
+
if (aLength !== nCommonFR && bLength !== nCommonFR) {
|
|
1252
|
+
const nChange = 0; // number of change items is not yet known
|
|
1253
|
+
const transposed = false; // call the original unwrapped functions
|
|
1254
|
+
const callbacks = [
|
|
1255
|
+
{
|
|
1256
|
+
foundSubsequence,
|
|
1257
|
+
isCommon
|
|
1258
|
+
}
|
|
1259
|
+
];
|
|
1260
|
+
|
|
1261
|
+
// Indexes in sequence a of last points in furthest reaching paths
|
|
1262
|
+
// from outside the start at top left in the forward direction:
|
|
1263
|
+
const aIndexesF = [NOT_YET_SET];
|
|
1264
|
+
// from the end at bottom right in the reverse direction:
|
|
1265
|
+
const aIndexesR = [NOT_YET_SET];
|
|
1266
|
+
|
|
1267
|
+
// Initialize one object as output of all calls to divide function.
|
|
1268
|
+
const division = {
|
|
1269
|
+
aCommonFollowing: NOT_YET_SET,
|
|
1270
|
+
aCommonPreceding: NOT_YET_SET,
|
|
1271
|
+
aEndPreceding: NOT_YET_SET,
|
|
1272
|
+
aStartFollowing: NOT_YET_SET,
|
|
1273
|
+
bCommonFollowing: NOT_YET_SET,
|
|
1274
|
+
bCommonPreceding: NOT_YET_SET,
|
|
1275
|
+
bEndPreceding: NOT_YET_SET,
|
|
1276
|
+
bStartFollowing: NOT_YET_SET,
|
|
1277
|
+
nChangeFollowing: NOT_YET_SET,
|
|
1278
|
+
nChangePreceding: NOT_YET_SET,
|
|
1279
|
+
nCommonFollowing: NOT_YET_SET,
|
|
1280
|
+
nCommonPreceding: NOT_YET_SET
|
|
1281
|
+
};
|
|
1282
|
+
|
|
1283
|
+
// Find and return common subsequences in the trimmed index intervals.
|
|
1284
|
+
findSubsequences(
|
|
1285
|
+
nChange,
|
|
1286
|
+
aStart,
|
|
1287
|
+
aEnd,
|
|
1288
|
+
bStart,
|
|
1289
|
+
bEnd,
|
|
1290
|
+
transposed,
|
|
1291
|
+
callbacks,
|
|
1292
|
+
aIndexesF,
|
|
1293
|
+
aIndexesR,
|
|
1294
|
+
division
|
|
1295
|
+
);
|
|
1296
|
+
}
|
|
1297
|
+
if (nCommonR !== 0) {
|
|
1298
|
+
foundSubsequence(nCommonR, aEnd, bEnd);
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
return build;
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1305
|
+
var buildExports = /*@__PURE__*/ requireBuild();
|
|
1306
|
+
var diffSequences = /*@__PURE__*/getDefaultExportFromCjs(buildExports);
|
|
1307
|
+
|
|
1308
|
+
function formatTrailingSpaces(line, trailingSpaceFormatter) {
|
|
1309
|
+
return line.replace(/\s+$/, (match) => trailingSpaceFormatter(match));
|
|
1310
|
+
}
|
|
1311
|
+
function printDiffLine(line, isFirstOrLast, color, indicator, trailingSpaceFormatter, emptyFirstOrLastLinePlaceholder) {
|
|
1312
|
+
return line.length !== 0 ? color(`${indicator} ${formatTrailingSpaces(line, trailingSpaceFormatter)}`) : indicator !== " " ? color(indicator) : isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0 ? color(`${indicator} ${emptyFirstOrLastLinePlaceholder}`) : "";
|
|
1313
|
+
}
|
|
1314
|
+
function printDeleteLine(line, isFirstOrLast, { aColor, aIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder }) {
|
|
1315
|
+
return printDiffLine(line, isFirstOrLast, aColor, aIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder);
|
|
1316
|
+
}
|
|
1317
|
+
function printInsertLine(line, isFirstOrLast, { bColor, bIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder }) {
|
|
1318
|
+
return printDiffLine(line, isFirstOrLast, bColor, bIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder);
|
|
1319
|
+
}
|
|
1320
|
+
function printCommonLine(line, isFirstOrLast, { commonColor, commonIndicator, commonLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder }) {
|
|
1321
|
+
return printDiffLine(line, isFirstOrLast, commonColor, commonIndicator, commonLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder);
|
|
1322
|
+
}
|
|
1323
|
+
// In GNU diff format, indexes are one-based instead of zero-based.
|
|
1324
|
+
function createPatchMark(aStart, aEnd, bStart, bEnd, { patchColor }) {
|
|
1325
|
+
return patchColor(`@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`);
|
|
1326
|
+
}
|
|
1327
|
+
// jest --no-expand
|
|
1328
|
+
//
|
|
1329
|
+
// Given array of aligned strings with inverse highlight formatting,
|
|
1330
|
+
// return joined lines with diff formatting (and patch marks, if needed).
|
|
1331
|
+
function joinAlignedDiffsNoExpand(diffs, options) {
|
|
1332
|
+
const iLength = diffs.length;
|
|
1333
|
+
const nContextLines = options.contextLines;
|
|
1334
|
+
const nContextLines2 = nContextLines + nContextLines;
|
|
1335
|
+
// First pass: count output lines and see if it has patches.
|
|
1336
|
+
let jLength = iLength;
|
|
1337
|
+
let hasExcessAtStartOrEnd = false;
|
|
1338
|
+
let nExcessesBetweenChanges = 0;
|
|
1339
|
+
let i = 0;
|
|
1340
|
+
while (i !== iLength) {
|
|
1341
|
+
const iStart = i;
|
|
1342
|
+
while (i !== iLength && diffs[i][0] === DIFF_EQUAL) {
|
|
1343
|
+
i += 1;
|
|
1344
|
+
}
|
|
1345
|
+
if (iStart !== i) {
|
|
1346
|
+
if (iStart === 0) {
|
|
1347
|
+
// at start
|
|
1348
|
+
if (i > nContextLines) {
|
|
1349
|
+
jLength -= i - nContextLines;
|
|
1350
|
+
hasExcessAtStartOrEnd = true;
|
|
1351
|
+
}
|
|
1352
|
+
} else if (i === iLength) {
|
|
1353
|
+
// at end
|
|
1354
|
+
const n = i - iStart;
|
|
1355
|
+
if (n > nContextLines) {
|
|
1356
|
+
jLength -= n - nContextLines;
|
|
1357
|
+
hasExcessAtStartOrEnd = true;
|
|
1358
|
+
}
|
|
1359
|
+
} else {
|
|
1360
|
+
// between changes
|
|
1361
|
+
const n = i - iStart;
|
|
1362
|
+
if (n > nContextLines2) {
|
|
1363
|
+
jLength -= n - nContextLines2;
|
|
1364
|
+
nExcessesBetweenChanges += 1;
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
while (i !== iLength && diffs[i][0] !== DIFF_EQUAL) {
|
|
1369
|
+
i += 1;
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd;
|
|
1373
|
+
if (nExcessesBetweenChanges !== 0) {
|
|
1374
|
+
jLength += nExcessesBetweenChanges + 1;
|
|
1375
|
+
} else if (hasExcessAtStartOrEnd) {
|
|
1376
|
+
jLength += 1;
|
|
1377
|
+
}
|
|
1378
|
+
const jLast = jLength - 1;
|
|
1379
|
+
const lines = [];
|
|
1380
|
+
let jPatchMark = 0;
|
|
1381
|
+
if (hasPatch) {
|
|
1382
|
+
lines.push("");
|
|
1383
|
+
}
|
|
1384
|
+
// Indexes of expected or received lines in current patch:
|
|
1385
|
+
let aStart = 0;
|
|
1386
|
+
let bStart = 0;
|
|
1387
|
+
let aEnd = 0;
|
|
1388
|
+
let bEnd = 0;
|
|
1389
|
+
const pushCommonLine = (line) => {
|
|
1390
|
+
const j = lines.length;
|
|
1391
|
+
lines.push(printCommonLine(line, j === 0 || j === jLast, options));
|
|
1392
|
+
aEnd += 1;
|
|
1393
|
+
bEnd += 1;
|
|
1394
|
+
};
|
|
1395
|
+
const pushDeleteLine = (line) => {
|
|
1396
|
+
const j = lines.length;
|
|
1397
|
+
lines.push(printDeleteLine(line, j === 0 || j === jLast, options));
|
|
1398
|
+
aEnd += 1;
|
|
1399
|
+
};
|
|
1400
|
+
const pushInsertLine = (line) => {
|
|
1401
|
+
const j = lines.length;
|
|
1402
|
+
lines.push(printInsertLine(line, j === 0 || j === jLast, options));
|
|
1403
|
+
bEnd += 1;
|
|
1404
|
+
};
|
|
1405
|
+
// Second pass: push lines with diff formatting (and patch marks, if needed).
|
|
1406
|
+
i = 0;
|
|
1407
|
+
while (i !== iLength) {
|
|
1408
|
+
let iStart = i;
|
|
1409
|
+
while (i !== iLength && diffs[i][0] === DIFF_EQUAL) {
|
|
1410
|
+
i += 1;
|
|
1411
|
+
}
|
|
1412
|
+
if (iStart !== i) {
|
|
1413
|
+
if (iStart === 0) {
|
|
1414
|
+
// at beginning
|
|
1415
|
+
if (i > nContextLines) {
|
|
1416
|
+
iStart = i - nContextLines;
|
|
1417
|
+
aStart = iStart;
|
|
1418
|
+
bStart = iStart;
|
|
1419
|
+
aEnd = aStart;
|
|
1420
|
+
bEnd = bStart;
|
|
1421
|
+
}
|
|
1422
|
+
for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
|
|
1423
|
+
pushCommonLine(diffs[iCommon][1]);
|
|
1424
|
+
}
|
|
1425
|
+
} else if (i === iLength) {
|
|
1426
|
+
// at end
|
|
1427
|
+
const iEnd = i - iStart > nContextLines ? iStart + nContextLines : i;
|
|
1428
|
+
for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
|
|
1429
|
+
pushCommonLine(diffs[iCommon][1]);
|
|
1430
|
+
}
|
|
1431
|
+
} else {
|
|
1432
|
+
// between changes
|
|
1433
|
+
const nCommon = i - iStart;
|
|
1434
|
+
if (nCommon > nContextLines2) {
|
|
1435
|
+
const iEnd = iStart + nContextLines;
|
|
1436
|
+
for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
|
|
1437
|
+
pushCommonLine(diffs[iCommon][1]);
|
|
1438
|
+
}
|
|
1439
|
+
lines[jPatchMark] = createPatchMark(aStart, aEnd, bStart, bEnd, options);
|
|
1440
|
+
jPatchMark = lines.length;
|
|
1441
|
+
lines.push("");
|
|
1442
|
+
const nOmit = nCommon - nContextLines2;
|
|
1443
|
+
aStart = aEnd + nOmit;
|
|
1444
|
+
bStart = bEnd + nOmit;
|
|
1445
|
+
aEnd = aStart;
|
|
1446
|
+
bEnd = bStart;
|
|
1447
|
+
for (let iCommon = i - nContextLines; iCommon !== i; iCommon += 1) {
|
|
1448
|
+
pushCommonLine(diffs[iCommon][1]);
|
|
1449
|
+
}
|
|
1450
|
+
} else {
|
|
1451
|
+
for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
|
|
1452
|
+
pushCommonLine(diffs[iCommon][1]);
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
while (i !== iLength && diffs[i][0] === DIFF_DELETE) {
|
|
1458
|
+
pushDeleteLine(diffs[i][1]);
|
|
1459
|
+
i += 1;
|
|
1460
|
+
}
|
|
1461
|
+
while (i !== iLength && diffs[i][0] === DIFF_INSERT) {
|
|
1462
|
+
pushInsertLine(diffs[i][1]);
|
|
1463
|
+
i += 1;
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
if (hasPatch) {
|
|
1467
|
+
lines[jPatchMark] = createPatchMark(aStart, aEnd, bStart, bEnd, options);
|
|
1468
|
+
}
|
|
1469
|
+
return lines.join("\n");
|
|
1470
|
+
}
|
|
1471
|
+
// jest --expand
|
|
1472
|
+
//
|
|
1473
|
+
// Given array of aligned strings with inverse highlight formatting,
|
|
1474
|
+
// return joined lines with diff formatting.
|
|
1475
|
+
function joinAlignedDiffsExpand(diffs, options) {
|
|
1476
|
+
return diffs.map((diff, i, diffs) => {
|
|
1477
|
+
const line = diff[1];
|
|
1478
|
+
const isFirstOrLast = i === 0 || i === diffs.length - 1;
|
|
1479
|
+
switch (diff[0]) {
|
|
1480
|
+
case DIFF_DELETE: return printDeleteLine(line, isFirstOrLast, options);
|
|
1481
|
+
case DIFF_INSERT: return printInsertLine(line, isFirstOrLast, options);
|
|
1482
|
+
default: return printCommonLine(line, isFirstOrLast, options);
|
|
1483
|
+
}
|
|
1484
|
+
}).join("\n");
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
const noColor = (string) => string;
|
|
1488
|
+
const DIFF_CONTEXT_DEFAULT = 5;
|
|
1489
|
+
const DIFF_TRUNCATE_THRESHOLD_DEFAULT = 0;
|
|
1490
|
+
function getDefaultOptions() {
|
|
1491
|
+
return {
|
|
1492
|
+
aAnnotation: "Expected",
|
|
1493
|
+
aColor: c.green,
|
|
1494
|
+
aIndicator: "-",
|
|
1495
|
+
bAnnotation: "Received",
|
|
1496
|
+
bColor: c.red,
|
|
1497
|
+
bIndicator: "+",
|
|
1498
|
+
changeColor: c.inverse,
|
|
1499
|
+
changeLineTrailingSpaceColor: noColor,
|
|
1500
|
+
commonColor: c.dim,
|
|
1501
|
+
commonIndicator: " ",
|
|
1502
|
+
commonLineTrailingSpaceColor: noColor,
|
|
1503
|
+
compareKeys: undefined,
|
|
1504
|
+
contextLines: DIFF_CONTEXT_DEFAULT,
|
|
1505
|
+
emptyFirstOrLastLinePlaceholder: "",
|
|
1506
|
+
expand: false,
|
|
1507
|
+
includeChangeCounts: false,
|
|
1508
|
+
omitAnnotationLines: false,
|
|
1509
|
+
patchColor: c.yellow,
|
|
1510
|
+
printBasicPrototype: false,
|
|
1511
|
+
truncateThreshold: DIFF_TRUNCATE_THRESHOLD_DEFAULT,
|
|
1512
|
+
truncateAnnotation: "... Diff result is truncated",
|
|
1513
|
+
truncateAnnotationColor: noColor
|
|
1514
|
+
};
|
|
1515
|
+
}
|
|
1516
|
+
function getCompareKeys(compareKeys) {
|
|
1517
|
+
return compareKeys && typeof compareKeys === "function" ? compareKeys : undefined;
|
|
1518
|
+
}
|
|
1519
|
+
function getContextLines(contextLines) {
|
|
1520
|
+
return typeof contextLines === "number" && Number.isSafeInteger(contextLines) && contextLines >= 0 ? contextLines : DIFF_CONTEXT_DEFAULT;
|
|
1521
|
+
}
|
|
1522
|
+
// Pure function returns options with all properties.
|
|
1523
|
+
function normalizeDiffOptions(options = {}) {
|
|
1524
|
+
return {
|
|
1525
|
+
...getDefaultOptions(),
|
|
1526
|
+
...options,
|
|
1527
|
+
compareKeys: getCompareKeys(options.compareKeys),
|
|
1528
|
+
contextLines: getContextLines(options.contextLines)
|
|
1529
|
+
};
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
function isEmptyString(lines) {
|
|
1533
|
+
return lines.length === 1 && lines[0].length === 0;
|
|
1534
|
+
}
|
|
1535
|
+
function countChanges(diffs) {
|
|
1536
|
+
let a = 0;
|
|
1537
|
+
let b = 0;
|
|
1538
|
+
diffs.forEach((diff) => {
|
|
1539
|
+
switch (diff[0]) {
|
|
1540
|
+
case DIFF_DELETE:
|
|
1541
|
+
a += 1;
|
|
1542
|
+
break;
|
|
1543
|
+
case DIFF_INSERT:
|
|
1544
|
+
b += 1;
|
|
1545
|
+
break;
|
|
1546
|
+
}
|
|
1547
|
+
});
|
|
1548
|
+
return {
|
|
1549
|
+
a,
|
|
1550
|
+
b
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
function printAnnotation({ aAnnotation, aColor, aIndicator, bAnnotation, bColor, bIndicator, includeChangeCounts, omitAnnotationLines }, changeCounts) {
|
|
1554
|
+
if (omitAnnotationLines) {
|
|
1555
|
+
return "";
|
|
1556
|
+
}
|
|
1557
|
+
let aRest = "";
|
|
1558
|
+
let bRest = "";
|
|
1559
|
+
if (includeChangeCounts) {
|
|
1560
|
+
const aCount = String(changeCounts.a);
|
|
1561
|
+
const bCount = String(changeCounts.b);
|
|
1562
|
+
// Padding right aligns the ends of the annotations.
|
|
1563
|
+
const baAnnotationLengthDiff = bAnnotation.length - aAnnotation.length;
|
|
1564
|
+
const aAnnotationPadding = " ".repeat(Math.max(0, baAnnotationLengthDiff));
|
|
1565
|
+
const bAnnotationPadding = " ".repeat(Math.max(0, -baAnnotationLengthDiff));
|
|
1566
|
+
// Padding left aligns the ends of the counts.
|
|
1567
|
+
const baCountLengthDiff = bCount.length - aCount.length;
|
|
1568
|
+
const aCountPadding = " ".repeat(Math.max(0, baCountLengthDiff));
|
|
1569
|
+
const bCountPadding = " ".repeat(Math.max(0, -baCountLengthDiff));
|
|
1570
|
+
aRest = `${aAnnotationPadding} ${aIndicator} ${aCountPadding}${aCount}`;
|
|
1571
|
+
bRest = `${bAnnotationPadding} ${bIndicator} ${bCountPadding}${bCount}`;
|
|
1572
|
+
}
|
|
1573
|
+
const a = `${aIndicator} ${aAnnotation}${aRest}`;
|
|
1574
|
+
const b = `${bIndicator} ${bAnnotation}${bRest}`;
|
|
1575
|
+
return `${aColor(a)}\n${bColor(b)}\n\n`;
|
|
1576
|
+
}
|
|
1577
|
+
function printDiffLines(diffs, truncated, options) {
|
|
1578
|
+
return printAnnotation(options, countChanges(diffs)) + (options.expand ? joinAlignedDiffsExpand(diffs, options) : joinAlignedDiffsNoExpand(diffs, options)) + (truncated ? options.truncateAnnotationColor(`\n${options.truncateAnnotation}`) : "");
|
|
1579
|
+
}
|
|
1580
|
+
// Compare two arrays of strings line-by-line. Format as comparison lines.
|
|
1581
|
+
function diffLinesUnified(aLines, bLines, options) {
|
|
1582
|
+
const normalizedOptions = normalizeDiffOptions(options);
|
|
1583
|
+
const [diffs, truncated] = diffLinesRaw(isEmptyString(aLines) ? [] : aLines, isEmptyString(bLines) ? [] : bLines, normalizedOptions);
|
|
1584
|
+
return printDiffLines(diffs, truncated, normalizedOptions);
|
|
1585
|
+
}
|
|
1586
|
+
// Given two pairs of arrays of strings:
|
|
1587
|
+
// Compare the pair of comparison arrays line-by-line.
|
|
1588
|
+
// Format the corresponding lines in the pair of displayable arrays.
|
|
1589
|
+
function diffLinesUnified2(aLinesDisplay, bLinesDisplay, aLinesCompare, bLinesCompare, options) {
|
|
1590
|
+
if (isEmptyString(aLinesDisplay) && isEmptyString(aLinesCompare)) {
|
|
1591
|
+
aLinesDisplay = [];
|
|
1592
|
+
aLinesCompare = [];
|
|
1593
|
+
}
|
|
1594
|
+
if (isEmptyString(bLinesDisplay) && isEmptyString(bLinesCompare)) {
|
|
1595
|
+
bLinesDisplay = [];
|
|
1596
|
+
bLinesCompare = [];
|
|
1597
|
+
}
|
|
1598
|
+
if (aLinesDisplay.length !== aLinesCompare.length || bLinesDisplay.length !== bLinesCompare.length) {
|
|
1599
|
+
// Fall back to diff of display lines.
|
|
1600
|
+
return diffLinesUnified(aLinesDisplay, bLinesDisplay, options);
|
|
1601
|
+
}
|
|
1602
|
+
const [diffs, truncated] = diffLinesRaw(aLinesCompare, bLinesCompare, options);
|
|
1603
|
+
// Replace comparison lines with displayable lines.
|
|
1604
|
+
let aIndex = 0;
|
|
1605
|
+
let bIndex = 0;
|
|
1606
|
+
diffs.forEach((diff) => {
|
|
1607
|
+
switch (diff[0]) {
|
|
1608
|
+
case DIFF_DELETE:
|
|
1609
|
+
diff[1] = aLinesDisplay[aIndex];
|
|
1610
|
+
aIndex += 1;
|
|
1611
|
+
break;
|
|
1612
|
+
case DIFF_INSERT:
|
|
1613
|
+
diff[1] = bLinesDisplay[bIndex];
|
|
1614
|
+
bIndex += 1;
|
|
1615
|
+
break;
|
|
1616
|
+
default:
|
|
1617
|
+
diff[1] = bLinesDisplay[bIndex];
|
|
1618
|
+
aIndex += 1;
|
|
1619
|
+
bIndex += 1;
|
|
1620
|
+
}
|
|
1621
|
+
});
|
|
1622
|
+
return printDiffLines(diffs, truncated, normalizeDiffOptions(options));
|
|
1623
|
+
}
|
|
1624
|
+
// Compare two arrays of strings line-by-line.
|
|
1625
|
+
function diffLinesRaw(aLines, bLines, options) {
|
|
1626
|
+
const truncate = (options === null || options === void 0 ? void 0 : options.truncateThreshold) ?? false;
|
|
1627
|
+
const truncateThreshold = Math.max(Math.floor((options === null || options === void 0 ? void 0 : options.truncateThreshold) ?? 0), 0);
|
|
1628
|
+
const aLength = truncate ? Math.min(aLines.length, truncateThreshold) : aLines.length;
|
|
1629
|
+
const bLength = truncate ? Math.min(bLines.length, truncateThreshold) : bLines.length;
|
|
1630
|
+
const truncated = aLength !== aLines.length || bLength !== bLines.length;
|
|
1631
|
+
const isCommon = (aIndex, bIndex) => aLines[aIndex] === bLines[bIndex];
|
|
1632
|
+
const diffs = [];
|
|
1633
|
+
let aIndex = 0;
|
|
1634
|
+
let bIndex = 0;
|
|
1635
|
+
const foundSubsequence = (nCommon, aCommon, bCommon) => {
|
|
1636
|
+
for (; aIndex !== aCommon; aIndex += 1) {
|
|
1637
|
+
diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]));
|
|
1638
|
+
}
|
|
1639
|
+
for (; bIndex !== bCommon; bIndex += 1) {
|
|
1640
|
+
diffs.push(new Diff(DIFF_INSERT, bLines[bIndex]));
|
|
1641
|
+
}
|
|
1642
|
+
for (; nCommon !== 0; nCommon -= 1, aIndex += 1, bIndex += 1) {
|
|
1643
|
+
diffs.push(new Diff(DIFF_EQUAL, bLines[bIndex]));
|
|
1644
|
+
}
|
|
1645
|
+
};
|
|
1646
|
+
diffSequences(aLength, bLength, isCommon, foundSubsequence);
|
|
1647
|
+
// After the last common subsequence, push remaining change items.
|
|
1648
|
+
for (; aIndex !== aLength; aIndex += 1) {
|
|
1649
|
+
diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]));
|
|
1650
|
+
}
|
|
1651
|
+
for (; bIndex !== bLength; bIndex += 1) {
|
|
1652
|
+
diffs.push(new Diff(DIFF_INSERT, bLines[bIndex]));
|
|
1653
|
+
}
|
|
1654
|
+
return [diffs, truncated];
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
// get the type of a value with handling the edge cases like `typeof []`
|
|
1658
|
+
// and `typeof null`
|
|
1659
|
+
function getType(value) {
|
|
1660
|
+
if (value === undefined) {
|
|
1661
|
+
return "undefined";
|
|
1662
|
+
} else if (value === null) {
|
|
1663
|
+
return "null";
|
|
1664
|
+
} else if (Array.isArray(value)) {
|
|
1665
|
+
return "array";
|
|
1666
|
+
} else if (typeof value === "boolean") {
|
|
1667
|
+
return "boolean";
|
|
1668
|
+
} else if (typeof value === "function") {
|
|
1669
|
+
return "function";
|
|
1670
|
+
} else if (typeof value === "number") {
|
|
1671
|
+
return "number";
|
|
1672
|
+
} else if (typeof value === "string") {
|
|
1673
|
+
return "string";
|
|
1674
|
+
} else if (typeof value === "bigint") {
|
|
1675
|
+
return "bigint";
|
|
1676
|
+
} else if (typeof value === "object") {
|
|
1677
|
+
if (value != null) {
|
|
1678
|
+
if (value.constructor === RegExp) {
|
|
1679
|
+
return "regexp";
|
|
1680
|
+
} else if (value.constructor === Map) {
|
|
1681
|
+
return "map";
|
|
1682
|
+
} else if (value.constructor === Set) {
|
|
1683
|
+
return "set";
|
|
1684
|
+
} else if (value.constructor === Date) {
|
|
1685
|
+
return "date";
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
return "object";
|
|
1689
|
+
} else if (typeof value === "symbol") {
|
|
1690
|
+
return "symbol";
|
|
1691
|
+
}
|
|
1692
|
+
throw new Error(`value of unknown type: ${value}`);
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
// platforms compatible
|
|
1696
|
+
function getNewLineSymbol(string) {
|
|
1697
|
+
return string.includes("\r\n") ? "\r\n" : "\n";
|
|
1698
|
+
}
|
|
1699
|
+
function diffStrings(a, b, options) {
|
|
1700
|
+
const truncate = (options === null || options === void 0 ? void 0 : options.truncateThreshold) ?? false;
|
|
1701
|
+
const truncateThreshold = Math.max(Math.floor((options === null || options === void 0 ? void 0 : options.truncateThreshold) ?? 0), 0);
|
|
1702
|
+
let aLength = a.length;
|
|
1703
|
+
let bLength = b.length;
|
|
1704
|
+
if (truncate) {
|
|
1705
|
+
const aMultipleLines = a.includes("\n");
|
|
1706
|
+
const bMultipleLines = b.includes("\n");
|
|
1707
|
+
const aNewLineSymbol = getNewLineSymbol(a);
|
|
1708
|
+
const bNewLineSymbol = getNewLineSymbol(b);
|
|
1709
|
+
// multiple-lines string expects a newline to be appended at the end
|
|
1710
|
+
const _a = aMultipleLines ? `${a.split(aNewLineSymbol, truncateThreshold).join(aNewLineSymbol)}\n` : a;
|
|
1711
|
+
const _b = bMultipleLines ? `${b.split(bNewLineSymbol, truncateThreshold).join(bNewLineSymbol)}\n` : b;
|
|
1712
|
+
aLength = _a.length;
|
|
1713
|
+
bLength = _b.length;
|
|
1714
|
+
}
|
|
1715
|
+
const truncated = aLength !== a.length || bLength !== b.length;
|
|
1716
|
+
const isCommon = (aIndex, bIndex) => a[aIndex] === b[bIndex];
|
|
1717
|
+
let aIndex = 0;
|
|
1718
|
+
let bIndex = 0;
|
|
1719
|
+
const diffs = [];
|
|
1720
|
+
const foundSubsequence = (nCommon, aCommon, bCommon) => {
|
|
1721
|
+
if (aIndex !== aCommon) {
|
|
1722
|
+
diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex, aCommon)));
|
|
1723
|
+
}
|
|
1724
|
+
if (bIndex !== bCommon) {
|
|
1725
|
+
diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex, bCommon)));
|
|
1726
|
+
}
|
|
1727
|
+
aIndex = aCommon + nCommon;
|
|
1728
|
+
bIndex = bCommon + nCommon;
|
|
1729
|
+
diffs.push(new Diff(DIFF_EQUAL, b.slice(bCommon, bIndex)));
|
|
1730
|
+
};
|
|
1731
|
+
diffSequences(aLength, bLength, isCommon, foundSubsequence);
|
|
1732
|
+
// After the last common subsequence, push remaining change items.
|
|
1733
|
+
if (aIndex !== aLength) {
|
|
1734
|
+
diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex)));
|
|
1735
|
+
}
|
|
1736
|
+
if (bIndex !== bLength) {
|
|
1737
|
+
diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex)));
|
|
1738
|
+
}
|
|
1739
|
+
return [diffs, truncated];
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
// Given change op and array of diffs, return concatenated string:
|
|
1743
|
+
// * include common strings
|
|
1744
|
+
// * include change strings which have argument op with changeColor
|
|
1745
|
+
// * exclude change strings which have opposite op
|
|
1746
|
+
function concatenateRelevantDiffs(op, diffs, changeColor) {
|
|
1747
|
+
return diffs.reduce((reduced, diff) => reduced + (diff[0] === DIFF_EQUAL ? diff[1] : diff[0] === op && diff[1].length !== 0 ? changeColor(diff[1]) : ""), "");
|
|
1748
|
+
}
|
|
1749
|
+
// Encapsulate change lines until either a common newline or the end.
|
|
1750
|
+
class ChangeBuffer {
|
|
1751
|
+
op;
|
|
1752
|
+
line;
|
|
1753
|
+
lines;
|
|
1754
|
+
changeColor;
|
|
1755
|
+
constructor(op, changeColor) {
|
|
1756
|
+
this.op = op;
|
|
1757
|
+
this.line = [];
|
|
1758
|
+
this.lines = [];
|
|
1759
|
+
this.changeColor = changeColor;
|
|
1760
|
+
}
|
|
1761
|
+
pushSubstring(substring) {
|
|
1762
|
+
this.pushDiff(new Diff(this.op, substring));
|
|
1763
|
+
}
|
|
1764
|
+
pushLine() {
|
|
1765
|
+
// Assume call only if line has at least one diff,
|
|
1766
|
+
// therefore an empty line must have a diff which has an empty string.
|
|
1767
|
+
// If line has multiple diffs, then assume it has a common diff,
|
|
1768
|
+
// therefore change diffs have change color;
|
|
1769
|
+
// otherwise then it has line color only.
|
|
1770
|
+
this.lines.push(this.line.length !== 1 ? new Diff(this.op, concatenateRelevantDiffs(this.op, this.line, this.changeColor)) : this.line[0][0] === this.op ? this.line[0] : new Diff(this.op, this.line[0][1]));
|
|
1771
|
+
this.line.length = 0;
|
|
1772
|
+
}
|
|
1773
|
+
isLineEmpty() {
|
|
1774
|
+
return this.line.length === 0;
|
|
1775
|
+
}
|
|
1776
|
+
// Minor input to buffer.
|
|
1777
|
+
pushDiff(diff) {
|
|
1778
|
+
this.line.push(diff);
|
|
1779
|
+
}
|
|
1780
|
+
// Main input to buffer.
|
|
1781
|
+
align(diff) {
|
|
1782
|
+
const string = diff[1];
|
|
1783
|
+
if (string.includes("\n")) {
|
|
1784
|
+
const substrings = string.split("\n");
|
|
1785
|
+
const iLast = substrings.length - 1;
|
|
1786
|
+
substrings.forEach((substring, i) => {
|
|
1787
|
+
if (i < iLast) {
|
|
1788
|
+
// The first substring completes the current change line.
|
|
1789
|
+
// A middle substring is a change line.
|
|
1790
|
+
this.pushSubstring(substring);
|
|
1791
|
+
this.pushLine();
|
|
1792
|
+
} else if (substring.length !== 0) {
|
|
1793
|
+
// The last substring starts a change line, if it is not empty.
|
|
1794
|
+
// Important: This non-empty condition also automatically omits
|
|
1795
|
+
// the newline appended to the end of expected and received strings.
|
|
1796
|
+
this.pushSubstring(substring);
|
|
1797
|
+
}
|
|
1798
|
+
});
|
|
1799
|
+
} else {
|
|
1800
|
+
// Append non-multiline string to current change line.
|
|
1801
|
+
this.pushDiff(diff);
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
// Output from buffer.
|
|
1805
|
+
moveLinesTo(lines) {
|
|
1806
|
+
if (!this.isLineEmpty()) {
|
|
1807
|
+
this.pushLine();
|
|
1808
|
+
}
|
|
1809
|
+
lines.push(...this.lines);
|
|
1810
|
+
this.lines.length = 0;
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
// Encapsulate common and change lines.
|
|
1814
|
+
class CommonBuffer {
|
|
1815
|
+
deleteBuffer;
|
|
1816
|
+
insertBuffer;
|
|
1817
|
+
lines;
|
|
1818
|
+
constructor(deleteBuffer, insertBuffer) {
|
|
1819
|
+
this.deleteBuffer = deleteBuffer;
|
|
1820
|
+
this.insertBuffer = insertBuffer;
|
|
1821
|
+
this.lines = [];
|
|
1822
|
+
}
|
|
1823
|
+
pushDiffCommonLine(diff) {
|
|
1824
|
+
this.lines.push(diff);
|
|
1825
|
+
}
|
|
1826
|
+
pushDiffChangeLines(diff) {
|
|
1827
|
+
const isDiffEmpty = diff[1].length === 0;
|
|
1828
|
+
// An empty diff string is redundant, unless a change line is empty.
|
|
1829
|
+
if (!isDiffEmpty || this.deleteBuffer.isLineEmpty()) {
|
|
1830
|
+
this.deleteBuffer.pushDiff(diff);
|
|
1831
|
+
}
|
|
1832
|
+
if (!isDiffEmpty || this.insertBuffer.isLineEmpty()) {
|
|
1833
|
+
this.insertBuffer.pushDiff(diff);
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
flushChangeLines() {
|
|
1837
|
+
this.deleteBuffer.moveLinesTo(this.lines);
|
|
1838
|
+
this.insertBuffer.moveLinesTo(this.lines);
|
|
1839
|
+
}
|
|
1840
|
+
// Input to buffer.
|
|
1841
|
+
align(diff) {
|
|
1842
|
+
const op = diff[0];
|
|
1843
|
+
const string = diff[1];
|
|
1844
|
+
if (string.includes("\n")) {
|
|
1845
|
+
const substrings = string.split("\n");
|
|
1846
|
+
const iLast = substrings.length - 1;
|
|
1847
|
+
substrings.forEach((substring, i) => {
|
|
1848
|
+
if (i === 0) {
|
|
1849
|
+
const subdiff = new Diff(op, substring);
|
|
1850
|
+
if (this.deleteBuffer.isLineEmpty() && this.insertBuffer.isLineEmpty()) {
|
|
1851
|
+
// If both current change lines are empty,
|
|
1852
|
+
// then the first substring is a common line.
|
|
1853
|
+
this.flushChangeLines();
|
|
1854
|
+
this.pushDiffCommonLine(subdiff);
|
|
1855
|
+
} else {
|
|
1856
|
+
// If either current change line is non-empty,
|
|
1857
|
+
// then the first substring completes the change lines.
|
|
1858
|
+
this.pushDiffChangeLines(subdiff);
|
|
1859
|
+
this.flushChangeLines();
|
|
1860
|
+
}
|
|
1861
|
+
} else if (i < iLast) {
|
|
1862
|
+
// A middle substring is a common line.
|
|
1863
|
+
this.pushDiffCommonLine(new Diff(op, substring));
|
|
1864
|
+
} else if (substring.length !== 0) {
|
|
1865
|
+
// The last substring starts a change line, if it is not empty.
|
|
1866
|
+
// Important: This non-empty condition also automatically omits
|
|
1867
|
+
// the newline appended to the end of expected and received strings.
|
|
1868
|
+
this.pushDiffChangeLines(new Diff(op, substring));
|
|
1869
|
+
}
|
|
1870
|
+
});
|
|
1871
|
+
} else {
|
|
1872
|
+
// Append non-multiline string to current change lines.
|
|
1873
|
+
// Important: It cannot be at the end following empty change lines,
|
|
1874
|
+
// because newline appended to the end of expected and received strings.
|
|
1875
|
+
this.pushDiffChangeLines(diff);
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
// Output from buffer.
|
|
1879
|
+
getLines() {
|
|
1880
|
+
this.flushChangeLines();
|
|
1881
|
+
return this.lines;
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
// Given diffs from expected and received strings,
|
|
1885
|
+
// return new array of diffs split or joined into lines.
|
|
1886
|
+
//
|
|
1887
|
+
// To correctly align a change line at the end, the algorithm:
|
|
1888
|
+
// * assumes that a newline was appended to the strings
|
|
1889
|
+
// * omits the last newline from the output array
|
|
1890
|
+
//
|
|
1891
|
+
// Assume the function is not called:
|
|
1892
|
+
// * if either expected or received is empty string
|
|
1893
|
+
// * if neither expected nor received is multiline string
|
|
1894
|
+
function getAlignedDiffs(diffs, changeColor) {
|
|
1895
|
+
const deleteBuffer = new ChangeBuffer(DIFF_DELETE, changeColor);
|
|
1896
|
+
const insertBuffer = new ChangeBuffer(DIFF_INSERT, changeColor);
|
|
1897
|
+
const commonBuffer = new CommonBuffer(deleteBuffer, insertBuffer);
|
|
1898
|
+
diffs.forEach((diff) => {
|
|
1899
|
+
switch (diff[0]) {
|
|
1900
|
+
case DIFF_DELETE:
|
|
1901
|
+
deleteBuffer.align(diff);
|
|
1902
|
+
break;
|
|
1903
|
+
case DIFF_INSERT:
|
|
1904
|
+
insertBuffer.align(diff);
|
|
1905
|
+
break;
|
|
1906
|
+
default: commonBuffer.align(diff);
|
|
1907
|
+
}
|
|
1908
|
+
});
|
|
1909
|
+
return commonBuffer.getLines();
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
function hasCommonDiff(diffs, isMultiline) {
|
|
1913
|
+
if (isMultiline) {
|
|
1914
|
+
// Important: Ignore common newline that was appended to multiline strings!
|
|
1915
|
+
const iLast = diffs.length - 1;
|
|
1916
|
+
return diffs.some((diff, i) => diff[0] === DIFF_EQUAL && (i !== iLast || diff[1] !== "\n"));
|
|
1917
|
+
}
|
|
1918
|
+
return diffs.some((diff) => diff[0] === DIFF_EQUAL);
|
|
1919
|
+
}
|
|
1920
|
+
// Compare two strings character-by-character.
|
|
1921
|
+
// Format as comparison lines in which changed substrings have inverse colors.
|
|
1922
|
+
function diffStringsUnified(a, b, options) {
|
|
1923
|
+
if (a !== b && a.length !== 0 && b.length !== 0) {
|
|
1924
|
+
const isMultiline = a.includes("\n") || b.includes("\n");
|
|
1925
|
+
// getAlignedDiffs assumes that a newline was appended to the strings.
|
|
1926
|
+
const [diffs, truncated] = diffStringsRaw(isMultiline ? `${a}\n` : a, isMultiline ? `${b}\n` : b, true, options);
|
|
1927
|
+
if (hasCommonDiff(diffs, isMultiline)) {
|
|
1928
|
+
const optionsNormalized = normalizeDiffOptions(options);
|
|
1929
|
+
const lines = getAlignedDiffs(diffs, optionsNormalized.changeColor);
|
|
1930
|
+
return printDiffLines(lines, truncated, optionsNormalized);
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
// Fall back to line-by-line diff.
|
|
1934
|
+
return diffLinesUnified(a.split("\n"), b.split("\n"), options);
|
|
1935
|
+
}
|
|
1936
|
+
// Compare two strings character-by-character.
|
|
1937
|
+
// Optionally clean up small common substrings, also known as chaff.
|
|
1938
|
+
function diffStringsRaw(a, b, cleanup, options) {
|
|
1939
|
+
const [diffs, truncated] = diffStrings(a, b, options);
|
|
1940
|
+
if (cleanup) {
|
|
1941
|
+
diff_cleanupSemantic(diffs);
|
|
1942
|
+
}
|
|
1943
|
+
return [diffs, truncated];
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
function getCommonMessage(message, options) {
|
|
1947
|
+
const { commonColor } = normalizeDiffOptions(options);
|
|
1948
|
+
return commonColor(message);
|
|
1949
|
+
}
|
|
1950
|
+
const { AsymmetricMatcher, DOMCollection, DOMElement, Immutable, ReactElement, ReactTestComponent } = plugins;
|
|
1951
|
+
const PLUGINS = [
|
|
1952
|
+
ReactTestComponent,
|
|
1953
|
+
ReactElement,
|
|
1954
|
+
DOMElement,
|
|
1955
|
+
DOMCollection,
|
|
1956
|
+
Immutable,
|
|
1957
|
+
AsymmetricMatcher,
|
|
1958
|
+
plugins.Error
|
|
1959
|
+
];
|
|
1960
|
+
const FORMAT_OPTIONS = {
|
|
1961
|
+
maxDepth: 20,
|
|
1962
|
+
plugins: PLUGINS
|
|
1963
|
+
};
|
|
1964
|
+
const FALLBACK_FORMAT_OPTIONS = {
|
|
1965
|
+
callToJSON: false,
|
|
1966
|
+
maxDepth: 8,
|
|
1967
|
+
plugins: PLUGINS
|
|
1968
|
+
};
|
|
1969
|
+
// Generate a string that will highlight the difference between two values
|
|
1970
|
+
// with green and red. (similar to how github does code diffing)
|
|
1971
|
+
/**
|
|
1972
|
+
* @param a Expected value
|
|
1973
|
+
* @param b Received value
|
|
1974
|
+
* @param options Diff options
|
|
1975
|
+
* @returns {string | null} a string diff
|
|
1976
|
+
*/
|
|
1977
|
+
function diff(a, b, options) {
|
|
1978
|
+
if (Object.is(a, b)) {
|
|
1979
|
+
return "";
|
|
1980
|
+
}
|
|
1981
|
+
const aType = getType(a);
|
|
1982
|
+
let expectedType = aType;
|
|
1983
|
+
let omitDifference = false;
|
|
1984
|
+
if (aType === "object" && typeof a.asymmetricMatch === "function") {
|
|
1985
|
+
if (a.$$typeof !== Symbol.for("jest.asymmetricMatcher")) {
|
|
1986
|
+
// Do not know expected type of user-defined asymmetric matcher.
|
|
1987
|
+
return undefined;
|
|
1988
|
+
}
|
|
1989
|
+
if (typeof a.getExpectedType !== "function") {
|
|
1990
|
+
// For example, expect.anything() matches either null or undefined
|
|
1991
|
+
return undefined;
|
|
1992
|
+
}
|
|
1993
|
+
expectedType = a.getExpectedType();
|
|
1994
|
+
// Primitive types boolean and number omit difference below.
|
|
1995
|
+
// For example, omit difference for expect.stringMatching(regexp)
|
|
1996
|
+
omitDifference = expectedType === "string";
|
|
1997
|
+
}
|
|
1998
|
+
if (expectedType !== getType(b)) {
|
|
1999
|
+
const { aAnnotation, aColor, aIndicator, bAnnotation, bColor, bIndicator } = normalizeDiffOptions(options);
|
|
2000
|
+
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options);
|
|
2001
|
+
let aDisplay = format(a, formatOptions);
|
|
2002
|
+
let bDisplay = format(b, formatOptions);
|
|
2003
|
+
// even if prettyFormat prints successfully big objects,
|
|
2004
|
+
// large string can choke later on (concatenation? RPC?),
|
|
2005
|
+
// so truncate it to a reasonable length here.
|
|
2006
|
+
// (For example, playwright's ElementHandle can become about 200_000_000 length string)
|
|
2007
|
+
const MAX_LENGTH = 1e5;
|
|
2008
|
+
function truncate(s) {
|
|
2009
|
+
return s.length <= MAX_LENGTH ? s : `${s.slice(0, MAX_LENGTH)}...`;
|
|
2010
|
+
}
|
|
2011
|
+
aDisplay = truncate(aDisplay);
|
|
2012
|
+
bDisplay = truncate(bDisplay);
|
|
2013
|
+
const aDiff = `${aColor(`${aIndicator} ${aAnnotation}:`)} \n${aDisplay}`;
|
|
2014
|
+
const bDiff = `${bColor(`${bIndicator} ${bAnnotation}:`)} \n${bDisplay}`;
|
|
2015
|
+
return `${aDiff}\n\n${bDiff}`;
|
|
2016
|
+
}
|
|
2017
|
+
if (omitDifference) {
|
|
2018
|
+
return undefined;
|
|
2019
|
+
}
|
|
2020
|
+
switch (aType) {
|
|
2021
|
+
case "string": return diffLinesUnified(a.split("\n"), b.split("\n"), options);
|
|
2022
|
+
case "boolean":
|
|
2023
|
+
case "number": return comparePrimitive(a, b, options);
|
|
2024
|
+
case "map": return compareObjects(sortMap(a), sortMap(b), options);
|
|
2025
|
+
case "set": return compareObjects(sortSet(a), sortSet(b), options);
|
|
2026
|
+
default: return compareObjects(a, b, options);
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
function comparePrimitive(a, b, options) {
|
|
2030
|
+
const aFormat = format(a, FORMAT_OPTIONS);
|
|
2031
|
+
const bFormat = format(b, FORMAT_OPTIONS);
|
|
2032
|
+
return aFormat === bFormat ? "" : diffLinesUnified(aFormat.split("\n"), bFormat.split("\n"), options);
|
|
2033
|
+
}
|
|
2034
|
+
function sortMap(map) {
|
|
2035
|
+
return new Map(Array.from(map.entries()).sort());
|
|
2036
|
+
}
|
|
2037
|
+
function sortSet(set) {
|
|
2038
|
+
return new Set(Array.from(set.values()).sort());
|
|
2039
|
+
}
|
|
2040
|
+
function compareObjects(a, b, options) {
|
|
2041
|
+
let difference;
|
|
2042
|
+
let hasThrown = false;
|
|
2043
|
+
try {
|
|
2044
|
+
const formatOptions = getFormatOptions(FORMAT_OPTIONS, options);
|
|
2045
|
+
difference = getObjectsDifference(a, b, formatOptions, options);
|
|
2046
|
+
} catch {
|
|
2047
|
+
hasThrown = true;
|
|
2048
|
+
}
|
|
2049
|
+
const noDiffMessage = getCommonMessage(NO_DIFF_MESSAGE, options);
|
|
2050
|
+
// If the comparison yields no results, compare again but this time
|
|
2051
|
+
// without calling `toJSON`. It's also possible that toJSON might throw.
|
|
2052
|
+
if (difference === undefined || difference === noDiffMessage) {
|
|
2053
|
+
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options);
|
|
2054
|
+
difference = getObjectsDifference(a, b, formatOptions, options);
|
|
2055
|
+
if (difference !== noDiffMessage && !hasThrown) {
|
|
2056
|
+
difference = `${getCommonMessage(SIMILAR_MESSAGE, options)}\n\n${difference}`;
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
return difference;
|
|
2060
|
+
}
|
|
2061
|
+
function getFormatOptions(formatOptions, options) {
|
|
2062
|
+
const { compareKeys, printBasicPrototype, maxDepth } = normalizeDiffOptions(options);
|
|
2063
|
+
return {
|
|
2064
|
+
...formatOptions,
|
|
2065
|
+
compareKeys,
|
|
2066
|
+
printBasicPrototype,
|
|
2067
|
+
maxDepth: maxDepth ?? formatOptions.maxDepth
|
|
2068
|
+
};
|
|
2069
|
+
}
|
|
2070
|
+
function getObjectsDifference(a, b, formatOptions, options) {
|
|
2071
|
+
const formatOptionsZeroIndent = {
|
|
2072
|
+
...formatOptions,
|
|
2073
|
+
indent: 0
|
|
2074
|
+
};
|
|
2075
|
+
const aCompare = format(a, formatOptionsZeroIndent);
|
|
2076
|
+
const bCompare = format(b, formatOptionsZeroIndent);
|
|
2077
|
+
if (aCompare === bCompare) {
|
|
2078
|
+
return getCommonMessage(NO_DIFF_MESSAGE, options);
|
|
2079
|
+
} else {
|
|
2080
|
+
const aDisplay = format(a, formatOptions);
|
|
2081
|
+
const bDisplay = format(b, formatOptions);
|
|
2082
|
+
return diffLinesUnified2(aDisplay.split("\n"), bDisplay.split("\n"), aCompare.split("\n"), bCompare.split("\n"), options);
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
const MAX_DIFF_STRING_LENGTH = 2e4;
|
|
2086
|
+
function isAsymmetricMatcher(data) {
|
|
2087
|
+
const type = getType$1(data);
|
|
2088
|
+
return type === "Object" && typeof data.asymmetricMatch === "function";
|
|
2089
|
+
}
|
|
2090
|
+
function isReplaceable(obj1, obj2) {
|
|
2091
|
+
const obj1Type = getType$1(obj1);
|
|
2092
|
+
const obj2Type = getType$1(obj2);
|
|
2093
|
+
return obj1Type === obj2Type && (obj1Type === "Object" || obj1Type === "Array");
|
|
2094
|
+
}
|
|
2095
|
+
function printDiffOrStringify(received, expected, options) {
|
|
2096
|
+
const { aAnnotation, bAnnotation } = normalizeDiffOptions(options);
|
|
2097
|
+
if (typeof expected === "string" && typeof received === "string" && expected.length > 0 && received.length > 0 && expected.length <= MAX_DIFF_STRING_LENGTH && received.length <= MAX_DIFF_STRING_LENGTH && expected !== received) {
|
|
2098
|
+
if (expected.includes("\n") || received.includes("\n")) {
|
|
2099
|
+
return diffStringsUnified(expected, received, options);
|
|
2100
|
+
}
|
|
2101
|
+
const [diffs] = diffStringsRaw(expected, received, true);
|
|
2102
|
+
const hasCommonDiff = diffs.some((diff) => diff[0] === DIFF_EQUAL);
|
|
2103
|
+
const printLabel = getLabelPrinter(aAnnotation, bAnnotation);
|
|
2104
|
+
const expectedLine = printLabel(aAnnotation) + printExpected(getCommonAndChangedSubstrings(diffs, DIFF_DELETE, hasCommonDiff));
|
|
2105
|
+
const receivedLine = printLabel(bAnnotation) + printReceived(getCommonAndChangedSubstrings(diffs, DIFF_INSERT, hasCommonDiff));
|
|
2106
|
+
return `${expectedLine}\n${receivedLine}`;
|
|
2107
|
+
}
|
|
2108
|
+
// if (isLineDiffable(expected, received)) {
|
|
2109
|
+
const clonedExpected = deepClone(expected, { forceWritable: true });
|
|
2110
|
+
const clonedReceived = deepClone(received, { forceWritable: true });
|
|
2111
|
+
const { replacedExpected, replacedActual } = replaceAsymmetricMatcher(clonedReceived, clonedExpected);
|
|
2112
|
+
const difference = diff(replacedExpected, replacedActual, options);
|
|
2113
|
+
return difference;
|
|
2114
|
+
// }
|
|
2115
|
+
// const printLabel = getLabelPrinter(aAnnotation, bAnnotation)
|
|
2116
|
+
// const expectedLine = printLabel(aAnnotation) + printExpected(expected)
|
|
2117
|
+
// const receivedLine
|
|
2118
|
+
// = printLabel(bAnnotation)
|
|
2119
|
+
// + (stringify(expected) === stringify(received)
|
|
2120
|
+
// ? 'serializes to the same string'
|
|
2121
|
+
// : printReceived(received))
|
|
2122
|
+
// return `${expectedLine}\n${receivedLine}`
|
|
2123
|
+
}
|
|
2124
|
+
function replaceAsymmetricMatcher(actual, expected, actualReplaced = new WeakSet(), expectedReplaced = new WeakSet()) {
|
|
2125
|
+
// handle asymmetric Error.cause diff
|
|
2126
|
+
if (actual instanceof Error && expected instanceof Error && typeof actual.cause !== "undefined" && typeof expected.cause === "undefined") {
|
|
2127
|
+
delete actual.cause;
|
|
2128
|
+
return {
|
|
2129
|
+
replacedActual: actual,
|
|
2130
|
+
replacedExpected: expected
|
|
2131
|
+
};
|
|
2132
|
+
}
|
|
2133
|
+
if (!isReplaceable(actual, expected)) {
|
|
2134
|
+
return {
|
|
2135
|
+
replacedActual: actual,
|
|
2136
|
+
replacedExpected: expected
|
|
2137
|
+
};
|
|
2138
|
+
}
|
|
2139
|
+
if (actualReplaced.has(actual) || expectedReplaced.has(expected)) {
|
|
2140
|
+
return {
|
|
2141
|
+
replacedActual: actual,
|
|
2142
|
+
replacedExpected: expected
|
|
2143
|
+
};
|
|
2144
|
+
}
|
|
2145
|
+
actualReplaced.add(actual);
|
|
2146
|
+
expectedReplaced.add(expected);
|
|
2147
|
+
getOwnProperties(expected).forEach((key) => {
|
|
2148
|
+
const expectedValue = expected[key];
|
|
2149
|
+
const actualValue = actual[key];
|
|
2150
|
+
if (isAsymmetricMatcher(expectedValue)) {
|
|
2151
|
+
if (expectedValue.asymmetricMatch(actualValue)) {
|
|
2152
|
+
// When matcher matches, replace expected with actual value
|
|
2153
|
+
// so they appear the same in the diff
|
|
2154
|
+
expected[key] = actualValue;
|
|
2155
|
+
} else if ("sample" in expectedValue && expectedValue.sample !== undefined && isReplaceable(actualValue, expectedValue.sample)) {
|
|
2156
|
+
// For container matchers (ArrayContaining, ObjectContaining), unwrap and recursively process
|
|
2157
|
+
// Matcher doesn't match: unwrap but keep structure to show mismatch
|
|
2158
|
+
const replaced = replaceAsymmetricMatcher(actualValue, expectedValue.sample, actualReplaced, expectedReplaced);
|
|
2159
|
+
actual[key] = replaced.replacedActual;
|
|
2160
|
+
expected[key] = replaced.replacedExpected;
|
|
2161
|
+
}
|
|
2162
|
+
} else if (isAsymmetricMatcher(actualValue)) {
|
|
2163
|
+
if (actualValue.asymmetricMatch(expectedValue)) {
|
|
2164
|
+
actual[key] = expectedValue;
|
|
2165
|
+
} else if ("sample" in actualValue && actualValue.sample !== undefined && isReplaceable(actualValue.sample, expectedValue)) {
|
|
2166
|
+
const replaced = replaceAsymmetricMatcher(actualValue.sample, expectedValue, actualReplaced, expectedReplaced);
|
|
2167
|
+
actual[key] = replaced.replacedActual;
|
|
2168
|
+
expected[key] = replaced.replacedExpected;
|
|
2169
|
+
}
|
|
2170
|
+
} else if (isReplaceable(actualValue, expectedValue)) {
|
|
2171
|
+
const replaced = replaceAsymmetricMatcher(actualValue, expectedValue, actualReplaced, expectedReplaced);
|
|
2172
|
+
actual[key] = replaced.replacedActual;
|
|
2173
|
+
expected[key] = replaced.replacedExpected;
|
|
2174
|
+
}
|
|
2175
|
+
});
|
|
2176
|
+
return {
|
|
2177
|
+
replacedActual: actual,
|
|
2178
|
+
replacedExpected: expected
|
|
2179
|
+
};
|
|
2180
|
+
}
|
|
2181
|
+
function getLabelPrinter(...strings) {
|
|
2182
|
+
const maxLength = strings.reduce((max, string) => string.length > max ? string.length : max, 0);
|
|
2183
|
+
return (string) => `${string}: ${" ".repeat(maxLength - string.length)}`;
|
|
2184
|
+
}
|
|
2185
|
+
const SPACE_SYMBOL = "·";
|
|
2186
|
+
function replaceTrailingSpaces(text) {
|
|
2187
|
+
return text.replace(/\s+$/gm, (spaces) => SPACE_SYMBOL.repeat(spaces.length));
|
|
2188
|
+
}
|
|
2189
|
+
function printReceived(object) {
|
|
2190
|
+
return c.red(replaceTrailingSpaces(stringify(object)));
|
|
2191
|
+
}
|
|
2192
|
+
function printExpected(value) {
|
|
2193
|
+
return c.green(replaceTrailingSpaces(stringify(value)));
|
|
2194
|
+
}
|
|
2195
|
+
function getCommonAndChangedSubstrings(diffs, op, hasCommonDiff) {
|
|
2196
|
+
return diffs.reduce((reduced, diff) => reduced + (diff[0] === DIFF_EQUAL ? diff[1] : diff[0] === op ? hasCommonDiff ? c.inverse(diff[1]) : diff[1] : ""), "");
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff, diff, diffLinesRaw, diffLinesUnified, diffLinesUnified2, diffStringsRaw, diffStringsUnified, getLabelPrinter, printDiffOrStringify, replaceAsymmetricMatcher };
|