A library for performing text diffs.
Go to file
2024-04-14 17:29:42 +02:00
.vscode fix(core): update 2021-12-16 09:20:48 +01:00
test fix(core): update 2021-12-17 01:39:51 +01:00
ts fix(core): update 2021-12-16 09:20:48 +01:00
.gitignore fix(core): update 2021-12-16 09:20:48 +01:00
.gitlab-ci.yml fix(core): update 2021-12-16 09:20:48 +01:00
license fix(core): update 2021-12-16 09:20:48 +01:00
npmextra.json update tsconfig 2024-04-14 17:29:42 +02:00
package-lock.json 1.0.3 2021-12-17 01:39:52 +01:00
package.json update tsconfig 2024-04-14 17:29:42 +02:00
pnpm-lock.yaml switch to new org scheme 2023-07-11 00:33:39 +02:00
readme.hints.md update tsconfig 2024-04-14 17:29:42 +02:00
readme.md update tsconfig 2024-04-14 17:29:42 +02:00
tsconfig.json update npmextra.json: githost 2024-04-01 21:34:36 +02:00

@push.rocks/smartdiff

a diffing lib for text

Install

To install @push.rocks/smartdiff, you need to have Node.js and npm installed. Once you have those set up, you can install the library by running the following command in your project directory:

npm install @push.rocks/smartdiff --save

Usage

@push.rocks/smartdiff is a simple yet powerful library designed for creating and applying text diffs. It leverages the fast-diff library for efficient diffing operations. This usage guide will take you through the process of creating diffs between two pieces of text and applying those diffs to text using TypeScript with ESM syntax.

Creating a Text Diff

The primary functionality of @push.rocks/smartdiff is to create a diff between two versions of text, which can then be used to update texts or synchronize changes. Heres how you can create a diff:

import { createDiff } from '@push.rocks/smartdiff';

const originalText = 'Hello World';
const revisedText = 'Hello smart World';

const diffString = createDiff(originalText, revisedText);
console.log(diffString);
// Output would be a JSON string representing the diff

This example demonstrates how to generate a diff. The createDiff function takes two arguments: the original text and the revised text. It returns a JSON string representing the changes required to go from the original text to the revised text.

Applying a Text Diff

Once you have a diff, you might want to apply it to the original text to reconstruct the revised version (or vice versa). Heres how you can apply a diff with smartdiff:

import { applyPatch } from '@push.rocks/smartdiff';

const originalText = 'Hello World';
const diffString = '[[-6,"smart "],[0," World"]]'; // An example diff string

const updatedText = applyPatch(originalText, diffString);
console.log(updatedText);
// Expected output: 'Hello smart World'

The applyPatch function takes the original text and a diff string as inputs and outputs the updated text after applying the diff.

Handling Complex Diffs

@push.rocks/smartdiff is capable of handling more complex diffs, including multi-line changes, deletions, and insertions. When working with complex texts, the process remains the same: generate a diff using createDiff and apply it with applyPatch. This ensures that you can track and synchronize changes efficiently, no matter how extensive they are.

Best Practices for Managing Diffs

  • Version Control: Always keep track of your base and revised texts in some form of version control. This can help in diagnosing issues or conflicts in diffs.
  • Testing: Ensure that you thoroughly test diffs, especially in applications where correctness is critical (e.g., legal documents or codebases).
  • Performance Considerations: While smartdiff is optimized for performance, generating and applying diffs across extremely large documents may require additional considerations, such as chunking texts.

In conclusion, @push.rocks/smartdiff offers a streamlined interface for working with text diffs in TypeScript applications. Whether you are developing a collaborative editing tool, implementing version control for texts, or simply need to track changes in documents, smartdiff provides the necessary tools to get the job done with minimal overhead.

For any questions or contributions, please refer to the project's GitHub repository or contact the maintainers. Happy coding!

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.