Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/ diff Public

A simple and fast typescript diff implementation for Deno

Notifications You must be signed in to change notification settings

dunosaurs/diff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

21 Commits

Repository files navigation

diff

Deno library for usage of diff Based on the O(n * m) DP solution to the LCS problem

import { diffCharacters } from "https://deno.land/x/diff/mod.ts";
diffCharacters("ABCBDAB", "BDCABA");

API

diffCharacters(oldString: string, newString: string, ignoreCase = false): DiffCharacter[]

Compares two strings by character and returns a list

oldString: The string to be transformed

newString: The string to be transformed into

ignoreCase: Whether case differences should be taken into account

Returns an array of DiffCharacter objects (explained below)

longestCommonSubsequence(a: string, b: string, ignoreCase = false)

Compares two strings by character and returns the longest common subsequence

Types

DiffCharacter

{
 character:
 string;
 wasAdded:
 boolean;
 wasRemoved:
 boolean;
}

The object contains the character and whether that character was removed, added, or neither. Here is example usage

let finalString = "";
for (const character of diffCharacters("boopa", "boop beep boppy")) {
 if (character.wasRemoved) {
 // print red if removed without newline
 finalString += `\x1b[31m${character.character}\x1b[0m`;
 } else if (character.wasAdded) {
 // print green if added
 finalString += `\x1b[32m${character.character}\x1b[0m`;
 } else {
 // print white if unchanged
 finalString += `\x1b[37m${character.character}\x1b[0m`;
 }
}
console.log(finalString);

image

About

A simple and fast typescript diff implementation for Deno

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

AltStyle によって変換されたページ (->オリジナル) /