@types/diff
- Version 6.0.0
- Published
- 20.1 kB
- No dependencies
- MIT license
Install
npm i @types/diff
yarn add @types/diff
pnpm add @types/diff
Overview
TypeScript definitions for diff
Index
Functions
Classes
Interfaces
Type Aliases
Functions
function applyPatch
applyPatch: ( source: string, patch: string | ParsedDiff | [ParsedDiff], options?: ApplyPatchOptions) => string | false;
Applies a unified diff patch.
Parameter patch
May be a string diff or the output from the
parsePatch()
orstructuredPatch()
methods.Returns
A string containing new version of provided data. false when failed
function applyPatches
applyPatches: ( patch: string | ParsedDiff[], options: ApplyPatchesOptions) => void;
Applies one or more patches. This method will iterate over the contents of the patch and apply to data provided through callbacks.
The general flow for each patch index is:
1.
options.loadFile(index, callback)
is called. The caller should then load the contents of the file and then pass that to thecallback(err, data)
callback. Passing anerr
will terminate further patch execution. 2.options.patched(index, content, callback)
is called once the patch has been applied.content
will be the return value fromapplyPatch()
. When it's ready, the caller should callcallback(err)
callback. Passing anerr
will terminate further patch execution. 3. Once all patches have been applied or an error occurs, theoptions.complete(err)
callback is made.
function canonicalize
canonicalize: (obj: any, stack: any[], replacementStack: any[]) => any;
function convertChangesToDMP
convertChangesToDMP: (changes: Change[]) => Array<[1 | 0 | -1, string]>;
Converts a list of changes to [DMP](http://code.google.com/p/google-diff-match-patch/wiki/API) format.
function convertChangesToXML
convertChangesToXML: (changes: Change[]) => string;
Converts a list of changes to a serialized XML format.
function createPatch
createPatch: { ( fileName: string, oldStr: string, newStr: string, oldHeader?: string, newHeader?: string, options?: PatchOptions ): string; ( fileName: string, oldStr: string, newStr: string, oldHeader?: string, newHeader?: string, options?: PatchOptions & CallbackOptions<string> ): void;};
Creates a unified diff patch. Just like
createTwoFilesPatch()
, but witholdFileName
being equal tonewFileName
.Parameter fileName
String to be output in the filename section.
Parameter oldStr
Original string value.
Parameter newStr
New string value.
Parameter oldHeader
Additional information to include in the old file header.
Parameter newHeader
Additional information to include in the new file header.
function createTwoFilesPatch
createTwoFilesPatch: { ( oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader?: string, newHeader?: string, options?: PatchOptions ): string; ( oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader?: string, newHeader?: string, options?: PatchOptions & CallbackOptions<string> ): void;};
Creates a unified diff patch.
Parameter oldFileName
String to be output in the filename section of the patch for the removals.
Parameter newFileName
String to be output in the filename section of the patch for the additions.
Parameter oldStr
Original string value.
Parameter newStr
New string value.
Parameter oldHeader
Additional information to include in the old file header.
Parameter newHeader
Additional information to include in the new file header.
function diffArrays
diffArrays: <TOld, TNew>( oldArr: TOld[], newArr: TNew[], options?: ArrayOptions<TOld, TNew>) => Array<ArrayChange<TOld | TNew>>;
Diffs two arrays, comparing each item for strict equality (
===
).Returns
A list of change objects.
function diffChars
diffChars: { (oldStr: string, newStr: string, options?: BaseOptions): Change[]; ( oldStr: string, newStr: string, options: Callback<Change[]> | (BaseOptions & CallbackOptions<Change[]>) ): void;};
Diffs two blocks of text, comparing character by character.
Returns
A list of change objects.
function diffCss
diffCss: { (oldStr: string, newStr: string, options?: BaseOptions): Change[]; ( oldStr: string, newStr: string, options: Callback<Change[]> | (BaseOptions & CallbackOptions<Change[]>) ): void;};
Diffs two blocks of text, comparing CSS tokens.
Returns
A list of change objects.
function diffJson
diffJson: { ( oldObj: string | object, newObj: string | object, options?: JsonOptions ): Change[]; ( oldObj: string | object, newObj: string | object, options: Callback<Change[]> | (JsonOptions & CallbackOptions<Change[]>) ): void;};
Diffs two JSON objects, comparing the fields defined on each. The order of fields, etc does not matter in this comparison.
Returns
A list of change objects.
function diffLines
diffLines: { (oldStr: string, newStr: string, options?: LinesOptions): Change[]; ( oldStr: string, newStr: string, options: Callback<Change[]> | (LinesOptions & CallbackOptions<Change[]>) ): void;};
Diffs two blocks of text, comparing line by line.
Returns
A list of change objects.
function diffSentences
diffSentences: { (oldStr: string, newStr: string, options?: BaseOptions): Change[]; ( oldStr: string, newStr: string, options: Callback<Change[]> | (BaseOptions & CallbackOptions<Change[]>) ): void;};
Diffs two blocks of text, comparing sentence by sentence.
Returns
A list of change objects.
function diffTrimmedLines
diffTrimmedLines: { (oldStr: string, newStr: string, options?: LinesOptions): Change[]; ( oldStr: string, newStr: string, options: Callback<Change[]> | (LinesOptions & CallbackOptions<Change[]>) ): void;};
Diffs two blocks of text, comparing line by line, ignoring leading and trailing whitespace.
Returns
A list of change objects.
function diffWords
diffWords: { (oldStr: string, newStr: string, options?: WordsOptions): Change[]; ( oldStr: string, newStr: string, options: Callback<Change[]> | (WordsOptions & CallbackOptions<Change[]>) ): void;};
Diffs two blocks of text, comparing word by word, ignoring whitespace.
Returns
A list of change objects.
function diffWordsWithSpace
diffWordsWithSpace: { (oldStr: string, newStr: string, options?: WordsOptions): Change[]; ( oldStr: string, newStr: string, options: Callback<Change[]> | (WordsOptions & CallbackOptions<Change[]>) ): void;};
Diffs two blocks of text, comparing word by word, treating whitespace as significant.
Returns
A list of change objects.
function formatPatch
formatPatch: (patch: ParsedDiff | ParsedDiff[]) => string;
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).
function merge
merge: (mine: string, theirs: string, base: string) => ParsedDiff;
function parsePatch
parsePatch: (uniDiff: string) => ParsedDiff[];
Parses a patch into structured data.
Returns
A JSON object representation of the a patch, suitable for use with the
applyPatch()
method.
function reversePatch
reversePatch: (patch: ParsedDiff | ParsedDiff[]) => ParsedDiff;
Returns a new structured patch which when applied will undo the original
patch
.patch
may be either a single structured patch object (as returned bystructuredPatch
) or an array of them (as returned byparsePatch
).
function structuredPatch
structuredPatch: { ( oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader?: string, newHeader?: string, options?: PatchOptions ): ParsedDiff; ( oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader?: string, newHeader?: string, options?: PatchOptions & CallbackOptions<ParsedDiff> ): void;};
This method is similar to
createTwoFilesPatch()
, but returns a data structure suitable for further processing. Parameters are the same ascreateTwoFilesPatch()
.Parameter oldFileName
String to be output in the
oldFileName
hunk property.Parameter newFileName
String to be output in the
newFileName
hunk property.Parameter oldStr
Original string value.
Parameter newStr
New string value.
Parameter oldHeader
Additional information to include in the
oldHeader
hunk property.Parameter newHeader
Additional information to include in the
newHeader
hunk property.Returns
An object with an array of hunk objects.
Classes
class Diff
class Diff {}
method castInput
castInput: (value: any, options: any) => any;
method diff
diff: ( oldString: string, newString: string, options?: | Callback<Change[]> | (ArrayOptions<any, any> & Partial<CallbackOptions<Change[]>>)) => Change[];
method equals
equals: (left: any, right: any, options: any) => boolean;
method extractCommon
extractCommon: ( basePath: BestPath, newString: string, oldString: string, diagonalPath: number) => number;
method join
join: (tokens: string[]) => string;
method postProcess
postProcess: (changes: Change[], options: any) => Change[];
method pushComponent
pushComponent: (components: Change[], added: boolean, removed: boolean) => void;
method removeEmpty
removeEmpty: (array: any[]) => any[];
method tokenize
tokenize: (value: string, options: any) => any;
Interfaces
interface ApplyPatchesOptions
interface ApplyPatchesOptions extends ApplyPatchOptions {}
interface ApplyPatchOptions
interface ApplyPatchOptions {}
property autoConvertLineEndings
autoConvertLineEndings?: boolean | undefined;
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), thenapplyPatch
will behave as if the line endings in the patch were the same as those in the source file. (Iffalse
, 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 totrue
.
property compareLine
compareLine?: | (( lineNumber: number, line: string, operation: '-' | ' ', patchContent: string ) => boolean) | undefined;
Callback used to compare to given lines to determine if they should be considered equal when patching. Should return
false
if the lines should be rejected.strict equality
property fuzzFactor
fuzzFactor?: number | undefined;
Number of lines that are allowed to differ before rejecting a patch. 0
interface ArrayChange
interface ArrayChange<T> {}
interface ArrayOptions
interface ArrayOptions<TLeft, TRight> extends BaseOptions {}
property comparator
comparator?: ((left: TLeft, right: TRight) => boolean) | undefined;
Comparator for custom equality checks.
interface BaseOptions
interface BaseOptions {}
property ignoreCase
ignoreCase?: boolean | undefined;
true
to ignore casing difference. false
property maxEditLength
maxEditLength?: number | undefined;
a number specifying the maximum edit distance to consider between the old and new texts. If the edit distance is higher than this, jsdiff will return
undefined
instead of a diff. You can use this to limit the computational cost of diffing large, very different texts by giving up early if the cost will be huge. Works for functions that return change objects and also forstructuredPatch
, but not other patch-generation functions.
property oneChangePerToken
oneChangePerToken?: boolean | undefined;
if
true
, the array of change objects returned will contain one change object per token (e.g. one per line if callingdiffLines
), instead of runs of consecutive tokens that are all added / all removed / all conserved being combined into a single change object.
interface BestPath
interface BestPath {}
property components
components: Change[];
property newPos
newPos: number;
interface CallbackOptions
interface CallbackOptions<T> {}
property callback
callback: Callback<T>;
if provided, the diff will be computed in async mode to avoid blocking the event loop while the diff is calculated. The value of the
callback
option should be a function and will be passed the computed diff or patch as its first argument.
interface Change
interface Change {}
interface Hunk
interface Hunk {}
interface JsonOptions
interface JsonOptions extends LinesOptions {}
property stringifyReplacer
stringifyReplacer?: ((key: string, value: any) => any) | undefined;
Replacer used to stringify the properties of the passed objects.
property undefinedReplacement
undefinedReplacement?: any;
The value to use when
undefined
values in the passed objects are encountered during stringification. Will only be used ifstringifyReplacer
option wasn't specified. undefined
interface LinesOptions
interface LinesOptions extends BaseOptions {}
property ignoreNewlineAtEof
ignoreNewlineAtEof?: boolean | undefined;
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 ifignoreWhitespace
ornewlineIsToken
are also true.
property ignoreWhitespace
ignoreWhitespace?: boolean | undefined;
true
to ignore leading and trailing whitespace. This is the same asdiffTrimmedLines()
.
property newlineIsToken
newlineIsToken?: boolean | undefined;
true
to treat newline characters as separate tokens. 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 ofdiffLines()
anddiffLines()
is better suited for patches and other computer friendly output.
interface ParsedDiff
interface ParsedDiff {}
property hunks
hunks: Hunk[];
property index
index?: string | undefined;
property newFileName
newFileName?: string | undefined;
property newHeader
newHeader?: string | undefined;
property oldFileName
oldFileName?: string | undefined;
property oldHeader
oldHeader?: string | undefined;
interface PatchOptions
interface PatchOptions extends LinesOptions {}
property context
context?: number | undefined;
Describes how many lines of context should be included. 4
interface WordsOptions
interface WordsOptions extends BaseOptions {}
property ignoreWhitespace
ignoreWhitespace?: boolean | undefined;
true
to ignore leading and trailing whitespace. This is the same asdiffWords()
.
property intlSegmenter
intlSegmenter?: Intl.Segmenter | undefined;
An optional [
Intl.Segmenter
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) object (which must have agranularity
of'word'
) fordiffWords
to use to split the text into words.By default,
diffWords
does not use anIntl.Segmenter
, just some regexes for splitting text into words. This will tend to give worse results thanIntl.Segmenter
would, but ensures the results are consistent across environments;Intl.Segmenter
behaviour is only loosely specced and the implementations in browsers could in principle change dramatically in future. If you want to usediffWords
with anIntl.Segmenter
but ensure it behaves the same whatever environment you run it in, use anIntl.Segmenter
polyfill instead of the JavaScript engine's nativeIntl.Segmenter
implementation.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.
Type Aliases
type Callback
type Callback<T> = (value: T) => void;
Package Files (1)
Dependencies (0)
No dependencies.
Dev Dependencies (0)
No dev dependencies.
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@types/diff
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@types/diff)
- HTML<a href="https://www.jsdocs.io/package/@types/diff"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 4544 ms. - Missing or incorrect documentation? Open an issue for this package.