kpdecker / jsdiff

A javascript text differencing implementation.
BSD 3-Clause "New" or "Revised" License
7.92k stars 496 forks source link

Expose the "formatPatch" function #399

Closed joffreyvillard closed 8 months ago

joffreyvillard commented 1 year ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch diff@5.1.0 for the project I'm working on.

I'd like to use the provided formatPatch function along with structuredPatch, so that to only display a diff if relevant to my application (cannot apply the conditions on a string representation of the diff, sounds pretty hacky and fragile!).

I think that formatPatch should hence be exported, to be used together with structuredPatch as I see fit, instead of directly via createTwoFilesPatch that simply combines (and is very suitable anyway!)

Here is the diff that solved my problem:

diff --git a/node_modules/diff/dist/diff.js b/node_modules/diff/dist/diff.js
index 7fa3a55..f7b3efd 100644
--- a/node_modules/diff/dist/diff.js
+++ b/node_modules/diff/dist/diff.js
@@ -1618,6 +1618,7 @@ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   exports.diffTrimmedLines = diffTrimmedLines;
   exports.diffWords = diffWords;
   exports.diffWordsWithSpace = diffWordsWithSpace;
+  exports.formatPatch = formatPatch;
   exports.merge = merge;
   exports.parsePatch = parsePatch;
   exports.structuredPatch = structuredPatch;

This issue body was partially generated by patch-package.

ExplodingCabbage commented 8 months ago

Done! https://github.com/kpdecker/jsdiff/pull/451

joffreyvillard commented 8 months ago

Thank you!

martolini commented 7 months ago

Any chance this can make it to npm, too? ❤️

ExplodingCabbage commented 7 months ago

Will do in the next few days, @martolini!

leonardobsjr commented 6 months ago

It's unfortunate that @type/diff doesn't expose this yet :|

ExplodingCabbage commented 6 months ago

@leonardobsjr I'm hoping to do https://github.com/kpdecker/jsdiff/issues/303 soon and have jsdiff take over its own type definitions instead of deferring the provision of them to DefinitelyTyped. I have absolutely no idea how to do this yet - first time! - so any help would be welcome. But if I succeed then the @typed/diff package will be deprecated and there'll be full and correct type in diff itself.

leonardobsjr commented 6 months ago

@ExplodingCabbage Did a super-barebones types inclusion: https://github.com/kpdecker/jsdiff/pull/496