-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
2577020
commit 974258f
Showing
18 changed files
with
215 additions
and
333 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,14 +1,65 @@ | ||
import { DiffResult } from "./types"; | ||
import { fillStrings } from "./utils/fillStrings"; | ||
import getDiff from "./utils/getDiff"; | ||
import { DiffResult, Character } from "./types"; | ||
import deleteRef from "./utils/deleteRef"; | ||
import getAllParts from "./utils/getAllParts"; | ||
import toCharacters from "./utils/toCharacters"; | ||
|
||
const striff = (str1: string, str2: string): DiffResult => { | ||
let [strArr1, strArr2] = fillStrings(str1, str2); | ||
const getDiff = (partsArr: Character[][], arr1: Character[], arr2: Character[]) => { | ||
let str2 = arr2.map((c: Character) => c.value).join(""); | ||
let matched = new Map<Symbol | null, string>(); | ||
let matchedIndicies: {[key: string]: number} = {}; | ||
|
||
partsArr.forEach((part: Character[]) => { | ||
let partString = part.map((c) => c.value).join(""); | ||
let pattern = new RegExp(partString); | ||
|
||
// If this little thing matches ANY part of the string, it's in. | ||
let result = pattern.exec(str2); | ||
|
||
// We found a match, so let's spread it into our 'matched' store. | ||
let pastMatch = matchedIndicies[result?.index || ""]; | ||
|
||
// Give matched strings of longer length over all others. | ||
if (result && (!pastMatch || partString.length > pastMatch)) { | ||
matchedIndicies[result.index] = partString.length; | ||
|
||
// Since this is matched, I can safely update the second | ||
// string with symbol references. | ||
let partIndex = 0; | ||
for (let i = result.index; i < part.length + result.index; i++) { | ||
arr2[i].ref = part[partIndex].ref; | ||
partIndex++; | ||
} | ||
|
||
// Throw each of the matched characters into storage. | ||
part.forEach((char) => matched.set(char.ref, char.value)); | ||
} | ||
}); | ||
|
||
let hasNoMatchingRef = (char: Character) => !matched.get(char.ref); | ||
|
||
return { | ||
added: getDiff(strArr2, strArr1), | ||
removed: getDiff(strArr1, strArr2), | ||
// First string characters NOT in the "matched" set. | ||
removed: arr1.filter(hasNoMatchingRef).map(deleteRef), | ||
|
||
// Second string characters NOT in the "matched" set. | ||
added: arr2.filter(hasNoMatchingRef).map(deleteRef), | ||
}; | ||
}; | ||
|
||
const striff = (str1: string, str2: string): DiffResult => { | ||
let strArr1 = toCharacters(str1, true); | ||
let strArr2 = toCharacters(str2).map((char, index) => { | ||
let str1Char = strArr1[index]; | ||
|
||
if (str1Char?.value === char.value) { | ||
char.ref = str1Char.ref; | ||
} | ||
|
||
return char; | ||
}); | ||
|
||
let parts = getAllParts(strArr1); | ||
return getDiff(parts, strArr1, strArr2); | ||
}; | ||
|
||
export default striff; |
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 |
---|---|---|
@@ -1,16 +1,12 @@ | ||
export interface Diff { | ||
character: string; | ||
index: number; | ||
export interface Character { | ||
index: number, | ||
value: string, | ||
ref: Symbol | null | ||
} | ||
|
||
export interface DiffResult { | ||
added: Diff[]; | ||
removed: Diff[]; | ||
} | ||
export type PrunedCharacter = Pick<Character, "index" | "value">; | ||
|
||
export interface Character { | ||
value: string | null, | ||
accountedFor: boolean | ||
export interface DiffResult { | ||
added: PrunedCharacter[]; | ||
removed: PrunedCharacter[]; | ||
} | ||
|
||
export type FilledString = (string | null)[]; |
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,22 @@ | ||
import deleteRef from "./deleteRef"; | ||
import toCharacters from "./toCharacters"; | ||
|
||
it("deletes refs from characters", () => { | ||
let string = toCharacters("abc"); | ||
let result = string.map(deleteRef); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
index: 0, | ||
value: "a", | ||
}, | ||
{ | ||
index: 1, | ||
value: "b", | ||
}, | ||
{ | ||
index: 2, | ||
value: "c", | ||
}, | ||
]); | ||
}); |
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,9 @@ | ||
import { Character, PrunedCharacter } from "../types"; | ||
|
||
let deleteRef = (char: Partial<Character>) => { | ||
delete char.ref; | ||
|
||
return char as PrunedCharacter; | ||
}; | ||
|
||
export default deleteRef; |
Oops, something went wrong.