@diffson/core 1.0.3 → 1.0.5

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.
Files changed (79) hide show
  1. package/dist/contract/constant/DiffConstants.d.ts +8 -0
  2. package/dist/contract/constant/DiffConstants.js +8 -0
  3. package/dist/contract/constant/PresetName.d.ts +6 -0
  4. package/dist/contract/constant/PresetName.js +7 -0
  5. package/dist/contract/constant/index.d.ts +2 -0
  6. package/dist/contract/constant/index.js +2 -0
  7. package/dist/contract/index.d.ts +2 -0
  8. package/dist/contract/index.js +2 -0
  9. package/dist/contract/type/ArrayComparator.d.ts +8 -0
  10. package/dist/contract/type/ArrayComparator.js +2 -0
  11. package/dist/contract/type/ComparatorOrchestrator.d.ts +9 -0
  12. package/dist/contract/type/ComparatorOrchestrator.js +2 -0
  13. package/dist/contract/type/DiffService.d.ts +27 -0
  14. package/dist/contract/type/DiffService.js +2 -0
  15. package/dist/contract/type/JsonTypes.d.ts +5 -0
  16. package/dist/contract/type/JsonTypes.js +1 -0
  17. package/dist/contract/type/NullComparator.d.ts +6 -0
  18. package/dist/contract/type/NullComparator.js +2 -0
  19. package/dist/contract/type/ObjectComparator.d.ts +8 -0
  20. package/dist/contract/type/ObjectComparator.js +2 -0
  21. package/dist/contract/type/OtherComparator.d.ts +7 -0
  22. package/dist/contract/type/OtherComparator.js +2 -0
  23. package/dist/contract/type/PrimitiveComparator.d.ts +6 -0
  24. package/dist/contract/type/PrimitiveComparator.js +2 -0
  25. package/dist/contract/type/Result.d.ts +7 -0
  26. package/dist/contract/type/Result.js +7 -0
  27. package/dist/contract/type/SingleNodeDifference.d.ts +7 -0
  28. package/dist/contract/type/SingleNodeDifference.js +12 -0
  29. package/dist/contract/type/index.d.ts +10 -0
  30. package/dist/contract/type/index.js +10 -0
  31. package/dist/index.d.ts +6 -0
  32. package/dist/index.js +8 -0
  33. package/dist/service/comparator/ComparatorOrchestrator.d.ts +13 -0
  34. package/dist/service/comparator/ComparatorOrchestrator.js +55 -0
  35. package/dist/service/comparator/array/AbstractArray.d.ts +11 -0
  36. package/dist/service/comparator/array/AbstractArray.js +40 -0
  37. package/dist/service/comparator/array/SequentialArrayComparator.d.ts +9 -0
  38. package/dist/service/comparator/array/SequentialArrayComparator.js +49 -0
  39. package/dist/service/comparator/array/SimilarArrayComparator.d.ts +21 -0
  40. package/dist/service/comparator/array/SimilarArrayComparator.js +152 -0
  41. package/dist/service/comparator/array/index.d.ts +3 -0
  42. package/dist/service/comparator/array/index.js +3 -0
  43. package/dist/service/comparator/index.d.ts +6 -0
  44. package/dist/service/comparator/index.js +6 -0
  45. package/dist/service/comparator/nulls/DefaultNullComparator.d.ts +6 -0
  46. package/dist/service/comparator/nulls/DefaultNullComparator.js +6 -0
  47. package/dist/service/comparator/nulls/index.d.ts +1 -0
  48. package/dist/service/comparator/nulls/index.js +1 -0
  49. package/dist/service/comparator/object/AbstractObject.d.ts +16 -0
  50. package/dist/service/comparator/object/AbstractObject.js +90 -0
  51. package/dist/service/comparator/object/LeftJoinObjectComparator.d.ts +8 -0
  52. package/dist/service/comparator/object/LeftJoinObjectComparator.js +25 -0
  53. package/dist/service/comparator/object/UnionKeyObjectComparator.d.ts +8 -0
  54. package/dist/service/comparator/object/UnionKeyObjectComparator.js +26 -0
  55. package/dist/service/comparator/object/index.d.ts +3 -0
  56. package/dist/service/comparator/object/index.js +3 -0
  57. package/dist/service/comparator/other/DefaultOtherComparator.d.ts +6 -0
  58. package/dist/service/comparator/other/DefaultOtherComparator.js +13 -0
  59. package/dist/service/comparator/other/index.d.ts +1 -0
  60. package/dist/service/comparator/other/index.js +1 -0
  61. package/dist/service/comparator/primitive/DefaultPrimitiveComparator.d.ts +6 -0
  62. package/dist/service/comparator/primitive/DefaultPrimitiveComparator.js +16 -0
  63. package/dist/service/comparator/primitive/index.d.ts +1 -0
  64. package/dist/service/comparator/primitive/index.js +1 -0
  65. package/dist/service/diff/DiffContext.d.ts +13 -0
  66. package/dist/service/diff/DiffContext.js +29 -0
  67. package/dist/service/diff/DiffService.d.ts +26 -0
  68. package/dist/service/diff/DiffService.js +184 -0
  69. package/dist/service/diff/PathTracker.d.ts +21 -0
  70. package/dist/service/diff/PathTracker.js +56 -0
  71. package/dist/service/diff/index.d.ts +1 -0
  72. package/dist/service/diff/index.js +1 -0
  73. package/dist/service/index.d.ts +2 -0
  74. package/dist/service/index.js +2 -0
  75. package/dist/util/TypeGuards.d.ts +6 -0
  76. package/dist/util/TypeGuards.js +32 -0
  77. package/dist/util/index.d.ts +1 -0
  78. package/dist/util/index.js +1 -0
  79. package/package.json +6 -4
@@ -0,0 +1,8 @@
1
+ export declare const DIFFERENT = false;
2
+ export declare const SAME = true;
3
+ export declare const SPLIT_PATH = "\\.";
4
+ export declare const MERGE_PATH = ".";
5
+ export declare const OBJECT_NULL: null;
6
+ export declare const TYPE_MODIFY = "MODIFY";
7
+ export declare const TYPE_ADD = "ADD";
8
+ export declare const TYPE_DELETE = "DELETE";
@@ -0,0 +1,8 @@
1
+ export const DIFFERENT = false;
2
+ export const SAME = true;
3
+ export const SPLIT_PATH = "\\.";
4
+ export const MERGE_PATH = ".";
5
+ export const OBJECT_NULL = null;
6
+ export const TYPE_MODIFY = "MODIFY";
7
+ export const TYPE_ADD = "ADD";
8
+ export const TYPE_DELETE = "DELETE";
@@ -0,0 +1,6 @@
1
+ export declare enum PresetName {
2
+ FullSmart = "fullSmart",
3
+ FullOrdered = "fullOrdered",
4
+ LeftSmart = "leftSmart",
5
+ LeftOrdered = "leftOrdered"
6
+ }
@@ -0,0 +1,7 @@
1
+ export var PresetName;
2
+ (function (PresetName) {
3
+ PresetName["FullSmart"] = "fullSmart";
4
+ PresetName["FullOrdered"] = "fullOrdered";
5
+ PresetName["LeftSmart"] = "leftSmart";
6
+ PresetName["LeftOrdered"] = "leftOrdered";
7
+ })(PresetName || (PresetName = {}));
@@ -0,0 +1,2 @@
1
+ export * from "./DiffConstants";
2
+ export * from "./PresetName";
@@ -0,0 +1,2 @@
1
+ export * from "./DiffConstants";
2
+ export * from "./PresetName";
@@ -0,0 +1,2 @@
1
+ export * from "./type";
2
+ export * from "./constant";
@@ -0,0 +1,2 @@
1
+ export * from "./type";
2
+ export * from "./constant";
@@ -0,0 +1,8 @@
1
+ import type { JsonArray, JsonValue } from "./JsonTypes";
2
+ import type { DiffContext } from "../../service/diff/DiffContext";
3
+ import type { PathTracker } from "../../service/diff/PathTracker";
4
+ export interface IArrayComparator {
5
+ diffArray(a: JsonArray, b: JsonArray, pathTracker: PathTracker): DiffContext;
6
+ diffElement(a: JsonValue | undefined, b: JsonValue | undefined, pathTracker: PathTracker): DiffContext;
7
+ }
8
+ export declare const IArrayComparator: import("@wendellhu/redi").IdentifierDecorator<IArrayComparator>;
@@ -0,0 +1,2 @@
1
+ import { createIdentifier } from "@wendellhu/redi";
2
+ export const IArrayComparator = createIdentifier("IArrayComparator");
@@ -0,0 +1,9 @@
1
+ import type { JsonValue } from "./JsonTypes";
2
+ import type { DiffContext } from "../../service/diff/DiffContext";
3
+ import type { PathTracker } from "../../service/diff/PathTracker";
4
+ import type { IArrayComparator } from "./ArrayComparator";
5
+ export interface IComparatorOrchestrator {
6
+ diffElement(a: JsonValue | undefined, b: JsonValue | undefined, pathTracker: PathTracker): DiffContext;
7
+ getArrayComparator(): IArrayComparator;
8
+ }
9
+ export declare const IComparatorOrchestrator: import("@wendellhu/redi").IdentifierDecorator<IComparatorOrchestrator>;
@@ -0,0 +1,2 @@
1
+ import { createIdentifier } from "@wendellhu/redi";
2
+ export const IComparatorOrchestrator = createIdentifier("IComparatorOrchestrator");
@@ -0,0 +1,27 @@
1
+ import type { JsonValue } from "./JsonTypes";
2
+ import type { Result } from "./Result";
3
+ export interface IDiffService {
4
+ /**
5
+ * Diff two JSON strings
6
+ * @param leftJson - Left JSON string
7
+ * @param rightJson - Right JSON string
8
+ * @returns Array of differences
9
+ */
10
+ diffJson(leftJson: string, rightJson: string, options?: {
11
+ noisePath?: string[];
12
+ specialPath?: string[];
13
+ parseNestedJson?: boolean;
14
+ }): Result[];
15
+ /**
16
+ * Diff two JSON objects directly
17
+ * @param left - Left JSON value
18
+ * @param right - Right JSON value
19
+ * @returns Array of differences
20
+ */
21
+ diffElement(left: JsonValue, right: JsonValue, options?: {
22
+ noisePath?: string[];
23
+ specialPath?: string[];
24
+ parseNestedJson?: boolean;
25
+ }): Result[];
26
+ }
27
+ export declare const IDiffService: import("@wendellhu/redi").IdentifierDecorator<IDiffService>;
@@ -0,0 +1,2 @@
1
+ import { createIdentifier } from "@wendellhu/redi";
2
+ export const IDiffService = createIdentifier("IDiffService");
@@ -0,0 +1,5 @@
1
+ export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
2
+ export type JsonObject = {
3
+ [key: string]: JsonValue;
4
+ };
5
+ export type JsonArray = JsonValue[];
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { DiffContext } from "../../service/diff/DiffContext";
2
+ import type { PathTracker } from "../../service/diff/PathTracker";
3
+ export interface INullComparator {
4
+ diff(a: null, b: null, pathTracker: PathTracker): DiffContext;
5
+ }
6
+ export declare const INullComparator: import("@wendellhu/redi").IdentifierDecorator<INullComparator>;
@@ -0,0 +1,2 @@
1
+ import { createIdentifier } from "@wendellhu/redi";
2
+ export const INullComparator = createIdentifier("INullComparator");
@@ -0,0 +1,8 @@
1
+ import type { JsonObject, JsonValue } from "./JsonTypes";
2
+ import type { DiffContext } from "../../service/diff/DiffContext";
3
+ import type { PathTracker } from "../../service/diff/PathTracker";
4
+ export interface IObjectComparator {
5
+ diff(a: JsonObject, b: JsonObject, pathTracker: PathTracker): DiffContext;
6
+ diffElement(a: JsonValue | undefined, b: JsonValue | undefined, pathTracker: PathTracker): DiffContext;
7
+ }
8
+ export declare const IObjectComparator: import("@wendellhu/redi").IdentifierDecorator<IObjectComparator>;
@@ -0,0 +1,2 @@
1
+ import { createIdentifier } from "@wendellhu/redi";
2
+ export const IObjectComparator = createIdentifier("IObjectComparator");
@@ -0,0 +1,7 @@
1
+ import type { JsonValue } from "./JsonTypes";
2
+ import type { DiffContext } from "../../service/diff/DiffContext";
3
+ import type { PathTracker } from "../../service/diff/PathTracker";
4
+ export interface IOtherComparator {
5
+ diff(a: JsonValue | undefined, b: JsonValue | undefined, pathTracker: PathTracker): DiffContext;
6
+ }
7
+ export declare const IOtherComparator: import("@wendellhu/redi").IdentifierDecorator<IOtherComparator>;
@@ -0,0 +1,2 @@
1
+ import { createIdentifier } from "@wendellhu/redi";
2
+ export const IOtherComparator = createIdentifier("IOtherComparator");
@@ -0,0 +1,6 @@
1
+ import type { DiffContext } from "../../service/diff/DiffContext";
2
+ import type { PathTracker } from "../../service/diff/PathTracker";
3
+ export interface IPrimitiveComparator {
4
+ diff(a: string | number | boolean, b: string | number | boolean, pathTracker: PathTracker): DiffContext;
5
+ }
6
+ export declare const IPrimitiveComparator: import("@wendellhu/redi").IdentifierDecorator<IPrimitiveComparator>;
@@ -0,0 +1,2 @@
1
+ import { createIdentifier } from "@wendellhu/redi";
2
+ export const IPrimitiveComparator = createIdentifier("IPrimitiveComparator");
@@ -0,0 +1,7 @@
1
+ export declare class Result {
2
+ leftPath: string | null;
3
+ rightPath: string | null;
4
+ left: unknown;
5
+ right: unknown;
6
+ diffType: string;
7
+ }
@@ -0,0 +1,7 @@
1
+ export class Result {
2
+ leftPath = null;
3
+ rightPath = null;
4
+ left = null;
5
+ right = null;
6
+ diffType = "";
7
+ }
@@ -0,0 +1,7 @@
1
+ export declare class SingleNodeDifference {
2
+ leftPath: string;
3
+ rightPath: string;
4
+ left: unknown;
5
+ right: unknown;
6
+ constructor(leftPath: string, rightPath: string, left: unknown, right: unknown);
7
+ }
@@ -0,0 +1,12 @@
1
+ export class SingleNodeDifference {
2
+ leftPath;
3
+ rightPath;
4
+ left;
5
+ right;
6
+ constructor(leftPath, rightPath, left, right) {
7
+ this.leftPath = leftPath;
8
+ this.rightPath = rightPath;
9
+ this.left = left;
10
+ this.right = right;
11
+ }
12
+ }
@@ -0,0 +1,10 @@
1
+ export * from "./JsonTypes";
2
+ export * from "./Result";
3
+ export * from "./SingleNodeDifference";
4
+ export * from "./ComparatorOrchestrator";
5
+ export * from "./ObjectComparator";
6
+ export * from "./ArrayComparator";
7
+ export * from "./PrimitiveComparator";
8
+ export * from "./NullComparator";
9
+ export * from "./OtherComparator";
10
+ export * from "./DiffService";
@@ -0,0 +1,10 @@
1
+ export * from "./JsonTypes";
2
+ export * from "./Result";
3
+ export * from "./SingleNodeDifference";
4
+ export * from "./ComparatorOrchestrator";
5
+ export * from "./ObjectComparator";
6
+ export * from "./ArrayComparator";
7
+ export * from "./PrimitiveComparator";
8
+ export * from "./NullComparator";
9
+ export * from "./OtherComparator";
10
+ export * from "./DiffService";
@@ -0,0 +1,6 @@
1
+ export type { JsonValue, JsonObject, JsonArray } from "./contract/type/JsonTypes";
2
+ export { Result } from "./contract/type/Result";
3
+ export * from "./contract/type/DiffService";
4
+ export { TYPE_ADD, TYPE_DELETE, TYPE_MODIFY, } from "./contract/constant";
5
+ export { PresetName } from "./contract/constant/PresetName";
6
+ export { DiffService } from "./service";
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { Result } from "./contract/type/Result";
2
+ export * from "./contract/type/DiffService";
3
+ // Public API - Constants
4
+ export { TYPE_ADD, TYPE_DELETE, TYPE_MODIFY, } from "./contract/constant";
5
+ // Public API - Enums
6
+ export { PresetName } from "./contract/constant/PresetName";
7
+ // Public API - Service
8
+ export { DiffService } from "./service";
@@ -0,0 +1,13 @@
1
+ import { type IComparatorOrchestrator, type IObjectComparator, type IArrayComparator, type IPrimitiveComparator, type INullComparator, type IOtherComparator, type JsonValue } from "../../contract/type";
2
+ import { DiffContext } from "../diff/DiffContext";
3
+ import type { PathTracker } from "../diff/PathTracker";
4
+ export declare class ComparatorOrchestrator implements IComparatorOrchestrator {
5
+ protected objectComparator: IObjectComparator;
6
+ protected arrayComparator: IArrayComparator;
7
+ protected primitiveComparator: IPrimitiveComparator;
8
+ protected nullComparator: INullComparator;
9
+ protected otherComparator: IOtherComparator;
10
+ constructor(objectComparator: IObjectComparator, arrayComparator: IArrayComparator, primitiveComparator: IPrimitiveComparator, nullComparator: INullComparator, otherComparator: IOtherComparator);
11
+ diffElement(a: JsonValue | undefined, b: JsonValue | undefined, pathTracker: PathTracker): DiffContext;
12
+ getArrayComparator(): IArrayComparator;
13
+ }
@@ -0,0 +1,55 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Inject } from "@wendellhu/redi";
11
+ import { IObjectComparator as IObjectComparatorToken, IArrayComparator as IArrayComparatorToken, IPrimitiveComparator as IPrimitiveComparatorToken, INullComparator as INullComparatorToken, IOtherComparator as IOtherComparatorToken, } from "../../contract/type";
12
+ import { DiffContext } from "../diff/DiffContext";
13
+ import { isJsonObject, isJsonArray, isJsonPrimitive, isJsonNull } from "../../util";
14
+ let ComparatorOrchestrator = class ComparatorOrchestrator {
15
+ objectComparator;
16
+ arrayComparator;
17
+ primitiveComparator;
18
+ nullComparator;
19
+ otherComparator;
20
+ constructor(objectComparator, arrayComparator, primitiveComparator, nullComparator, otherComparator) {
21
+ this.objectComparator = objectComparator;
22
+ this.arrayComparator = arrayComparator;
23
+ this.primitiveComparator = primitiveComparator;
24
+ this.nullComparator = nullComparator;
25
+ this.otherComparator = otherComparator;
26
+ }
27
+ diffElement(a, b, pathTracker) {
28
+ if (isJsonObject(a) && isJsonObject(b)) {
29
+ return this.objectComparator.diff(a, b, pathTracker);
30
+ }
31
+ else if (isJsonArray(a) && isJsonArray(b)) {
32
+ return this.arrayComparator.diffArray(a, b, pathTracker);
33
+ }
34
+ else if (isJsonPrimitive(a) && isJsonPrimitive(b)) {
35
+ return this.primitiveComparator.diff(a, b, pathTracker);
36
+ }
37
+ else if (isJsonNull(a) && isJsonNull(b)) {
38
+ return this.nullComparator.diff(a, b, pathTracker);
39
+ }
40
+ else {
41
+ return this.otherComparator.diff(a, b, pathTracker);
42
+ }
43
+ }
44
+ getArrayComparator() {
45
+ return this.arrayComparator;
46
+ }
47
+ };
48
+ ComparatorOrchestrator = __decorate([
49
+ __param(0, Inject(IObjectComparatorToken)),
50
+ __param(1, Inject(IArrayComparatorToken)),
51
+ __param(2, Inject(IPrimitiveComparatorToken)),
52
+ __param(3, Inject(INullComparatorToken)),
53
+ __param(4, Inject(IOtherComparatorToken))
54
+ ], ComparatorOrchestrator);
55
+ export { ComparatorOrchestrator };
@@ -0,0 +1,11 @@
1
+ import type { IArrayComparator, IComparatorOrchestrator, JsonArray, JsonValue } from "../../../contract/type";
2
+ import { DiffContext } from "../../diff/DiffContext";
3
+ import type { PathTracker } from "../../diff/PathTracker";
4
+ export declare abstract class AbstractArray implements IArrayComparator {
5
+ protected orchestrator: IComparatorOrchestrator;
6
+ constructor(orchestrator: IComparatorOrchestrator);
7
+ abstract diffArray(a: JsonArray, b: JsonArray, pathTracker: PathTracker): DiffContext;
8
+ diffElement(a: JsonValue | undefined, b: JsonValue | undefined, pathTracker: PathTracker): DiffContext;
9
+ protected parentContextAddChildContext(parentResult: DiffContext, childResult: DiffContext): void;
10
+ protected constructArrayPath(i: number): string;
11
+ }
@@ -0,0 +1,40 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Inject } from "@wendellhu/redi";
11
+ import { IComparatorOrchestrator as IComparatorOrchestratorToken } from "../../../contract/type";
12
+ import { DiffContext } from "../../diff/DiffContext";
13
+ import { DIFFERENT } from "../../../contract/constant";
14
+ let AbstractArray = class AbstractArray {
15
+ orchestrator;
16
+ constructor(orchestrator) {
17
+ this.orchestrator = orchestrator;
18
+ }
19
+ diffElement(a, b, pathTracker) {
20
+ return this.orchestrator.diffElement(a, b, pathTracker);
21
+ }
22
+ parentContextAddChildContext(parentResult, childResult) {
23
+ if (childResult.isSame() === DIFFERENT) {
24
+ for (const singleNodeDifference of childResult.getDiffResultModels()) {
25
+ parentResult.getDiffResultModels().push(singleNodeDifference);
26
+ }
27
+ parentResult.setSame(false);
28
+ }
29
+ }
30
+ constructArrayPath(i) {
31
+ if (i === null || i === undefined || i < 0) {
32
+ throw new Error("数组索引号入参为空或者为负。 入参:" + i);
33
+ }
34
+ return "[" + i + "]";
35
+ }
36
+ };
37
+ AbstractArray = __decorate([
38
+ __param(0, Inject(IComparatorOrchestratorToken))
39
+ ], AbstractArray);
40
+ export { AbstractArray };
@@ -0,0 +1,9 @@
1
+ import { AbstractArray } from "./AbstractArray";
2
+ import { DiffContext } from "../../diff/DiffContext";
3
+ import type { PathTracker } from "../../diff/PathTracker";
4
+ import type { JsonArray, IComparatorOrchestrator } from "../../../contract/type";
5
+ export declare class SequentialArrayComparator extends AbstractArray {
6
+ constructor(orchestrator: IComparatorOrchestrator);
7
+ diffArray(a: JsonArray, b: JsonArray, pathTracker: PathTracker): DiffContext;
8
+ private generateDiffResult;
9
+ }
@@ -0,0 +1,49 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Inject } from "@wendellhu/redi";
11
+ import { AbstractArray } from "./AbstractArray";
12
+ import { DiffContext } from "../../diff/DiffContext";
13
+ import { IComparatorOrchestrator as IComparatorOrchestratorToken } from "../../../contract/type";
14
+ let SequentialArrayComparator = class SequentialArrayComparator extends AbstractArray {
15
+ constructor(orchestrator) {
16
+ super(orchestrator);
17
+ }
18
+ diffArray(a, b, pathTracker) {
19
+ const arrayDiffContext = new DiffContext();
20
+ const maxLength = Math.max(a.length, b.length);
21
+ for (let i = 0; i < maxLength; i++) {
22
+ pathTracker.addAllpath(this.constructArrayPath(i));
23
+ const diffContext = this.generateDiffResult(a, b, i, pathTracker);
24
+ this.parentContextAddChildContext(arrayDiffContext, diffContext);
25
+ pathTracker.removeAllLastPath();
26
+ }
27
+ return arrayDiffContext;
28
+ }
29
+ generateDiffResult(a, b, i, pathTracker) {
30
+ if (i >= a.length && i >= b.length) {
31
+ throw new Error("数组索引号入参超过数组长度。 索引号:" + i + " 数组a:" + a + "数组b:" + b);
32
+ }
33
+ let diffContext;
34
+ if (i < a.length && i < b.length) {
35
+ diffContext = this.diffElement(a[i], b[i], pathTracker);
36
+ }
37
+ else if (i >= a.length) {
38
+ diffContext = this.diffElement(undefined, b[i], pathTracker);
39
+ }
40
+ else {
41
+ diffContext = this.diffElement(a[i], undefined, pathTracker);
42
+ }
43
+ return diffContext;
44
+ }
45
+ };
46
+ SequentialArrayComparator = __decorate([
47
+ __param(0, Inject(IComparatorOrchestratorToken))
48
+ ], SequentialArrayComparator);
49
+ export { SequentialArrayComparator };
@@ -0,0 +1,21 @@
1
+ import { AbstractArray } from "./AbstractArray";
2
+ import { DiffContext } from "../../diff/DiffContext";
3
+ import type { PathTracker } from "../../diff/PathTracker";
4
+ import type { JsonArray, IComparatorOrchestrator } from "../../../contract/type";
5
+ export declare class SimilarArrayComparator extends AbstractArray {
6
+ private readonly USEABLE;
7
+ private readonly USED;
8
+ constructor(orchestrator: IComparatorOrchestrator);
9
+ diffArray(a: JsonArray, b: JsonArray, pathTracker: PathTracker): DiffContext;
10
+ private exchangeResult;
11
+ private exchangePathAndResult;
12
+ private exchangeLeftAndRightPath;
13
+ diff(a: JsonArray, b: JsonArray, pathTracker: PathTracker): DiffContext;
14
+ private obtainDiffResult;
15
+ private obtainAddDiffResult;
16
+ private obtainModifyDiffResult;
17
+ private constructAddContext;
18
+ private constructModifyContext;
19
+ private constructSimilarMatrix;
20
+ private existSpecialPath;
21
+ }
@@ -0,0 +1,152 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Inject } from "@wendellhu/redi";
11
+ import { AbstractArray } from "./AbstractArray";
12
+ import { DiffContext } from "../../diff/DiffContext";
13
+ import { SingleNodeDifference, IComparatorOrchestrator as IComparatorOrchestratorToken } from "../../../contract/type";
14
+ let SimilarArrayComparator = class SimilarArrayComparator extends AbstractArray {
15
+ USEABLE = false;
16
+ USED = true;
17
+ constructor(orchestrator) {
18
+ super(orchestrator);
19
+ }
20
+ diffArray(a, b, pathTracker) {
21
+ let diffContext;
22
+ if (a.length <= b.length) {
23
+ diffContext = this.diff(a, b, pathTracker);
24
+ }
25
+ else {
26
+ this.exchangeLeftAndRightPath(pathTracker);
27
+ diffContext = this.diff(b, a, pathTracker);
28
+ this.exchangeLeftAndRightPath(pathTracker);
29
+ this.exchangeResult(diffContext);
30
+ }
31
+ return diffContext;
32
+ }
33
+ exchangeResult(diffContext) {
34
+ const singleNodeDifferences = diffContext.getDiffResultModels();
35
+ for (const singleNodeDifference of singleNodeDifferences) {
36
+ this.exchangePathAndResult(singleNodeDifference);
37
+ }
38
+ }
39
+ exchangePathAndResult(singleNodeDifference) {
40
+ const tempStringA = singleNodeDifference.leftPath;
41
+ const tempLeft = singleNodeDifference.left;
42
+ singleNodeDifference.leftPath = singleNodeDifference.rightPath;
43
+ singleNodeDifference.rightPath = tempStringA;
44
+ singleNodeDifference.left = singleNodeDifference.right;
45
+ singleNodeDifference.right = tempLeft;
46
+ }
47
+ exchangeLeftAndRightPath(pathTracker) {
48
+ const tempA = pathTracker.getLeftPath();
49
+ pathTracker.setLeftPath(pathTracker.getRightPath());
50
+ pathTracker.setRightPath(tempA);
51
+ }
52
+ diff(a, b, pathTracker) {
53
+ const rowlength = a.length;
54
+ const linelength = b.length;
55
+ const similarMatrix = Array.from({ length: rowlength }, () => Array(linelength).fill(0));
56
+ const row = Array(rowlength).fill(false);
57
+ const line = Array(linelength).fill(false);
58
+ for (let i = 0; i < rowlength; i++) {
59
+ pathTracker.addLeftPath(this.constructArrayPath(i));
60
+ this.constructSimilarMatrix(a, b, i, pathTracker, similarMatrix, row, line);
61
+ pathTracker.removeLastLeftPath();
62
+ }
63
+ return this.obtainDiffResult(a, b, pathTracker, row, line, similarMatrix);
64
+ }
65
+ obtainDiffResult(a, b, pathTracker, row, line, similarMatrix) {
66
+ const arrayDiffContext = new DiffContext();
67
+ this.obtainModifyDiffResult(a, b, pathTracker, row, line, similarMatrix, arrayDiffContext);
68
+ this.obtainAddDiffResult(b, pathTracker, line, arrayDiffContext);
69
+ return arrayDiffContext;
70
+ }
71
+ obtainAddDiffResult(b, pathTracker, line, arrayDiffContext) {
72
+ for (let j = 0; j < line.length; j++) {
73
+ if (line[j] === this.USED) {
74
+ continue;
75
+ }
76
+ const addOrDeleteDiffContext = this.constructAddContext(b, j, pathTracker);
77
+ this.parentContextAddChildContext(arrayDiffContext, addOrDeleteDiffContext);
78
+ }
79
+ }
80
+ obtainModifyDiffResult(a, b, pathTracker, row, line, similarMatrix, arrayDiffContext) {
81
+ let counts = 0;
82
+ for (const value of row) {
83
+ if (value === this.USEABLE) {
84
+ counts++;
85
+ }
86
+ }
87
+ for (let n = 0; n < counts; n++) {
88
+ let bestLineIndex = 0;
89
+ let bestRowIndex = 0;
90
+ let minDiffPair = Number.MAX_SAFE_INTEGER;
91
+ for (let i = 0; i < row.length; i++) {
92
+ for (let j = 0; j < line.length; j++) {
93
+ if (row[i] === this.USED || line[j] === this.USED) {
94
+ continue;
95
+ }
96
+ if (similarMatrix[i][j] < minDiffPair) {
97
+ bestRowIndex = i;
98
+ bestLineIndex = j;
99
+ minDiffPair = similarMatrix[i][j];
100
+ }
101
+ }
102
+ }
103
+ const modifyDiffContext = this.constructModifyContext(a, b, bestRowIndex, bestLineIndex, pathTracker);
104
+ row[bestRowIndex] = this.USED;
105
+ line[bestLineIndex] = this.USED;
106
+ this.parentContextAddChildContext(arrayDiffContext, modifyDiffContext);
107
+ }
108
+ }
109
+ constructAddContext(b, index, pathTracker) {
110
+ pathTracker.addAllpath(this.constructArrayPath(index));
111
+ const diffContext = this.diffElement(undefined, b[index], pathTracker);
112
+ pathTracker.removeAllLastPath();
113
+ return diffContext;
114
+ }
115
+ constructModifyContext(a, b, i, bestLineIndex, pathTracker) {
116
+ pathTracker.addLeftPath(this.constructArrayPath(i));
117
+ pathTracker.addRightPath(this.constructArrayPath(bestLineIndex));
118
+ const diffContext = this.diffElement(a[i], b[bestLineIndex], pathTracker);
119
+ pathTracker.removeAllLastPath();
120
+ return diffContext;
121
+ }
122
+ constructSimilarMatrix(arrayA, arrayB, rowIndex, pathTracker, similarArray, row, line) {
123
+ if (rowIndex < 0 || rowIndex >= arrayB.length) {
124
+ throw new Error("索引号入参超出数组长度。 索引号:" + rowIndex + " 数组B:" + JSON.stringify(arrayB));
125
+ }
126
+ for (let j = 0; j < arrayB.length; j++) {
127
+ if (line[j] === this.USEABLE) {
128
+ pathTracker.addRightPath(this.constructArrayPath(j));
129
+ const diffContext = this.diffElement(arrayA[rowIndex], arrayB[j], pathTracker);
130
+ pathTracker.removeLastRightPath();
131
+ if (diffContext.isSame()) {
132
+ row[rowIndex] = this.USED;
133
+ line[j] = this.USED;
134
+ return;
135
+ }
136
+ else if (this.existSpecialPath(diffContext.getSpecialPathResult())) {
137
+ similarArray[rowIndex][j] = 0;
138
+ }
139
+ else {
140
+ similarArray[rowIndex][j] = diffContext.getDiffResultModels().length;
141
+ }
142
+ }
143
+ }
144
+ }
145
+ existSpecialPath(specialPathResult) {
146
+ return specialPathResult !== null && specialPathResult.length > 0;
147
+ }
148
+ };
149
+ SimilarArrayComparator = __decorate([
150
+ __param(0, Inject(IComparatorOrchestratorToken))
151
+ ], SimilarArrayComparator);
152
+ export { SimilarArrayComparator };
@@ -0,0 +1,3 @@
1
+ export * from "./AbstractArray";
2
+ export * from "./SequentialArrayComparator";
3
+ export * from "./SimilarArrayComparator";
@@ -0,0 +1,3 @@
1
+ export * from "./AbstractArray";
2
+ export * from "./SequentialArrayComparator";
3
+ export * from "./SimilarArrayComparator";
@@ -0,0 +1,6 @@
1
+ export * from "./ComparatorOrchestrator";
2
+ export * from "./array";
3
+ export * from "./object";
4
+ export * from "./primitive";
5
+ export * from "./nulls";
6
+ export * from "./other";