-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# diff | ||
|
||
> Deno library for usage of diff | ||
> Based on the O(n * m) DP solution to the LCS problem | ||
```typescript | ||
import { diffCharacters } from "https://deno.land/x/diff/mod.ts"; | ||
|
||
diffCharacters("ABCBDAB", "BDCABA") | ||
``` | ||
|
||
## API | ||
|
||
*diffCharacters(oldString: string, newString: string)* | ||
Compares two strings by character and returns a list `IDiffCharacter` objects (explained below) | ||
|
||
*longestCommonSubsequence(a: string, b: string)* | ||
Compares two strings by character and returns the [longest common subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem) | ||
|
||
## Types | ||
|
||
*IDiffCharacter* | ||
```typescript | ||
{ | ||
character: string; | ||
wasAdded: boolean; | ||
wasRemoved: boolean; | ||
} | ||
``` | ||
The object contains the character and whether that character was removed, added, or neither. Here is example usage | ||
```typescript | ||
for (const character of diffCharacters("boopa", "boop beep boppy")) { | ||
let finalString = ""; | ||
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); | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters