feature: vor und nachteile

This commit is contained in:
Hikyu
2026-05-31 13:54:31 +02:00
parent 552a947a1c
commit e3dad17446
23270 changed files with 3024794 additions and 200 deletions
+202
View File
@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2006 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+68
View File
@@ -0,0 +1,68 @@
# diff-match-patch-es
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![bundle][bundle-src]][bundle-href]
[![JSDocs][jsdocs-src]][jsdocs-href]
[![License][license-src]][license-href]
ESM and TypeScript rewrite of Google's [diff-match-patch](https://github.com/google/diff-match-patch) (for JavaScript).
## Features
- Rewritten in ESM and TypeScript, ships with type declarations
- Published as dual ESM/CJS formats
- Refactor all to pure functions, fully tree-shakable
## Migration
Migration from [`diff-match-patch` npm package](https://github.com/JackuB/diff-match-patch)
- Default export and the class constructor has been removed
- Function name has been unified to camelCase
- Previous options like `Diff_Timeout` and `Diff_EditCost` are now passed as an options object in the arguments if needed
```js
// before
import DiffMatchPatch from 'diff-match-patch'
const dmp = new DiffMatchPatch()
dmp.Diff_Timeout = 1
const result = dmp.diff_main('hello', 'world')
```
```js
// after
import { diff } from 'diff-match-patch-es'
const result = diff('hello', 'world', { diffTimeout: 1 })
```
## Why?
Well, the code source repo from Google [hasn't been updated for 5 years](https://github.com/google/diff-match-patch), and the npm package `diff-match-patch` [hasn't been published for 4 years](https://github.com/JackuB/diff-match-patch). And the code is not ESM nor tree-shakable. While this library is a composition of `diff` `patch` and `match` functions, sometimes you don't need all of them but since all functions were bound to a single class, all the code has to be included in the bundle. It's probably not a big deal as this whole library is still relatively small, but it's a bit annoying to me when you know something wasn't optimal. So I took an afternoon porting and rewriting the library to ESM and TypeScript. Helpfully it will lead to better maintainability and help the community to move forward with the modern JavaScript ecosystem.
## Sponsors
<p align="center">
<a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
<img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
</a>
</p>
## License
[Apache-2.0](./LICENSE) License
<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/v/diff-match-patch-es?style=flat&colorA=080f12&colorB=1fa669
[npm-version-href]: https://npmjs.com/package/diff-match-patch-es
[npm-downloads-src]: https://img.shields.io/npm/dm/diff-match-patch-es?style=flat&colorA=080f12&colorB=1fa669
[npm-downloads-href]: https://npmjs.com/package/diff-match-patch-es
[bundle-src]: https://img.shields.io/bundlephobia/minzip/diff-match-patch-es?style=flat&colorA=080f12&colorB=1fa669&label=minzip
[bundle-href]: https://bundlephobia.com/result?p=diff-match-patch-es
[license-src]: https://img.shields.io/github/license/antfu/diff-match-patch-es.svg?style=flat&colorA=080f12&colorB=1fa669
[license-href]: https://github.com/antfu/diff-match-patch-es/blob/main/LICENSE
[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669
[jsdocs-href]: https://www.jsdocs.io/package/diff-match-patch-es
File diff suppressed because it is too large Load Diff
+292
View File
@@ -0,0 +1,292 @@
interface DiffMatchPathOptions {
/**
* Number of seconds to map a diff before giving up (0 for infinity).
* @default 1.0
*/
diffTimeout?: number;
/**
* Cost of an empty edit operation in terms of edit characters.
* @default 4
*/
diffEditCost?: number;
/**
* At what point is no match declared (0.0 = perfection, 1.0 = very loose).
* @default 0.5
*/
matchThreshold?: number;
/**
* How far to search for a match (0 = exact location, 1000+ = broad match).
* @default 1000
*/
matchDistance?: number;
/**
* When deleting a large block of text (over ~64 characters), how close do
* the contents have to be to match the expected contents.
* (0.0 = perfection, 1.0 = very loose).
* @default 0.5
*/
patchDeleteThreshold?: number;
/**
* Chunk size for context length.
* @default 4
*/
patchMargin?: number;
/**
* The number of bits in an int.
* @default 32
*/
matchMaxBits?: number;
}
type ResolvedOptions = Required<DiffMatchPathOptions>;
/**
* DIFF_DELETE: -1
*
* DIFF_INSERT: 1
*
* DIFF_EQUAL: 0
*/
type DiffOperation = -1 | 0 | 1;
/**
* The data structure representing a diff is an array of tuples:
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
* which means: delete 'Hello', add 'Goodbye' and keep ' world.'
*/
type Diff = [DiffOperation, string];
interface Patch {
diffs: Diff[];
start1: number;
start2: number;
length1: number;
length2: number;
}
declare const DIFF_DELETE = -1;
declare const DIFF_INSERT = 1;
declare const DIFF_EQUAL = 0;
/**
* Find the differences between two texts. Simplifies the problem by stripping
* any common prefix or suffix off the texts before diffing.
* @param text1 Old string to be diffed.
* @param text2 New string to be diffed.
* @param options Diff options
* @param opt_checklines Optional speedup flag. If present and false,
* then don't run a line-level diff first to identify the changed areas.
* Defaults to true, which does a faster, slightly less optimal diff.
* @param opt_deadline Optional time when the diff should be complete
* by. Used internally for recursive calls. Users should set DiffTimeout
* instead.
* @return {Diff[]} Array of diff tuples.
*/
declare function diffMain(text1: string, text2: string, options?: DiffMatchPathOptions | ResolvedOptions, opt_checklines?: boolean, opt_deadline?: number): Diff[];
/**
* Split two texts into an array of strings. Reduce the texts to a string of
* hashes where each Unicode character represents one line.
* @param {string} text1 First string.
* @param {string} text2 Second string.
* @return {{chars1: string, chars2: string, lineArray: !Array.<string>}}
* An object containing the encoded text1, the encoded text2 and
* the array of unique strings.
* The zeroth element of the array of unique strings is intentionally blank.
* @private
*/
declare function diffLinesToChars(text1: string, text2: string): {
chars1: string;
chars2: string;
lineArray: string[];
};
/**
* Rehydrate the text in a diff from a string of line hashes to real lines of
* text.
* @param {Diff[]} diffs Array of diff tuples.
* @param {!Array.<string>} lineArray Array of unique strings.
* @private
*/
declare function diffCharsToLines(diffs: Diff[], lineArray: string[]): void;
/**
* Determine the common prefix of two strings.
* @param {string} text1 First string.
* @param {string} text2 Second string.
* @return {number} The number of characters common to the start of each
* string.
*/
declare function diffCommonPrefix(text1: string, text2: string): number;
/**
* Determine the common suffix of two strings.
* @param {string} text1 First string.
* @param {string} text2 Second string.
* @return {number} The number of characters common to the end of each string.
*/
declare function diffCommonSuffix(text1: string, text2: string): number;
/**
* Reduce the number of edits by eliminating semantically trivial equalities.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupSemantic(diffs: Diff[]): void;
/**
* Look for single edits surrounded on both sides by equalities
* which can be shifted sideways to align the edit to a word boundary.
* e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupSemanticLossless(diffs: Diff[]): void;
/**
* Reduce the number of edits by eliminating operationally trivial equalities.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupEfficiency(diffs: Diff[], options?: DiffMatchPathOptions): void;
/**
* Reorder and merge like edit sections. Merge equalities.
* Any edit section can move as long as it doesn't cross an equality.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupMerge(diffs: Diff[]): void;
/**
* loc is a location in text1, compute and return the equivalent location in
* text2.
* e.g. 'The cat' vs 'The big cat', 1->1, 5->8
* @param {Diff[]} diffs Array of diff tuples.
* @param {number} loc Location within text1.
* @return {number} Location within text2.
*/
declare function diffXIndex(diffs: Diff[], loc: number): number;
/**
* Convert a diff array into a pretty HTML report.
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} HTML representation.
*/
declare function diffPrettyHtml(diffs: Diff[]): string;
/**
* Compute and return the source text (all equalities and deletions).
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} Source text.
*/
declare function diffText1(diffs: Diff[]): string;
/**
* Compute and return the destination text (all equalities and insertions).
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} Destination text.
*/
declare function diffText2(diffs: Diff[]): string;
/**
* Compute the Levenshtein distance; the number of inserted, deleted or
* substituted characters.
* @param {Diff[]} diffs Array of diff tuples.
* @return {number} Number of changes.
*/
declare function diffLevenshtein(diffs: Diff[]): number;
/**
* Crush the diff into an encoded string which describes the operations
* required to transform text1 into text2.
* E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'.
* Operations are tab-separated. Inserted text is escaped using %xx notation.
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} Delta text.
*/
declare function diffToDelta(diffs: Diff[]): string;
/**
* Given the original text1, and an encoded string which describes the
* operations required to transform text1 into text2, compute the full diff.
* @param {string} text1 Source string for the diff.
* @param {string} delta Delta text.
* @return {Diff[]} Array of diff tuples.
* @throws {!Error} If invalid input.
*/
declare function diffFromDelta(text1: string, delta: string): Diff[];
/**
* Locate the best instance of 'pattern' in 'text' near 'loc'.
* @param {string} text The text to search.
* @param {string} pattern The pattern to search for.
* @param {number} loc The location to search around.
* @return {number} Best match index or -1.
*/
declare function matchMain(text: string, pattern: string, loc: number, options?: DiffMatchPathOptions): number;
/**
* Locate the best instance of 'pattern' in 'text' near 'loc' using the
* Bitap algorithm.
* @param {string} text The text to search.
* @param {string} pattern The pattern to search for.
* @param {number} loc The location to search around.
* @param [options] The options
* @return {number} Best match index or -1.
* @private
*/
declare function matchBitap(text: string, pattern: string, loc: number, options?: DiffMatchPathOptions): number;
/**
* Initialise the alphabet for the Bitap algorithm.
* @param {string} pattern The text to encode.
* @return {!object} Hash of character locations.
* @private
*/
declare function matchAlphabet(pattern: string): Record<string, number>;
declare const defaultOptions: ResolvedOptions;
declare function resolveOptions(options?: DiffMatchPathOptions): ResolvedOptions;
/**
* Compute a list of patches to turn text1 into text2.
* Use diffs if provided, otherwise compute it ourselves.
* There are four ways to call this function, depending on what data is
* available to the caller:
* Method 1:
* a = text1, b = text2
* Method 2:
* a = diffs
* Method 3 (optimal):
* a = text1, b = diffs
* Method 4 (deprecated, use method 3):
* a = text1, b = text2, c = diffs
*
* @param {string|Diff[]} a text1 (methods 1,3,4) or
* Array of diff tuples for text1 to text2 (method 2).
* @param {string|Diff[]=} opt_b text2 (methods 1,4) or
* Array of diff tuples for text1 to text2 (method 3) or undefined (method 2).
* @param {string|Diff[]=} opt_c Array of diff tuples
* for text1 to text2 (method 4) or undefined (methods 1,2,3).
* @return {Patch[]} Array of Patch objects.
*/
declare function patchMake(a: string | Diff[], opt_b?: string | Diff[], opt_c?: string | Diff[], options?: DiffMatchPathOptions): Patch[];
/**
* Given an array of patches, return another array that is identical.
* @param {Patch[]} patches Array of Patch objects.
* @return {Patch[]} Array of Patch objects.
*/
declare function patchDeepCopy(patches: Patch[]): Patch[];
/**
* Merge a set of patches onto the text. Return a patched text, as well
* as a list of true/false values indicating which patches were applied.
* @param {Patch[]} patches Array of Patch objects.
* @param {string} text Old text.
* @return {!Array.<string|!Array.<boolean>>} Two element Array, containing the
* new text and an array of boolean values.
*/
declare function patchApply(patches: Patch[], text: string, options?: DiffMatchPathOptions): (string | boolean[])[];
/**
* Add some padding on text start and end so that edges can match something.
* Intended to be called only from within patch_apply.
* @param {Patch[]} patches Array of Patch objects.
* @return {string} The padding string added to each side.
*/
declare function patchAddPadding(patches: Patch[], options?: DiffMatchPathOptions): string;
/**
* Look through the patches and break up any which are longer than the maximum
* limit of the match algorithm.
* Intended to be called only from within patch_apply.
* @param {Patch[]} patches Array of Patch objects.
*/
declare function patchSplitMax(patches: Patch[], options?: DiffMatchPathOptions): void;
/**
* Take a list of patches and return a textual representation.
* @param {Patch[]} patches Array of Patch objects.
* @return {string} Text representation of patches.
*/
declare function patchToText(patches: Patch[]): string;
/**
* Parse a textual representation of patches and return a list of Patch objects.
* @param {string} textline Text representation of patches.
* @return {Patch[]} Array of Patch objects.
* @throws {!Error} If invalid input.
*/
declare function patchFromText(textline: string): Patch[];
export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, type Diff, type DiffMatchPathOptions, type DiffOperation, type Patch, type ResolvedOptions, defaultOptions, diffMain as diff, diffCharsToLines, diffCleanupEfficiency, diffCleanupMerge, diffCleanupSemantic, diffCleanupSemanticLossless, diffCommonPrefix, diffCommonSuffix, diffFromDelta, diffLevenshtein, diffLinesToChars, diffMain, diffPrettyHtml, diffText1, diffText2, diffToDelta, diffXIndex, matchMain as match, matchAlphabet, matchBitap, matchMain, patchMake as patch, patchAddPadding, patchApply, patchDeepCopy, patchFromText, patchMake, patchSplitMax, patchToText, resolveOptions };
+292
View File
@@ -0,0 +1,292 @@
interface DiffMatchPathOptions {
/**
* Number of seconds to map a diff before giving up (0 for infinity).
* @default 1.0
*/
diffTimeout?: number;
/**
* Cost of an empty edit operation in terms of edit characters.
* @default 4
*/
diffEditCost?: number;
/**
* At what point is no match declared (0.0 = perfection, 1.0 = very loose).
* @default 0.5
*/
matchThreshold?: number;
/**
* How far to search for a match (0 = exact location, 1000+ = broad match).
* @default 1000
*/
matchDistance?: number;
/**
* When deleting a large block of text (over ~64 characters), how close do
* the contents have to be to match the expected contents.
* (0.0 = perfection, 1.0 = very loose).
* @default 0.5
*/
patchDeleteThreshold?: number;
/**
* Chunk size for context length.
* @default 4
*/
patchMargin?: number;
/**
* The number of bits in an int.
* @default 32
*/
matchMaxBits?: number;
}
type ResolvedOptions = Required<DiffMatchPathOptions>;
/**
* DIFF_DELETE: -1
*
* DIFF_INSERT: 1
*
* DIFF_EQUAL: 0
*/
type DiffOperation = -1 | 0 | 1;
/**
* The data structure representing a diff is an array of tuples:
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
* which means: delete 'Hello', add 'Goodbye' and keep ' world.'
*/
type Diff = [DiffOperation, string];
interface Patch {
diffs: Diff[];
start1: number;
start2: number;
length1: number;
length2: number;
}
declare const DIFF_DELETE = -1;
declare const DIFF_INSERT = 1;
declare const DIFF_EQUAL = 0;
/**
* Find the differences between two texts. Simplifies the problem by stripping
* any common prefix or suffix off the texts before diffing.
* @param text1 Old string to be diffed.
* @param text2 New string to be diffed.
* @param options Diff options
* @param opt_checklines Optional speedup flag. If present and false,
* then don't run a line-level diff first to identify the changed areas.
* Defaults to true, which does a faster, slightly less optimal diff.
* @param opt_deadline Optional time when the diff should be complete
* by. Used internally for recursive calls. Users should set DiffTimeout
* instead.
* @return {Diff[]} Array of diff tuples.
*/
declare function diffMain(text1: string, text2: string, options?: DiffMatchPathOptions | ResolvedOptions, opt_checklines?: boolean, opt_deadline?: number): Diff[];
/**
* Split two texts into an array of strings. Reduce the texts to a string of
* hashes where each Unicode character represents one line.
* @param {string} text1 First string.
* @param {string} text2 Second string.
* @return {{chars1: string, chars2: string, lineArray: !Array.<string>}}
* An object containing the encoded text1, the encoded text2 and
* the array of unique strings.
* The zeroth element of the array of unique strings is intentionally blank.
* @private
*/
declare function diffLinesToChars(text1: string, text2: string): {
chars1: string;
chars2: string;
lineArray: string[];
};
/**
* Rehydrate the text in a diff from a string of line hashes to real lines of
* text.
* @param {Diff[]} diffs Array of diff tuples.
* @param {!Array.<string>} lineArray Array of unique strings.
* @private
*/
declare function diffCharsToLines(diffs: Diff[], lineArray: string[]): void;
/**
* Determine the common prefix of two strings.
* @param {string} text1 First string.
* @param {string} text2 Second string.
* @return {number} The number of characters common to the start of each
* string.
*/
declare function diffCommonPrefix(text1: string, text2: string): number;
/**
* Determine the common suffix of two strings.
* @param {string} text1 First string.
* @param {string} text2 Second string.
* @return {number} The number of characters common to the end of each string.
*/
declare function diffCommonSuffix(text1: string, text2: string): number;
/**
* Reduce the number of edits by eliminating semantically trivial equalities.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupSemantic(diffs: Diff[]): void;
/**
* Look for single edits surrounded on both sides by equalities
* which can be shifted sideways to align the edit to a word boundary.
* e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupSemanticLossless(diffs: Diff[]): void;
/**
* Reduce the number of edits by eliminating operationally trivial equalities.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupEfficiency(diffs: Diff[], options?: DiffMatchPathOptions): void;
/**
* Reorder and merge like edit sections. Merge equalities.
* Any edit section can move as long as it doesn't cross an equality.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupMerge(diffs: Diff[]): void;
/**
* loc is a location in text1, compute and return the equivalent location in
* text2.
* e.g. 'The cat' vs 'The big cat', 1->1, 5->8
* @param {Diff[]} diffs Array of diff tuples.
* @param {number} loc Location within text1.
* @return {number} Location within text2.
*/
declare function diffXIndex(diffs: Diff[], loc: number): number;
/**
* Convert a diff array into a pretty HTML report.
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} HTML representation.
*/
declare function diffPrettyHtml(diffs: Diff[]): string;
/**
* Compute and return the source text (all equalities and deletions).
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} Source text.
*/
declare function diffText1(diffs: Diff[]): string;
/**
* Compute and return the destination text (all equalities and insertions).
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} Destination text.
*/
declare function diffText2(diffs: Diff[]): string;
/**
* Compute the Levenshtein distance; the number of inserted, deleted or
* substituted characters.
* @param {Diff[]} diffs Array of diff tuples.
* @return {number} Number of changes.
*/
declare function diffLevenshtein(diffs: Diff[]): number;
/**
* Crush the diff into an encoded string which describes the operations
* required to transform text1 into text2.
* E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'.
* Operations are tab-separated. Inserted text is escaped using %xx notation.
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} Delta text.
*/
declare function diffToDelta(diffs: Diff[]): string;
/**
* Given the original text1, and an encoded string which describes the
* operations required to transform text1 into text2, compute the full diff.
* @param {string} text1 Source string for the diff.
* @param {string} delta Delta text.
* @return {Diff[]} Array of diff tuples.
* @throws {!Error} If invalid input.
*/
declare function diffFromDelta(text1: string, delta: string): Diff[];
/**
* Locate the best instance of 'pattern' in 'text' near 'loc'.
* @param {string} text The text to search.
* @param {string} pattern The pattern to search for.
* @param {number} loc The location to search around.
* @return {number} Best match index or -1.
*/
declare function matchMain(text: string, pattern: string, loc: number, options?: DiffMatchPathOptions): number;
/**
* Locate the best instance of 'pattern' in 'text' near 'loc' using the
* Bitap algorithm.
* @param {string} text The text to search.
* @param {string} pattern The pattern to search for.
* @param {number} loc The location to search around.
* @param [options] The options
* @return {number} Best match index or -1.
* @private
*/
declare function matchBitap(text: string, pattern: string, loc: number, options?: DiffMatchPathOptions): number;
/**
* Initialise the alphabet for the Bitap algorithm.
* @param {string} pattern The text to encode.
* @return {!object} Hash of character locations.
* @private
*/
declare function matchAlphabet(pattern: string): Record<string, number>;
declare const defaultOptions: ResolvedOptions;
declare function resolveOptions(options?: DiffMatchPathOptions): ResolvedOptions;
/**
* Compute a list of patches to turn text1 into text2.
* Use diffs if provided, otherwise compute it ourselves.
* There are four ways to call this function, depending on what data is
* available to the caller:
* Method 1:
* a = text1, b = text2
* Method 2:
* a = diffs
* Method 3 (optimal):
* a = text1, b = diffs
* Method 4 (deprecated, use method 3):
* a = text1, b = text2, c = diffs
*
* @param {string|Diff[]} a text1 (methods 1,3,4) or
* Array of diff tuples for text1 to text2 (method 2).
* @param {string|Diff[]=} opt_b text2 (methods 1,4) or
* Array of diff tuples for text1 to text2 (method 3) or undefined (method 2).
* @param {string|Diff[]=} opt_c Array of diff tuples
* for text1 to text2 (method 4) or undefined (methods 1,2,3).
* @return {Patch[]} Array of Patch objects.
*/
declare function patchMake(a: string | Diff[], opt_b?: string | Diff[], opt_c?: string | Diff[], options?: DiffMatchPathOptions): Patch[];
/**
* Given an array of patches, return another array that is identical.
* @param {Patch[]} patches Array of Patch objects.
* @return {Patch[]} Array of Patch objects.
*/
declare function patchDeepCopy(patches: Patch[]): Patch[];
/**
* Merge a set of patches onto the text. Return a patched text, as well
* as a list of true/false values indicating which patches were applied.
* @param {Patch[]} patches Array of Patch objects.
* @param {string} text Old text.
* @return {!Array.<string|!Array.<boolean>>} Two element Array, containing the
* new text and an array of boolean values.
*/
declare function patchApply(patches: Patch[], text: string, options?: DiffMatchPathOptions): (string | boolean[])[];
/**
* Add some padding on text start and end so that edges can match something.
* Intended to be called only from within patch_apply.
* @param {Patch[]} patches Array of Patch objects.
* @return {string} The padding string added to each side.
*/
declare function patchAddPadding(patches: Patch[], options?: DiffMatchPathOptions): string;
/**
* Look through the patches and break up any which are longer than the maximum
* limit of the match algorithm.
* Intended to be called only from within patch_apply.
* @param {Patch[]} patches Array of Patch objects.
*/
declare function patchSplitMax(patches: Patch[], options?: DiffMatchPathOptions): void;
/**
* Take a list of patches and return a textual representation.
* @param {Patch[]} patches Array of Patch objects.
* @return {string} Text representation of patches.
*/
declare function patchToText(patches: Patch[]): string;
/**
* Parse a textual representation of patches and return a list of Patch objects.
* @param {string} textline Text representation of patches.
* @return {Patch[]} Array of Patch objects.
* @throws {!Error} If invalid input.
*/
declare function patchFromText(textline: string): Patch[];
export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, type Diff, type DiffMatchPathOptions, type DiffOperation, type Patch, type ResolvedOptions, defaultOptions, diffMain as diff, diffCharsToLines, diffCleanupEfficiency, diffCleanupMerge, diffCleanupSemantic, diffCleanupSemanticLossless, diffCommonPrefix, diffCommonSuffix, diffFromDelta, diffLevenshtein, diffLinesToChars, diffMain, diffPrettyHtml, diffText1, diffText2, diffToDelta, diffXIndex, matchMain as match, matchAlphabet, matchBitap, matchMain, patchMake as patch, patchAddPadding, patchApply, patchDeepCopy, patchFromText, patchMake, patchSplitMax, patchToText, resolveOptions };
+292
View File
@@ -0,0 +1,292 @@
interface DiffMatchPathOptions {
/**
* Number of seconds to map a diff before giving up (0 for infinity).
* @default 1.0
*/
diffTimeout?: number;
/**
* Cost of an empty edit operation in terms of edit characters.
* @default 4
*/
diffEditCost?: number;
/**
* At what point is no match declared (0.0 = perfection, 1.0 = very loose).
* @default 0.5
*/
matchThreshold?: number;
/**
* How far to search for a match (0 = exact location, 1000+ = broad match).
* @default 1000
*/
matchDistance?: number;
/**
* When deleting a large block of text (over ~64 characters), how close do
* the contents have to be to match the expected contents.
* (0.0 = perfection, 1.0 = very loose).
* @default 0.5
*/
patchDeleteThreshold?: number;
/**
* Chunk size for context length.
* @default 4
*/
patchMargin?: number;
/**
* The number of bits in an int.
* @default 32
*/
matchMaxBits?: number;
}
type ResolvedOptions = Required<DiffMatchPathOptions>;
/**
* DIFF_DELETE: -1
*
* DIFF_INSERT: 1
*
* DIFF_EQUAL: 0
*/
type DiffOperation = -1 | 0 | 1;
/**
* The data structure representing a diff is an array of tuples:
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
* which means: delete 'Hello', add 'Goodbye' and keep ' world.'
*/
type Diff = [DiffOperation, string];
interface Patch {
diffs: Diff[];
start1: number;
start2: number;
length1: number;
length2: number;
}
declare const DIFF_DELETE = -1;
declare const DIFF_INSERT = 1;
declare const DIFF_EQUAL = 0;
/**
* Find the differences between two texts. Simplifies the problem by stripping
* any common prefix or suffix off the texts before diffing.
* @param text1 Old string to be diffed.
* @param text2 New string to be diffed.
* @param options Diff options
* @param opt_checklines Optional speedup flag. If present and false,
* then don't run a line-level diff first to identify the changed areas.
* Defaults to true, which does a faster, slightly less optimal diff.
* @param opt_deadline Optional time when the diff should be complete
* by. Used internally for recursive calls. Users should set DiffTimeout
* instead.
* @return {Diff[]} Array of diff tuples.
*/
declare function diffMain(text1: string, text2: string, options?: DiffMatchPathOptions | ResolvedOptions, opt_checklines?: boolean, opt_deadline?: number): Diff[];
/**
* Split two texts into an array of strings. Reduce the texts to a string of
* hashes where each Unicode character represents one line.
* @param {string} text1 First string.
* @param {string} text2 Second string.
* @return {{chars1: string, chars2: string, lineArray: !Array.<string>}}
* An object containing the encoded text1, the encoded text2 and
* the array of unique strings.
* The zeroth element of the array of unique strings is intentionally blank.
* @private
*/
declare function diffLinesToChars(text1: string, text2: string): {
chars1: string;
chars2: string;
lineArray: string[];
};
/**
* Rehydrate the text in a diff from a string of line hashes to real lines of
* text.
* @param {Diff[]} diffs Array of diff tuples.
* @param {!Array.<string>} lineArray Array of unique strings.
* @private
*/
declare function diffCharsToLines(diffs: Diff[], lineArray: string[]): void;
/**
* Determine the common prefix of two strings.
* @param {string} text1 First string.
* @param {string} text2 Second string.
* @return {number} The number of characters common to the start of each
* string.
*/
declare function diffCommonPrefix(text1: string, text2: string): number;
/**
* Determine the common suffix of two strings.
* @param {string} text1 First string.
* @param {string} text2 Second string.
* @return {number} The number of characters common to the end of each string.
*/
declare function diffCommonSuffix(text1: string, text2: string): number;
/**
* Reduce the number of edits by eliminating semantically trivial equalities.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupSemantic(diffs: Diff[]): void;
/**
* Look for single edits surrounded on both sides by equalities
* which can be shifted sideways to align the edit to a word boundary.
* e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupSemanticLossless(diffs: Diff[]): void;
/**
* Reduce the number of edits by eliminating operationally trivial equalities.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupEfficiency(diffs: Diff[], options?: DiffMatchPathOptions): void;
/**
* Reorder and merge like edit sections. Merge equalities.
* Any edit section can move as long as it doesn't cross an equality.
* @param {Diff[]} diffs Array of diff tuples.
*/
declare function diffCleanupMerge(diffs: Diff[]): void;
/**
* loc is a location in text1, compute and return the equivalent location in
* text2.
* e.g. 'The cat' vs 'The big cat', 1->1, 5->8
* @param {Diff[]} diffs Array of diff tuples.
* @param {number} loc Location within text1.
* @return {number} Location within text2.
*/
declare function diffXIndex(diffs: Diff[], loc: number): number;
/**
* Convert a diff array into a pretty HTML report.
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} HTML representation.
*/
declare function diffPrettyHtml(diffs: Diff[]): string;
/**
* Compute and return the source text (all equalities and deletions).
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} Source text.
*/
declare function diffText1(diffs: Diff[]): string;
/**
* Compute and return the destination text (all equalities and insertions).
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} Destination text.
*/
declare function diffText2(diffs: Diff[]): string;
/**
* Compute the Levenshtein distance; the number of inserted, deleted or
* substituted characters.
* @param {Diff[]} diffs Array of diff tuples.
* @return {number} Number of changes.
*/
declare function diffLevenshtein(diffs: Diff[]): number;
/**
* Crush the diff into an encoded string which describes the operations
* required to transform text1 into text2.
* E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'.
* Operations are tab-separated. Inserted text is escaped using %xx notation.
* @param {Diff[]} diffs Array of diff tuples.
* @return {string} Delta text.
*/
declare function diffToDelta(diffs: Diff[]): string;
/**
* Given the original text1, and an encoded string which describes the
* operations required to transform text1 into text2, compute the full diff.
* @param {string} text1 Source string for the diff.
* @param {string} delta Delta text.
* @return {Diff[]} Array of diff tuples.
* @throws {!Error} If invalid input.
*/
declare function diffFromDelta(text1: string, delta: string): Diff[];
/**
* Locate the best instance of 'pattern' in 'text' near 'loc'.
* @param {string} text The text to search.
* @param {string} pattern The pattern to search for.
* @param {number} loc The location to search around.
* @return {number} Best match index or -1.
*/
declare function matchMain(text: string, pattern: string, loc: number, options?: DiffMatchPathOptions): number;
/**
* Locate the best instance of 'pattern' in 'text' near 'loc' using the
* Bitap algorithm.
* @param {string} text The text to search.
* @param {string} pattern The pattern to search for.
* @param {number} loc The location to search around.
* @param [options] The options
* @return {number} Best match index or -1.
* @private
*/
declare function matchBitap(text: string, pattern: string, loc: number, options?: DiffMatchPathOptions): number;
/**
* Initialise the alphabet for the Bitap algorithm.
* @param {string} pattern The text to encode.
* @return {!object} Hash of character locations.
* @private
*/
declare function matchAlphabet(pattern: string): Record<string, number>;
declare const defaultOptions: ResolvedOptions;
declare function resolveOptions(options?: DiffMatchPathOptions): ResolvedOptions;
/**
* Compute a list of patches to turn text1 into text2.
* Use diffs if provided, otherwise compute it ourselves.
* There are four ways to call this function, depending on what data is
* available to the caller:
* Method 1:
* a = text1, b = text2
* Method 2:
* a = diffs
* Method 3 (optimal):
* a = text1, b = diffs
* Method 4 (deprecated, use method 3):
* a = text1, b = text2, c = diffs
*
* @param {string|Diff[]} a text1 (methods 1,3,4) or
* Array of diff tuples for text1 to text2 (method 2).
* @param {string|Diff[]=} opt_b text2 (methods 1,4) or
* Array of diff tuples for text1 to text2 (method 3) or undefined (method 2).
* @param {string|Diff[]=} opt_c Array of diff tuples
* for text1 to text2 (method 4) or undefined (methods 1,2,3).
* @return {Patch[]} Array of Patch objects.
*/
declare function patchMake(a: string | Diff[], opt_b?: string | Diff[], opt_c?: string | Diff[], options?: DiffMatchPathOptions): Patch[];
/**
* Given an array of patches, return another array that is identical.
* @param {Patch[]} patches Array of Patch objects.
* @return {Patch[]} Array of Patch objects.
*/
declare function patchDeepCopy(patches: Patch[]): Patch[];
/**
* Merge a set of patches onto the text. Return a patched text, as well
* as a list of true/false values indicating which patches were applied.
* @param {Patch[]} patches Array of Patch objects.
* @param {string} text Old text.
* @return {!Array.<string|!Array.<boolean>>} Two element Array, containing the
* new text and an array of boolean values.
*/
declare function patchApply(patches: Patch[], text: string, options?: DiffMatchPathOptions): (string | boolean[])[];
/**
* Add some padding on text start and end so that edges can match something.
* Intended to be called only from within patch_apply.
* @param {Patch[]} patches Array of Patch objects.
* @return {string} The padding string added to each side.
*/
declare function patchAddPadding(patches: Patch[], options?: DiffMatchPathOptions): string;
/**
* Look through the patches and break up any which are longer than the maximum
* limit of the match algorithm.
* Intended to be called only from within patch_apply.
* @param {Patch[]} patches Array of Patch objects.
*/
declare function patchSplitMax(patches: Patch[], options?: DiffMatchPathOptions): void;
/**
* Take a list of patches and return a textual representation.
* @param {Patch[]} patches Array of Patch objects.
* @return {string} Text representation of patches.
*/
declare function patchToText(patches: Patch[]): string;
/**
* Parse a textual representation of patches and return a list of Patch objects.
* @param {string} textline Text representation of patches.
* @return {Patch[]} Array of Patch objects.
* @throws {!Error} If invalid input.
*/
declare function patchFromText(textline: string): Patch[];
export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, type Diff, type DiffMatchPathOptions, type DiffOperation, type Patch, type ResolvedOptions, defaultOptions, diffMain as diff, diffCharsToLines, diffCleanupEfficiency, diffCleanupMerge, diffCleanupSemantic, diffCleanupSemanticLossless, diffCommonPrefix, diffCommonSuffix, diffFromDelta, diffLevenshtein, diffLinesToChars, diffMain, diffPrettyHtml, diffText1, diffText2, diffToDelta, diffXIndex, matchMain as match, matchAlphabet, matchBitap, matchMain, patchMake as patch, patchAddPadding, patchApply, patchDeepCopy, patchFromText, patchMake, patchSplitMax, patchToText, resolveOptions };
File diff suppressed because it is too large Load Diff
+76
View File
@@ -0,0 +1,76 @@
{
"name": "diff-match-patch-es",
"type": "module",
"version": "0.1.1",
"packageManager": "pnpm@9.12.2",
"description": "ESM and TypeScript rewrite of Google's diff-match-patch",
"license": "Apache-2.0",
"funding": "https://github.com/sponsors/antfu",
"homepage": "https://github.com/antfu/diff-match-patch-es#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/antfu/diff-match-patch-es.git"
},
"bugs": "https://github.com/antfu/diff-match-patch-es/issues",
"keywords": [
"diff-match-patch",
"diff",
"esm"
],
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/*",
"./dist/index.d.ts"
]
}
},
"files": [
"dist"
],
"scripts": {
"build": "unbuild",
"dev": "unbuild --stub",
"lint": "eslint .",
"prepublishOnly": "nr build",
"release": "bumpp && npm publish",
"start": "esno src/index.ts",
"test": "vitest",
"typecheck": "tsc --noEmit",
"prepare": "simple-git-hooks"
},
"devDependencies": {
"@antfu/eslint-config": "^3.8.0",
"@antfu/ni": "^0.23.0",
"@antfu/utils": "^0.7.10",
"@types/node": "^22.7.6",
"bumpp": "^9.7.1",
"eslint": "^9.12.0",
"esno": "^4.8.0",
"lint-staged": "^15.2.10",
"pnpm": "^9.12.2",
"rimraf": "^6.0.1",
"simple-git-hooks": "^2.11.1",
"typescript": "^5.6.3",
"unbuild": "^2.0.0",
"vite": "^5.4.9",
"vitest": "^2.1.3"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
},
"lint-staged": {
"*": "eslint --fix"
}
}