Skip to content

Disable text diffs by default #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 10, 2023
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
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ jobs:
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm publish
working-directory: ./packages/jsondiffpatch
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ In a browser, you can load a bundle using a tool like [esm.sh](https://esm.sh) o

```javascript
import * as jsondiffpatch from 'jsondiffpatch';

// Only import if you want text diffs using diff-match-patch
import DiffMatchPatch from 'diff-match-patch';

const jsondiffpatchInstance = jsondiffpatch.create({
// used to match objects when diffing arrays, by default only === operator is used
objectHash: function (obj) {
Expand All @@ -200,6 +204,9 @@ const jsondiffpatchInstance = jsondiffpatch.create({
includeValueOnMove: false,
},
textDiff: {
// If using text diffs, it's required to pass in the diff-match-patch library in through this proprty.
// Alternatively, you can import jsondiffpatch using `jsondiffpatch/with-text-diffs` to avoid having to pass in diff-match-patch through the options.
diffMatchPatch: DiffMatchPatch,
// default 60, minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch
minLength: 60,
},
Expand Down
2 changes: 1 addition & 1 deletion demos/console-demo/demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as jsondiffpatch from 'jsondiffpatch';
import * as jsondiffpatch from 'jsondiffpatch/with-text-diffs';
import * as consoleFormatter from 'jsondiffpatch/formatters/console';

const instance = jsondiffpatch.create({
Expand Down
2 changes: 1 addition & 1 deletion demos/html-demo/demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as jsondiffpatch from 'jsondiffpatch';
import * as jsondiffpatch from 'jsondiffpatch/with-text-diffs';
import * as annotatedFormatter from 'jsondiffpatch/formatters/annotated';
import * as htmlFormatter from 'jsondiffpatch/formatters/html';

Expand Down
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions packages/jsondiffpatch/bin/jsondiffpatch

This file was deleted.

18 changes: 18 additions & 0 deletions packages/jsondiffpatch/bin/jsondiffpatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node

import { readFileSync } from 'node:fs';
import * as jsondiffpatch from '../lib/with-text-diffs.js';
import * as consoleFormatter from '../lib/formatters/console.js';

const fileLeft = process.argv[2];
const fileRight = process.argv[3];

if (!fileLeft || !fileRight) {
console.log('\n USAGE: jsondiffpatch left.json right.json');
} else {
const left = JSON.parse(readFileSync(fileLeft));
const right = JSON.parse(readFileSync(fileRight));

const delta = jsondiffpatch.diff(left, right);
consoleFormatter.log(delta);
}
7 changes: 6 additions & 1 deletion packages/jsondiffpatch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
"Benjamin Eidelman <[email protected]>"
],
"type": "module",
"sideEffects": [
"*.css"
],
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"exports": {
".": "./lib/index.js",
"./with-text-diffs": "./lib/with-text-diffs.js",
"./formatters/*": "./lib/formatters/*.js",
"./formatters/styles/*.css": "./lib/formatters/styles/*.css"
},
Expand All @@ -19,7 +23,7 @@
"lib"
],
"bin": {
"jsondiffpatch": "./bin/jsondiffpatch"
"jsondiffpatch": "./bin/jsondiffpatch.js"
},
"scripts": {
"build": "tsc && ncp ./src/formatters/styles/ ./lib/formatters/styles/",
Expand All @@ -39,6 +43,7 @@
"patch"
],
"dependencies": {
"@types/diff-match-patch": "^1.0.36",
"chalk": "^5.3.0",
"diff-match-patch": "^1.0.5"
},
Expand Down
128 changes: 0 additions & 128 deletions packages/jsondiffpatch/src/diff-match-patch.d.cts

This file was deleted.

50 changes: 21 additions & 29 deletions packages/jsondiffpatch/src/filters/texts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dmp from 'diff-match-patch';
import type dmp from 'diff-match-patch';
import type DiffContext from '../contexts/diff.js';
import type PatchContext from '../contexts/patch.js';
import type ReverseContext from '../contexts/reverse.js';
Expand All @@ -8,13 +8,10 @@ import type {
Filter,
ModifiedDelta,
MovedDelta,
Options,
TextDiffDelta,
} from '../types.js';

declare global {
const diff_match_patch: typeof dmp | undefined;
}

interface DiffPatch {
diff: (txt1: string, txt2: string) => string;
patch: (txt1: string, string: string) => string;
Expand All @@ -24,42 +21,37 @@ const TEXT_DIFF = 2;
const DEFAULT_MIN_LENGTH = 60;
let cachedDiffPatch: DiffPatch | null = null;

function getDiffMatchPatch(required: true): DiffPatch;
function getDiffMatchPatch(required?: boolean): DiffPatch;
function getDiffMatchPatch(required?: boolean) {
function getDiffMatchPatch(
options: Options | undefined,
required: true,
): DiffPatch;
function getDiffMatchPatch(
options: Options | undefined,
required?: boolean,
): DiffPatch | null;
function getDiffMatchPatch(options: Options | undefined, required?: boolean) {
if (!cachedDiffPatch) {
let instance: dmp | null | undefined;
if (typeof diff_match_patch !== 'undefined') {
// already loaded, probably a browser
instance =
typeof diff_match_patch === 'function'
? new diff_match_patch()
: new (diff_match_patch as typeof dmp).diff_match_patch();
} else if (dmp) {
try {
instance = dmp && new dmp();
} catch (err) {
instance = null;
}
}
if (!instance) {
let instance: dmp;
if (options?.textDiff?.diffMatchPatch) {
instance = new options.textDiff.diffMatchPatch();
} else {
if (!required) {
return null;
}
const error: Error & { diff_match_patch_not_found?: boolean } = new Error(
'text diff_match_patch library not found',
'The diff-match-patch library was not provided. Pass the library in through the options or use the `jsondiffpatch/with-text-diffs` entry-point.',
);
// eslint-disable-next-line camelcase
error.diff_match_patch_not_found = true;
throw error;
}
cachedDiffPatch = {
diff: function (txt1, txt2) {
return instance!.patch_toText(instance!.patch_make(txt1, txt2));
return instance.patch_toText(instance.patch_make(txt1, txt2));
},
patch: function (txt1, patch) {
const results = instance!.patch_apply(
instance!.patch_fromText(patch),
const results = instance.patch_apply(
instance.patch_fromText(patch),
txt1,
);
for (let i = 0; i < results[1].length; i++) {
Expand Down Expand Up @@ -95,7 +87,7 @@ export const diffFilter: Filter<DiffContext> = function textsDiffFilter(
return;
}
// large text, try to use a text-diff algorithm
const diffMatchPatch = getDiffMatchPatch();
const diffMatchPatch = getDiffMatchPatch(context.options);
if (!diffMatchPatch) {
// diff-match-patch library not available,
// fallback to regular string replace
Expand Down Expand Up @@ -125,7 +117,7 @@ export const patchFilter: Filter<PatchContext> = function textsPatchFilter(
const textDiffDelta = nonNestedDelta as TextDiffDelta;

// text-diff, use a text-patch algorithm
const patch = getDiffMatchPatch(true).patch;
const patch = getDiffMatchPatch(context.options, true).patch;
context.setResult(patch(context.left as string, textDiffDelta[0])).exit();
};
patchFilter.filterName = 'texts';
Expand Down
2 changes: 2 additions & 0 deletions packages/jsondiffpatch/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type dmp from 'diff-match-patch';
import type Context from './contexts/context.js';
import type DiffContext from './contexts/diff.js';

Expand All @@ -9,6 +10,7 @@ export interface Options {
includeValueOnMove?: boolean;
};
textDiff?: {
diffMatchPatch: typeof dmp;
minLength?: number;
};
propertyFilter?: (name: string, context: DiffContext) => boolean;
Expand Down
Loading