diff --git a/README.md b/README.md
index b1ceeca..eabee30 100644
--- a/README.md
+++ b/README.md
@@ -46,7 +46,7 @@
-
+
Tracking coordinate changes with getGeoDiff
diff --git a/assets/get-geo-diff-demo-ok.png b/assets/get-geo-diff-demo-ok.png
new file mode 100644
index 0000000..43aa3c3
Binary files /dev/null and b/assets/get-geo-diff-demo-ok.png differ
diff --git a/assets/get-geo-diff-demo.png b/assets/get-geo-diff-demo.png
deleted file mode 100644
index e6a8de9..0000000
Binary files a/assets/get-geo-diff-demo.png and /dev/null differ
diff --git a/package.json b/package.json
index 230b4d6..bc56ef3 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@donedeal0/superdiff",
- "version": "4.2.2",
+ "version": "4.2.3",
"type": "module",
"description": "Superdiff provides a rich and readable diff for arrays, objects, texts and coordinates. It supports stream and file inputs for handling large datasets efficiently, is battle-tested, has zero dependencies, and offers a top-tier performance.",
"main": "dist/index.js",
diff --git a/src/lib/list-diff/index.ts b/src/lib/list-diff/index.ts
index b283047..fb9a942 100644
--- a/src/lib/list-diff/index.ts
+++ b/src/lib/list-diff/index.ts
@@ -19,12 +19,12 @@ function getDiffStatus(statusMap: Set): ListStatus {
return ListStatus.UPDATED;
}
-function getLeanDiff(
- diff: ListDiff["diff"],
+function getLeanDiff(
+ diff: ListDiff["diff"],
showOnly: ListDiffOptions["showOnly"] = [],
-): ListDiff["diff"] {
+): ListDiff["diff"] {
const set = new Set(showOnly);
- const leanDiff: ListDiff["diff"] = [];
+ const leanDiff: ListDiff["diff"] = [];
for (let i = 0; i < diff.length; i++) {
if (set.has(diff[i].status)) {
leanDiff.push(diff[i]);
@@ -36,9 +36,9 @@ function getLeanDiff(
function formatSingleListDiff(
listData: T[],
status: ListStatus,
- options: ListDiffOptions,
-): ListDiff {
- const diff: ListDiff["diff"] = new Array(listData.length);
+ options: ListDiffOptions,
+): ListDiff {
+ const diff: ListDiff["diff"] = new Array(listData.length);
const isAdded = status === ListStatus.ADDED;
for (let i = 0; i < listData.length; i++) {
diff[i] = {
@@ -88,8 +88,8 @@ function getNextStatus(
export const getListDiff = (
prevList: T[] | undefined | null,
nextList: T[] | undefined | null,
- options: ListDiffOptions = DEFAULT_LIST_DIFF_OPTIONS,
-): ListDiff => {
+ options: ListDiffOptions = DEFAULT_LIST_DIFF_OPTIONS,
+): ListDiff => {
if (!prevList && !nextList) {
return { type: "list", status: ListStatus.EQUAL, diff: [] };
}
@@ -100,7 +100,7 @@ export const getListDiff = (
return formatSingleListDiff(prevList as T[], ListStatus.DELETED, options);
}
- const diff: ListDiff["diff"] = [];
+ const diff: ListDiff["diff"] = [];
const previousMap = new Map>();
const statusMap = new Set();
const { referenceKey, ignoreArrayOrder } = options;
diff --git a/src/lib/stream-list-diff/server/worker/utils.ts b/src/lib/stream-list-diff/server/worker/utils.ts
index ff2edf1..9a46b59 100644
--- a/src/lib/stream-list-diff/server/worker/utils.ts
+++ b/src/lib/stream-list-diff/server/worker/utils.ts
@@ -54,7 +54,7 @@ export async function generateWorker>(
worker.terminate();
}
});
- worker.on(WorkerEvent.Error, (err) =>
+ worker.on(WorkerEvent.Error, (err: Error) =>
emitter.emit(StreamEvent.Error, new Error(err.message)),
);
} catch (err) {
diff --git a/src/lib/text-diff/index.ts b/src/lib/text-diff/index.ts
index 186b63d..b7adcbb 100644
--- a/src/lib/text-diff/index.ts
+++ b/src/lib/text-diff/index.ts
@@ -31,14 +31,10 @@ export function getTextDiff(
currentText: string | null | undefined,
options: TextDiffOptions = DEFAULT_TEXT_DIFF_OPTIONS,
): TextDiff {
- const previousTokens =
+ const tokenize = (text: string | null | undefined) =>
options?.accuracy === "high"
- ? tokenizeStrictText(previousText, options)
- : tokenizeNormalText(previousText, options);
- const currentTokens =
- options?.accuracy === "high"
- ? tokenizeStrictText(currentText, options)
- : tokenizeNormalText(currentText, options);
+ ? tokenizeStrictText(text, options)
+ : tokenizeNormalText(text, options);
if (!previousText && !currentText) {
return { type: "text", status: TextStatus.EQUAL, diff: [] };
@@ -48,7 +44,7 @@ export function getTextDiff(
return {
type: "text",
status: TextStatus.ADDED,
- diff: currentTokens.map((token, i) => ({
+ diff: tokenize(currentText).map((token, i) => ({
value: token.value,
index: i,
previousIndex: null,
@@ -60,7 +56,7 @@ export function getTextDiff(
return {
type: "text",
status: TextStatus.DELETED,
- diff: previousTokens.map((token, i) => ({
+ diff: tokenize(previousText).map((token, i) => ({
value: token.value,
index: null,
previousIndex: i,
@@ -68,6 +64,10 @@ export function getTextDiff(
})),
};
}
+
+ const previousTokens = tokenize(previousText);
+ const currentTokens = tokenize(currentText);
+
if (options.detectMoves) {
return getPositionalTextDiff(previousTokens, currentTokens);
}
diff --git a/src/models/geo/index.ts b/src/models/geo/index.ts
index 577af29..989b9f4 100644
--- a/src/models/geo/index.ts
+++ b/src/models/geo/index.ts
@@ -49,7 +49,7 @@ export enum GeoDirection {
export type GeoDiff = {
type: "geo";
- status: GeoStatus;
+ status: `${GeoStatus}`;
diff: {
coordinates: GeoCoordinates | null;
previousCoordinates: GeoCoordinates | null;
diff --git a/src/models/list/index.ts b/src/models/list/index.ts
index daf32fd..0bf5214 100644
--- a/src/models/list/index.ts
+++ b/src/models/list/index.ts
@@ -3,7 +3,7 @@ export const DEFAULT_LIST_DIFF_OPTIONS = {
referenceKey: undefined,
considerMoveAsUpdate: false,
ignoreArrayOrder: false,
-};
+} satisfies ListDiffOptions;
export enum ListStatus {
ADDED = "added",
@@ -18,9 +18,9 @@ export enum ListType {
NEXT = "nextList",
}
-export type ListDiffOptions = {
+export type ListDiffOptions = {
showOnly?: `${ListStatus}`[];
- referenceKey?: string;
+ referenceKey?: T extends Record ? keyof T & string : string;
considerMoveAsUpdate?: boolean;
ignoreArrayOrder?: boolean;
};
@@ -30,11 +30,11 @@ export type ListData = {
value: T;
};
-export type ListDiff = {
+export type ListDiff = {
type: "list";
status: `${ListStatus}`;
diff: {
- value: unknown;
+ value: T;
previousIndex: number | null;
index: number | null;
status: `${ListStatus}`;
diff --git a/src/models/text/index.ts b/src/models/text/index.ts
index 2c25d83..4ec3815 100644
--- a/src/models/text/index.ts
+++ b/src/models/text/index.ts
@@ -51,6 +51,6 @@ export type TextDiff = {
index: number | null;
previousValue?: string;
previousIndex: number | null;
- status: TextStatus;
+ status: `${TextStatus}`;
}[];
};