-
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
28 changed files
with
378 additions
and
372 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env", | ||
"@babel/typescript" | ||
], | ||
"presets": ["@babel/preset-env", "@babel/typescript"], | ||
"plugins": [ | ||
"@babel/proposal-class-properties", | ||
"@babel/proposal-object-rest-spread" | ||
] | ||
} | ||
} |
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,6 @@ | ||
{ | ||
"printWidth": 80, | ||
"semi": false, | ||
"singleQuote": true, | ||
"arrowParens": "avoid" | ||
} |
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 |
---|---|---|
|
@@ -14,4 +14,4 @@ install: | |
|
||
cache: | ||
directories: | ||
- node_modules | ||
- node_modules |
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,47 +1,47 @@ | ||
import faker from "faker"; | ||
import { insert, drop } from "../src/utils"; | ||
import faker from 'faker' | ||
import { insert, drop } from '../src/utils' | ||
|
||
export const genSentences = (iterations: number = 100): string => { | ||
let text = "", | ||
i = 0; | ||
let text = '', | ||
i = 0 | ||
|
||
for (i; i < iterations; i++) { | ||
text += faker.lorem.sentences(); | ||
text += faker.lorem.sentences() | ||
} | ||
|
||
return text; | ||
}; | ||
return text | ||
} | ||
|
||
let simbols = | ||
"!@#$%^&*()_+~`| }{[]:;?><,./-=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
'!@#$%^&*()_+~`| }{[]:;?><,./-=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
|
||
export const getRandSimbol = (): string => { | ||
return simbols[Math.floor(Math.random() * simbols.length)]; | ||
}; | ||
return simbols[Math.floor(Math.random() * simbols.length)] | ||
} | ||
|
||
export const randomizeString = (source: string) => { | ||
const rand = Math.random(); | ||
const rand = Math.random() | ||
|
||
if (rand < 0.6) return source; | ||
if (rand < 0.6) return source | ||
|
||
const index = Math.floor(Math.random() * source.length); | ||
const index = Math.floor(Math.random() * source.length) | ||
|
||
if (rand > 0.8) return insert(source, getRandSimbol(), index) as string; | ||
if (rand > 0.8) return insert(source, getRandSimbol(), index) as string | ||
|
||
return drop(source, index, 1) as string; | ||
}; | ||
return drop(source, index, 1) as string | ||
} | ||
|
||
export const randomizeText = (text: string = ""): string => { | ||
const arr = text.split(" "); | ||
export const randomizeText = (text: string = ''): string => { | ||
const arr = text.split(' ') | ||
|
||
const randomArr = arr.reduce((acc: string[], el: string) => { | ||
const rand = Math.random(); | ||
const rand = Math.random() | ||
|
||
if (rand < 0.3) return acc; | ||
if (rand > 0.7) return [...acc, faker.random.words()]; | ||
if (rand < 0.3) return acc | ||
if (rand > 0.7) return [...acc, faker.random.words()] | ||
|
||
return [...acc, randomizeString(el)]; | ||
}, []); | ||
return [...acc, randomizeString(el)] | ||
}, []) | ||
|
||
return randomArr.join(" "); | ||
}; | ||
return randomArr.join(' ') | ||
} |
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,41 +1,41 @@ | ||
import { diffWordsWithSpace } from "diff"; | ||
import { diff_match_patch } from "diff-match-patch"; | ||
import { stringDiffToOps } from "../src/compare/string-diff-to-operations"; | ||
import { applyOps } from "../src/operations"; | ||
import { genSentences, randomizeText } from "./benchmark-utils"; | ||
import { diffWordsWithSpace } from 'diff' | ||
import { diff_match_patch } from 'diff-match-patch' | ||
import { stringDiffToOps } from '../src/compare/string-diff-to-operations' | ||
import { applyOps } from '../src/operations' | ||
import { genSentences, randomizeText } from './benchmark-utils' | ||
|
||
describe("benchmark diff compare", () => { | ||
const origin = genSentences(50); | ||
const modifyed = randomizeText(origin); | ||
describe('benchmark diff compare', () => { | ||
const origin = genSentences(50) | ||
const modifyed = randomizeText(origin) | ||
|
||
let dmp: any = new diff_match_patch(); | ||
let dmp: any = new diff_match_patch() | ||
|
||
console.time("csto stringDiffToOps"); | ||
const operations = stringDiffToOps(origin, modifyed); | ||
console.timeEnd("csto stringDiffToOps"); | ||
console.time('csto stringDiffToOps') | ||
const operations = stringDiffToOps(origin, modifyed) | ||
console.timeEnd('csto stringDiffToOps') | ||
|
||
console.time("jsdiff diffWordsWithSpace"); | ||
const diff = diffWordsWithSpace(origin, modifyed); | ||
console.timeEnd("jsdiff diffWordsWithSpace"); | ||
console.time('jsdiff diffWordsWithSpace') | ||
const diff = diffWordsWithSpace(origin, modifyed) | ||
console.timeEnd('jsdiff diffWordsWithSpace') | ||
|
||
console.time("diff-match-patch patch_make"); | ||
const dmp_patch = dmp.patch_make(origin, modifyed); | ||
console.timeEnd("diff-match-patch patch_make"); | ||
console.time('diff-match-patch patch_make') | ||
const dmp_patch = dmp.patch_make(origin, modifyed) | ||
console.timeEnd('diff-match-patch patch_make') | ||
|
||
it("string scto equals", () => { | ||
expect(applyOps(origin, operations)).toBe(modifyed); | ||
}); | ||
it('string scto equals', () => { | ||
expect(applyOps(origin, operations)).toBe(modifyed) | ||
}) | ||
|
||
it("string jsdiff equals", () => { | ||
it('string jsdiff equals', () => { | ||
const result = diff.reduce( | ||
(acc: any, el) => (el.removed ? acc : acc + el.value), | ||
"" | ||
); | ||
'' | ||
) | ||
|
||
expect(result).toBe(modifyed); | ||
}); | ||
expect(result).toBe(modifyed) | ||
}) | ||
|
||
it("string diff-match-patch equals", () => { | ||
expect(dmp.patch_apply(dmp_patch, origin)[0]).toBe(modifyed); | ||
}); | ||
}); | ||
it('string diff-match-patch equals', () => { | ||
expect(dmp.patch_apply(dmp_patch, origin)[0]).toBe(modifyed) | ||
}) | ||
}) |
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,21 +1,21 @@ | ||
import { collateDiffToOps } from "./collate-diff-to-operations"; | ||
import { applyOps } from "../operations"; | ||
import { collateDiffToOps } from './collate-diff-to-operations' | ||
import { applyOps } from '../operations' | ||
|
||
import { origin, modifyed } from "../../text-source"; | ||
import { origin, modifyed } from '../../text-source' | ||
|
||
describe("source diff to operations", () => { | ||
it("list diff equals one", () => { | ||
const one = origin.split(" "); | ||
const two = modifyed.split(" "); | ||
describe('source diff to operations', () => { | ||
it('list diff equals one', () => { | ||
const one = origin.split(' ') | ||
const two = modifyed.split(' ') | ||
|
||
const applyed = applyOps(one, collateDiffToOps(one, two)); | ||
const applyed = applyOps(one, collateDiffToOps(one, two)) | ||
|
||
expect(applyed).toStrictEqual(two); | ||
}); | ||
expect(applyed).toStrictEqual(two) | ||
}) | ||
|
||
it("string diff equals", () => { | ||
const applyed = applyOps(origin, collateDiffToOps(origin, modifyed)); | ||
it('string diff equals', () => { | ||
const applyed = applyOps(origin, collateDiffToOps(origin, modifyed)) | ||
|
||
expect(applyed).toStrictEqual(modifyed); | ||
}); | ||
}); | ||
expect(applyed).toStrictEqual(modifyed) | ||
}) | ||
}) |
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,39 +1,39 @@ | ||
import { shiftCompare } from "./shift-compare"; | ||
import { drop, insert } from "../utils"; | ||
import { shiftCompare } from './shift-compare' | ||
import { drop, insert } from '../utils' | ||
|
||
import { Operation, Collate, Replace, Insert, Drop } from "../operations"; | ||
import { Operation, Collate, Replace, Insert, Drop } from '../operations' | ||
|
||
export const collateDiffToOps = ( | ||
origin: Collate, | ||
modifyed: Collate | ||
): Operation[] => { | ||
const maxLen = Math.max(origin.length, modifyed.length), | ||
operations = []; | ||
operations = [] | ||
|
||
let i = 0; | ||
let i = 0 | ||
|
||
for (i; i <= maxLen; i++) { | ||
let operation: Operation | null = | ||
origin[i] !== modifyed[i] ? shiftCompare(origin, modifyed, i) : null; | ||
origin[i] !== modifyed[i] ? shiftCompare(origin, modifyed, i) : null | ||
|
||
if (operation) { | ||
operations.push(operation); | ||
operations.push(operation) | ||
|
||
const { type } = operation; | ||
const { type } = operation | ||
|
||
const { data } = operation as Replace | Insert; | ||
const { shift } = operation as Replace | Drop; | ||
const { data } = operation as Replace | Insert | ||
const { shift } = operation as Replace | Drop | ||
|
||
if (type === "insert") { | ||
origin = insert(origin, data, i); | ||
i += data.length; | ||
} else if (type === "replace") { | ||
origin = insert(origin, data, i, shift); | ||
} else if (type === "drop") { | ||
origin = drop(origin, i, shift); | ||
if (type === 'insert') { | ||
origin = insert(origin, data, i) | ||
i += data.length | ||
} else if (type === 'replace') { | ||
origin = insert(origin, data, i, shift) | ||
} else if (type === 'drop') { | ||
origin = drop(origin, i, shift) | ||
} | ||
} | ||
} | ||
|
||
return operations; | ||
}; | ||
return operations | ||
} |
Oops, something went wrong.