Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

<br/>

![superdiff-geo-diff-demo](https://raw.githubusercontent.com/DoneDeal0/superdiff/main/assets/get-geo-diff-demo.png)
![superdiff-geo-diff-demo](https://raw.githubusercontent.com/DoneDeal0/superdiff/main/assets/get-geo-diff-demo-ok.png)

<p align="center">
<sub>Tracking coordinate changes with <code>getGeoDiff</code></sub>
Expand Down
Binary file added assets/get-geo-diff-demo-ok.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/get-geo-diff-demo.png
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 10 additions & 10 deletions src/lib/list-diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ function getDiffStatus(statusMap: Set<ListStatus>): ListStatus {
return ListStatus.UPDATED;
}

function getLeanDiff(
diff: ListDiff["diff"],
function getLeanDiff<T>(
diff: ListDiff<T>["diff"],
showOnly: ListDiffOptions["showOnly"] = [],
): ListDiff["diff"] {
): ListDiff<T>["diff"] {
const set = new Set(showOnly);
const leanDiff: ListDiff["diff"] = [];
const leanDiff: ListDiff<T>["diff"] = [];
for (let i = 0; i < diff.length; i++) {
if (set.has(diff[i].status)) {
leanDiff.push(diff[i]);
Expand All @@ -36,9 +36,9 @@ function getLeanDiff(
function formatSingleListDiff<T>(
listData: T[],
status: ListStatus,
options: ListDiffOptions,
): ListDiff {
const diff: ListDiff["diff"] = new Array(listData.length);
options: ListDiffOptions<T>,
): ListDiff<T> {
const diff: ListDiff<T>["diff"] = new Array(listData.length);
const isAdded = status === ListStatus.ADDED;
for (let i = 0; i < listData.length; i++) {
diff[i] = {
Expand Down Expand Up @@ -88,8 +88,8 @@ function getNextStatus(
export const getListDiff = <T>(
prevList: T[] | undefined | null,
nextList: T[] | undefined | null,
options: ListDiffOptions = DEFAULT_LIST_DIFF_OPTIONS,
): ListDiff => {
options: ListDiffOptions<T> = DEFAULT_LIST_DIFF_OPTIONS,
): ListDiff<T> => {
if (!prevList && !nextList) {
return { type: "list", status: ListStatus.EQUAL, diff: [] };
}
Expand All @@ -100,7 +100,7 @@ export const getListDiff = <T>(
return formatSingleListDiff(prevList as T[], ListStatus.DELETED, options);
}

const diff: ListDiff["diff"] = [];
const diff: ListDiff<T>["diff"] = [];
const previousMap = new Map<string, ListData<T>>();
const statusMap = new Set<ListStatus>();
const { referenceKey, ignoreArrayOrder } = options;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stream-list-diff/server/worker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function generateWorker<T extends Record<string, unknown>>(
worker.terminate();
}
});
worker.on(WorkerEvent.Error, (err) =>
worker.on(WorkerEvent.Error, (err: Error) =>
emitter.emit(StreamEvent.Error, new Error(err.message)),
);
} catch (err) {
Expand Down
18 changes: 9 additions & 9 deletions src/lib/text-diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] };
Expand All @@ -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,
Expand All @@ -60,14 +56,18 @@ 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,
status: TextStatus.DELETED,
})),
};
}

const previousTokens = tokenize(previousText);
const currentTokens = tokenize(currentText);

if (options.detectMoves) {
return getPositionalTextDiff(previousTokens, currentTokens);
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/geo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export enum GeoDirection {

export type GeoDiff = {
type: "geo";
status: GeoStatus;
status: `${GeoStatus}`;
diff: {
coordinates: GeoCoordinates | null;
previousCoordinates: GeoCoordinates | null;
Expand Down
10 changes: 5 additions & 5 deletions src/models/list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const DEFAULT_LIST_DIFF_OPTIONS = {
referenceKey: undefined,
considerMoveAsUpdate: false,
ignoreArrayOrder: false,
};
} satisfies ListDiffOptions;

export enum ListStatus {
ADDED = "added",
Expand All @@ -18,9 +18,9 @@ export enum ListType {
NEXT = "nextList",
}

export type ListDiffOptions = {
export type ListDiffOptions<T = unknown> = {
showOnly?: `${ListStatus}`[];
referenceKey?: string;
referenceKey?: T extends Record<string, unknown> ? keyof T & string : string;
considerMoveAsUpdate?: boolean;
ignoreArrayOrder?: boolean;
};
Expand All @@ -30,11 +30,11 @@ export type ListData<T> = {
value: T;
};

export type ListDiff = {
export type ListDiff<T = unknown> = {
type: "list";
status: `${ListStatus}`;
diff: {
value: unknown;
value: T;
previousIndex: number | null;
index: number | null;
status: `${ListStatus}`;
Expand Down
2 changes: 1 addition & 1 deletion src/models/text/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ export type TextDiff = {
index: number | null;
previousValue?: string;
previousIndex: number | null;
status: TextStatus;
status: `${TextStatus}`;
}[];
};