Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
152 commits
Select commit Hold shift + click to select a range
3157696
First steps towards TypeScript conversion
ExplodingCabbage Feb 28, 2025
0e25a14
Convert xml.js->xml.ts
ExplodingCabbage Mar 7, 2025
0262a2e
Add compilation to build step
ExplodingCabbage Mar 7, 2025
5aaa633
Rewrite base.js in TypeScript (maybe shit, need to review own work on…
ExplodingCabbage Mar 9, 2025
cfafa10
Rewrite json.js -> json.ts and get tests passing (I'm honestly shocke…
ExplodingCabbage Mar 10, 2025
2ac1627
array.js -> array.ts
ExplodingCabbage Mar 10, 2025
f4f7880
character.js -> character.ts
ExplodingCabbage Mar 10, 2025
2317d9c
css.js->css.ts
ExplodingCabbage Mar 10, 2025
4bd69ee
sentence.js->sentence.ts
ExplodingCabbage Mar 10, 2025
9b4fc4f
word.js -> word.ts
ExplodingCabbage Mar 10, 2025
cb03e0a
line.js -> line.ts
ExplodingCabbage Mar 10, 2025
90cbe7b
index.js->index.ts
ExplodingCabbage Mar 10, 2025
56cce5c
Convert a couple more fiiles
ExplodingCabbage Mar 10, 2025
143706b
Convert params.ts
ExplodingCabbage Mar 10, 2025
4cb52b4
string.js -> string.ts
ExplodingCabbage Mar 10, 2025
5ad83c6
Partly convert patch-related files to TypeScript; more to do
ExplodingCabbage Mar 10, 2025
2dd993e
Type more stuff
ExplodingCabbage Mar 11, 2025
88838c9
Begin converting line-endings.js. Interesting TypeScript bug along th…
ExplodingCabbage Mar 11, 2025
7e0d68b
Bump TypeScript
ExplodingCabbage Mar 11, 2025
bbe9520
Add link to bug report
ExplodingCabbage Mar 11, 2025
a21648e
Finish converted line-endings.js to TypeScript
ExplodingCabbage Mar 12, 2025
4b3691a
Add missing overloads to support case where argument type isn't known…
ExplodingCabbage Mar 12, 2025
88a5513
reverse.js -> reverse.ts
ExplodingCabbage Mar 12, 2025
72bc4dc
add todo
ExplodingCabbage Mar 12, 2025
c62827e
Rewrite base.ts types (breaks everything for now)
ExplodingCabbage Mar 12, 2025
be36cc0
Per-diff-function options types. Still not compiling but close.
ExplodingCabbage Mar 12, 2025
268e96b
Fix build
ExplodingCabbage Mar 14, 2025
0be74a5
Merge remote-tracking branch 'origin/master' into add-typescript
ExplodingCabbage Mar 14, 2025
f0cfaf6
Run 'yarn add @eslint/js typescript-eslint --save-dev' as suggested a…
ExplodingCabbage Mar 14, 2025
38faa69
Turn on recommended eslint rules as recommended by Getting Started gu…
ExplodingCabbage Mar 14, 2025
3de3195
Get linting of TypeScript working (now with the officially recommende…
ExplodingCabbage Mar 14, 2025
3714370
yarn lint --fix
ExplodingCabbage Mar 14, 2025
457e40a
Tweak some indentation that eslint broke
ExplodingCabbage Mar 14, 2025
2f07bc3
Fix a couple of linting errors
ExplodingCabbage Mar 14, 2025
9c865af
Allow explicit 'any'
ExplodingCabbage Mar 14, 2025
7a13d9f
Eliminate needless explicit respecification of rules that eslint.conf…
ExplodingCabbage Mar 14, 2025
1b6cf7e
Fix another eslint config bug
ExplodingCabbage Mar 14, 2025
4809714
Start using arrow functions and thereby resolve a https://typescript-…
ExplodingCabbage Mar 14, 2025
fa3cb01
Fix some linting errors about pointless escape sequences
ExplodingCabbage Mar 14, 2025
36580d0
Fix more linting errors about pointless escape sequences
ExplodingCabbage Mar 14, 2025
9f93ce0
Eliminate a util made redundant by Object.keys, and fix a linting err…
ExplodingCabbage Mar 14, 2025
91885c8
Fix a no-prototype-builtins linting error
ExplodingCabbage Mar 14, 2025
5e6adc7
Disable no-use-before-define for TypeScript code, where it's broken
ExplodingCabbage Mar 14, 2025
3fe6e57
Liberalise more rules
ExplodingCabbage Mar 14, 2025
ed5aaa0
Fix lint errors in parse.ts
ExplodingCabbage Mar 14, 2025
3a52b9e
Fix lint errors in apply.ts
ExplodingCabbage Mar 14, 2025
173bc03
Fix remaining linting errors
ExplodingCabbage Mar 14, 2025
5688499
Add missing newline at EOF
ExplodingCabbage Mar 17, 2025
0cf1dbd
Remove a couple of unused ts-expect-error directives. (My editor was …
ExplodingCabbage Mar 17, 2025
b9ea31f
create.js->create.ts
ExplodingCabbage Mar 17, 2025
27023aa
Fix some linting errors
ExplodingCabbage Mar 17, 2025
00fcf4b
Merge branch 'master' into add-typescript
ExplodingCabbage Mar 17, 2025
811b746
Merge branch 'master' into add-typescript
ExplodingCabbage Mar 17, 2025
b16b1df
Lint on build, like before
ExplodingCabbage Mar 17, 2025
9cda2b5
Generate type declarations on build
ExplodingCabbage Mar 18, 2025
47b55cc
Merge remote-tracking branch 'origin/master' into add-typescript
ExplodingCabbage Mar 18, 2025
12c553e
Migrate to using tsconfig.json instead of CLI args
ExplodingCabbage Mar 18, 2025
cc088d5
Start adding tsd tests... but actually maybe should use dtslint?
ExplodingCabbage Mar 18, 2025
d97cc58
Correctly mark a load of arguments as optional
ExplodingCabbage Mar 19, 2025
f10133e
convert tests to tsd style
ExplodingCabbage Mar 19, 2025
52559a3
Fix type test
ExplodingCabbage Mar 19, 2025
ce74623
Fix some more type test things
ExplodingCabbage Mar 19, 2025
4584dfb
Undo some testing silliness
ExplodingCabbage Mar 19, 2025
ddb99ec
aaaaaaaaaaargh
ExplodingCabbage Mar 19, 2025
9db441a
Get most of the way to fixing type definitions based on https://githu…
ExplodingCabbage Mar 28, 2025
e17b4c2
Fix index.ts
ExplodingCabbage Apr 1, 2025
1e0200e
Fix remaining compilation errors
ExplodingCabbage Apr 1, 2025
f26bd1a
Restore 'Change' type from DefinitelyTyped
ExplodingCabbage Apr 1, 2025
87de9bf
tsd test syntax fix
ExplodingCabbage Apr 1, 2025
0da807a
Fix: oldHeader & newHeader are allowed to be explicitly given as unde…
ExplodingCabbage Apr 1, 2025
90ca769
Fix 'Abortable' types in patch/create
ExplodingCabbage Apr 1, 2025
e5daedb
Fix another typing error
ExplodingCabbage Apr 1, 2025
416388e
Fix error-handling-related types in patch/apply
ExplodingCabbage Apr 1, 2025
af16b40
Analyse errors I'm seeing in tests copied from DT & add TODOs
ExplodingCabbage Apr 1, 2025
ef8cc08
Begin adding some docs
ExplodingCabbage Apr 1, 2025
b0dde34
Fix a load of test cases where the DT behaviour was just wrong
ExplodingCabbage Apr 1, 2025
830e8b4
Make diffArrays generic instead of using any
ExplodingCabbage Apr 2, 2025
3cb305f
Make DiffArraysOptions types generic
ExplodingCabbage Apr 2, 2025
48f6f77
Rename ParsedDiff -> StructuredPatch
ExplodingCabbage Apr 2, 2025
9b45e37
Export ApplyPatchOptions, ApplyPatchesOptions
ExplodingCabbage Apr 2, 2025
52e8002
Make all methods public for backwards compat
ExplodingCabbage Apr 2, 2025
ecea629
Fix remaining type error
ExplodingCabbage Apr 2, 2025
5227872
Fix ignoring of test-d
ExplodingCabbage Apr 2, 2025
8f1a313
Copy latest change from DT (https://github.com/DefinitelyTyped/Defini…
ExplodingCabbage Apr 2, 2025
04c07e0
Flesh out docs
ExplodingCabbage Apr 2, 2025
e71d0a3
Resolve ignoreWhitespace dilemma
ExplodingCabbage Apr 2, 2025
96f3dce
nah
ExplodingCabbage Apr 2, 2025
f312f7e
Remove obsolete TODOs
ExplodingCabbage Apr 2, 2025
06a3c48
Fix bad .gitignore rule
ExplodingCabbage Apr 2, 2025
bc0b794
Add another type test file
ExplodingCabbage Apr 2, 2025
e80f324
v0.0.1
ExplodingCabbage Apr 4, 2025
e66ce84
v0.0.2
ExplodingCabbage Apr 4, 2025
fe02244
.npmignore more stuff
ExplodingCabbage Apr 4, 2025
ef8b1e4
Move test-d/ out of types/ so yarn clean won't delete it
ExplodingCabbage Apr 4, 2025
967fc15
Add attw
ExplodingCabbage Apr 4, 2025
3a26b69
durrrr
ExplodingCabbage Apr 4, 2025
836056b
failed fix attempt
ExplodingCabbage Apr 4, 2025
14d6610
Get attw passing (hallelujah!)
ExplodingCabbage Apr 7, 2025
c628619
Enable "strict": true
ExplodingCabbage Apr 7, 2025
e737e50
Fix a strict mode error
ExplodingCabbage Apr 7, 2025
892c450
Fix a strict mode error
ExplodingCabbage Apr 7, 2025
0639ed8
Fix more strict mode errors
ExplodingCabbage Apr 7, 2025
2f5e15a
Silence a silly strict mode error
ExplodingCabbage Apr 7, 2025
3b369f7
Further placate strict mode
ExplodingCabbage Apr 7, 2025
298654b
Further placate strict mode
ExplodingCabbage Apr 7, 2025
89f8f86
Fix a legit type error that strict mode found
ExplodingCabbage Apr 7, 2025
d9eb888
Merge branch 'master' into add-typescript
ExplodingCabbage Apr 7, 2025
031450c
Further placate strict mode
ExplodingCabbage Apr 7, 2025
4ffca31
Get TypeScript happy with create.ts
ExplodingCabbage Apr 7, 2025
b782edd
Further placate strict mode
ExplodingCabbage Apr 7, 2025
297fbfd
Further placate strict mode
ExplodingCabbage Apr 7, 2025
3e614d6
Placate compiler about apply.ts
ExplodingCabbage Apr 7, 2025
27234cb
Fix a load more strict mode errors
ExplodingCabbage Apr 7, 2025
6a526f7
Placate strict mode further
ExplodingCabbage Apr 7, 2025
d734b59
Merge branch 'master' into add-typescript
ExplodingCabbage Apr 7, 2025
770718c
Merge remote-tracking branch 'origin/master' into add-typescript
ExplodingCabbage Apr 7, 2025
3433cb7
Fix some more compilation errors
ExplodingCabbage Apr 7, 2025
23d561e
Fix one more compilation error
ExplodingCabbage Apr 7, 2025
64d2c54
Fix remaining base.ts errors
ExplodingCabbage Apr 7, 2025
2fb9180
Fix line.ts
ExplodingCabbage Apr 7, 2025
6865260
Type a variable to remove a slew of compilation errors
ExplodingCabbage Apr 7, 2025
9e8d869
Add more types to resolve more errors
ExplodingCabbage Apr 7, 2025
4929698
Add even more types to resolve even more errors
ExplodingCabbage Apr 7, 2025
fe4015f
Fix remaining type errors. Huzzah!!!
ExplodingCabbage Apr 7, 2025
3deafc3
Address verbatimModuleSyntax guidance
ExplodingCabbage Apr 7, 2025
f740326
Remove obsolete comment
ExplodingCabbage Apr 7, 2025
2e7d690
Get @typescript-eslint/consistent-type-exports enabled
ExplodingCabbage Apr 7, 2025
9b3adaa
Un-disable no-duplicate-type-constituents and let it autofix. I guess…
ExplodingCabbage Apr 8, 2025
44a4ee6
Reenable another rule
ExplodingCabbage Apr 9, 2025
e5dee6b
Remove no-longer-needed gitignore rule from earlier in this PR
ExplodingCabbage Apr 9, 2025
cead6a7
Remove an obsolete disabling of a rule from early in my eslint tinker…
ExplodingCabbage Apr 9, 2025
a7ed5ca
Fix deps being wrongly declared as non-dev deps
ExplodingCabbage Apr 9, 2025
aa78695
Bump new deps to latest
ExplodingCabbage Apr 9, 2025
3092635
Re-add support for imports from /lib/, maybe?
ExplodingCabbage Apr 10, 2025
a43c5cf
Restore support for subpath imports in Node 12.19 and earlier (see ht…
ExplodingCabbage Apr 11, 2025
74c1ba4
Restore es5 support
ExplodingCabbage Apr 11, 2025
6faead8
Improve docs; add more exports to obviate need to import stuff from lib/
ExplodingCabbage Apr 11, 2025
bed132e
Fix rollup
ExplodingCabbage Apr 11, 2025
8c313a1
Improve docs; restore package.json top-level fields that maybe are us…
ExplodingCabbage Apr 11, 2025
19eb667
Stop using babel with rollup.
ExplodingCabbage Apr 11, 2025
33a2a71
Remove unused dependency on Babel CLI
ExplodingCabbage Apr 11, 2025
8530bcd
Remove another unused dep
ExplodingCabbage Apr 11, 2025
4c320a8
Get karma working
ExplodingCabbage Apr 11, 2025
292b6a6
Remove apparently unneeded eslint ignore directives
ExplodingCabbage Apr 11, 2025
658d195
Fix misplaced bullet in release notes
ExplodingCabbage Apr 11, 2025
f0dd179
bah
ExplodingCabbage Apr 11, 2025
310e7d7
Begin the tedious work of copying and pasting the README documentatio…
ExplodingCabbage Apr 11, 2025
9be3db7
More copying and pasting. I should probably test this before moving on
ExplodingCabbage Apr 11, 2025
c65abbf
More copying and pasting of docs
ExplodingCabbage Apr 11, 2025
68d7987
Finish pasting docs
ExplodingCabbage Apr 11, 2025
eb280e1
Revert temporary package name change
ExplodingCabbage Apr 11, 2025
f8b44a0
Hackily resolve the Intl.Segmenter issue
ExplodingCabbage Apr 11, 2025
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
Prev Previous commit
Next Next commit
Improve docs; add more exports to obviate need to import stuff from lib/
  • Loading branch information
ExplodingCabbage committed Apr 11, 2025
commit 6faead8d8eaffdd1f262dc5859f2e9e9df60b769
90 changes: 55 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,29 @@ Based on the algorithm proposed in
npm install diff --save
```

## Usage
## Getting started

Broadly, jsdiff's diff functions all take an old text and a new text and perform three steps:
### Imports

In an environment where you can use imports, everything you need can be imported directly from `diff`. e.g.

ESM:

```
import {diffChars, createPatch} from 'diff';
```

CommonJS

```
const {diffChars, createPatch} = require('diff');
```

If you want to serve jsdiff to a web page without using a module system, you can use `dist/diff.js` or `dist/diff.min.js`. These create a global called `Diff` that contains the entire JsDiff API as its properties.

### Usage

jsdiff's diff functions all take an old text and a new text and perform three steps:

1. Split both texts into arrays of "tokens". What constitutes a token varies; in `diffChars`, each character is a token, while in `diffLines`, each line is a token.

Expand All @@ -22,9 +42,9 @@ Broadly, jsdiff's diff functions all take an old text and a new text and perform

3. Return an array representing the transformation computed in the previous step as a series of [change objects](#change-objects). The array is ordered from the start of the input to the end, and each change object represents *inserting* one or more tokens, *deleting* one or more tokens, or *keeping* one or more tokens.

### API
## API

* `Diff.diffChars(oldStr, newStr[, options])` - diffs two blocks of text, treating each character as a token.
* `diffChars(oldStr, newStr[, options])` - diffs two blocks of text, treating each character as a token.

("Characters" here means Unicode code points - the elements you get when you loop over a string with a `for ... of ...` loop.)

Expand All @@ -33,7 +53,7 @@ Broadly, jsdiff's diff functions all take an old text and a new text and perform
Options
* `ignoreCase`: If `true`, the uppercase and lowercase forms of a character are considered equal. Defaults to `false`.

* `Diff.diffWords(oldStr, newStr[, options])` - diffs two blocks of text, treating each word and each punctuation mark as a token. Whitespace is ignored when computing the diff (but preserved as far as possible in the final change objects).
* `diffWords(oldStr, newStr[, options])` - diffs two blocks of text, treating each word and each punctuation mark as a token. Whitespace is ignored when computing the diff (but preserved as far as possible in the final change objects).

Returns a list of [change objects](#change-objects).

Expand All @@ -45,47 +65,47 @@ Broadly, jsdiff's diff functions all take an old text and a new text and perform

Using an `Intl.Segmenter` should allow better word-level diffing of non-English text than the default behaviour. For instance, `Intl.Segmenter`s can generally identify via built-in dictionaries which sequences of adjacent Chinese characters form words, allowing word-level diffing of Chinese. By specifying a language when instantiating the segmenter (e.g. `new Intl.Segmenter('sv', {granularity: 'word'})`) you can also support language-specific rules, like treating Swedish's colon separated contractions (like *k:a* for *kyrka*) as single words; by default this would be seen as two words separated by a colon.

* `Diff.diffWordsWithSpace(oldStr, newStr[, options])` - diffs two blocks of text, treating each word, punctuation mark, newline, or run of (non-newline) whitespace as a token.
* `diffWordsWithSpace(oldStr, newStr[, options])` - diffs two blocks of text, treating each word, punctuation mark, newline, or run of (non-newline) whitespace as a token.

* `Diff.diffLines(oldStr, newStr[, options])` - diffs two blocks of text, treating each line as a token.
* `diffLines(oldStr, newStr[, options])` - diffs two blocks of text, treating each line as a token.

Options
* `ignoreWhitespace`: `true` to ignore leading and trailing whitespace characters when checking if two lines are equal. Defaults to `false`.
* `ignoreNewlineAtEof`: `true` to ignore a missing newline character at the end of the last line when comparing it to other lines. (By default, the line `'b\n'` in text `'a\nb\nc'` is not considered equal to the line `'b'` in text `'a\nb'`; this option makes them be considered equal.) Ignored if `ignoreWhitespace` or `newlineIsToken` are also true.
* `stripTrailingCr`: `true` to remove all trailing CR (`\r`) characters before performing the diff. Defaults to `false`.
* `stripTrailingCr`: `true` to remove all trailing CR (`\r`) characters before performing the Defaults to `false`.
This helps to get a useful diff when diffing UNIX text files against Windows text files.
* `newlineIsToken`: `true` to treat the newline character at the end of each line as its own token. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of `diffLines`; the default behavior with this option turned off is better suited for patches and other computer friendly output. Defaults to `false`.

Note that while using `ignoreWhitespace` in combination with `newlineIsToken` is not an error, results may not be as expected. With `ignoreWhitespace: true` and `newlineIsToken: false`, changing a completely empty line to contain some spaces is treated as a non-change, but with `ignoreWhitespace: true` and `newlineIsToken: true`, it is treated as an insertion. This is because the content of a completely blank line is not a token at all in `newlineIsToken` mode.

Returns a list of [change objects](#change-objects).

* `Diff.diffSentences(oldStr, newStr[, options])` - diffs two blocks of text, treating each sentence, and the whitespace between each pair of sentences, as a token. The characters `.`, `!`, and `?`, when followed by whitespace, are treated as marking the end of a sentence; nothing else besides the end of the string is considered to mark a sentence end.
* `diffSentences(oldStr, newStr[, options])` - diffs two blocks of text, treating each sentence, and the whitespace between each pair of sentences, as a token. The characters `.`, `!`, and `?`, when followed by whitespace, are treated as marking the end of a sentence; nothing else besides the end of the string is considered to mark a sentence end.

(For more sophisticated detection of sentence breaks, including support for non-English punctuation, consider instead tokenizing with an [`Intl.Segmenter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) with `granularity: 'sentence'` and passing the result to `Diff.diffArrays`.)
(For more sophisticated detection of sentence breaks, including support for non-English punctuation, consider instead tokenizing with an [`Intl.Segmenter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) with `granularity: 'sentence'` and passing the result to `diffArrays`.)

Returns a list of [change objects](#change-objects).

* `Diff.diffCss(oldStr, newStr[, options])` - diffs two blocks of text, comparing CSS tokens.
* `diffCss(oldStr, newStr[, options])` - diffs two blocks of text, comparing CSS tokens.

Returns a list of [change objects](#change-objects).

* `Diff.diffJson(oldObj, newObj[, options])` - diffs two JSON-serializable objects by first serializing them to prettily-formatted JSON and then treating each line of the JSON as a token. Object properties are ordered alphabetically in the serialized JSON, so the order of properties in the objects being compared doesn't affect the result.
* `diffJson(oldObj, newObj[, options])` - diffs two JSON-serializable objects by first serializing them to prettily-formatted JSON and then treating each line of the JSON as a token. Object properties are ordered alphabetically in the serialized JSON, so the order of properties in the objects being compared doesn't affect the result.

Returns a list of [change objects](#change-objects).

Options
* `stringifyReplacer`: A custom replacer function. Operates similarly to the `replacer` parameter to [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#the_replacer_parameter), but must be a function.
* `undefinedReplacement`: A value to replace `undefined` with. Ignored if a `stringifyReplacer` is provided.

* `Diff.diffArrays(oldArr, newArr[, options])` - diffs two arrays of tokens, comparing each item for strict equality (===).
* `diffArrays(oldArr, newArr[, options])` - diffs two arrays of tokens, comparing each item for strict equality (===).

Options
* `comparator`: `function(left, right)` for custom equality checks

Returns a list of [change objects](#change-objects).

* `Diff.createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr[, oldHeader[, newHeader[, options]]])` - creates a unified diff patch by first computing a diff with `diffLines` and then serializing it to unified diff format.
* `createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr[, oldHeader[, newHeader[, options]]])` - creates a unified diff patch by first computing a diff with `diffLines` and then serializing it to unified diff format.

Parameters:
* `oldFileName` : String to be output in the filename section of the patch for the removals
Expand All @@ -99,15 +119,15 @@ Broadly, jsdiff's diff functions all take an old text and a new text and perform
- `ignoreWhitespace`: Same as in `diffLines`. Defaults to `false`.
- `stripTrailingCr`: Same as in `diffLines`. Defaults to `false`.

* `Diff.createPatch(fileName, oldStr, newStr[, oldHeader[, newHeader[, options]]])` - creates a unified diff patch.
* `createPatch(fileName, oldStr, newStr[, oldHeader[, newHeader[, options]]])` - creates a unified diff patch.

Just like Diff.createTwoFilesPatch, but with oldFileName being equal to newFileName.
Just like createTwoFilesPatch, but with oldFileName being equal to newFileName.

* `Diff.formatPatch(patch)` - creates a unified diff patch.
* `formatPatch(patch)` - creates a unified diff patch.

`patch` may be either a single structured patch object (as returned by `structuredPatch`) or an array of them (as returned by `parsePatch`).

* `Diff.structuredPatch(oldFileName, newFileName, oldStr, newStr[, oldHeader[, newHeader[, options]]])` - returns an object with an array of hunk objects.
* `structuredPatch(oldFileName, newFileName, oldStr, newStr[, oldHeader[, newHeader[, options]]])` - returns an object with an array of hunk objects.

This method is similar to createTwoFilesPatch, but returns a data structure
suitable for further processing. Parameters are the same as createTwoFilesPatch. The data structure returned may look like this:
Expand All @@ -123,7 +143,7 @@ Broadly, jsdiff's diff functions all take an old text and a new text and perform
}
```

* `Diff.applyPatch(source, patch[, options])` - attempts to apply a unified diff patch.
* `applyPatch(source, patch[, options])` - attempts to apply a unified diff patch.

Hunks are applied first to last. `applyPatch` first tries to apply the first hunk at the line number specified in the hunk header, and with all context lines matching exactly. If that fails, it tries scanning backwards and forwards, one line at a time, to find a place to apply the hunk where the context lines match exactly. If that still fails, and `fuzzFactor` is greater than zero, it increments the maximum number of mismatches (missing, extra, or changed context lines) that there can be between the hunk context and a region where we are trying to apply the patch such that the hunk will still be considered to match. Regardless of `fuzzFactor`, lines to be deleted in the hunk *must* be present for a hunk to match, and the context lines *immediately* before and after an insertion must match exactly.

Expand All @@ -143,7 +163,7 @@ Broadly, jsdiff's diff functions all take an old text and a new text and perform
- `autoConvertLineEndings`: If `true`, and if the file to be patched consistently uses different line endings to the patch (i.e. either the file always uses Unix line endings while the patch uses Windows ones, or vice versa), then `applyPatch` will behave as if the line endings in the patch were the same as those in the source file. (If `false`, the patch will usually fail to apply in such circumstances since lines deleted in the patch won't be considered to match those in the source file.) Defaults to `true`.
- `compareLine(lineNumber, line, operation, patchContent)`: Callback used to compare to given lines to determine if they should be considered equal when patching. Defaults to strict equality but may be overridden to provide fuzzier comparison. Should return false if the lines should be rejected.

* `Diff.applyPatches(patch, options)` - applies one or more patches.
* `applyPatches(patch, options)` - applies one or more patches.

`patch` may be either an array of structured patch objects, or a string representing a patch in unified diff format (which may patch one or more files).

Expand All @@ -154,17 +174,17 @@ Broadly, jsdiff's diff functions all take an old text and a new text and perform

Once all patches have been applied or an error occurs, the `options.complete(err)` callback is made.

* `Diff.parsePatch(diffStr)` - Parses a patch into structured data
* `parsePatch(diffStr)` - Parses a patch into structured data

Return a JSON object representation of the a patch, suitable for use with the `applyPatch` method. This parses to the same structure returned by `Diff.structuredPatch`.
Return a JSON object representation of the a patch, suitable for use with the `applyPatch` method. This parses to the same structure returned by `structuredPatch`.

* `Diff.reversePatch(patch)` - Returns a new structured patch which when applied will undo the original `patch`.
* `reversePatch(patch)` - Returns a new structured patch which when applied will undo the original `patch`.

`patch` may be either a single structured patch object (as returned by `structuredPatch`) or an array of them (as returned by `parsePatch`).

* `Diff.convertChangesToXML(changes)` - converts a list of change objects to a serialized XML format
* `convertChangesToXML(changes)` - converts a list of change objects to a serialized XML format

* `Diff.convertChangesToDMP(changes)` - converts a list of change objects to the format returned by Google's [diff-match-patch](https://github.com/google/diff-match-patch) library
* `convertChangesToDMP(changes)` - converts a list of change objects to the format returned by Google's [diff-match-patch](https://github.com/google/diff-match-patch) library

#### Universal `options`

Expand All @@ -188,7 +208,7 @@ The simplest way to customize tokenization behavior is to simply tokenize the te

To customize the notion of token equality used, use the `comparator` option to `diffArrays`.

For even more customisation of the diffing behavior, you can extend the `Diff.Diff()` class, override its `castInput`, `tokenize`, `removeEmpty`, `equals`, and `join` properties with your own functions, then call its `diff(oldString, newString[, options])` method. The methods you can override are used as follows:
For even more customisation of the diffing behavior, you can extend the `Diff()` class, override its `castInput`, `tokenize`, `removeEmpty`, `equals`, and `join` properties with your own functions, then call its `diff(oldString, newString[, options])` method. The methods you can override are used as follows:

* `castInput(value, options)`: used to transform the `oldString` and `newString` before any other steps in the diffing algorithm happen. For instance, `diffJson` uses `castInput` to serialize the objects being diffed to JSON. Defaults to a no-op.
* `tokenize(value, options)`: used to convert each of `oldString` and `newString` (after they've gone through `castInput`) to an array of tokens. Defaults to returning `value.split('')` (returning an array of individual characters).
Expand All @@ -213,12 +233,12 @@ Many of the methods above return change objects. These objects consist of the fo

```js
require('colors');
const Diff = require('diff');
const {diffChars} = require('diff');

const one = 'beep boop';
const other = 'beep boob blah';

const diff = Diff.diffChars(one, other);
const diff = diffChars(one, other);

diff.forEach((part) => {
// green for additions, red for deletions
Expand Down Expand Up @@ -275,10 +295,10 @@ Open the above .html file in a browser and you should see
The code below is roughly equivalent to the Unix command `diff -u file1.txt file2.txt > mydiff.patch`:

```
const Diff = require('diff');
const {createTwoFilesPatch} = require('diff');
const file1Contents = fs.readFileSync("file1.txt").toString();
const file2Contents = fs.readFileSync("file2.txt").toString();
const patch = Diff.createTwoFilesPatch("file1.txt", "file2.txt", file1Contents, file2Contents);
const patch = createTwoFilesPatch("file1.txt", "file2.txt", file1Contents, file2Contents);
fs.writeFileSync("mydiff.patch", patch);
```

Expand All @@ -289,10 +309,10 @@ fs.writeFileSync("mydiff.patch", patch);
The code below is roughly equivalent to the Unix command `patch file1.txt mydiff.patch`:

```
const Diff = require('diff');
const {applyPatch} = require('diff');
const file1Contents = fs.readFileSync("file1.txt").toString();
const patch = fs.readFileSync("mydiff.patch").toString();
const patchedFile = Diff.applyPatch(file1Contents, patch);
const patchedFile = applyPatch(file1Contents, patch);
fs.writeFileSync("file1.txt", patchedFile);
```

Expand All @@ -301,9 +321,9 @@ fs.writeFileSync("file1.txt", patchedFile);
The code below is roughly equivalent to the Unix command `patch < mydiff.patch`:

```
const Diff = require('diff');
const {applyPatches} = require('diff');
const patch = fs.readFileSync("mydiff.patch").toString();
Diff.applyPatches(patch, {
applyPatches(patch, {
loadFile: (patch, callback) => {
let fileContents;
try {
Expand Down Expand Up @@ -332,7 +352,7 @@ Diff.applyPatches(patch, {

## Compatibility

jsdiff supports all ES3 environments with some known issues on IE8 and below. Under these browsers some diff algorithms such as word diff and others may fail due to lack of support for capturing groups in the `split` operation.
jsdiff should support all ES5 environments. If you find one that it doesn't support, please [open an issue](https://github.com/kpdecker/jsdiff/issues).

## TypeScript

Expand Down
10 changes: 5 additions & 5 deletions examples/node_example.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require('colors');
let Diff = require('../');
const {diffChars} = require('diff');

let one = 'beep boop';
let other = 'beep boob blah';
const one = 'beep boop';
const other = 'beep boob blah';

let diff = Diff.diffChars(one, other);
const diff = diffChars(one, other);

diff.forEach(function(part) {
diff.forEach((part) => {
// green for additions, red for deletions
let text = part.added ? part.value.bgGreen :
part.removed ? part.value.bgRed :
Expand Down
2 changes: 1 addition & 1 deletion src/diff/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class JsonDiff extends Diff<string, string> {
}
}

const jsonDiff = new JsonDiff();
export const jsonDiff = new JsonDiff();


export function diffJson(
Expand Down
Loading